builtin-diff.c 23 KB

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