builtin-diff.c 25 KB

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