builtin-diff.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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__DELTA_ABS,
  31. PERF_HPP_DIFF__MAX_INDEX
  32. };
  33. struct diff_hpp_fmt {
  34. struct perf_hpp_fmt fmt;
  35. int idx;
  36. char *header;
  37. int header_width;
  38. };
  39. struct data__file {
  40. struct perf_session *session;
  41. struct perf_data_file file;
  42. int idx;
  43. struct hists *hists;
  44. struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX];
  45. };
  46. static struct data__file *data__files;
  47. static int data__files_cnt;
  48. #define data__for_each_file_start(i, d, s) \
  49. for (i = s, d = &data__files[s]; \
  50. i < data__files_cnt; \
  51. i++, d = &data__files[i])
  52. #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
  53. #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
  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_DELTA_ABS,
  66. COMPUTE_MAX,
  67. };
  68. const char *compute_names[COMPUTE_MAX] = {
  69. [COMPUTE_DELTA] = "delta",
  70. [COMPUTE_DELTA_ABS] = "delta-abs",
  71. [COMPUTE_RATIO] = "ratio",
  72. [COMPUTE_WEIGHTED_DIFF] = "wdiff",
  73. };
  74. static int compute;
  75. static int compute_2_hpp[COMPUTE_MAX] = {
  76. [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA,
  77. [COMPUTE_DELTA_ABS] = PERF_HPP_DIFF__DELTA_ABS,
  78. [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO,
  79. [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF,
  80. };
  81. #define MAX_COL_WIDTH 70
  82. static struct header_column {
  83. const char *name;
  84. int width;
  85. } columns[PERF_HPP_DIFF__MAX_INDEX] = {
  86. [PERF_HPP_DIFF__BASELINE] = {
  87. .name = "Baseline",
  88. },
  89. [PERF_HPP_DIFF__PERIOD] = {
  90. .name = "Period",
  91. .width = 14,
  92. },
  93. [PERF_HPP_DIFF__PERIOD_BASELINE] = {
  94. .name = "Base period",
  95. .width = 14,
  96. },
  97. [PERF_HPP_DIFF__DELTA] = {
  98. .name = "Delta",
  99. .width = 7,
  100. },
  101. [PERF_HPP_DIFF__DELTA_ABS] = {
  102. .name = "Delta Abs",
  103. .width = 7,
  104. },
  105. [PERF_HPP_DIFF__RATIO] = {
  106. .name = "Ratio",
  107. .width = 14,
  108. },
  109. [PERF_HPP_DIFF__WEIGHTED_DIFF] = {
  110. .name = "Weighted diff",
  111. .width = 14,
  112. },
  113. [PERF_HPP_DIFF__FORMULA] = {
  114. .name = "Formula",
  115. .width = MAX_COL_WIDTH,
  116. }
  117. };
  118. static int setup_compute_opt_wdiff(char *opt)
  119. {
  120. char *w1_str = opt;
  121. char *w2_str;
  122. int ret = -EINVAL;
  123. if (!opt)
  124. goto out;
  125. w2_str = strchr(opt, ',');
  126. if (!w2_str)
  127. goto out;
  128. *w2_str++ = 0x0;
  129. if (!*w2_str)
  130. goto out;
  131. compute_wdiff_w1 = strtol(w1_str, NULL, 10);
  132. compute_wdiff_w2 = strtol(w2_str, NULL, 10);
  133. if (!compute_wdiff_w1 || !compute_wdiff_w2)
  134. goto out;
  135. pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
  136. compute_wdiff_w1, compute_wdiff_w2);
  137. ret = 0;
  138. out:
  139. if (ret)
  140. pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
  141. return ret;
  142. }
  143. static int setup_compute_opt(char *opt)
  144. {
  145. if (compute == COMPUTE_WEIGHTED_DIFF)
  146. return setup_compute_opt_wdiff(opt);
  147. if (opt) {
  148. pr_err("Failed: extra option specified '%s'", opt);
  149. return -EINVAL;
  150. }
  151. return 0;
  152. }
  153. static int setup_compute(const struct option *opt, const char *str,
  154. int unset __maybe_unused)
  155. {
  156. int *cp = (int *) opt->value;
  157. char *cstr = (char *) str;
  158. char buf[50];
  159. unsigned i;
  160. char *option;
  161. if (!str) {
  162. *cp = COMPUTE_DELTA;
  163. return 0;
  164. }
  165. option = strchr(str, ':');
  166. if (option) {
  167. unsigned len = option++ - str;
  168. /*
  169. * The str data are not writeable, so we need
  170. * to use another buffer.
  171. */
  172. /* No option value is longer. */
  173. if (len >= sizeof(buf))
  174. return -EINVAL;
  175. strncpy(buf, str, len);
  176. buf[len] = 0x0;
  177. cstr = buf;
  178. }
  179. for (i = 0; i < COMPUTE_MAX; i++)
  180. if (!strcmp(cstr, compute_names[i])) {
  181. *cp = i;
  182. return setup_compute_opt(option);
  183. }
  184. pr_err("Failed: '%s' is not computation method "
  185. "(use 'delta','ratio' or 'wdiff')\n", str);
  186. return -EINVAL;
  187. }
  188. static double period_percent(struct hist_entry *he, u64 period)
  189. {
  190. u64 total = hists__total_period(he->hists);
  191. return (period * 100.0) / total;
  192. }
  193. static double compute_delta(struct hist_entry *he, struct hist_entry *pair)
  194. {
  195. double old_percent = period_percent(he, he->stat.period);
  196. double new_percent = period_percent(pair, pair->stat.period);
  197. pair->diff.period_ratio_delta = new_percent - old_percent;
  198. pair->diff.computed = true;
  199. return pair->diff.period_ratio_delta;
  200. }
  201. static double compute_ratio(struct hist_entry *he, struct hist_entry *pair)
  202. {
  203. double old_period = he->stat.period ?: 1;
  204. double new_period = pair->stat.period;
  205. pair->diff.computed = true;
  206. pair->diff.period_ratio = new_period / old_period;
  207. return pair->diff.period_ratio;
  208. }
  209. static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair)
  210. {
  211. u64 old_period = he->stat.period;
  212. u64 new_period = pair->stat.period;
  213. pair->diff.computed = true;
  214. pair->diff.wdiff = new_period * compute_wdiff_w2 -
  215. old_period * compute_wdiff_w1;
  216. return pair->diff.wdiff;
  217. }
  218. static int formula_delta(struct hist_entry *he, struct hist_entry *pair,
  219. char *buf, size_t size)
  220. {
  221. u64 he_total = he->hists->stats.total_period;
  222. u64 pair_total = pair->hists->stats.total_period;
  223. if (symbol_conf.filter_relative) {
  224. he_total = he->hists->stats.total_non_filtered_period;
  225. pair_total = pair->hists->stats.total_non_filtered_period;
  226. }
  227. return scnprintf(buf, size,
  228. "(%" PRIu64 " * 100 / %" PRIu64 ") - "
  229. "(%" PRIu64 " * 100 / %" PRIu64 ")",
  230. pair->stat.period, pair_total,
  231. he->stat.period, he_total);
  232. }
  233. static int formula_ratio(struct hist_entry *he, struct hist_entry *pair,
  234. char *buf, size_t size)
  235. {
  236. double old_period = he->stat.period;
  237. double new_period = pair->stat.period;
  238. return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
  239. }
  240. static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair,
  241. char *buf, size_t size)
  242. {
  243. u64 old_period = he->stat.period;
  244. u64 new_period = pair->stat.period;
  245. return scnprintf(buf, size,
  246. "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
  247. new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
  248. }
  249. static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,
  250. char *buf, size_t size)
  251. {
  252. switch (compute) {
  253. case COMPUTE_DELTA:
  254. case COMPUTE_DELTA_ABS:
  255. return formula_delta(he, pair, buf, size);
  256. case COMPUTE_RATIO:
  257. return formula_ratio(he, pair, buf, size);
  258. case COMPUTE_WEIGHTED_DIFF:
  259. return formula_wdiff(he, pair, buf, size);
  260. default:
  261. BUG_ON(1);
  262. }
  263. return -1;
  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. int ret = -1;
  274. if (machine__resolve(machine, &al, sample) < 0) {
  275. pr_warning("problem processing %d event, skipping it.\n",
  276. event->header.type);
  277. return -1;
  278. }
  279. if (!hists__add_entry(hists, &al, NULL, NULL, NULL, sample, true)) {
  280. pr_warning("problem incrementing symbol period, skipping event\n");
  281. goto out_put;
  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. ret = 0;
  293. out_put:
  294. addr_location__put(&al);
  295. return ret;
  296. }
  297. static struct perf_tool tool = {
  298. .sample = diff__process_sample_event,
  299. .mmap = perf_event__process_mmap,
  300. .mmap2 = perf_event__process_mmap2,
  301. .comm = perf_event__process_comm,
  302. .exit = perf_event__process_exit,
  303. .fork = perf_event__process_fork,
  304. .lost = perf_event__process_lost,
  305. .ordered_events = true,
  306. .ordering_requires_timestamps = true,
  307. };
  308. static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
  309. struct perf_evlist *evlist)
  310. {
  311. struct perf_evsel *e;
  312. evlist__for_each_entry(evlist, e) {
  313. if (perf_evsel__match2(evsel, e))
  314. return e;
  315. }
  316. return NULL;
  317. }
  318. static void perf_evlist__collapse_resort(struct perf_evlist *evlist)
  319. {
  320. struct perf_evsel *evsel;
  321. evlist__for_each_entry(evlist, evsel) {
  322. struct hists *hists = evsel__hists(evsel);
  323. hists__collapse_resort(hists, NULL);
  324. }
  325. }
  326. static struct data__file *fmt_to_data_file(struct perf_hpp_fmt *fmt)
  327. {
  328. struct diff_hpp_fmt *dfmt = container_of(fmt, struct diff_hpp_fmt, fmt);
  329. void *ptr = dfmt - dfmt->idx;
  330. struct data__file *d = container_of(ptr, struct data__file, fmt);
  331. return d;
  332. }
  333. static struct hist_entry*
  334. get_pair_data(struct hist_entry *he, struct data__file *d)
  335. {
  336. if (hist_entry__has_pairs(he)) {
  337. struct hist_entry *pair;
  338. list_for_each_entry(pair, &he->pairs.head, pairs.node)
  339. if (pair->hists == d->hists)
  340. return pair;
  341. }
  342. return NULL;
  343. }
  344. static struct hist_entry*
  345. get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt)
  346. {
  347. struct data__file *d = fmt_to_data_file(&dfmt->fmt);
  348. return get_pair_data(he, d);
  349. }
  350. static void hists__baseline_only(struct hists *hists)
  351. {
  352. struct rb_root *root;
  353. struct rb_node *next;
  354. if (hists__has(hists, need_collapse))
  355. root = &hists->entries_collapsed;
  356. else
  357. root = hists->entries_in;
  358. next = rb_first(root);
  359. while (next != NULL) {
  360. struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
  361. next = rb_next(&he->rb_node_in);
  362. if (!hist_entry__next_pair(he)) {
  363. rb_erase(&he->rb_node_in, root);
  364. hist_entry__delete(he);
  365. }
  366. }
  367. }
  368. static void hists__precompute(struct hists *hists)
  369. {
  370. struct rb_root *root;
  371. struct rb_node *next;
  372. if (hists__has(hists, need_collapse))
  373. root = &hists->entries_collapsed;
  374. else
  375. root = hists->entries_in;
  376. next = rb_first(root);
  377. while (next != NULL) {
  378. struct hist_entry *he, *pair;
  379. struct data__file *d;
  380. int i;
  381. he = rb_entry(next, struct hist_entry, rb_node_in);
  382. next = rb_next(&he->rb_node_in);
  383. data__for_each_file_new(i, d) {
  384. pair = get_pair_data(he, d);
  385. if (!pair)
  386. continue;
  387. switch (compute) {
  388. case COMPUTE_DELTA:
  389. case COMPUTE_DELTA_ABS:
  390. compute_delta(he, pair);
  391. break;
  392. case COMPUTE_RATIO:
  393. compute_ratio(he, pair);
  394. break;
  395. case COMPUTE_WEIGHTED_DIFF:
  396. compute_wdiff(he, pair);
  397. break;
  398. default:
  399. BUG_ON(1);
  400. }
  401. }
  402. }
  403. }
  404. static int64_t cmp_doubles(double l, double r)
  405. {
  406. if (l > r)
  407. return -1;
  408. else if (l < r)
  409. return 1;
  410. else
  411. return 0;
  412. }
  413. static int64_t
  414. __hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  415. int c)
  416. {
  417. switch (c) {
  418. case COMPUTE_DELTA:
  419. {
  420. double l = left->diff.period_ratio_delta;
  421. double r = right->diff.period_ratio_delta;
  422. return cmp_doubles(l, r);
  423. }
  424. case COMPUTE_DELTA_ABS:
  425. {
  426. double l = fabs(left->diff.period_ratio_delta);
  427. double r = fabs(right->diff.period_ratio_delta);
  428. return cmp_doubles(l, r);
  429. }
  430. case COMPUTE_RATIO:
  431. {
  432. double l = left->diff.period_ratio;
  433. double r = right->diff.period_ratio;
  434. return cmp_doubles(l, r);
  435. }
  436. case COMPUTE_WEIGHTED_DIFF:
  437. {
  438. s64 l = left->diff.wdiff;
  439. s64 r = right->diff.wdiff;
  440. return r - l;
  441. }
  442. default:
  443. BUG_ON(1);
  444. }
  445. return 0;
  446. }
  447. static int64_t
  448. hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  449. int c, int sort_idx)
  450. {
  451. bool pairs_left = hist_entry__has_pairs(left);
  452. bool pairs_right = hist_entry__has_pairs(right);
  453. struct hist_entry *p_right, *p_left;
  454. if (!pairs_left && !pairs_right)
  455. return 0;
  456. if (!pairs_left || !pairs_right)
  457. return pairs_left ? -1 : 1;
  458. p_left = get_pair_data(left, &data__files[sort_idx]);
  459. p_right = get_pair_data(right, &data__files[sort_idx]);
  460. if (!p_left && !p_right)
  461. return 0;
  462. if (!p_left || !p_right)
  463. return p_left ? -1 : 1;
  464. /*
  465. * We have 2 entries of same kind, let's
  466. * make the data comparison.
  467. */
  468. return __hist_entry__cmp_compute(p_left, p_right, c);
  469. }
  470. static int64_t
  471. hist_entry__cmp_compute_idx(struct hist_entry *left, struct hist_entry *right,
  472. int c, int sort_idx)
  473. {
  474. struct hist_entry *p_right, *p_left;
  475. p_left = get_pair_data(left, &data__files[sort_idx]);
  476. p_right = get_pair_data(right, &data__files[sort_idx]);
  477. if (!p_left && !p_right)
  478. return 0;
  479. if (!p_left || !p_right)
  480. return p_left ? -1 : 1;
  481. if (c != COMPUTE_DELTA && c != COMPUTE_DELTA_ABS) {
  482. /*
  483. * The delta can be computed without the baseline, but
  484. * others are not. Put those entries which have no
  485. * values below.
  486. */
  487. if (left->dummy && right->dummy)
  488. return 0;
  489. if (left->dummy || right->dummy)
  490. return left->dummy ? 1 : -1;
  491. }
  492. return __hist_entry__cmp_compute(p_left, p_right, c);
  493. }
  494. static int64_t
  495. hist_entry__cmp_nop(struct perf_hpp_fmt *fmt __maybe_unused,
  496. struct hist_entry *left __maybe_unused,
  497. struct hist_entry *right __maybe_unused)
  498. {
  499. return 0;
  500. }
  501. static int64_t
  502. hist_entry__cmp_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
  503. struct hist_entry *left, struct hist_entry *right)
  504. {
  505. if (left->stat.period == right->stat.period)
  506. return 0;
  507. return left->stat.period > right->stat.period ? 1 : -1;
  508. }
  509. static int64_t
  510. hist_entry__cmp_delta(struct perf_hpp_fmt *fmt,
  511. struct hist_entry *left, struct hist_entry *right)
  512. {
  513. struct data__file *d = fmt_to_data_file(fmt);
  514. return hist_entry__cmp_compute(right, left, COMPUTE_DELTA, d->idx);
  515. }
  516. static int64_t
  517. hist_entry__cmp_delta_abs(struct perf_hpp_fmt *fmt,
  518. struct hist_entry *left, struct hist_entry *right)
  519. {
  520. struct data__file *d = fmt_to_data_file(fmt);
  521. return hist_entry__cmp_compute(right, left, COMPUTE_DELTA_ABS, d->idx);
  522. }
  523. static int64_t
  524. hist_entry__cmp_ratio(struct perf_hpp_fmt *fmt,
  525. struct hist_entry *left, struct hist_entry *right)
  526. {
  527. struct data__file *d = fmt_to_data_file(fmt);
  528. return hist_entry__cmp_compute(right, left, COMPUTE_RATIO, d->idx);
  529. }
  530. static int64_t
  531. hist_entry__cmp_wdiff(struct perf_hpp_fmt *fmt,
  532. struct hist_entry *left, struct hist_entry *right)
  533. {
  534. struct data__file *d = fmt_to_data_file(fmt);
  535. return hist_entry__cmp_compute(right, left, COMPUTE_WEIGHTED_DIFF, d->idx);
  536. }
  537. static int64_t
  538. hist_entry__cmp_delta_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  539. struct hist_entry *left, struct hist_entry *right)
  540. {
  541. return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA,
  542. sort_compute);
  543. }
  544. static int64_t
  545. hist_entry__cmp_delta_abs_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  546. struct hist_entry *left, struct hist_entry *right)
  547. {
  548. return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA_ABS,
  549. sort_compute);
  550. }
  551. static int64_t
  552. hist_entry__cmp_ratio_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  553. struct hist_entry *left, struct hist_entry *right)
  554. {
  555. return hist_entry__cmp_compute_idx(right, left, COMPUTE_RATIO,
  556. sort_compute);
  557. }
  558. static int64_t
  559. hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  560. struct hist_entry *left, struct hist_entry *right)
  561. {
  562. return hist_entry__cmp_compute_idx(right, left, COMPUTE_WEIGHTED_DIFF,
  563. sort_compute);
  564. }
  565. static void hists__process(struct hists *hists)
  566. {
  567. if (show_baseline_only)
  568. hists__baseline_only(hists);
  569. hists__precompute(hists);
  570. hists__output_resort(hists, NULL);
  571. hists__fprintf(hists, true, 0, 0, 0, stdout,
  572. symbol_conf.use_callchain);
  573. }
  574. static void data__fprintf(void)
  575. {
  576. struct data__file *d;
  577. int i;
  578. fprintf(stdout, "# Data files:\n");
  579. data__for_each_file(i, d)
  580. fprintf(stdout, "# [%d] %s %s\n",
  581. d->idx, d->file.path,
  582. !d->idx ? "(Baseline)" : "");
  583. fprintf(stdout, "#\n");
  584. }
  585. static void data_process(void)
  586. {
  587. struct perf_evlist *evlist_base = data__files[0].session->evlist;
  588. struct perf_evsel *evsel_base;
  589. bool first = true;
  590. evlist__for_each_entry(evlist_base, evsel_base) {
  591. struct hists *hists_base = evsel__hists(evsel_base);
  592. struct data__file *d;
  593. int i;
  594. data__for_each_file_new(i, d) {
  595. struct perf_evlist *evlist = d->session->evlist;
  596. struct perf_evsel *evsel;
  597. struct hists *hists;
  598. evsel = evsel_match(evsel_base, evlist);
  599. if (!evsel)
  600. continue;
  601. hists = evsel__hists(evsel);
  602. d->hists = hists;
  603. hists__match(hists_base, hists);
  604. if (!show_baseline_only)
  605. hists__link(hists_base, hists);
  606. }
  607. fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
  608. perf_evsel__name(evsel_base));
  609. first = false;
  610. if (verbose || data__files_cnt > 2)
  611. data__fprintf();
  612. /* Don't sort callchain for perf diff */
  613. perf_evsel__reset_sample_bit(evsel_base, CALLCHAIN);
  614. hists__process(hists_base);
  615. }
  616. }
  617. static void data__free(struct data__file *d)
  618. {
  619. int col;
  620. for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) {
  621. struct diff_hpp_fmt *fmt = &d->fmt[col];
  622. zfree(&fmt->header);
  623. }
  624. }
  625. static int __cmd_diff(void)
  626. {
  627. struct data__file *d;
  628. int ret = -EINVAL, i;
  629. data__for_each_file(i, d) {
  630. d->session = perf_session__new(&d->file, false, &tool);
  631. if (!d->session) {
  632. pr_err("Failed to open %s\n", d->file.path);
  633. ret = -1;
  634. goto out_delete;
  635. }
  636. ret = perf_session__process_events(d->session);
  637. if (ret) {
  638. pr_err("Failed to process %s\n", d->file.path);
  639. goto out_delete;
  640. }
  641. perf_evlist__collapse_resort(d->session->evlist);
  642. }
  643. data_process();
  644. out_delete:
  645. data__for_each_file(i, d) {
  646. perf_session__delete(d->session);
  647. data__free(d);
  648. }
  649. free(data__files);
  650. return ret;
  651. }
  652. static const char * const diff_usage[] = {
  653. "perf diff [<options>] [old_file] [new_file]",
  654. NULL,
  655. };
  656. static const struct option options[] = {
  657. OPT_INCR('v', "verbose", &verbose,
  658. "be more verbose (show symbol address, etc)"),
  659. OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
  660. "Show only items with match in baseline"),
  661. OPT_CALLBACK('c', "compute", &compute,
  662. "delta,delta-abs,ratio,wdiff:w1,w2 (default delta)",
  663. "Entries differential computation selection",
  664. setup_compute),
  665. OPT_BOOLEAN('p', "period", &show_period,
  666. "Show period values."),
  667. OPT_BOOLEAN('F', "formula", &show_formula,
  668. "Show formula."),
  669. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  670. "dump raw trace in ASCII"),
  671. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  672. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  673. "file", "kallsyms pathname"),
  674. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  675. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  676. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  677. "only consider symbols in these dsos"),
  678. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  679. "only consider symbols in these comms"),
  680. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  681. "only consider these symbols"),
  682. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  683. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  684. " Please refer the man page for the complete list."),
  685. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  686. "separator for columns, no spaces will be added between "
  687. "columns '.' is reserved."),
  688. OPT_CALLBACK(0, "symfs", NULL, "directory",
  689. "Look for files with symbols relative to this directory",
  690. symbol__config_symfs),
  691. OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
  692. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  693. "How to display percentage of filtered entries", parse_filter_percentage),
  694. OPT_END()
  695. };
  696. static double baseline_percent(struct hist_entry *he)
  697. {
  698. u64 total = hists__total_period(he->hists);
  699. return 100.0 * he->stat.period / total;
  700. }
  701. static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
  702. struct perf_hpp *hpp, struct hist_entry *he)
  703. {
  704. struct diff_hpp_fmt *dfmt =
  705. container_of(fmt, struct diff_hpp_fmt, fmt);
  706. double percent = baseline_percent(he);
  707. char pfmt[20] = " ";
  708. if (!he->dummy) {
  709. scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
  710. return percent_color_snprintf(hpp->buf, hpp->size,
  711. pfmt, percent);
  712. } else
  713. return scnprintf(hpp->buf, hpp->size, "%*s",
  714. dfmt->header_width, pfmt);
  715. }
  716. static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
  717. {
  718. double percent = baseline_percent(he);
  719. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  720. int ret = 0;
  721. if (!he->dummy)
  722. ret = scnprintf(buf, size, fmt, percent);
  723. return ret;
  724. }
  725. static int __hpp__color_compare(struct perf_hpp_fmt *fmt,
  726. struct perf_hpp *hpp, struct hist_entry *he,
  727. int comparison_method)
  728. {
  729. struct diff_hpp_fmt *dfmt =
  730. container_of(fmt, struct diff_hpp_fmt, fmt);
  731. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  732. double diff;
  733. s64 wdiff;
  734. char pfmt[20] = " ";
  735. if (!pair)
  736. goto no_print;
  737. switch (comparison_method) {
  738. case COMPUTE_DELTA:
  739. if (pair->diff.computed)
  740. diff = pair->diff.period_ratio_delta;
  741. else
  742. diff = compute_delta(he, pair);
  743. scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1);
  744. return percent_color_snprintf(hpp->buf, hpp->size,
  745. pfmt, diff);
  746. case COMPUTE_RATIO:
  747. if (he->dummy)
  748. goto dummy_print;
  749. if (pair->diff.computed)
  750. diff = pair->diff.period_ratio;
  751. else
  752. diff = compute_ratio(he, pair);
  753. scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width);
  754. return value_color_snprintf(hpp->buf, hpp->size,
  755. pfmt, diff);
  756. case COMPUTE_WEIGHTED_DIFF:
  757. if (he->dummy)
  758. goto dummy_print;
  759. if (pair->diff.computed)
  760. wdiff = pair->diff.wdiff;
  761. else
  762. wdiff = compute_wdiff(he, pair);
  763. scnprintf(pfmt, 20, "%%14ld", dfmt->header_width);
  764. return color_snprintf(hpp->buf, hpp->size,
  765. get_percent_color(wdiff),
  766. pfmt, wdiff);
  767. default:
  768. BUG_ON(1);
  769. }
  770. dummy_print:
  771. return scnprintf(hpp->buf, hpp->size, "%*s",
  772. dfmt->header_width, "N/A");
  773. no_print:
  774. return scnprintf(hpp->buf, hpp->size, "%*s",
  775. dfmt->header_width, pfmt);
  776. }
  777. static int hpp__color_delta(struct perf_hpp_fmt *fmt,
  778. struct perf_hpp *hpp, struct hist_entry *he)
  779. {
  780. return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA);
  781. }
  782. static int hpp__color_ratio(struct perf_hpp_fmt *fmt,
  783. struct perf_hpp *hpp, struct hist_entry *he)
  784. {
  785. return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO);
  786. }
  787. static int hpp__color_wdiff(struct perf_hpp_fmt *fmt,
  788. struct perf_hpp *hpp, struct hist_entry *he)
  789. {
  790. return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF);
  791. }
  792. static void
  793. hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
  794. {
  795. switch (idx) {
  796. case PERF_HPP_DIFF__PERIOD_BASELINE:
  797. scnprintf(buf, size, "%" PRIu64, he->stat.period);
  798. break;
  799. default:
  800. break;
  801. }
  802. }
  803. static void
  804. hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
  805. int idx, char *buf, size_t size)
  806. {
  807. double diff;
  808. double ratio;
  809. s64 wdiff;
  810. switch (idx) {
  811. case PERF_HPP_DIFF__DELTA:
  812. case PERF_HPP_DIFF__DELTA_ABS:
  813. if (pair->diff.computed)
  814. diff = pair->diff.period_ratio_delta;
  815. else
  816. diff = compute_delta(he, pair);
  817. scnprintf(buf, size, "%+4.2F%%", diff);
  818. break;
  819. case PERF_HPP_DIFF__RATIO:
  820. /* No point for ratio number if we are dummy.. */
  821. if (he->dummy) {
  822. scnprintf(buf, size, "N/A");
  823. break;
  824. }
  825. if (pair->diff.computed)
  826. ratio = pair->diff.period_ratio;
  827. else
  828. ratio = compute_ratio(he, pair);
  829. if (ratio > 0.0)
  830. scnprintf(buf, size, "%14.6F", ratio);
  831. break;
  832. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  833. /* No point for wdiff number if we are dummy.. */
  834. if (he->dummy) {
  835. scnprintf(buf, size, "N/A");
  836. break;
  837. }
  838. if (pair->diff.computed)
  839. wdiff = pair->diff.wdiff;
  840. else
  841. wdiff = compute_wdiff(he, pair);
  842. if (wdiff != 0)
  843. scnprintf(buf, size, "%14ld", wdiff);
  844. break;
  845. case PERF_HPP_DIFF__FORMULA:
  846. formula_fprintf(he, pair, buf, size);
  847. break;
  848. case PERF_HPP_DIFF__PERIOD:
  849. scnprintf(buf, size, "%" PRIu64, pair->stat.period);
  850. break;
  851. default:
  852. BUG_ON(1);
  853. };
  854. }
  855. static void
  856. __hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
  857. char *buf, size_t size)
  858. {
  859. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  860. int idx = dfmt->idx;
  861. /* baseline is special */
  862. if (idx == PERF_HPP_DIFF__BASELINE)
  863. hpp__entry_baseline(he, buf, size);
  864. else {
  865. if (pair)
  866. hpp__entry_pair(he, pair, idx, buf, size);
  867. else
  868. hpp__entry_unpair(he, idx, buf, size);
  869. }
  870. }
  871. static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
  872. struct hist_entry *he)
  873. {
  874. struct diff_hpp_fmt *dfmt =
  875. container_of(_fmt, struct diff_hpp_fmt, fmt);
  876. char buf[MAX_COL_WIDTH] = " ";
  877. __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
  878. if (symbol_conf.field_sep)
  879. return scnprintf(hpp->buf, hpp->size, "%s", buf);
  880. else
  881. return scnprintf(hpp->buf, hpp->size, "%*s",
  882. dfmt->header_width, buf);
  883. }
  884. static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  885. struct hists *hists __maybe_unused,
  886. int line __maybe_unused,
  887. int *span __maybe_unused)
  888. {
  889. struct diff_hpp_fmt *dfmt =
  890. container_of(fmt, struct diff_hpp_fmt, fmt);
  891. BUG_ON(!dfmt->header);
  892. return scnprintf(hpp->buf, hpp->size, dfmt->header);
  893. }
  894. static int hpp__width(struct perf_hpp_fmt *fmt,
  895. struct perf_hpp *hpp __maybe_unused,
  896. struct hists *hists __maybe_unused)
  897. {
  898. struct diff_hpp_fmt *dfmt =
  899. container_of(fmt, struct diff_hpp_fmt, fmt);
  900. BUG_ON(dfmt->header_width <= 0);
  901. return dfmt->header_width;
  902. }
  903. static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
  904. {
  905. #define MAX_HEADER_NAME 100
  906. char buf_indent[MAX_HEADER_NAME];
  907. char buf[MAX_HEADER_NAME];
  908. const char *header = NULL;
  909. int width = 0;
  910. BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
  911. header = columns[dfmt->idx].name;
  912. width = columns[dfmt->idx].width;
  913. /* Only our defined HPP fmts should appear here. */
  914. BUG_ON(!header);
  915. if (data__files_cnt > 2)
  916. scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
  917. #define NAME (data__files_cnt > 2 ? buf : header)
  918. dfmt->header_width = width;
  919. width = (int) strlen(NAME);
  920. if (dfmt->header_width < width)
  921. dfmt->header_width = width;
  922. scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
  923. dfmt->header_width, NAME);
  924. dfmt->header = strdup(buf_indent);
  925. #undef MAX_HEADER_NAME
  926. #undef NAME
  927. }
  928. static void data__hpp_register(struct data__file *d, int idx)
  929. {
  930. struct diff_hpp_fmt *dfmt = &d->fmt[idx];
  931. struct perf_hpp_fmt *fmt = &dfmt->fmt;
  932. dfmt->idx = idx;
  933. fmt->header = hpp__header;
  934. fmt->width = hpp__width;
  935. fmt->entry = hpp__entry_global;
  936. fmt->cmp = hist_entry__cmp_nop;
  937. fmt->collapse = hist_entry__cmp_nop;
  938. /* TODO more colors */
  939. switch (idx) {
  940. case PERF_HPP_DIFF__BASELINE:
  941. fmt->color = hpp__color_baseline;
  942. fmt->sort = hist_entry__cmp_baseline;
  943. break;
  944. case PERF_HPP_DIFF__DELTA:
  945. fmt->color = hpp__color_delta;
  946. fmt->sort = hist_entry__cmp_delta;
  947. break;
  948. case PERF_HPP_DIFF__RATIO:
  949. fmt->color = hpp__color_ratio;
  950. fmt->sort = hist_entry__cmp_ratio;
  951. break;
  952. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  953. fmt->color = hpp__color_wdiff;
  954. fmt->sort = hist_entry__cmp_wdiff;
  955. break;
  956. case PERF_HPP_DIFF__DELTA_ABS:
  957. fmt->color = hpp__color_delta;
  958. fmt->sort = hist_entry__cmp_delta_abs;
  959. break;
  960. default:
  961. fmt->sort = hist_entry__cmp_nop;
  962. break;
  963. }
  964. init_header(d, dfmt);
  965. perf_hpp__column_register(fmt);
  966. perf_hpp__register_sort_field(fmt);
  967. }
  968. static int ui_init(void)
  969. {
  970. struct data__file *d;
  971. struct perf_hpp_fmt *fmt;
  972. int i;
  973. data__for_each_file(i, d) {
  974. /*
  975. * Baseline or compute realted columns:
  976. *
  977. * PERF_HPP_DIFF__BASELINE
  978. * PERF_HPP_DIFF__DELTA
  979. * PERF_HPP_DIFF__RATIO
  980. * PERF_HPP_DIFF__WEIGHTED_DIFF
  981. */
  982. data__hpp_register(d, i ? compute_2_hpp[compute] :
  983. PERF_HPP_DIFF__BASELINE);
  984. /*
  985. * And the rest:
  986. *
  987. * PERF_HPP_DIFF__FORMULA
  988. * PERF_HPP_DIFF__PERIOD
  989. * PERF_HPP_DIFF__PERIOD_BASELINE
  990. */
  991. if (show_formula && i)
  992. data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
  993. if (show_period)
  994. data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
  995. PERF_HPP_DIFF__PERIOD_BASELINE);
  996. }
  997. if (!sort_compute)
  998. return 0;
  999. /*
  1000. * Prepend an fmt to sort on columns at 'sort_compute' first.
  1001. * This fmt is added only to the sort list but not to the
  1002. * output fields list.
  1003. *
  1004. * Note that this column (data) can be compared twice - one
  1005. * for this 'sort_compute' fmt and another for the normal
  1006. * diff_hpp_fmt. But it shouldn't a problem as most entries
  1007. * will be sorted out by first try or baseline and comparing
  1008. * is not a costly operation.
  1009. */
  1010. fmt = zalloc(sizeof(*fmt));
  1011. if (fmt == NULL) {
  1012. pr_err("Memory allocation failed\n");
  1013. return -1;
  1014. }
  1015. fmt->cmp = hist_entry__cmp_nop;
  1016. fmt->collapse = hist_entry__cmp_nop;
  1017. switch (compute) {
  1018. case COMPUTE_DELTA:
  1019. fmt->sort = hist_entry__cmp_delta_idx;
  1020. break;
  1021. case COMPUTE_RATIO:
  1022. fmt->sort = hist_entry__cmp_ratio_idx;
  1023. break;
  1024. case COMPUTE_WEIGHTED_DIFF:
  1025. fmt->sort = hist_entry__cmp_wdiff_idx;
  1026. break;
  1027. case COMPUTE_DELTA_ABS:
  1028. fmt->sort = hist_entry__cmp_delta_abs_idx;
  1029. break;
  1030. default:
  1031. BUG_ON(1);
  1032. }
  1033. perf_hpp__prepend_sort_field(fmt);
  1034. return 0;
  1035. }
  1036. static int data_init(int argc, const char **argv)
  1037. {
  1038. struct data__file *d;
  1039. static const char *defaults[] = {
  1040. "perf.data.old",
  1041. "perf.data",
  1042. };
  1043. bool use_default = true;
  1044. int i;
  1045. data__files_cnt = 2;
  1046. if (argc) {
  1047. if (argc == 1)
  1048. defaults[1] = argv[0];
  1049. else {
  1050. data__files_cnt = argc;
  1051. use_default = false;
  1052. }
  1053. } else if (perf_guest) {
  1054. defaults[0] = "perf.data.host";
  1055. defaults[1] = "perf.data.guest";
  1056. }
  1057. if (sort_compute >= (unsigned int) data__files_cnt) {
  1058. pr_err("Order option out of limit.\n");
  1059. return -EINVAL;
  1060. }
  1061. data__files = zalloc(sizeof(*data__files) * data__files_cnt);
  1062. if (!data__files)
  1063. return -ENOMEM;
  1064. data__for_each_file(i, d) {
  1065. struct perf_data_file *file = &d->file;
  1066. file->path = use_default ? defaults[i] : argv[i];
  1067. file->mode = PERF_DATA_MODE_READ,
  1068. file->force = force,
  1069. d->idx = i;
  1070. }
  1071. return 0;
  1072. }
  1073. int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
  1074. {
  1075. int ret = hists__init();
  1076. if (ret < 0)
  1077. return ret;
  1078. argc = parse_options(argc, argv, options, diff_usage, 0);
  1079. if (symbol__init(NULL) < 0)
  1080. return -1;
  1081. if (data_init(argc, argv) < 0)
  1082. return -1;
  1083. if (ui_init() < 0)
  1084. return -1;
  1085. sort__mode = SORT_MODE__DIFF;
  1086. if (setup_sorting(NULL) < 0)
  1087. usage_with_options(diff_usage, options);
  1088. setup_pager();
  1089. sort__setup_elide(NULL);
  1090. return __cmd_diff();
  1091. }