builtin-diff.c 21 KB

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