builtin-diff.c 29 KB

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