builtin-diff.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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, period, weight, transaction) != NULL)
  256. return 0;
  257. return -ENOMEM;
  258. }
  259. static int diff__process_sample_event(struct perf_tool *tool __maybe_unused,
  260. union perf_event *event,
  261. struct perf_sample *sample,
  262. struct perf_evsel *evsel,
  263. struct machine *machine)
  264. {
  265. struct addr_location al;
  266. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  267. pr_warning("problem processing %d event, skipping it.\n",
  268. event->header.type);
  269. return -1;
  270. }
  271. if (al.filtered)
  272. return 0;
  273. if (hists__add_entry(&evsel->hists, &al, sample->period,
  274. sample->weight, sample->transaction)) {
  275. pr_warning("problem incrementing symbol period, skipping event\n");
  276. return -1;
  277. }
  278. evsel->hists.stats.total_period += sample->period;
  279. return 0;
  280. }
  281. static struct perf_tool tool = {
  282. .sample = diff__process_sample_event,
  283. .mmap = perf_event__process_mmap,
  284. .comm = perf_event__process_comm,
  285. .exit = perf_event__process_exit,
  286. .fork = perf_event__process_fork,
  287. .lost = perf_event__process_lost,
  288. .ordered_samples = true,
  289. .ordering_requires_timestamps = true,
  290. };
  291. static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
  292. struct perf_evlist *evlist)
  293. {
  294. struct perf_evsel *e;
  295. list_for_each_entry(e, &evlist->entries, node)
  296. if (perf_evsel__match2(evsel, e))
  297. return e;
  298. return NULL;
  299. }
  300. static void perf_evlist__collapse_resort(struct perf_evlist *evlist)
  301. {
  302. struct perf_evsel *evsel;
  303. list_for_each_entry(evsel, &evlist->entries, node) {
  304. struct hists *hists = &evsel->hists;
  305. hists__collapse_resort(hists, NULL);
  306. }
  307. }
  308. static struct hist_entry*
  309. get_pair_data(struct hist_entry *he, struct data__file *d)
  310. {
  311. if (hist_entry__has_pairs(he)) {
  312. struct hist_entry *pair;
  313. list_for_each_entry(pair, &he->pairs.head, pairs.node)
  314. if (pair->hists == d->hists)
  315. return pair;
  316. }
  317. return NULL;
  318. }
  319. static struct hist_entry*
  320. get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt)
  321. {
  322. void *ptr = dfmt - dfmt->idx;
  323. struct data__file *d = container_of(ptr, struct data__file, fmt);
  324. return get_pair_data(he, d);
  325. }
  326. static void hists__baseline_only(struct hists *hists)
  327. {
  328. struct rb_root *root;
  329. struct rb_node *next;
  330. if (sort__need_collapse)
  331. root = &hists->entries_collapsed;
  332. else
  333. root = hists->entries_in;
  334. next = rb_first(root);
  335. while (next != NULL) {
  336. struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
  337. next = rb_next(&he->rb_node_in);
  338. if (!hist_entry__next_pair(he)) {
  339. rb_erase(&he->rb_node_in, root);
  340. hist_entry__free(he);
  341. }
  342. }
  343. }
  344. static void hists__precompute(struct hists *hists)
  345. {
  346. struct rb_root *root;
  347. struct rb_node *next;
  348. if (sort__need_collapse)
  349. root = &hists->entries_collapsed;
  350. else
  351. root = hists->entries_in;
  352. next = rb_first(root);
  353. while (next != NULL) {
  354. struct hist_entry *he, *pair;
  355. he = rb_entry(next, struct hist_entry, rb_node_in);
  356. next = rb_next(&he->rb_node_in);
  357. pair = get_pair_data(he, &data__files[sort_compute]);
  358. if (!pair)
  359. continue;
  360. switch (compute) {
  361. case COMPUTE_DELTA:
  362. compute_delta(he, pair);
  363. break;
  364. case COMPUTE_RATIO:
  365. compute_ratio(he, pair);
  366. break;
  367. case COMPUTE_WEIGHTED_DIFF:
  368. compute_wdiff(he, pair);
  369. break;
  370. default:
  371. BUG_ON(1);
  372. }
  373. }
  374. }
  375. static int64_t cmp_doubles(double l, double r)
  376. {
  377. if (l > r)
  378. return -1;
  379. else if (l < r)
  380. return 1;
  381. else
  382. return 0;
  383. }
  384. static int64_t
  385. __hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  386. int c)
  387. {
  388. switch (c) {
  389. case COMPUTE_DELTA:
  390. {
  391. double l = left->diff.period_ratio_delta;
  392. double r = right->diff.period_ratio_delta;
  393. return cmp_doubles(l, r);
  394. }
  395. case COMPUTE_RATIO:
  396. {
  397. double l = left->diff.period_ratio;
  398. double r = right->diff.period_ratio;
  399. return cmp_doubles(l, r);
  400. }
  401. case COMPUTE_WEIGHTED_DIFF:
  402. {
  403. s64 l = left->diff.wdiff;
  404. s64 r = right->diff.wdiff;
  405. return r - l;
  406. }
  407. default:
  408. BUG_ON(1);
  409. }
  410. return 0;
  411. }
  412. static int64_t
  413. hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  414. int c)
  415. {
  416. bool pairs_left = hist_entry__has_pairs(left);
  417. bool pairs_right = hist_entry__has_pairs(right);
  418. struct hist_entry *p_right, *p_left;
  419. if (!pairs_left && !pairs_right)
  420. return 0;
  421. if (!pairs_left || !pairs_right)
  422. return pairs_left ? -1 : 1;
  423. p_left = get_pair_data(left, &data__files[sort_compute]);
  424. p_right = get_pair_data(right, &data__files[sort_compute]);
  425. if (!p_left && !p_right)
  426. return 0;
  427. if (!p_left || !p_right)
  428. return p_left ? -1 : 1;
  429. /*
  430. * We have 2 entries of same kind, let's
  431. * make the data comparison.
  432. */
  433. return __hist_entry__cmp_compute(p_left, p_right, c);
  434. }
  435. static void insert_hist_entry_by_compute(struct rb_root *root,
  436. struct hist_entry *he,
  437. int c)
  438. {
  439. struct rb_node **p = &root->rb_node;
  440. struct rb_node *parent = NULL;
  441. struct hist_entry *iter;
  442. while (*p != NULL) {
  443. parent = *p;
  444. iter = rb_entry(parent, struct hist_entry, rb_node);
  445. if (hist_entry__cmp_compute(he, iter, c) < 0)
  446. p = &(*p)->rb_left;
  447. else
  448. p = &(*p)->rb_right;
  449. }
  450. rb_link_node(&he->rb_node, parent, p);
  451. rb_insert_color(&he->rb_node, root);
  452. }
  453. static void hists__compute_resort(struct hists *hists)
  454. {
  455. struct rb_root *root;
  456. struct rb_node *next;
  457. if (sort__need_collapse)
  458. root = &hists->entries_collapsed;
  459. else
  460. root = hists->entries_in;
  461. hists->entries = RB_ROOT;
  462. next = rb_first(root);
  463. hists->nr_entries = 0;
  464. hists->stats.total_period = 0;
  465. hists__reset_col_len(hists);
  466. while (next != NULL) {
  467. struct hist_entry *he;
  468. he = rb_entry(next, struct hist_entry, rb_node_in);
  469. next = rb_next(&he->rb_node_in);
  470. insert_hist_entry_by_compute(&hists->entries, he, compute);
  471. hists__inc_nr_entries(hists, he);
  472. }
  473. }
  474. static void hists__process(struct hists *hists)
  475. {
  476. if (show_baseline_only)
  477. hists__baseline_only(hists);
  478. if (sort_compute) {
  479. hists__precompute(hists);
  480. hists__compute_resort(hists);
  481. } else {
  482. hists__output_resort(hists);
  483. }
  484. hists__fprintf(hists, true, 0, 0, 0, stdout);
  485. }
  486. static void data__fprintf(void)
  487. {
  488. struct data__file *d;
  489. int i;
  490. fprintf(stdout, "# Data files:\n");
  491. data__for_each_file(i, d)
  492. fprintf(stdout, "# [%d] %s %s\n",
  493. d->idx, d->file.path,
  494. !d->idx ? "(Baseline)" : "");
  495. fprintf(stdout, "#\n");
  496. }
  497. static void data_process(void)
  498. {
  499. struct perf_evlist *evlist_base = data__files[0].session->evlist;
  500. struct perf_evsel *evsel_base;
  501. bool first = true;
  502. list_for_each_entry(evsel_base, &evlist_base->entries, node) {
  503. struct data__file *d;
  504. int i;
  505. data__for_each_file_new(i, d) {
  506. struct perf_evlist *evlist = d->session->evlist;
  507. struct perf_evsel *evsel;
  508. evsel = evsel_match(evsel_base, evlist);
  509. if (!evsel)
  510. continue;
  511. d->hists = &evsel->hists;
  512. hists__match(&evsel_base->hists, &evsel->hists);
  513. if (!show_baseline_only)
  514. hists__link(&evsel_base->hists,
  515. &evsel->hists);
  516. }
  517. fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
  518. perf_evsel__name(evsel_base));
  519. first = false;
  520. if (verbose || data__files_cnt > 2)
  521. data__fprintf();
  522. hists__process(&evsel_base->hists);
  523. }
  524. }
  525. static void data__free(struct data__file *d)
  526. {
  527. int col;
  528. for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) {
  529. struct diff_hpp_fmt *fmt = &d->fmt[col];
  530. free(fmt->header);
  531. }
  532. }
  533. static int __cmd_diff(void)
  534. {
  535. struct data__file *d;
  536. int ret = -EINVAL, i;
  537. data__for_each_file(i, d) {
  538. d->session = perf_session__new(&d->file, false, &tool);
  539. if (!d->session) {
  540. pr_err("Failed to open %s\n", d->file.path);
  541. ret = -ENOMEM;
  542. goto out_delete;
  543. }
  544. ret = perf_session__process_events(d->session, &tool);
  545. if (ret) {
  546. pr_err("Failed to process %s\n", d->file.path);
  547. goto out_delete;
  548. }
  549. perf_evlist__collapse_resort(d->session->evlist);
  550. }
  551. data_process();
  552. out_delete:
  553. data__for_each_file(i, d) {
  554. if (d->session)
  555. perf_session__delete(d->session);
  556. data__free(d);
  557. }
  558. free(data__files);
  559. return ret;
  560. }
  561. static const char * const diff_usage[] = {
  562. "perf diff [<options>] [old_file] [new_file]",
  563. NULL,
  564. };
  565. static const struct option options[] = {
  566. OPT_INCR('v', "verbose", &verbose,
  567. "be more verbose (show symbol address, etc)"),
  568. OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
  569. "Show only items with match in baseline"),
  570. OPT_CALLBACK('c', "compute", &compute,
  571. "delta,ratio,wdiff:w1,w2 (default delta)",
  572. "Entries differential computation selection",
  573. setup_compute),
  574. OPT_BOOLEAN('p', "period", &show_period,
  575. "Show period values."),
  576. OPT_BOOLEAN('F', "formula", &show_formula,
  577. "Show formula."),
  578. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  579. "dump raw trace in ASCII"),
  580. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  581. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  582. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  583. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  584. "only consider symbols in these dsos"),
  585. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  586. "only consider symbols in these comms"),
  587. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  588. "only consider these symbols"),
  589. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  590. "sort by key(s): pid, comm, dso, symbol, parent"),
  591. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  592. "separator for columns, no spaces will be added between "
  593. "columns '.' is reserved."),
  594. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  595. "Look for files with symbols relative to this directory"),
  596. OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
  597. OPT_END()
  598. };
  599. static double baseline_percent(struct hist_entry *he)
  600. {
  601. struct hists *hists = he->hists;
  602. return 100.0 * he->stat.period / hists->stats.total_period;
  603. }
  604. static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
  605. struct perf_hpp *hpp, struct hist_entry *he)
  606. {
  607. struct diff_hpp_fmt *dfmt =
  608. container_of(fmt, struct diff_hpp_fmt, fmt);
  609. double percent = baseline_percent(he);
  610. char pfmt[20] = " ";
  611. if (!he->dummy) {
  612. scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
  613. return percent_color_snprintf(hpp->buf, hpp->size,
  614. pfmt, percent);
  615. } else
  616. return scnprintf(hpp->buf, hpp->size, "%*s",
  617. dfmt->header_width, pfmt);
  618. }
  619. static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
  620. {
  621. double percent = baseline_percent(he);
  622. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  623. int ret = 0;
  624. if (!he->dummy)
  625. ret = scnprintf(buf, size, fmt, percent);
  626. return ret;
  627. }
  628. static void
  629. hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
  630. {
  631. switch (idx) {
  632. case PERF_HPP_DIFF__PERIOD_BASELINE:
  633. scnprintf(buf, size, "%" PRIu64, he->stat.period);
  634. break;
  635. default:
  636. break;
  637. }
  638. }
  639. static void
  640. hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
  641. int idx, char *buf, size_t size)
  642. {
  643. double diff;
  644. double ratio;
  645. s64 wdiff;
  646. switch (idx) {
  647. case PERF_HPP_DIFF__DELTA:
  648. if (pair->diff.computed)
  649. diff = pair->diff.period_ratio_delta;
  650. else
  651. diff = compute_delta(he, pair);
  652. if (fabs(diff) >= 0.01)
  653. scnprintf(buf, size, "%+4.2F%%", diff);
  654. break;
  655. case PERF_HPP_DIFF__RATIO:
  656. /* No point for ratio number if we are dummy.. */
  657. if (he->dummy)
  658. break;
  659. if (pair->diff.computed)
  660. ratio = pair->diff.period_ratio;
  661. else
  662. ratio = compute_ratio(he, pair);
  663. if (ratio > 0.0)
  664. scnprintf(buf, size, "%14.6F", ratio);
  665. break;
  666. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  667. /* No point for wdiff number if we are dummy.. */
  668. if (he->dummy)
  669. break;
  670. if (pair->diff.computed)
  671. wdiff = pair->diff.wdiff;
  672. else
  673. wdiff = compute_wdiff(he, pair);
  674. if (wdiff != 0)
  675. scnprintf(buf, size, "%14ld", wdiff);
  676. break;
  677. case PERF_HPP_DIFF__FORMULA:
  678. formula_fprintf(he, pair, buf, size);
  679. break;
  680. case PERF_HPP_DIFF__PERIOD:
  681. scnprintf(buf, size, "%" PRIu64, pair->stat.period);
  682. break;
  683. default:
  684. BUG_ON(1);
  685. };
  686. }
  687. static void
  688. __hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
  689. char *buf, size_t size)
  690. {
  691. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  692. int idx = dfmt->idx;
  693. /* baseline is special */
  694. if (idx == PERF_HPP_DIFF__BASELINE)
  695. hpp__entry_baseline(he, buf, size);
  696. else {
  697. if (pair)
  698. hpp__entry_pair(he, pair, idx, buf, size);
  699. else
  700. hpp__entry_unpair(he, idx, buf, size);
  701. }
  702. }
  703. static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
  704. struct hist_entry *he)
  705. {
  706. struct diff_hpp_fmt *dfmt =
  707. container_of(_fmt, struct diff_hpp_fmt, fmt);
  708. char buf[MAX_COL_WIDTH] = " ";
  709. __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
  710. if (symbol_conf.field_sep)
  711. return scnprintf(hpp->buf, hpp->size, "%s", buf);
  712. else
  713. return scnprintf(hpp->buf, hpp->size, "%*s",
  714. dfmt->header_width, buf);
  715. }
  716. static int hpp__header(struct perf_hpp_fmt *fmt,
  717. struct perf_hpp *hpp)
  718. {
  719. struct diff_hpp_fmt *dfmt =
  720. container_of(fmt, struct diff_hpp_fmt, fmt);
  721. BUG_ON(!dfmt->header);
  722. return scnprintf(hpp->buf, hpp->size, dfmt->header);
  723. }
  724. static int hpp__width(struct perf_hpp_fmt *fmt,
  725. struct perf_hpp *hpp __maybe_unused)
  726. {
  727. struct diff_hpp_fmt *dfmt =
  728. container_of(fmt, struct diff_hpp_fmt, fmt);
  729. BUG_ON(dfmt->header_width <= 0);
  730. return dfmt->header_width;
  731. }
  732. static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
  733. {
  734. #define MAX_HEADER_NAME 100
  735. char buf_indent[MAX_HEADER_NAME];
  736. char buf[MAX_HEADER_NAME];
  737. const char *header = NULL;
  738. int width = 0;
  739. BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
  740. header = columns[dfmt->idx].name;
  741. width = columns[dfmt->idx].width;
  742. /* Only our defined HPP fmts should appear here. */
  743. BUG_ON(!header);
  744. if (data__files_cnt > 2)
  745. scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
  746. #define NAME (data__files_cnt > 2 ? buf : header)
  747. dfmt->header_width = width;
  748. width = (int) strlen(NAME);
  749. if (dfmt->header_width < width)
  750. dfmt->header_width = width;
  751. scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
  752. dfmt->header_width, NAME);
  753. dfmt->header = strdup(buf_indent);
  754. #undef MAX_HEADER_NAME
  755. #undef NAME
  756. }
  757. static void data__hpp_register(struct data__file *d, int idx)
  758. {
  759. struct diff_hpp_fmt *dfmt = &d->fmt[idx];
  760. struct perf_hpp_fmt *fmt = &dfmt->fmt;
  761. dfmt->idx = idx;
  762. fmt->header = hpp__header;
  763. fmt->width = hpp__width;
  764. fmt->entry = hpp__entry_global;
  765. /* TODO more colors */
  766. if (idx == PERF_HPP_DIFF__BASELINE)
  767. fmt->color = hpp__color_baseline;
  768. init_header(d, dfmt);
  769. perf_hpp__column_register(fmt);
  770. }
  771. static void ui_init(void)
  772. {
  773. struct data__file *d;
  774. int i;
  775. data__for_each_file(i, d) {
  776. /*
  777. * Baseline or compute realted columns:
  778. *
  779. * PERF_HPP_DIFF__BASELINE
  780. * PERF_HPP_DIFF__DELTA
  781. * PERF_HPP_DIFF__RATIO
  782. * PERF_HPP_DIFF__WEIGHTED_DIFF
  783. */
  784. data__hpp_register(d, i ? compute_2_hpp[compute] :
  785. PERF_HPP_DIFF__BASELINE);
  786. /*
  787. * And the rest:
  788. *
  789. * PERF_HPP_DIFF__FORMULA
  790. * PERF_HPP_DIFF__PERIOD
  791. * PERF_HPP_DIFF__PERIOD_BASELINE
  792. */
  793. if (show_formula && i)
  794. data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
  795. if (show_period)
  796. data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
  797. PERF_HPP_DIFF__PERIOD_BASELINE);
  798. }
  799. }
  800. static int data_init(int argc, const char **argv)
  801. {
  802. struct data__file *d;
  803. static const char *defaults[] = {
  804. "perf.data.old",
  805. "perf.data",
  806. };
  807. bool use_default = true;
  808. int i;
  809. data__files_cnt = 2;
  810. if (argc) {
  811. if (argc == 1)
  812. defaults[1] = argv[0];
  813. else {
  814. data__files_cnt = argc;
  815. use_default = false;
  816. }
  817. } else if (symbol_conf.default_guest_vmlinux_name ||
  818. symbol_conf.default_guest_kallsyms) {
  819. defaults[0] = "perf.data.host";
  820. defaults[1] = "perf.data.guest";
  821. }
  822. if (sort_compute >= (unsigned int) data__files_cnt) {
  823. pr_err("Order option out of limit.\n");
  824. return -EINVAL;
  825. }
  826. data__files = zalloc(sizeof(*data__files) * data__files_cnt);
  827. if (!data__files)
  828. return -ENOMEM;
  829. data__for_each_file(i, d) {
  830. struct perf_data_file *file = &d->file;
  831. file->path = use_default ? defaults[i] : argv[i];
  832. file->mode = PERF_DATA_MODE_READ,
  833. file->force = force,
  834. d->idx = i;
  835. }
  836. return 0;
  837. }
  838. int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
  839. {
  840. sort_order = diff__default_sort_order;
  841. argc = parse_options(argc, argv, options, diff_usage, 0);
  842. if (symbol__init() < 0)
  843. return -1;
  844. if (data_init(argc, argv) < 0)
  845. return -1;
  846. ui_init();
  847. if (setup_sorting() < 0)
  848. usage_with_options(diff_usage, options);
  849. setup_pager();
  850. sort__setup_elide(NULL);
  851. return __cmd_diff();
  852. }