builtin-diff.c 31 KB

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