builtin-diff.c 26 KB

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