builtin-diff.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. * builtin-diff.c
  3. *
  4. * Builtin diff command: Analyze two perf.data input files, look up and read
  5. * DSOs and symbol information, sort them and produce a diff.
  6. */
  7. #include "builtin.h"
  8. #include "util/debug.h"
  9. #include "util/event.h"
  10. #include "util/hist.h"
  11. #include "util/evsel.h"
  12. #include "util/evlist.h"
  13. #include "util/session.h"
  14. #include "util/tool.h"
  15. #include "util/sort.h"
  16. #include "util/symbol.h"
  17. #include "util/util.h"
  18. #include "util/data.h"
  19. #include <stdlib.h>
  20. #include <math.h>
  21. /* Diff command specific HPP columns. */
  22. enum {
  23. PERF_HPP_DIFF__BASELINE,
  24. PERF_HPP_DIFF__PERIOD,
  25. PERF_HPP_DIFF__PERIOD_BASELINE,
  26. PERF_HPP_DIFF__DELTA,
  27. PERF_HPP_DIFF__RATIO,
  28. PERF_HPP_DIFF__WEIGHTED_DIFF,
  29. PERF_HPP_DIFF__FORMULA,
  30. PERF_HPP_DIFF__MAX_INDEX
  31. };
  32. struct diff_hpp_fmt {
  33. struct perf_hpp_fmt fmt;
  34. int idx;
  35. char *header;
  36. int header_width;
  37. };
  38. struct data__file {
  39. struct perf_session *session;
  40. struct perf_data_file file;
  41. int idx;
  42. struct hists *hists;
  43. struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX];
  44. };
  45. static struct data__file *data__files;
  46. static int data__files_cnt;
  47. #define data__for_each_file_start(i, d, s) \
  48. for (i = s, d = &data__files[s]; \
  49. i < data__files_cnt; \
  50. i++, d = &data__files[i])
  51. #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
  52. #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
  53. static char diff__default_sort_order[] = "dso,symbol";
  54. static bool force;
  55. static bool show_period;
  56. static bool show_formula;
  57. static bool show_baseline_only;
  58. static unsigned int sort_compute;
  59. static s64 compute_wdiff_w1;
  60. static s64 compute_wdiff_w2;
  61. enum {
  62. COMPUTE_DELTA,
  63. COMPUTE_RATIO,
  64. COMPUTE_WEIGHTED_DIFF,
  65. COMPUTE_MAX,
  66. };
  67. const char *compute_names[COMPUTE_MAX] = {
  68. [COMPUTE_DELTA] = "delta",
  69. [COMPUTE_RATIO] = "ratio",
  70. [COMPUTE_WEIGHTED_DIFF] = "wdiff",
  71. };
  72. static int compute;
  73. static int compute_2_hpp[COMPUTE_MAX] = {
  74. [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA,
  75. [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO,
  76. [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF,
  77. };
  78. #define MAX_COL_WIDTH 70
  79. static struct header_column {
  80. const char *name;
  81. int width;
  82. } columns[PERF_HPP_DIFF__MAX_INDEX] = {
  83. [PERF_HPP_DIFF__BASELINE] = {
  84. .name = "Baseline",
  85. },
  86. [PERF_HPP_DIFF__PERIOD] = {
  87. .name = "Period",
  88. .width = 14,
  89. },
  90. [PERF_HPP_DIFF__PERIOD_BASELINE] = {
  91. .name = "Base period",
  92. .width = 14,
  93. },
  94. [PERF_HPP_DIFF__DELTA] = {
  95. .name = "Delta",
  96. .width = 7,
  97. },
  98. [PERF_HPP_DIFF__RATIO] = {
  99. .name = "Ratio",
  100. .width = 14,
  101. },
  102. [PERF_HPP_DIFF__WEIGHTED_DIFF] = {
  103. .name = "Weighted diff",
  104. .width = 14,
  105. },
  106. [PERF_HPP_DIFF__FORMULA] = {
  107. .name = "Formula",
  108. .width = MAX_COL_WIDTH,
  109. }
  110. };
  111. static int setup_compute_opt_wdiff(char *opt)
  112. {
  113. char *w1_str = opt;
  114. char *w2_str;
  115. int ret = -EINVAL;
  116. if (!opt)
  117. goto out;
  118. w2_str = strchr(opt, ',');
  119. if (!w2_str)
  120. goto out;
  121. *w2_str++ = 0x0;
  122. if (!*w2_str)
  123. goto out;
  124. compute_wdiff_w1 = strtol(w1_str, NULL, 10);
  125. compute_wdiff_w2 = strtol(w2_str, NULL, 10);
  126. if (!compute_wdiff_w1 || !compute_wdiff_w2)
  127. goto out;
  128. pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
  129. compute_wdiff_w1, compute_wdiff_w2);
  130. ret = 0;
  131. out:
  132. if (ret)
  133. pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
  134. return ret;
  135. }
  136. static int setup_compute_opt(char *opt)
  137. {
  138. if (compute == COMPUTE_WEIGHTED_DIFF)
  139. return setup_compute_opt_wdiff(opt);
  140. if (opt) {
  141. pr_err("Failed: extra option specified '%s'", opt);
  142. return -EINVAL;
  143. }
  144. return 0;
  145. }
  146. static int setup_compute(const struct option *opt, const char *str,
  147. int unset __maybe_unused)
  148. {
  149. int *cp = (int *) opt->value;
  150. char *cstr = (char *) str;
  151. char buf[50];
  152. unsigned i;
  153. char *option;
  154. if (!str) {
  155. *cp = COMPUTE_DELTA;
  156. return 0;
  157. }
  158. option = strchr(str, ':');
  159. if (option) {
  160. unsigned len = option++ - str;
  161. /*
  162. * The str data are not writeable, so we need
  163. * to use another buffer.
  164. */
  165. /* No option value is longer. */
  166. if (len >= sizeof(buf))
  167. return -EINVAL;
  168. strncpy(buf, str, len);
  169. buf[len] = 0x0;
  170. cstr = buf;
  171. }
  172. for (i = 0; i < COMPUTE_MAX; i++)
  173. if (!strcmp(cstr, compute_names[i])) {
  174. *cp = i;
  175. return setup_compute_opt(option);
  176. }
  177. pr_err("Failed: '%s' is not computation method "
  178. "(use 'delta','ratio' or 'wdiff')\n", str);
  179. return -EINVAL;
  180. }
  181. static double period_percent(struct hist_entry *he, u64 period)
  182. {
  183. u64 total = he->hists->stats.total_period;
  184. return (period * 100.0) / total;
  185. }
  186. static double compute_delta(struct hist_entry *he, struct hist_entry *pair)
  187. {
  188. double old_percent = period_percent(he, he->stat.period);
  189. double new_percent = period_percent(pair, pair->stat.period);
  190. pair->diff.period_ratio_delta = new_percent - old_percent;
  191. pair->diff.computed = true;
  192. return pair->diff.period_ratio_delta;
  193. }
  194. static double compute_ratio(struct hist_entry *he, struct hist_entry *pair)
  195. {
  196. double old_period = he->stat.period ?: 1;
  197. double new_period = pair->stat.period;
  198. pair->diff.computed = true;
  199. pair->diff.period_ratio = new_period / old_period;
  200. return pair->diff.period_ratio;
  201. }
  202. static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair)
  203. {
  204. u64 old_period = he->stat.period;
  205. u64 new_period = pair->stat.period;
  206. pair->diff.computed = true;
  207. pair->diff.wdiff = new_period * compute_wdiff_w2 -
  208. old_period * compute_wdiff_w1;
  209. return pair->diff.wdiff;
  210. }
  211. static int formula_delta(struct hist_entry *he, struct hist_entry *pair,
  212. char *buf, size_t size)
  213. {
  214. return scnprintf(buf, size,
  215. "(%" PRIu64 " * 100 / %" PRIu64 ") - "
  216. "(%" PRIu64 " * 100 / %" PRIu64 ")",
  217. pair->stat.period, pair->hists->stats.total_period,
  218. he->stat.period, he->hists->stats.total_period);
  219. }
  220. static int formula_ratio(struct hist_entry *he, struct hist_entry *pair,
  221. char *buf, size_t size)
  222. {
  223. double old_period = he->stat.period;
  224. double new_period = pair->stat.period;
  225. return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
  226. }
  227. static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair,
  228. char *buf, size_t size)
  229. {
  230. u64 old_period = he->stat.period;
  231. u64 new_period = pair->stat.period;
  232. return scnprintf(buf, size,
  233. "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
  234. new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
  235. }
  236. static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,
  237. char *buf, size_t size)
  238. {
  239. switch (compute) {
  240. case COMPUTE_DELTA:
  241. return formula_delta(he, pair, buf, size);
  242. case COMPUTE_RATIO:
  243. return formula_ratio(he, pair, buf, size);
  244. case COMPUTE_WEIGHTED_DIFF:
  245. return formula_wdiff(he, pair, buf, size);
  246. default:
  247. BUG_ON(1);
  248. }
  249. return -1;
  250. }
  251. static int hists__add_entry(struct hists *hists,
  252. struct addr_location *al, u64 period,
  253. u64 weight, u64 transaction)
  254. {
  255. if (__hists__add_entry(hists, al, NULL, NULL, NULL, period, weight,
  256. transaction) != NULL)
  257. return 0;
  258. return -ENOMEM;
  259. }
  260. static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
  261. union perf_event *event,
  262. struct perf_sample *sample,
  263. struct perf_evsel *evsel,
  264. struct machine *machine)
  265. {
  266. struct addr_location al;
  267. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  268. pr_warning("problem processing %d event, skipping it.\n",
  269. event->header.type);
  270. return -1;
  271. }
  272. if (al.filtered)
  273. return 0;
  274. if (hists__add_entry(&evsel->hists, &al, sample->period,
  275. sample->weight, sample->transaction)) {
  276. pr_warning("problem incrementing symbol period, skipping event\n");
  277. return -1;
  278. }
  279. evsel->hists.stats.total_period += sample->period;
  280. return 0;
  281. }
  282. static struct perf_tool tool = {
  283. .sample = diff__process_sample_event,
  284. .mmap = perf_event__process_mmap,
  285. .comm = perf_event__process_comm,
  286. .exit = perf_event__process_exit,
  287. .fork = perf_event__process_fork,
  288. .lost = perf_event__process_lost,
  289. .ordered_samples = true,
  290. .ordering_requires_timestamps = true,
  291. };
  292. static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
  293. struct perf_evlist *evlist)
  294. {
  295. struct perf_evsel *e;
  296. list_for_each_entry(e, &evlist->entries, node)
  297. if (perf_evsel__match2(evsel, e))
  298. return e;
  299. return NULL;
  300. }
  301. static void perf_evlist__collapse_resort(struct perf_evlist *evlist)
  302. {
  303. struct perf_evsel *evsel;
  304. list_for_each_entry(evsel, &evlist->entries, node) {
  305. struct hists *hists = &evsel->hists;
  306. hists__collapse_resort(hists, NULL);
  307. }
  308. }
  309. static struct hist_entry*
  310. get_pair_data(struct hist_entry *he, struct data__file *d)
  311. {
  312. if (hist_entry__has_pairs(he)) {
  313. struct hist_entry *pair;
  314. list_for_each_entry(pair, &he->pairs.head, pairs.node)
  315. if (pair->hists == d->hists)
  316. return pair;
  317. }
  318. return NULL;
  319. }
  320. static struct hist_entry*
  321. get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt)
  322. {
  323. void *ptr = dfmt - dfmt->idx;
  324. struct data__file *d = container_of(ptr, struct data__file, fmt);
  325. return get_pair_data(he, d);
  326. }
  327. static void hists__baseline_only(struct hists *hists)
  328. {
  329. struct rb_root *root;
  330. struct rb_node *next;
  331. if (sort__need_collapse)
  332. root = &hists->entries_collapsed;
  333. else
  334. root = hists->entries_in;
  335. next = rb_first(root);
  336. while (next != NULL) {
  337. struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
  338. next = rb_next(&he->rb_node_in);
  339. if (!hist_entry__next_pair(he)) {
  340. rb_erase(&he->rb_node_in, root);
  341. hist_entry__free(he);
  342. }
  343. }
  344. }
  345. static void hists__precompute(struct hists *hists)
  346. {
  347. struct rb_root *root;
  348. struct rb_node *next;
  349. if (sort__need_collapse)
  350. root = &hists->entries_collapsed;
  351. else
  352. root = hists->entries_in;
  353. next = rb_first(root);
  354. while (next != NULL) {
  355. struct hist_entry *he, *pair;
  356. he = rb_entry(next, struct hist_entry, rb_node_in);
  357. next = rb_next(&he->rb_node_in);
  358. pair = get_pair_data(he, &data__files[sort_compute]);
  359. if (!pair)
  360. continue;
  361. switch (compute) {
  362. case COMPUTE_DELTA:
  363. compute_delta(he, pair);
  364. break;
  365. case COMPUTE_RATIO:
  366. compute_ratio(he, pair);
  367. break;
  368. case COMPUTE_WEIGHTED_DIFF:
  369. compute_wdiff(he, pair);
  370. break;
  371. default:
  372. BUG_ON(1);
  373. }
  374. }
  375. }
  376. static int64_t cmp_doubles(double l, double r)
  377. {
  378. if (l > r)
  379. return -1;
  380. else if (l < r)
  381. return 1;
  382. else
  383. return 0;
  384. }
  385. static int64_t
  386. __hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  387. int c)
  388. {
  389. switch (c) {
  390. case COMPUTE_DELTA:
  391. {
  392. double l = left->diff.period_ratio_delta;
  393. double r = right->diff.period_ratio_delta;
  394. return cmp_doubles(l, r);
  395. }
  396. case COMPUTE_RATIO:
  397. {
  398. double l = left->diff.period_ratio;
  399. double r = right->diff.period_ratio;
  400. return cmp_doubles(l, r);
  401. }
  402. case COMPUTE_WEIGHTED_DIFF:
  403. {
  404. s64 l = left->diff.wdiff;
  405. s64 r = right->diff.wdiff;
  406. return r - l;
  407. }
  408. default:
  409. BUG_ON(1);
  410. }
  411. return 0;
  412. }
  413. static int64_t
  414. hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  415. int c)
  416. {
  417. bool pairs_left = hist_entry__has_pairs(left);
  418. bool pairs_right = hist_entry__has_pairs(right);
  419. struct hist_entry *p_right, *p_left;
  420. if (!pairs_left && !pairs_right)
  421. return 0;
  422. if (!pairs_left || !pairs_right)
  423. return pairs_left ? -1 : 1;
  424. p_left = get_pair_data(left, &data__files[sort_compute]);
  425. p_right = get_pair_data(right, &data__files[sort_compute]);
  426. if (!p_left && !p_right)
  427. return 0;
  428. if (!p_left || !p_right)
  429. return p_left ? -1 : 1;
  430. /*
  431. * We have 2 entries of same kind, let's
  432. * make the data comparison.
  433. */
  434. return __hist_entry__cmp_compute(p_left, p_right, c);
  435. }
  436. static void insert_hist_entry_by_compute(struct rb_root *root,
  437. struct hist_entry *he,
  438. int c)
  439. {
  440. struct rb_node **p = &root->rb_node;
  441. struct rb_node *parent = NULL;
  442. struct hist_entry *iter;
  443. while (*p != NULL) {
  444. parent = *p;
  445. iter = rb_entry(parent, struct hist_entry, rb_node);
  446. if (hist_entry__cmp_compute(he, iter, c) < 0)
  447. p = &(*p)->rb_left;
  448. else
  449. p = &(*p)->rb_right;
  450. }
  451. rb_link_node(&he->rb_node, parent, p);
  452. rb_insert_color(&he->rb_node, root);
  453. }
  454. static void hists__compute_resort(struct hists *hists)
  455. {
  456. struct rb_root *root;
  457. struct rb_node *next;
  458. if (sort__need_collapse)
  459. root = &hists->entries_collapsed;
  460. else
  461. root = hists->entries_in;
  462. hists->entries = RB_ROOT;
  463. next = rb_first(root);
  464. hists->nr_entries = 0;
  465. hists->stats.total_period = 0;
  466. hists__reset_col_len(hists);
  467. while (next != NULL) {
  468. struct hist_entry *he;
  469. he = rb_entry(next, struct hist_entry, rb_node_in);
  470. next = rb_next(&he->rb_node_in);
  471. insert_hist_entry_by_compute(&hists->entries, he, compute);
  472. hists__inc_nr_entries(hists, he);
  473. }
  474. }
  475. static void hists__process(struct hists *hists)
  476. {
  477. if (show_baseline_only)
  478. hists__baseline_only(hists);
  479. if (sort_compute) {
  480. hists__precompute(hists);
  481. hists__compute_resort(hists);
  482. } else {
  483. hists__output_resort(hists);
  484. }
  485. hists__fprintf(hists, true, 0, 0, 0, stdout);
  486. }
  487. static void data__fprintf(void)
  488. {
  489. struct data__file *d;
  490. int i;
  491. fprintf(stdout, "# Data files:\n");
  492. data__for_each_file(i, d)
  493. fprintf(stdout, "# [%d] %s %s\n",
  494. d->idx, d->file.path,
  495. !d->idx ? "(Baseline)" : "");
  496. fprintf(stdout, "#\n");
  497. }
  498. static void data_process(void)
  499. {
  500. struct perf_evlist *evlist_base = data__files[0].session->evlist;
  501. struct perf_evsel *evsel_base;
  502. bool first = true;
  503. list_for_each_entry(evsel_base, &evlist_base->entries, node) {
  504. struct data__file *d;
  505. int i;
  506. data__for_each_file_new(i, d) {
  507. struct perf_evlist *evlist = d->session->evlist;
  508. struct perf_evsel *evsel;
  509. evsel = evsel_match(evsel_base, evlist);
  510. if (!evsel)
  511. continue;
  512. d->hists = &evsel->hists;
  513. hists__match(&evsel_base->hists, &evsel->hists);
  514. if (!show_baseline_only)
  515. hists__link(&evsel_base->hists,
  516. &evsel->hists);
  517. }
  518. fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
  519. perf_evsel__name(evsel_base));
  520. first = false;
  521. if (verbose || data__files_cnt > 2)
  522. data__fprintf();
  523. hists__process(&evsel_base->hists);
  524. }
  525. }
  526. static void data__free(struct data__file *d)
  527. {
  528. int col;
  529. for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) {
  530. struct diff_hpp_fmt *fmt = &d->fmt[col];
  531. free(fmt->header);
  532. }
  533. }
  534. static int __cmd_diff(void)
  535. {
  536. struct data__file *d;
  537. int ret = -EINVAL, i;
  538. data__for_each_file(i, d) {
  539. d->session = perf_session__new(&d->file, false, &tool);
  540. if (!d->session) {
  541. pr_err("Failed to open %s\n", d->file.path);
  542. ret = -ENOMEM;
  543. goto out_delete;
  544. }
  545. ret = perf_session__process_events(d->session, &tool);
  546. if (ret) {
  547. pr_err("Failed to process %s\n", d->file.path);
  548. goto out_delete;
  549. }
  550. perf_evlist__collapse_resort(d->session->evlist);
  551. }
  552. data_process();
  553. out_delete:
  554. data__for_each_file(i, d) {
  555. if (d->session)
  556. perf_session__delete(d->session);
  557. data__free(d);
  558. }
  559. free(data__files);
  560. return ret;
  561. }
  562. static const char * const diff_usage[] = {
  563. "perf diff [<options>] [old_file] [new_file]",
  564. NULL,
  565. };
  566. static const struct option options[] = {
  567. OPT_INCR('v', "verbose", &verbose,
  568. "be more verbose (show symbol address, etc)"),
  569. OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
  570. "Show only items with match in baseline"),
  571. OPT_CALLBACK('c', "compute", &compute,
  572. "delta,ratio,wdiff:w1,w2 (default delta)",
  573. "Entries differential computation selection",
  574. setup_compute),
  575. OPT_BOOLEAN('p', "period", &show_period,
  576. "Show period values."),
  577. OPT_BOOLEAN('F', "formula", &show_formula,
  578. "Show formula."),
  579. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  580. "dump raw trace in ASCII"),
  581. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  582. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  583. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  584. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  585. "only consider symbols in these dsos"),
  586. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  587. "only consider symbols in these comms"),
  588. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  589. "only consider these symbols"),
  590. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  591. "sort by key(s): pid, comm, dso, symbol, parent"),
  592. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  593. "separator for columns, no spaces will be added between "
  594. "columns '.' is reserved."),
  595. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  596. "Look for files with symbols relative to this directory"),
  597. OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
  598. OPT_END()
  599. };
  600. static double baseline_percent(struct hist_entry *he)
  601. {
  602. struct hists *hists = he->hists;
  603. return 100.0 * he->stat.period / hists->stats.total_period;
  604. }
  605. static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
  606. struct perf_hpp *hpp, struct hist_entry *he)
  607. {
  608. struct diff_hpp_fmt *dfmt =
  609. container_of(fmt, struct diff_hpp_fmt, fmt);
  610. double percent = baseline_percent(he);
  611. char pfmt[20] = " ";
  612. if (!he->dummy) {
  613. scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
  614. return percent_color_snprintf(hpp->buf, hpp->size,
  615. pfmt, percent);
  616. } else
  617. return scnprintf(hpp->buf, hpp->size, "%*s",
  618. dfmt->header_width, pfmt);
  619. }
  620. static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
  621. {
  622. double percent = baseline_percent(he);
  623. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  624. int ret = 0;
  625. if (!he->dummy)
  626. ret = scnprintf(buf, size, fmt, percent);
  627. return ret;
  628. }
  629. static void
  630. hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
  631. {
  632. switch (idx) {
  633. case PERF_HPP_DIFF__PERIOD_BASELINE:
  634. scnprintf(buf, size, "%" PRIu64, he->stat.period);
  635. break;
  636. default:
  637. break;
  638. }
  639. }
  640. static void
  641. hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
  642. int idx, char *buf, size_t size)
  643. {
  644. double diff;
  645. double ratio;
  646. s64 wdiff;
  647. switch (idx) {
  648. case PERF_HPP_DIFF__DELTA:
  649. if (pair->diff.computed)
  650. diff = pair->diff.period_ratio_delta;
  651. else
  652. diff = compute_delta(he, pair);
  653. if (fabs(diff) >= 0.01)
  654. scnprintf(buf, size, "%+4.2F%%", diff);
  655. break;
  656. case PERF_HPP_DIFF__RATIO:
  657. /* No point for ratio number if we are dummy.. */
  658. if (he->dummy)
  659. break;
  660. if (pair->diff.computed)
  661. ratio = pair->diff.period_ratio;
  662. else
  663. ratio = compute_ratio(he, pair);
  664. if (ratio > 0.0)
  665. scnprintf(buf, size, "%14.6F", ratio);
  666. break;
  667. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  668. /* No point for wdiff number if we are dummy.. */
  669. if (he->dummy)
  670. break;
  671. if (pair->diff.computed)
  672. wdiff = pair->diff.wdiff;
  673. else
  674. wdiff = compute_wdiff(he, pair);
  675. if (wdiff != 0)
  676. scnprintf(buf, size, "%14ld", wdiff);
  677. break;
  678. case PERF_HPP_DIFF__FORMULA:
  679. formula_fprintf(he, pair, buf, size);
  680. break;
  681. case PERF_HPP_DIFF__PERIOD:
  682. scnprintf(buf, size, "%" PRIu64, pair->stat.period);
  683. break;
  684. default:
  685. BUG_ON(1);
  686. };
  687. }
  688. static void
  689. __hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
  690. char *buf, size_t size)
  691. {
  692. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  693. int idx = dfmt->idx;
  694. /* baseline is special */
  695. if (idx == PERF_HPP_DIFF__BASELINE)
  696. hpp__entry_baseline(he, buf, size);
  697. else {
  698. if (pair)
  699. hpp__entry_pair(he, pair, idx, buf, size);
  700. else
  701. hpp__entry_unpair(he, idx, buf, size);
  702. }
  703. }
  704. static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
  705. struct hist_entry *he)
  706. {
  707. struct diff_hpp_fmt *dfmt =
  708. container_of(_fmt, struct diff_hpp_fmt, fmt);
  709. char buf[MAX_COL_WIDTH] = " ";
  710. __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
  711. if (symbol_conf.field_sep)
  712. return scnprintf(hpp->buf, hpp->size, "%s", buf);
  713. else
  714. return scnprintf(hpp->buf, hpp->size, "%*s",
  715. dfmt->header_width, buf);
  716. }
  717. static int hpp__header(struct perf_hpp_fmt *fmt,
  718. struct perf_hpp *hpp)
  719. {
  720. struct diff_hpp_fmt *dfmt =
  721. container_of(fmt, struct diff_hpp_fmt, fmt);
  722. BUG_ON(!dfmt->header);
  723. return scnprintf(hpp->buf, hpp->size, dfmt->header);
  724. }
  725. static int hpp__width(struct perf_hpp_fmt *fmt,
  726. struct perf_hpp *hpp __maybe_unused)
  727. {
  728. struct diff_hpp_fmt *dfmt =
  729. container_of(fmt, struct diff_hpp_fmt, fmt);
  730. BUG_ON(dfmt->header_width <= 0);
  731. return dfmt->header_width;
  732. }
  733. static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
  734. {
  735. #define MAX_HEADER_NAME 100
  736. char buf_indent[MAX_HEADER_NAME];
  737. char buf[MAX_HEADER_NAME];
  738. const char *header = NULL;
  739. int width = 0;
  740. BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
  741. header = columns[dfmt->idx].name;
  742. width = columns[dfmt->idx].width;
  743. /* Only our defined HPP fmts should appear here. */
  744. BUG_ON(!header);
  745. if (data__files_cnt > 2)
  746. scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
  747. #define NAME (data__files_cnt > 2 ? buf : header)
  748. dfmt->header_width = width;
  749. width = (int) strlen(NAME);
  750. if (dfmt->header_width < width)
  751. dfmt->header_width = width;
  752. scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
  753. dfmt->header_width, NAME);
  754. dfmt->header = strdup(buf_indent);
  755. #undef MAX_HEADER_NAME
  756. #undef NAME
  757. }
  758. static void data__hpp_register(struct data__file *d, int idx)
  759. {
  760. struct diff_hpp_fmt *dfmt = &d->fmt[idx];
  761. struct perf_hpp_fmt *fmt = &dfmt->fmt;
  762. dfmt->idx = idx;
  763. fmt->header = hpp__header;
  764. fmt->width = hpp__width;
  765. fmt->entry = hpp__entry_global;
  766. /* TODO more colors */
  767. if (idx == PERF_HPP_DIFF__BASELINE)
  768. fmt->color = hpp__color_baseline;
  769. init_header(d, dfmt);
  770. perf_hpp__column_register(fmt);
  771. }
  772. static void ui_init(void)
  773. {
  774. struct data__file *d;
  775. int i;
  776. data__for_each_file(i, d) {
  777. /*
  778. * Baseline or compute realted columns:
  779. *
  780. * PERF_HPP_DIFF__BASELINE
  781. * PERF_HPP_DIFF__DELTA
  782. * PERF_HPP_DIFF__RATIO
  783. * PERF_HPP_DIFF__WEIGHTED_DIFF
  784. */
  785. data__hpp_register(d, i ? compute_2_hpp[compute] :
  786. PERF_HPP_DIFF__BASELINE);
  787. /*
  788. * And the rest:
  789. *
  790. * PERF_HPP_DIFF__FORMULA
  791. * PERF_HPP_DIFF__PERIOD
  792. * PERF_HPP_DIFF__PERIOD_BASELINE
  793. */
  794. if (show_formula && i)
  795. data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
  796. if (show_period)
  797. data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
  798. PERF_HPP_DIFF__PERIOD_BASELINE);
  799. }
  800. }
  801. static int data_init(int argc, const char **argv)
  802. {
  803. struct data__file *d;
  804. static const char *defaults[] = {
  805. "perf.data.old",
  806. "perf.data",
  807. };
  808. bool use_default = true;
  809. int i;
  810. data__files_cnt = 2;
  811. if (argc) {
  812. if (argc == 1)
  813. defaults[1] = argv[0];
  814. else {
  815. data__files_cnt = argc;
  816. use_default = false;
  817. }
  818. } else if (symbol_conf.default_guest_vmlinux_name ||
  819. symbol_conf.default_guest_kallsyms) {
  820. defaults[0] = "perf.data.host";
  821. defaults[1] = "perf.data.guest";
  822. }
  823. if (sort_compute >= (unsigned int) data__files_cnt) {
  824. pr_err("Order option out of limit.\n");
  825. return -EINVAL;
  826. }
  827. data__files = zalloc(sizeof(*data__files) * data__files_cnt);
  828. if (!data__files)
  829. return -ENOMEM;
  830. data__for_each_file(i, d) {
  831. struct perf_data_file *file = &d->file;
  832. file->path = use_default ? defaults[i] : argv[i];
  833. file->mode = PERF_DATA_MODE_READ,
  834. file->force = force,
  835. d->idx = i;
  836. }
  837. return 0;
  838. }
  839. int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
  840. {
  841. sort_order = diff__default_sort_order;
  842. argc = parse_options(argc, argv, options, diff_usage, 0);
  843. if (symbol__init() < 0)
  844. return -1;
  845. if (data_init(argc, argv) < 0)
  846. return -1;
  847. ui_init();
  848. if (setup_sorting() < 0)
  849. usage_with_options(diff_usage, options);
  850. setup_pager();
  851. sort__setup_elide(NULL);
  852. return __cmd_diff();
  853. }