builtin-diff.c 21 KB

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