builtin-diff.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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 hists__add_entry(struct hists *hists,
  257. struct addr_location *al, u64 period,
  258. u64 weight, u64 transaction)
  259. {
  260. if (__hists__add_entry(hists, al, NULL, NULL, NULL, period, weight,
  261. transaction, true) != NULL)
  262. return 0;
  263. return -ENOMEM;
  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 (perf_event__preprocess_sample(event, 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, sample->period,
  280. sample->weight, sample->transaction)) {
  281. pr_warning("problem incrementing symbol period, skipping event\n");
  282. goto out_put;
  283. }
  284. /*
  285. * The total_period is updated here before going to the output
  286. * tree since normally only the baseline hists will call
  287. * hists__output_resort() and precompute needs the total
  288. * period in order to sort entries by percentage delta.
  289. */
  290. hists->stats.total_period += sample->period;
  291. if (!al.filtered)
  292. hists->stats.total_non_filtered_period += sample->period;
  293. ret = 0;
  294. out_put:
  295. addr_location__put(&al);
  296. return ret;
  297. }
  298. static struct perf_tool tool = {
  299. .sample = diff__process_sample_event,
  300. .mmap = perf_event__process_mmap,
  301. .mmap2 = perf_event__process_mmap2,
  302. .comm = perf_event__process_comm,
  303. .exit = perf_event__process_exit,
  304. .fork = perf_event__process_fork,
  305. .lost = perf_event__process_lost,
  306. .ordered_events = true,
  307. .ordering_requires_timestamps = true,
  308. };
  309. static struct perf_evsel *evsel_match(struct perf_evsel *evsel,
  310. struct perf_evlist *evlist)
  311. {
  312. struct perf_evsel *e;
  313. evlist__for_each(evlist, e) {
  314. if (perf_evsel__match2(evsel, e))
  315. return e;
  316. }
  317. return NULL;
  318. }
  319. static void perf_evlist__collapse_resort(struct perf_evlist *evlist)
  320. {
  321. struct perf_evsel *evsel;
  322. evlist__for_each(evlist, evsel) {
  323. struct hists *hists = evsel__hists(evsel);
  324. hists__collapse_resort(hists, NULL);
  325. }
  326. }
  327. static struct data__file *fmt_to_data_file(struct perf_hpp_fmt *fmt)
  328. {
  329. struct diff_hpp_fmt *dfmt = container_of(fmt, struct diff_hpp_fmt, fmt);
  330. void *ptr = dfmt - dfmt->idx;
  331. struct data__file *d = container_of(ptr, struct data__file, fmt);
  332. return d;
  333. }
  334. static struct hist_entry*
  335. get_pair_data(struct hist_entry *he, struct data__file *d)
  336. {
  337. if (hist_entry__has_pairs(he)) {
  338. struct hist_entry *pair;
  339. list_for_each_entry(pair, &he->pairs.head, pairs.node)
  340. if (pair->hists == d->hists)
  341. return pair;
  342. }
  343. return NULL;
  344. }
  345. static struct hist_entry*
  346. get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt)
  347. {
  348. struct data__file *d = fmt_to_data_file(&dfmt->fmt);
  349. return get_pair_data(he, d);
  350. }
  351. static void hists__baseline_only(struct hists *hists)
  352. {
  353. struct rb_root *root;
  354. struct rb_node *next;
  355. if (sort__need_collapse)
  356. root = &hists->entries_collapsed;
  357. else
  358. root = hists->entries_in;
  359. next = rb_first(root);
  360. while (next != NULL) {
  361. struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
  362. next = rb_next(&he->rb_node_in);
  363. if (!hist_entry__next_pair(he)) {
  364. rb_erase(&he->rb_node_in, root);
  365. hist_entry__delete(he);
  366. }
  367. }
  368. }
  369. static void hists__precompute(struct hists *hists)
  370. {
  371. struct rb_root *root;
  372. struct rb_node *next;
  373. if (sort__need_collapse)
  374. root = &hists->entries_collapsed;
  375. else
  376. root = hists->entries_in;
  377. next = rb_first(root);
  378. while (next != NULL) {
  379. struct hist_entry *he, *pair;
  380. struct data__file *d;
  381. int i;
  382. he = rb_entry(next, struct hist_entry, rb_node_in);
  383. next = rb_next(&he->rb_node_in);
  384. data__for_each_file_new(i, d) {
  385. pair = get_pair_data(he, d);
  386. if (!pair)
  387. continue;
  388. switch (compute) {
  389. case COMPUTE_DELTA:
  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_RATIO:
  425. {
  426. double l = left->diff.period_ratio;
  427. double r = right->diff.period_ratio;
  428. return cmp_doubles(l, r);
  429. }
  430. case COMPUTE_WEIGHTED_DIFF:
  431. {
  432. s64 l = left->diff.wdiff;
  433. s64 r = right->diff.wdiff;
  434. return r - l;
  435. }
  436. default:
  437. BUG_ON(1);
  438. }
  439. return 0;
  440. }
  441. static int64_t
  442. hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  443. int c, int sort_idx)
  444. {
  445. bool pairs_left = hist_entry__has_pairs(left);
  446. bool pairs_right = hist_entry__has_pairs(right);
  447. struct hist_entry *p_right, *p_left;
  448. if (!pairs_left && !pairs_right)
  449. return 0;
  450. if (!pairs_left || !pairs_right)
  451. return pairs_left ? -1 : 1;
  452. p_left = get_pair_data(left, &data__files[sort_idx]);
  453. p_right = get_pair_data(right, &data__files[sort_idx]);
  454. if (!p_left && !p_right)
  455. return 0;
  456. if (!p_left || !p_right)
  457. return p_left ? -1 : 1;
  458. /*
  459. * We have 2 entries of same kind, let's
  460. * make the data comparison.
  461. */
  462. return __hist_entry__cmp_compute(p_left, p_right, c);
  463. }
  464. static int64_t
  465. hist_entry__cmp_compute_idx(struct hist_entry *left, struct hist_entry *right,
  466. int c, int sort_idx)
  467. {
  468. struct hist_entry *p_right, *p_left;
  469. p_left = get_pair_data(left, &data__files[sort_idx]);
  470. p_right = get_pair_data(right, &data__files[sort_idx]);
  471. if (!p_left && !p_right)
  472. return 0;
  473. if (!p_left || !p_right)
  474. return p_left ? -1 : 1;
  475. if (c != COMPUTE_DELTA) {
  476. /*
  477. * The delta can be computed without the baseline, but
  478. * others are not. Put those entries which have no
  479. * values below.
  480. */
  481. if (left->dummy && right->dummy)
  482. return 0;
  483. if (left->dummy || right->dummy)
  484. return left->dummy ? 1 : -1;
  485. }
  486. return __hist_entry__cmp_compute(p_left, p_right, c);
  487. }
  488. static int64_t
  489. hist_entry__cmp_nop(struct perf_hpp_fmt *fmt __maybe_unused,
  490. struct hist_entry *left __maybe_unused,
  491. struct hist_entry *right __maybe_unused)
  492. {
  493. return 0;
  494. }
  495. static int64_t
  496. hist_entry__cmp_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
  497. struct hist_entry *left, struct hist_entry *right)
  498. {
  499. if (left->stat.period == right->stat.period)
  500. return 0;
  501. return left->stat.period > right->stat.period ? 1 : -1;
  502. }
  503. static int64_t
  504. hist_entry__cmp_delta(struct perf_hpp_fmt *fmt,
  505. struct hist_entry *left, struct hist_entry *right)
  506. {
  507. struct data__file *d = fmt_to_data_file(fmt);
  508. return hist_entry__cmp_compute(right, left, COMPUTE_DELTA, d->idx);
  509. }
  510. static int64_t
  511. hist_entry__cmp_ratio(struct perf_hpp_fmt *fmt,
  512. struct hist_entry *left, struct hist_entry *right)
  513. {
  514. struct data__file *d = fmt_to_data_file(fmt);
  515. return hist_entry__cmp_compute(right, left, COMPUTE_RATIO, d->idx);
  516. }
  517. static int64_t
  518. hist_entry__cmp_wdiff(struct perf_hpp_fmt *fmt,
  519. struct hist_entry *left, struct hist_entry *right)
  520. {
  521. struct data__file *d = fmt_to_data_file(fmt);
  522. return hist_entry__cmp_compute(right, left, COMPUTE_WEIGHTED_DIFF, d->idx);
  523. }
  524. static int64_t
  525. hist_entry__cmp_delta_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  526. struct hist_entry *left, struct hist_entry *right)
  527. {
  528. return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA,
  529. sort_compute);
  530. }
  531. static int64_t
  532. hist_entry__cmp_ratio_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  533. struct hist_entry *left, struct hist_entry *right)
  534. {
  535. return hist_entry__cmp_compute_idx(right, left, COMPUTE_RATIO,
  536. sort_compute);
  537. }
  538. static int64_t
  539. hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  540. struct hist_entry *left, struct hist_entry *right)
  541. {
  542. return hist_entry__cmp_compute_idx(right, left, COMPUTE_WEIGHTED_DIFF,
  543. sort_compute);
  544. }
  545. static void hists__process(struct hists *hists)
  546. {
  547. if (show_baseline_only)
  548. hists__baseline_only(hists);
  549. hists__precompute(hists);
  550. hists__output_resort(hists, NULL);
  551. hists__fprintf(hists, true, 0, 0, 0, stdout);
  552. }
  553. static void data__fprintf(void)
  554. {
  555. struct data__file *d;
  556. int i;
  557. fprintf(stdout, "# Data files:\n");
  558. data__for_each_file(i, d)
  559. fprintf(stdout, "# [%d] %s %s\n",
  560. d->idx, d->file.path,
  561. !d->idx ? "(Baseline)" : "");
  562. fprintf(stdout, "#\n");
  563. }
  564. static void data_process(void)
  565. {
  566. struct perf_evlist *evlist_base = data__files[0].session->evlist;
  567. struct perf_evsel *evsel_base;
  568. bool first = true;
  569. evlist__for_each(evlist_base, evsel_base) {
  570. struct hists *hists_base = evsel__hists(evsel_base);
  571. struct data__file *d;
  572. int i;
  573. data__for_each_file_new(i, d) {
  574. struct perf_evlist *evlist = d->session->evlist;
  575. struct perf_evsel *evsel;
  576. struct hists *hists;
  577. evsel = evsel_match(evsel_base, evlist);
  578. if (!evsel)
  579. continue;
  580. hists = evsel__hists(evsel);
  581. d->hists = hists;
  582. hists__match(hists_base, hists);
  583. if (!show_baseline_only)
  584. hists__link(hists_base, hists);
  585. }
  586. fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
  587. perf_evsel__name(evsel_base));
  588. first = false;
  589. if (verbose || data__files_cnt > 2)
  590. data__fprintf();
  591. hists__process(hists_base);
  592. }
  593. }
  594. static void data__free(struct data__file *d)
  595. {
  596. int col;
  597. for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) {
  598. struct diff_hpp_fmt *fmt = &d->fmt[col];
  599. zfree(&fmt->header);
  600. }
  601. }
  602. static int __cmd_diff(void)
  603. {
  604. struct data__file *d;
  605. int ret = -EINVAL, i;
  606. data__for_each_file(i, d) {
  607. d->session = perf_session__new(&d->file, false, &tool);
  608. if (!d->session) {
  609. pr_err("Failed to open %s\n", d->file.path);
  610. ret = -1;
  611. goto out_delete;
  612. }
  613. ret = perf_session__process_events(d->session);
  614. if (ret) {
  615. pr_err("Failed to process %s\n", d->file.path);
  616. goto out_delete;
  617. }
  618. perf_evlist__collapse_resort(d->session->evlist);
  619. }
  620. data_process();
  621. out_delete:
  622. data__for_each_file(i, d) {
  623. if (d->session)
  624. perf_session__delete(d->session);
  625. data__free(d);
  626. }
  627. free(data__files);
  628. return ret;
  629. }
  630. static const char * const diff_usage[] = {
  631. "perf diff [<options>] [old_file] [new_file]",
  632. NULL,
  633. };
  634. static const struct option options[] = {
  635. OPT_INCR('v', "verbose", &verbose,
  636. "be more verbose (show symbol address, etc)"),
  637. OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
  638. "Show only items with match in baseline"),
  639. OPT_CALLBACK('c', "compute", &compute,
  640. "delta,ratio,wdiff:w1,w2 (default delta)",
  641. "Entries differential computation selection",
  642. setup_compute),
  643. OPT_BOOLEAN('p', "period", &show_period,
  644. "Show period values."),
  645. OPT_BOOLEAN('F', "formula", &show_formula,
  646. "Show formula."),
  647. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  648. "dump raw trace in ASCII"),
  649. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  650. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  651. "file", "kallsyms pathname"),
  652. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  653. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  654. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  655. "only consider symbols in these dsos"),
  656. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  657. "only consider symbols in these comms"),
  658. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  659. "only consider these symbols"),
  660. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  661. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  662. " Please refer the man page for the complete list."),
  663. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  664. "separator for columns, no spaces will be added between "
  665. "columns '.' is reserved."),
  666. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  667. "Look for files with symbols relative to this directory"),
  668. OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
  669. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  670. "How to display percentage of filtered entries", parse_filter_percentage),
  671. OPT_END()
  672. };
  673. static double baseline_percent(struct hist_entry *he)
  674. {
  675. u64 total = hists__total_period(he->hists);
  676. return 100.0 * he->stat.period / total;
  677. }
  678. static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
  679. struct perf_hpp *hpp, struct hist_entry *he)
  680. {
  681. struct diff_hpp_fmt *dfmt =
  682. container_of(fmt, struct diff_hpp_fmt, fmt);
  683. double percent = baseline_percent(he);
  684. char pfmt[20] = " ";
  685. if (!he->dummy) {
  686. scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
  687. return percent_color_snprintf(hpp->buf, hpp->size,
  688. pfmt, percent);
  689. } else
  690. return scnprintf(hpp->buf, hpp->size, "%*s",
  691. dfmt->header_width, pfmt);
  692. }
  693. static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
  694. {
  695. double percent = baseline_percent(he);
  696. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  697. int ret = 0;
  698. if (!he->dummy)
  699. ret = scnprintf(buf, size, fmt, percent);
  700. return ret;
  701. }
  702. static int __hpp__color_compare(struct perf_hpp_fmt *fmt,
  703. struct perf_hpp *hpp, struct hist_entry *he,
  704. int comparison_method)
  705. {
  706. struct diff_hpp_fmt *dfmt =
  707. container_of(fmt, struct diff_hpp_fmt, fmt);
  708. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  709. double diff;
  710. s64 wdiff;
  711. char pfmt[20] = " ";
  712. if (!pair)
  713. goto no_print;
  714. switch (comparison_method) {
  715. case COMPUTE_DELTA:
  716. if (pair->diff.computed)
  717. diff = pair->diff.period_ratio_delta;
  718. else
  719. diff = compute_delta(he, pair);
  720. scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1);
  721. return percent_color_snprintf(hpp->buf, hpp->size,
  722. pfmt, diff);
  723. case COMPUTE_RATIO:
  724. if (he->dummy)
  725. goto dummy_print;
  726. if (pair->diff.computed)
  727. diff = pair->diff.period_ratio;
  728. else
  729. diff = compute_ratio(he, pair);
  730. scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width);
  731. return value_color_snprintf(hpp->buf, hpp->size,
  732. pfmt, diff);
  733. case COMPUTE_WEIGHTED_DIFF:
  734. if (he->dummy)
  735. goto dummy_print;
  736. if (pair->diff.computed)
  737. wdiff = pair->diff.wdiff;
  738. else
  739. wdiff = compute_wdiff(he, pair);
  740. scnprintf(pfmt, 20, "%%14ld", dfmt->header_width);
  741. return color_snprintf(hpp->buf, hpp->size,
  742. get_percent_color(wdiff),
  743. pfmt, wdiff);
  744. default:
  745. BUG_ON(1);
  746. }
  747. dummy_print:
  748. return scnprintf(hpp->buf, hpp->size, "%*s",
  749. dfmt->header_width, "N/A");
  750. no_print:
  751. return scnprintf(hpp->buf, hpp->size, "%*s",
  752. dfmt->header_width, pfmt);
  753. }
  754. static int hpp__color_delta(struct perf_hpp_fmt *fmt,
  755. struct perf_hpp *hpp, struct hist_entry *he)
  756. {
  757. return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA);
  758. }
  759. static int hpp__color_ratio(struct perf_hpp_fmt *fmt,
  760. struct perf_hpp *hpp, struct hist_entry *he)
  761. {
  762. return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO);
  763. }
  764. static int hpp__color_wdiff(struct perf_hpp_fmt *fmt,
  765. struct perf_hpp *hpp, struct hist_entry *he)
  766. {
  767. return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF);
  768. }
  769. static void
  770. hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
  771. {
  772. switch (idx) {
  773. case PERF_HPP_DIFF__PERIOD_BASELINE:
  774. scnprintf(buf, size, "%" PRIu64, he->stat.period);
  775. break;
  776. default:
  777. break;
  778. }
  779. }
  780. static void
  781. hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
  782. int idx, char *buf, size_t size)
  783. {
  784. double diff;
  785. double ratio;
  786. s64 wdiff;
  787. switch (idx) {
  788. case PERF_HPP_DIFF__DELTA:
  789. if (pair->diff.computed)
  790. diff = pair->diff.period_ratio_delta;
  791. else
  792. diff = compute_delta(he, pair);
  793. scnprintf(buf, size, "%+4.2F%%", diff);
  794. break;
  795. case PERF_HPP_DIFF__RATIO:
  796. /* No point for ratio number if we are dummy.. */
  797. if (he->dummy) {
  798. scnprintf(buf, size, "N/A");
  799. break;
  800. }
  801. if (pair->diff.computed)
  802. ratio = pair->diff.period_ratio;
  803. else
  804. ratio = compute_ratio(he, pair);
  805. if (ratio > 0.0)
  806. scnprintf(buf, size, "%14.6F", ratio);
  807. break;
  808. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  809. /* No point for wdiff number if we are dummy.. */
  810. if (he->dummy) {
  811. scnprintf(buf, size, "N/A");
  812. break;
  813. }
  814. if (pair->diff.computed)
  815. wdiff = pair->diff.wdiff;
  816. else
  817. wdiff = compute_wdiff(he, pair);
  818. if (wdiff != 0)
  819. scnprintf(buf, size, "%14ld", wdiff);
  820. break;
  821. case PERF_HPP_DIFF__FORMULA:
  822. formula_fprintf(he, pair, buf, size);
  823. break;
  824. case PERF_HPP_DIFF__PERIOD:
  825. scnprintf(buf, size, "%" PRIu64, pair->stat.period);
  826. break;
  827. default:
  828. BUG_ON(1);
  829. };
  830. }
  831. static void
  832. __hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
  833. char *buf, size_t size)
  834. {
  835. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  836. int idx = dfmt->idx;
  837. /* baseline is special */
  838. if (idx == PERF_HPP_DIFF__BASELINE)
  839. hpp__entry_baseline(he, buf, size);
  840. else {
  841. if (pair)
  842. hpp__entry_pair(he, pair, idx, buf, size);
  843. else
  844. hpp__entry_unpair(he, idx, buf, size);
  845. }
  846. }
  847. static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
  848. struct hist_entry *he)
  849. {
  850. struct diff_hpp_fmt *dfmt =
  851. container_of(_fmt, struct diff_hpp_fmt, fmt);
  852. char buf[MAX_COL_WIDTH] = " ";
  853. __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
  854. if (symbol_conf.field_sep)
  855. return scnprintf(hpp->buf, hpp->size, "%s", buf);
  856. else
  857. return scnprintf(hpp->buf, hpp->size, "%*s",
  858. dfmt->header_width, buf);
  859. }
  860. static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  861. struct perf_evsel *evsel __maybe_unused)
  862. {
  863. struct diff_hpp_fmt *dfmt =
  864. container_of(fmt, struct diff_hpp_fmt, fmt);
  865. BUG_ON(!dfmt->header);
  866. return scnprintf(hpp->buf, hpp->size, dfmt->header);
  867. }
  868. static int hpp__width(struct perf_hpp_fmt *fmt,
  869. struct perf_hpp *hpp __maybe_unused,
  870. struct perf_evsel *evsel __maybe_unused)
  871. {
  872. struct diff_hpp_fmt *dfmt =
  873. container_of(fmt, struct diff_hpp_fmt, fmt);
  874. BUG_ON(dfmt->header_width <= 0);
  875. return dfmt->header_width;
  876. }
  877. static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
  878. {
  879. #define MAX_HEADER_NAME 100
  880. char buf_indent[MAX_HEADER_NAME];
  881. char buf[MAX_HEADER_NAME];
  882. const char *header = NULL;
  883. int width = 0;
  884. BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
  885. header = columns[dfmt->idx].name;
  886. width = columns[dfmt->idx].width;
  887. /* Only our defined HPP fmts should appear here. */
  888. BUG_ON(!header);
  889. if (data__files_cnt > 2)
  890. scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
  891. #define NAME (data__files_cnt > 2 ? buf : header)
  892. dfmt->header_width = width;
  893. width = (int) strlen(NAME);
  894. if (dfmt->header_width < width)
  895. dfmt->header_width = width;
  896. scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
  897. dfmt->header_width, NAME);
  898. dfmt->header = strdup(buf_indent);
  899. #undef MAX_HEADER_NAME
  900. #undef NAME
  901. }
  902. static void data__hpp_register(struct data__file *d, int idx)
  903. {
  904. struct diff_hpp_fmt *dfmt = &d->fmt[idx];
  905. struct perf_hpp_fmt *fmt = &dfmt->fmt;
  906. dfmt->idx = idx;
  907. fmt->header = hpp__header;
  908. fmt->width = hpp__width;
  909. fmt->entry = hpp__entry_global;
  910. fmt->cmp = hist_entry__cmp_nop;
  911. fmt->collapse = hist_entry__cmp_nop;
  912. /* TODO more colors */
  913. switch (idx) {
  914. case PERF_HPP_DIFF__BASELINE:
  915. fmt->color = hpp__color_baseline;
  916. fmt->sort = hist_entry__cmp_baseline;
  917. break;
  918. case PERF_HPP_DIFF__DELTA:
  919. fmt->color = hpp__color_delta;
  920. fmt->sort = hist_entry__cmp_delta;
  921. break;
  922. case PERF_HPP_DIFF__RATIO:
  923. fmt->color = hpp__color_ratio;
  924. fmt->sort = hist_entry__cmp_ratio;
  925. break;
  926. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  927. fmt->color = hpp__color_wdiff;
  928. fmt->sort = hist_entry__cmp_wdiff;
  929. break;
  930. default:
  931. fmt->sort = hist_entry__cmp_nop;
  932. break;
  933. }
  934. init_header(d, dfmt);
  935. perf_hpp__column_register(fmt);
  936. perf_hpp__register_sort_field(fmt);
  937. }
  938. static int ui_init(void)
  939. {
  940. struct data__file *d;
  941. struct perf_hpp_fmt *fmt;
  942. int i;
  943. data__for_each_file(i, d) {
  944. /*
  945. * Baseline or compute realted columns:
  946. *
  947. * PERF_HPP_DIFF__BASELINE
  948. * PERF_HPP_DIFF__DELTA
  949. * PERF_HPP_DIFF__RATIO
  950. * PERF_HPP_DIFF__WEIGHTED_DIFF
  951. */
  952. data__hpp_register(d, i ? compute_2_hpp[compute] :
  953. PERF_HPP_DIFF__BASELINE);
  954. /*
  955. * And the rest:
  956. *
  957. * PERF_HPP_DIFF__FORMULA
  958. * PERF_HPP_DIFF__PERIOD
  959. * PERF_HPP_DIFF__PERIOD_BASELINE
  960. */
  961. if (show_formula && i)
  962. data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
  963. if (show_period)
  964. data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
  965. PERF_HPP_DIFF__PERIOD_BASELINE);
  966. }
  967. if (!sort_compute)
  968. return 0;
  969. /*
  970. * Prepend an fmt to sort on columns at 'sort_compute' first.
  971. * This fmt is added only to the sort list but not to the
  972. * output fields list.
  973. *
  974. * Note that this column (data) can be compared twice - one
  975. * for this 'sort_compute' fmt and another for the normal
  976. * diff_hpp_fmt. But it shouldn't a problem as most entries
  977. * will be sorted out by first try or baseline and comparing
  978. * is not a costly operation.
  979. */
  980. fmt = zalloc(sizeof(*fmt));
  981. if (fmt == NULL) {
  982. pr_err("Memory allocation failed\n");
  983. return -1;
  984. }
  985. fmt->cmp = hist_entry__cmp_nop;
  986. fmt->collapse = hist_entry__cmp_nop;
  987. switch (compute) {
  988. case COMPUTE_DELTA:
  989. fmt->sort = hist_entry__cmp_delta_idx;
  990. break;
  991. case COMPUTE_RATIO:
  992. fmt->sort = hist_entry__cmp_ratio_idx;
  993. break;
  994. case COMPUTE_WEIGHTED_DIFF:
  995. fmt->sort = hist_entry__cmp_wdiff_idx;
  996. break;
  997. default:
  998. BUG_ON(1);
  999. }
  1000. list_add(&fmt->sort_list, &perf_hpp__sort_list);
  1001. return 0;
  1002. }
  1003. static int data_init(int argc, const char **argv)
  1004. {
  1005. struct data__file *d;
  1006. static const char *defaults[] = {
  1007. "perf.data.old",
  1008. "perf.data",
  1009. };
  1010. bool use_default = true;
  1011. int i;
  1012. data__files_cnt = 2;
  1013. if (argc) {
  1014. if (argc == 1)
  1015. defaults[1] = argv[0];
  1016. else {
  1017. data__files_cnt = argc;
  1018. use_default = false;
  1019. }
  1020. } else if (perf_guest) {
  1021. defaults[0] = "perf.data.host";
  1022. defaults[1] = "perf.data.guest";
  1023. }
  1024. if (sort_compute >= (unsigned int) data__files_cnt) {
  1025. pr_err("Order option out of limit.\n");
  1026. return -EINVAL;
  1027. }
  1028. data__files = zalloc(sizeof(*data__files) * data__files_cnt);
  1029. if (!data__files)
  1030. return -ENOMEM;
  1031. data__for_each_file(i, d) {
  1032. struct perf_data_file *file = &d->file;
  1033. file->path = use_default ? defaults[i] : argv[i];
  1034. file->mode = PERF_DATA_MODE_READ,
  1035. file->force = force,
  1036. d->idx = i;
  1037. }
  1038. return 0;
  1039. }
  1040. int cmd_diff(int argc, const char **argv, const char *prefix __maybe_unused)
  1041. {
  1042. int ret = hists__init();
  1043. if (ret < 0)
  1044. return ret;
  1045. perf_config(perf_default_config, NULL);
  1046. argc = parse_options(argc, argv, options, diff_usage, 0);
  1047. if (symbol__init(NULL) < 0)
  1048. return -1;
  1049. if (data_init(argc, argv) < 0)
  1050. return -1;
  1051. if (ui_init() < 0)
  1052. return -1;
  1053. sort__mode = SORT_MODE__DIFF;
  1054. if (setup_sorting() < 0)
  1055. usage_with_options(diff_usage, options);
  1056. setup_pager();
  1057. sort__setup_elide(NULL);
  1058. return __cmd_diff();
  1059. }