builtin-diff.c 26 KB

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