builtin-diff.c 25 KB

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