builtin-diff.c 22 KB

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