builtin-diff.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-diff.c
  4. *
  5. * Builtin diff command: Analyze two perf.data input files, look up and read
  6. * DSOs and symbol information, sort them and produce a diff.
  7. */
  8. #include "builtin.h"
  9. #include "util/debug.h"
  10. #include "util/event.h"
  11. #include "util/hist.h"
  12. #include "util/evsel.h"
  13. #include "util/evlist.h"
  14. #include "util/session.h"
  15. #include "util/tool.h"
  16. #include "util/sort.h"
  17. #include "util/srcline.h"
  18. #include "util/symbol.h"
  19. #include "util/data.h"
  20. #include "util/config.h"
  21. #include "util/time-utils.h"
  22. #include "util/annotate.h"
  23. #include "util/map.h"
  24. #include "util/spark.h"
  25. #include "util/block-info.h"
  26. #include "util/stream.h"
  27. #include "util/util.h"
  28. #include <linux/err.h>
  29. #include <linux/zalloc.h>
  30. #include <subcmd/pager.h>
  31. #include <subcmd/parse-options.h>
  32. #include <errno.h>
  33. #include <inttypes.h>
  34. #include <stdlib.h>
  35. #include <math.h>
  36. struct perf_diff {
  37. struct perf_tool tool;
  38. const char *time_str;
  39. struct perf_time_interval *ptime_range;
  40. int range_size;
  41. int range_num;
  42. bool has_br_stack;
  43. bool stream;
  44. };
  45. /* Diff command specific HPP columns. */
  46. enum {
  47. PERF_HPP_DIFF__BASELINE,
  48. PERF_HPP_DIFF__PERIOD,
  49. PERF_HPP_DIFF__PERIOD_BASELINE,
  50. PERF_HPP_DIFF__DELTA,
  51. PERF_HPP_DIFF__RATIO,
  52. PERF_HPP_DIFF__WEIGHTED_DIFF,
  53. PERF_HPP_DIFF__FORMULA,
  54. PERF_HPP_DIFF__DELTA_ABS,
  55. PERF_HPP_DIFF__CYCLES,
  56. PERF_HPP_DIFF__CYCLES_HIST,
  57. PERF_HPP_DIFF__MAX_INDEX
  58. };
  59. struct diff_hpp_fmt {
  60. struct perf_hpp_fmt fmt;
  61. int idx;
  62. char *header;
  63. int header_width;
  64. };
  65. struct data__file {
  66. struct perf_session *session;
  67. struct perf_data data;
  68. int idx;
  69. struct hists *hists;
  70. struct evlist_streams *evlist_streams;
  71. struct diff_hpp_fmt fmt[PERF_HPP_DIFF__MAX_INDEX];
  72. };
  73. static struct data__file *data__files;
  74. static int data__files_cnt;
  75. #define data__for_each_file_start(i, d, s) \
  76. for (i = s, d = &data__files[s]; \
  77. i < data__files_cnt; \
  78. i++, d = &data__files[i])
  79. #define data__for_each_file(i, d) data__for_each_file_start(i, d, 0)
  80. #define data__for_each_file_new(i, d) data__for_each_file_start(i, d, 1)
  81. static bool force;
  82. static bool show_period;
  83. static bool show_formula;
  84. static bool show_baseline_only;
  85. static bool cycles_hist;
  86. static unsigned int sort_compute = 1;
  87. static s64 compute_wdiff_w1;
  88. static s64 compute_wdiff_w2;
  89. static const char *cpu_list;
  90. static DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  91. enum {
  92. COMPUTE_DELTA,
  93. COMPUTE_RATIO,
  94. COMPUTE_WEIGHTED_DIFF,
  95. COMPUTE_DELTA_ABS,
  96. COMPUTE_CYCLES,
  97. COMPUTE_MAX,
  98. COMPUTE_STREAM, /* After COMPUTE_MAX to avoid use current compute arrays */
  99. };
  100. const char *compute_names[COMPUTE_MAX] = {
  101. [COMPUTE_DELTA] = "delta",
  102. [COMPUTE_DELTA_ABS] = "delta-abs",
  103. [COMPUTE_RATIO] = "ratio",
  104. [COMPUTE_WEIGHTED_DIFF] = "wdiff",
  105. [COMPUTE_CYCLES] = "cycles",
  106. };
  107. static int compute = COMPUTE_DELTA_ABS;
  108. static int compute_2_hpp[COMPUTE_MAX] = {
  109. [COMPUTE_DELTA] = PERF_HPP_DIFF__DELTA,
  110. [COMPUTE_DELTA_ABS] = PERF_HPP_DIFF__DELTA_ABS,
  111. [COMPUTE_RATIO] = PERF_HPP_DIFF__RATIO,
  112. [COMPUTE_WEIGHTED_DIFF] = PERF_HPP_DIFF__WEIGHTED_DIFF,
  113. [COMPUTE_CYCLES] = PERF_HPP_DIFF__CYCLES,
  114. };
  115. #define MAX_COL_WIDTH 70
  116. static struct header_column {
  117. const char *name;
  118. int width;
  119. } columns[PERF_HPP_DIFF__MAX_INDEX] = {
  120. [PERF_HPP_DIFF__BASELINE] = {
  121. .name = "Baseline",
  122. },
  123. [PERF_HPP_DIFF__PERIOD] = {
  124. .name = "Period",
  125. .width = 14,
  126. },
  127. [PERF_HPP_DIFF__PERIOD_BASELINE] = {
  128. .name = "Base period",
  129. .width = 14,
  130. },
  131. [PERF_HPP_DIFF__DELTA] = {
  132. .name = "Delta",
  133. .width = 7,
  134. },
  135. [PERF_HPP_DIFF__DELTA_ABS] = {
  136. .name = "Delta Abs",
  137. .width = 7,
  138. },
  139. [PERF_HPP_DIFF__RATIO] = {
  140. .name = "Ratio",
  141. .width = 14,
  142. },
  143. [PERF_HPP_DIFF__WEIGHTED_DIFF] = {
  144. .name = "Weighted diff",
  145. .width = 14,
  146. },
  147. [PERF_HPP_DIFF__FORMULA] = {
  148. .name = "Formula",
  149. .width = MAX_COL_WIDTH,
  150. },
  151. [PERF_HPP_DIFF__CYCLES] = {
  152. .name = "[Program Block Range] Cycles Diff",
  153. .width = 70,
  154. },
  155. [PERF_HPP_DIFF__CYCLES_HIST] = {
  156. .name = "stddev/Hist",
  157. .width = NUM_SPARKS + 9,
  158. }
  159. };
  160. static int setup_compute_opt_wdiff(char *opt)
  161. {
  162. char *w1_str = opt;
  163. char *w2_str;
  164. int ret = -EINVAL;
  165. if (!opt)
  166. goto out;
  167. w2_str = strchr(opt, ',');
  168. if (!w2_str)
  169. goto out;
  170. *w2_str++ = 0x0;
  171. if (!*w2_str)
  172. goto out;
  173. compute_wdiff_w1 = strtol(w1_str, NULL, 10);
  174. compute_wdiff_w2 = strtol(w2_str, NULL, 10);
  175. if (!compute_wdiff_w1 || !compute_wdiff_w2)
  176. goto out;
  177. pr_debug("compute wdiff w1(%" PRId64 ") w2(%" PRId64 ")\n",
  178. compute_wdiff_w1, compute_wdiff_w2);
  179. ret = 0;
  180. out:
  181. if (ret)
  182. pr_err("Failed: wrong weight data, use 'wdiff:w1,w2'\n");
  183. return ret;
  184. }
  185. static int setup_compute_opt(char *opt)
  186. {
  187. if (compute == COMPUTE_WEIGHTED_DIFF)
  188. return setup_compute_opt_wdiff(opt);
  189. if (opt) {
  190. pr_err("Failed: extra option specified '%s'", opt);
  191. return -EINVAL;
  192. }
  193. return 0;
  194. }
  195. static int setup_compute(const struct option *opt, const char *str,
  196. int unset __maybe_unused)
  197. {
  198. int *cp = (int *) opt->value;
  199. char *cstr = (char *) str;
  200. char buf[50];
  201. unsigned i;
  202. char *option;
  203. if (!str) {
  204. *cp = COMPUTE_DELTA;
  205. return 0;
  206. }
  207. option = strchr(str, ':');
  208. if (option) {
  209. unsigned len = option++ - str;
  210. /*
  211. * The str data are not writeable, so we need
  212. * to use another buffer.
  213. */
  214. /* No option value is longer. */
  215. if (len >= sizeof(buf))
  216. return -EINVAL;
  217. strncpy(buf, str, len);
  218. buf[len] = 0x0;
  219. cstr = buf;
  220. }
  221. for (i = 0; i < COMPUTE_MAX; i++)
  222. if (!strcmp(cstr, compute_names[i])) {
  223. *cp = i;
  224. return setup_compute_opt(option);
  225. }
  226. pr_err("Failed: '%s' is not computation method "
  227. "(use 'delta','ratio' or 'wdiff')\n", str);
  228. return -EINVAL;
  229. }
  230. static double period_percent(struct hist_entry *he, u64 period)
  231. {
  232. u64 total = hists__total_period(he->hists);
  233. return (period * 100.0) / total;
  234. }
  235. static double compute_delta(struct hist_entry *he, struct hist_entry *pair)
  236. {
  237. double old_percent = period_percent(he, he->stat.period);
  238. double new_percent = period_percent(pair, pair->stat.period);
  239. pair->diff.period_ratio_delta = new_percent - old_percent;
  240. pair->diff.computed = true;
  241. return pair->diff.period_ratio_delta;
  242. }
  243. static double compute_ratio(struct hist_entry *he, struct hist_entry *pair)
  244. {
  245. double old_period = he->stat.period ?: 1;
  246. double new_period = pair->stat.period;
  247. pair->diff.computed = true;
  248. pair->diff.period_ratio = new_period / old_period;
  249. return pair->diff.period_ratio;
  250. }
  251. static s64 compute_wdiff(struct hist_entry *he, struct hist_entry *pair)
  252. {
  253. u64 old_period = he->stat.period;
  254. u64 new_period = pair->stat.period;
  255. pair->diff.computed = true;
  256. pair->diff.wdiff = new_period * compute_wdiff_w2 -
  257. old_period * compute_wdiff_w1;
  258. return pair->diff.wdiff;
  259. }
  260. static int formula_delta(struct hist_entry *he, struct hist_entry *pair,
  261. char *buf, size_t size)
  262. {
  263. u64 he_total = he->hists->stats.total_period;
  264. u64 pair_total = pair->hists->stats.total_period;
  265. if (symbol_conf.filter_relative) {
  266. he_total = he->hists->stats.total_non_filtered_period;
  267. pair_total = pair->hists->stats.total_non_filtered_period;
  268. }
  269. return scnprintf(buf, size,
  270. "(%" PRIu64 " * 100 / %" PRIu64 ") - "
  271. "(%" PRIu64 " * 100 / %" PRIu64 ")",
  272. pair->stat.period, pair_total,
  273. he->stat.period, he_total);
  274. }
  275. static int formula_ratio(struct hist_entry *he, struct hist_entry *pair,
  276. char *buf, size_t size)
  277. {
  278. double old_period = he->stat.period;
  279. double new_period = pair->stat.period;
  280. return scnprintf(buf, size, "%.0F / %.0F", new_period, old_period);
  281. }
  282. static int formula_wdiff(struct hist_entry *he, struct hist_entry *pair,
  283. char *buf, size_t size)
  284. {
  285. u64 old_period = he->stat.period;
  286. u64 new_period = pair->stat.period;
  287. return scnprintf(buf, size,
  288. "(%" PRIu64 " * " "%" PRId64 ") - (%" PRIu64 " * " "%" PRId64 ")",
  289. new_period, compute_wdiff_w2, old_period, compute_wdiff_w1);
  290. }
  291. static int formula_fprintf(struct hist_entry *he, struct hist_entry *pair,
  292. char *buf, size_t size)
  293. {
  294. switch (compute) {
  295. case COMPUTE_DELTA:
  296. case COMPUTE_DELTA_ABS:
  297. return formula_delta(he, pair, buf, size);
  298. case COMPUTE_RATIO:
  299. return formula_ratio(he, pair, buf, size);
  300. case COMPUTE_WEIGHTED_DIFF:
  301. return formula_wdiff(he, pair, buf, size);
  302. default:
  303. BUG_ON(1);
  304. }
  305. return -1;
  306. }
  307. static void *block_hist_zalloc(size_t size)
  308. {
  309. struct block_hist *bh;
  310. bh = zalloc(size + sizeof(*bh));
  311. if (!bh)
  312. return NULL;
  313. return &bh->he;
  314. }
  315. static void block_hist_free(void *he)
  316. {
  317. struct block_hist *bh;
  318. bh = container_of(he, struct block_hist, he);
  319. hists__delete_entries(&bh->block_hists);
  320. free(bh);
  321. }
  322. struct hist_entry_ops block_hist_ops = {
  323. .new = block_hist_zalloc,
  324. .free = block_hist_free,
  325. };
  326. static int diff__process_sample_event(const struct perf_tool *tool,
  327. union perf_event *event,
  328. struct perf_sample *sample,
  329. struct evsel *evsel,
  330. struct machine *machine)
  331. {
  332. struct perf_diff *pdiff = container_of(tool, struct perf_diff, tool);
  333. struct addr_location al;
  334. struct hists *hists = evsel__hists(evsel);
  335. struct hist_entry_iter iter = {
  336. .evsel = evsel,
  337. .sample = sample,
  338. .ops = &hist_iter_normal,
  339. };
  340. int ret = -1;
  341. if (perf_time__ranges_skip_sample(pdiff->ptime_range, pdiff->range_num,
  342. sample->time)) {
  343. return 0;
  344. }
  345. addr_location__init(&al);
  346. if (machine__resolve(machine, &al, sample) < 0) {
  347. pr_warning("problem processing %d event, skipping it.\n",
  348. event->header.type);
  349. ret = -1;
  350. goto out;
  351. }
  352. if (cpu_list && !test_bit(sample->cpu, cpu_bitmap)) {
  353. ret = 0;
  354. goto out;
  355. }
  356. switch (compute) {
  357. case COMPUTE_CYCLES:
  358. if (!hists__add_entry_ops(hists, &block_hist_ops, &al, NULL,
  359. NULL, NULL, NULL, sample, true)) {
  360. pr_warning("problem incrementing symbol period, "
  361. "skipping event\n");
  362. goto out;
  363. }
  364. hist__account_cycles(sample->branch_stack, &al, sample,
  365. false, NULL, evsel);
  366. break;
  367. case COMPUTE_STREAM:
  368. if (hist_entry_iter__add(&iter, &al, PERF_MAX_STACK_DEPTH,
  369. NULL)) {
  370. pr_debug("problem adding hist entry, skipping event\n");
  371. goto out;
  372. }
  373. break;
  374. default:
  375. if (!hists__add_entry(hists, &al, NULL, NULL, NULL, NULL, sample,
  376. true)) {
  377. pr_warning("problem incrementing symbol period, "
  378. "skipping event\n");
  379. goto out;
  380. }
  381. }
  382. /*
  383. * The total_period is updated here before going to the output
  384. * tree since normally only the baseline hists will call
  385. * hists__output_resort() and precompute needs the total
  386. * period in order to sort entries by percentage delta.
  387. */
  388. hists->stats.total_period += sample->period;
  389. if (!al.filtered)
  390. hists->stats.total_non_filtered_period += sample->period;
  391. ret = 0;
  392. out:
  393. addr_location__exit(&al);
  394. return ret;
  395. }
  396. static struct perf_diff pdiff;
  397. static struct evsel *evsel_match(struct evsel *evsel,
  398. struct evlist *evlist)
  399. {
  400. struct evsel *e;
  401. evlist__for_each_entry(evlist, e) {
  402. if (evsel__match2(evsel, e))
  403. return e;
  404. }
  405. return NULL;
  406. }
  407. static void evlist__collapse_resort(struct evlist *evlist)
  408. {
  409. struct evsel *evsel;
  410. evlist__for_each_entry(evlist, evsel) {
  411. struct hists *hists = evsel__hists(evsel);
  412. hists__collapse_resort(hists, NULL);
  413. }
  414. }
  415. static struct data__file *fmt_to_data_file(struct perf_hpp_fmt *fmt)
  416. {
  417. struct diff_hpp_fmt *dfmt = container_of(fmt, struct diff_hpp_fmt, fmt);
  418. void *ptr = dfmt - dfmt->idx;
  419. struct data__file *d = container_of(ptr, struct data__file, fmt);
  420. return d;
  421. }
  422. static struct hist_entry*
  423. get_pair_data(struct hist_entry *he, struct data__file *d)
  424. {
  425. if (hist_entry__has_pairs(he)) {
  426. struct hist_entry *pair;
  427. list_for_each_entry(pair, &he->pairs.head, pairs.node)
  428. if (pair->hists == d->hists)
  429. return pair;
  430. }
  431. return NULL;
  432. }
  433. static struct hist_entry*
  434. get_pair_fmt(struct hist_entry *he, struct diff_hpp_fmt *dfmt)
  435. {
  436. struct data__file *d = fmt_to_data_file(&dfmt->fmt);
  437. return get_pair_data(he, d);
  438. }
  439. static void hists__baseline_only(struct hists *hists)
  440. {
  441. struct rb_root_cached *root;
  442. struct rb_node *next;
  443. if (hists__has(hists, need_collapse))
  444. root = &hists->entries_collapsed;
  445. else
  446. root = hists->entries_in;
  447. next = rb_first_cached(root);
  448. while (next != NULL) {
  449. struct hist_entry *he = rb_entry(next, struct hist_entry, rb_node_in);
  450. next = rb_next(&he->rb_node_in);
  451. if (!hist_entry__next_pair(he)) {
  452. rb_erase_cached(&he->rb_node_in, root);
  453. hist_entry__delete(he);
  454. }
  455. }
  456. }
  457. static int64_t block_cycles_diff_cmp(struct hist_entry *left,
  458. struct hist_entry *right)
  459. {
  460. bool pairs_left = hist_entry__has_pairs(left);
  461. bool pairs_right = hist_entry__has_pairs(right);
  462. s64 l, r;
  463. if (!pairs_left && !pairs_right)
  464. return 0;
  465. l = llabs(left->diff.cycles);
  466. r = llabs(right->diff.cycles);
  467. return r - l;
  468. }
  469. static int64_t block_sort(struct perf_hpp_fmt *fmt __maybe_unused,
  470. struct hist_entry *left, struct hist_entry *right)
  471. {
  472. return block_cycles_diff_cmp(right, left);
  473. }
  474. static void init_block_hist(struct block_hist *bh)
  475. {
  476. __hists__init(&bh->block_hists, &bh->block_list);
  477. perf_hpp_list__init(&bh->block_list);
  478. INIT_LIST_HEAD(&bh->block_fmt.list);
  479. INIT_LIST_HEAD(&bh->block_fmt.sort_list);
  480. bh->block_fmt.cmp = block_info__cmp;
  481. bh->block_fmt.sort = block_sort;
  482. perf_hpp_list__register_sort_field(&bh->block_list,
  483. &bh->block_fmt);
  484. bh->valid = true;
  485. }
  486. static struct hist_entry *get_block_pair(struct hist_entry *he,
  487. struct hists *hists_pair)
  488. {
  489. struct rb_root_cached *root = hists_pair->entries_in;
  490. struct rb_node *next = rb_first_cached(root);
  491. int64_t cmp;
  492. while (next != NULL) {
  493. struct hist_entry *he_pair = rb_entry(next, struct hist_entry,
  494. rb_node_in);
  495. next = rb_next(&he_pair->rb_node_in);
  496. cmp = __block_info__cmp(he_pair, he);
  497. if (!cmp)
  498. return he_pair;
  499. }
  500. return NULL;
  501. }
  502. static void init_spark_values(unsigned long *svals, int num)
  503. {
  504. for (int i = 0; i < num; i++)
  505. svals[i] = 0;
  506. }
  507. static void update_spark_value(unsigned long *svals, int num,
  508. struct stats *stats, u64 val)
  509. {
  510. int n = stats->n;
  511. if (n < num)
  512. svals[n] = val;
  513. }
  514. static void compute_cycles_diff(struct hist_entry *he,
  515. struct hist_entry *pair)
  516. {
  517. pair->diff.computed = true;
  518. if (pair->block_info->num && he->block_info->num) {
  519. pair->diff.cycles =
  520. pair->block_info->cycles_aggr / pair->block_info->num_aggr -
  521. he->block_info->cycles_aggr / he->block_info->num_aggr;
  522. if (!cycles_hist)
  523. return;
  524. init_stats(&pair->diff.stats);
  525. init_spark_values(pair->diff.svals, NUM_SPARKS);
  526. for (int i = 0; i < pair->block_info->num; i++) {
  527. u64 val;
  528. if (i >= he->block_info->num || i >= NUM_SPARKS)
  529. break;
  530. val = llabs(pair->block_info->cycles_spark[i] -
  531. he->block_info->cycles_spark[i]);
  532. update_spark_value(pair->diff.svals, NUM_SPARKS,
  533. &pair->diff.stats, val);
  534. update_stats(&pair->diff.stats, val);
  535. }
  536. }
  537. }
  538. static void block_hists_match(struct hists *hists_base,
  539. struct hists *hists_pair)
  540. {
  541. struct rb_root_cached *root = hists_base->entries_in;
  542. struct rb_node *next = rb_first_cached(root);
  543. while (next != NULL) {
  544. struct hist_entry *he = rb_entry(next, struct hist_entry,
  545. rb_node_in);
  546. struct hist_entry *pair = get_block_pair(he, hists_pair);
  547. next = rb_next(&he->rb_node_in);
  548. if (pair) {
  549. hist_entry__add_pair(pair, he);
  550. compute_cycles_diff(he, pair);
  551. }
  552. }
  553. }
  554. static void hists__precompute(struct hists *hists)
  555. {
  556. struct rb_root_cached *root;
  557. struct rb_node *next;
  558. if (hists__has(hists, need_collapse))
  559. root = &hists->entries_collapsed;
  560. else
  561. root = hists->entries_in;
  562. next = rb_first_cached(root);
  563. while (next != NULL) {
  564. struct block_hist *bh, *pair_bh;
  565. struct hist_entry *he, *pair;
  566. struct data__file *d;
  567. int i;
  568. he = rb_entry(next, struct hist_entry, rb_node_in);
  569. next = rb_next(&he->rb_node_in);
  570. if (compute == COMPUTE_CYCLES) {
  571. bh = container_of(he, struct block_hist, he);
  572. init_block_hist(bh);
  573. block_info__process_sym(he, bh, NULL, 0, 0);
  574. }
  575. data__for_each_file_new(i, d) {
  576. pair = get_pair_data(he, d);
  577. if (!pair)
  578. continue;
  579. switch (compute) {
  580. case COMPUTE_DELTA:
  581. case COMPUTE_DELTA_ABS:
  582. compute_delta(he, pair);
  583. break;
  584. case COMPUTE_RATIO:
  585. compute_ratio(he, pair);
  586. break;
  587. case COMPUTE_WEIGHTED_DIFF:
  588. compute_wdiff(he, pair);
  589. break;
  590. case COMPUTE_CYCLES:
  591. pair_bh = container_of(pair, struct block_hist,
  592. he);
  593. init_block_hist(pair_bh);
  594. block_info__process_sym(pair, pair_bh, NULL, 0, 0);
  595. bh = container_of(he, struct block_hist, he);
  596. if (bh->valid && pair_bh->valid) {
  597. block_hists_match(&bh->block_hists,
  598. &pair_bh->block_hists);
  599. hists__output_resort(&pair_bh->block_hists,
  600. NULL);
  601. }
  602. break;
  603. default:
  604. BUG_ON(1);
  605. }
  606. }
  607. }
  608. }
  609. static int64_t cmp_doubles(double l, double r)
  610. {
  611. if (l > r)
  612. return -1;
  613. else if (l < r)
  614. return 1;
  615. else
  616. return 0;
  617. }
  618. static int64_t
  619. __hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  620. int c)
  621. {
  622. switch (c) {
  623. case COMPUTE_DELTA:
  624. {
  625. double l = left->diff.period_ratio_delta;
  626. double r = right->diff.period_ratio_delta;
  627. return cmp_doubles(l, r);
  628. }
  629. case COMPUTE_DELTA_ABS:
  630. {
  631. double l = fabs(left->diff.period_ratio_delta);
  632. double r = fabs(right->diff.period_ratio_delta);
  633. return cmp_doubles(l, r);
  634. }
  635. case COMPUTE_RATIO:
  636. {
  637. double l = left->diff.period_ratio;
  638. double r = right->diff.period_ratio;
  639. return cmp_doubles(l, r);
  640. }
  641. case COMPUTE_WEIGHTED_DIFF:
  642. {
  643. s64 l = left->diff.wdiff;
  644. s64 r = right->diff.wdiff;
  645. return r - l;
  646. }
  647. default:
  648. BUG_ON(1);
  649. }
  650. return 0;
  651. }
  652. static int64_t
  653. hist_entry__cmp_compute(struct hist_entry *left, struct hist_entry *right,
  654. int c, int sort_idx)
  655. {
  656. bool pairs_left = hist_entry__has_pairs(left);
  657. bool pairs_right = hist_entry__has_pairs(right);
  658. struct hist_entry *p_right, *p_left;
  659. if (!pairs_left && !pairs_right)
  660. return 0;
  661. if (!pairs_left || !pairs_right)
  662. return pairs_left ? -1 : 1;
  663. p_left = get_pair_data(left, &data__files[sort_idx]);
  664. p_right = get_pair_data(right, &data__files[sort_idx]);
  665. if (!p_left && !p_right)
  666. return 0;
  667. if (!p_left || !p_right)
  668. return p_left ? -1 : 1;
  669. /*
  670. * We have 2 entries of same kind, let's
  671. * make the data comparison.
  672. */
  673. return __hist_entry__cmp_compute(p_left, p_right, c);
  674. }
  675. static int64_t
  676. hist_entry__cmp_compute_idx(struct hist_entry *left, struct hist_entry *right,
  677. int c, int sort_idx)
  678. {
  679. struct hist_entry *p_right, *p_left;
  680. p_left = get_pair_data(left, &data__files[sort_idx]);
  681. p_right = get_pair_data(right, &data__files[sort_idx]);
  682. if (!p_left && !p_right)
  683. return 0;
  684. if (!p_left || !p_right)
  685. return p_left ? -1 : 1;
  686. if (c != COMPUTE_DELTA && c != COMPUTE_DELTA_ABS) {
  687. /*
  688. * The delta can be computed without the baseline, but
  689. * others are not. Put those entries which have no
  690. * values below.
  691. */
  692. if (left->dummy && right->dummy)
  693. return 0;
  694. if (left->dummy || right->dummy)
  695. return left->dummy ? 1 : -1;
  696. }
  697. return __hist_entry__cmp_compute(p_left, p_right, c);
  698. }
  699. static int64_t
  700. hist_entry__cmp_nop(struct perf_hpp_fmt *fmt __maybe_unused,
  701. struct hist_entry *left __maybe_unused,
  702. struct hist_entry *right __maybe_unused)
  703. {
  704. return 0;
  705. }
  706. static int64_t
  707. hist_entry__cmp_baseline(struct perf_hpp_fmt *fmt __maybe_unused,
  708. struct hist_entry *left, struct hist_entry *right)
  709. {
  710. if (left->stat.period == right->stat.period)
  711. return 0;
  712. return left->stat.period > right->stat.period ? 1 : -1;
  713. }
  714. static int64_t
  715. hist_entry__cmp_delta(struct perf_hpp_fmt *fmt,
  716. struct hist_entry *left, struct hist_entry *right)
  717. {
  718. struct data__file *d = fmt_to_data_file(fmt);
  719. return hist_entry__cmp_compute(right, left, COMPUTE_DELTA, d->idx);
  720. }
  721. static int64_t
  722. hist_entry__cmp_delta_abs(struct perf_hpp_fmt *fmt,
  723. struct hist_entry *left, struct hist_entry *right)
  724. {
  725. struct data__file *d = fmt_to_data_file(fmt);
  726. return hist_entry__cmp_compute(right, left, COMPUTE_DELTA_ABS, d->idx);
  727. }
  728. static int64_t
  729. hist_entry__cmp_ratio(struct perf_hpp_fmt *fmt,
  730. struct hist_entry *left, struct hist_entry *right)
  731. {
  732. struct data__file *d = fmt_to_data_file(fmt);
  733. return hist_entry__cmp_compute(right, left, COMPUTE_RATIO, d->idx);
  734. }
  735. static int64_t
  736. hist_entry__cmp_wdiff(struct perf_hpp_fmt *fmt,
  737. struct hist_entry *left, struct hist_entry *right)
  738. {
  739. struct data__file *d = fmt_to_data_file(fmt);
  740. return hist_entry__cmp_compute(right, left, COMPUTE_WEIGHTED_DIFF, d->idx);
  741. }
  742. static int64_t
  743. hist_entry__cmp_delta_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  744. struct hist_entry *left, struct hist_entry *right)
  745. {
  746. return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA,
  747. sort_compute);
  748. }
  749. static int64_t
  750. hist_entry__cmp_delta_abs_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  751. struct hist_entry *left, struct hist_entry *right)
  752. {
  753. return hist_entry__cmp_compute_idx(right, left, COMPUTE_DELTA_ABS,
  754. sort_compute);
  755. }
  756. static int64_t
  757. hist_entry__cmp_ratio_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  758. struct hist_entry *left, struct hist_entry *right)
  759. {
  760. return hist_entry__cmp_compute_idx(right, left, COMPUTE_RATIO,
  761. sort_compute);
  762. }
  763. static int64_t
  764. hist_entry__cmp_wdiff_idx(struct perf_hpp_fmt *fmt __maybe_unused,
  765. struct hist_entry *left, struct hist_entry *right)
  766. {
  767. return hist_entry__cmp_compute_idx(right, left, COMPUTE_WEIGHTED_DIFF,
  768. sort_compute);
  769. }
  770. static void hists__process(struct hists *hists)
  771. {
  772. if (show_baseline_only)
  773. hists__baseline_only(hists);
  774. hists__precompute(hists);
  775. hists__output_resort(hists, NULL);
  776. if (compute == COMPUTE_CYCLES)
  777. symbol_conf.report_block = true;
  778. hists__fprintf(hists, !quiet, 0, 0, 0, stdout,
  779. !symbol_conf.use_callchain);
  780. }
  781. static void data__fprintf(void)
  782. {
  783. struct data__file *d;
  784. int i;
  785. fprintf(stdout, "# Data files:\n");
  786. data__for_each_file(i, d)
  787. fprintf(stdout, "# [%d] %s %s\n",
  788. d->idx, d->data.path,
  789. !d->idx ? "(Baseline)" : "");
  790. fprintf(stdout, "#\n");
  791. }
  792. static void data_process(void)
  793. {
  794. struct evlist *evlist_base = data__files[0].session->evlist;
  795. struct evsel *evsel_base;
  796. bool first = true;
  797. evlist__for_each_entry(evlist_base, evsel_base) {
  798. struct hists *hists_base = evsel__hists(evsel_base);
  799. struct data__file *d;
  800. int i;
  801. data__for_each_file_new(i, d) {
  802. struct evlist *evlist = d->session->evlist;
  803. struct evsel *evsel;
  804. struct hists *hists;
  805. evsel = evsel_match(evsel_base, evlist);
  806. if (!evsel)
  807. continue;
  808. hists = evsel__hists(evsel);
  809. d->hists = hists;
  810. hists__match(hists_base, hists);
  811. if (!show_baseline_only)
  812. hists__link(hists_base, hists);
  813. }
  814. if (!quiet) {
  815. fprintf(stdout, "%s# Event '%s'\n#\n", first ? "" : "\n",
  816. evsel__name(evsel_base));
  817. }
  818. first = false;
  819. if (verbose > 0 || ((data__files_cnt > 2) && !quiet))
  820. data__fprintf();
  821. /* Don't sort callchain for perf diff */
  822. evsel__reset_sample_bit(evsel_base, CALLCHAIN);
  823. hists__process(hists_base);
  824. }
  825. }
  826. static int process_base_stream(struct data__file *data_base,
  827. struct data__file *data_pair,
  828. const char *title __maybe_unused)
  829. {
  830. struct evlist *evlist_base = data_base->session->evlist;
  831. struct evlist *evlist_pair = data_pair->session->evlist;
  832. struct evsel *evsel_base, *evsel_pair;
  833. struct evsel_streams *es_base, *es_pair;
  834. evlist__for_each_entry(evlist_base, evsel_base) {
  835. evsel_pair = evsel_match(evsel_base, evlist_pair);
  836. if (!evsel_pair)
  837. continue;
  838. es_base = evsel_streams__entry(data_base->evlist_streams,
  839. evsel_base->core.idx);
  840. if (!es_base)
  841. return -1;
  842. es_pair = evsel_streams__entry(data_pair->evlist_streams,
  843. evsel_pair->core.idx);
  844. if (!es_pair)
  845. return -1;
  846. evsel_streams__match(es_base, es_pair);
  847. evsel_streams__report(es_base, es_pair);
  848. }
  849. return 0;
  850. }
  851. static void stream_process(void)
  852. {
  853. /*
  854. * Stream comparison only supports two data files.
  855. * perf.data.old and perf.data. data__files[0] is perf.data.old,
  856. * data__files[1] is perf.data.
  857. */
  858. process_base_stream(&data__files[0], &data__files[1],
  859. "# Output based on old perf data:\n#\n");
  860. }
  861. static void data__free(struct data__file *d)
  862. {
  863. int col;
  864. if (d->evlist_streams)
  865. evlist_streams__delete(d->evlist_streams);
  866. for (col = 0; col < PERF_HPP_DIFF__MAX_INDEX; col++) {
  867. struct diff_hpp_fmt *fmt = &d->fmt[col];
  868. zfree(&fmt->header);
  869. }
  870. }
  871. static int abstime_str_dup(char **pstr)
  872. {
  873. char *str = NULL;
  874. if (pdiff.time_str && strchr(pdiff.time_str, ':')) {
  875. str = strdup(pdiff.time_str);
  876. if (!str)
  877. return -ENOMEM;
  878. }
  879. *pstr = str;
  880. return 0;
  881. }
  882. static int parse_absolute_time(struct data__file *d, char **pstr)
  883. {
  884. char *p = *pstr;
  885. int ret;
  886. /*
  887. * Absolute timestamp for one file has the format: a.b,c.d
  888. * For multiple files, the format is: a.b,c.d:a.b,c.d
  889. */
  890. p = strchr(*pstr, ':');
  891. if (p) {
  892. if (p == *pstr) {
  893. pr_err("Invalid time string\n");
  894. return -EINVAL;
  895. }
  896. *p = 0;
  897. p++;
  898. if (*p == 0) {
  899. pr_err("Invalid time string\n");
  900. return -EINVAL;
  901. }
  902. }
  903. ret = perf_time__parse_for_ranges(*pstr, d->session,
  904. &pdiff.ptime_range,
  905. &pdiff.range_size,
  906. &pdiff.range_num);
  907. if (ret < 0)
  908. return ret;
  909. if (!p || *p == 0)
  910. *pstr = NULL;
  911. else
  912. *pstr = p;
  913. return ret;
  914. }
  915. static int parse_percent_time(struct data__file *d)
  916. {
  917. int ret;
  918. ret = perf_time__parse_for_ranges(pdiff.time_str, d->session,
  919. &pdiff.ptime_range,
  920. &pdiff.range_size,
  921. &pdiff.range_num);
  922. return ret;
  923. }
  924. static int parse_time_str(struct data__file *d, char *abstime_ostr,
  925. char **pabstime_tmp)
  926. {
  927. int ret = 0;
  928. if (abstime_ostr)
  929. ret = parse_absolute_time(d, pabstime_tmp);
  930. else if (pdiff.time_str)
  931. ret = parse_percent_time(d);
  932. return ret;
  933. }
  934. static int check_file_brstack(void)
  935. {
  936. struct data__file *d;
  937. bool has_br_stack;
  938. int i;
  939. data__for_each_file(i, d) {
  940. d->session = perf_session__new(&d->data, &pdiff.tool);
  941. if (IS_ERR(d->session)) {
  942. pr_err("Failed to open %s\n", d->data.path);
  943. return PTR_ERR(d->session);
  944. }
  945. has_br_stack = perf_header__has_feat(&d->session->header,
  946. HEADER_BRANCH_STACK);
  947. perf_session__delete(d->session);
  948. if (!has_br_stack)
  949. return 0;
  950. }
  951. /* Set only all files having branch stacks */
  952. pdiff.has_br_stack = true;
  953. return 0;
  954. }
  955. static int __cmd_diff(void)
  956. {
  957. struct data__file *d;
  958. int ret, i;
  959. char *abstime_ostr, *abstime_tmp;
  960. ret = abstime_str_dup(&abstime_ostr);
  961. if (ret)
  962. return ret;
  963. abstime_tmp = abstime_ostr;
  964. ret = -EINVAL;
  965. data__for_each_file(i, d) {
  966. d->session = perf_session__new(&d->data, &pdiff.tool);
  967. if (IS_ERR(d->session)) {
  968. ret = PTR_ERR(d->session);
  969. pr_err("Failed to open %s\n", d->data.path);
  970. goto out_delete;
  971. }
  972. if (pdiff.time_str) {
  973. ret = parse_time_str(d, abstime_ostr, &abstime_tmp);
  974. if (ret < 0)
  975. goto out_delete;
  976. }
  977. if (cpu_list) {
  978. ret = perf_session__cpu_bitmap(d->session, cpu_list,
  979. cpu_bitmap);
  980. if (ret < 0)
  981. goto out_delete;
  982. }
  983. ret = perf_session__process_events(d->session);
  984. if (ret) {
  985. pr_err("Failed to process %s\n", d->data.path);
  986. goto out_delete;
  987. }
  988. evlist__collapse_resort(d->session->evlist);
  989. if (pdiff.ptime_range)
  990. zfree(&pdiff.ptime_range);
  991. if (compute == COMPUTE_STREAM) {
  992. d->evlist_streams = evlist__create_streams(
  993. d->session->evlist, 5);
  994. if (!d->evlist_streams) {
  995. ret = -ENOMEM;
  996. goto out_delete;
  997. }
  998. }
  999. }
  1000. if (compute == COMPUTE_STREAM)
  1001. stream_process();
  1002. else
  1003. data_process();
  1004. out_delete:
  1005. data__for_each_file(i, d) {
  1006. if (!IS_ERR(d->session))
  1007. perf_session__delete(d->session);
  1008. data__free(d);
  1009. }
  1010. free(data__files);
  1011. if (pdiff.ptime_range)
  1012. zfree(&pdiff.ptime_range);
  1013. if (abstime_ostr)
  1014. free(abstime_ostr);
  1015. return ret;
  1016. }
  1017. static const char * const diff_usage[] = {
  1018. "perf diff [<options>] [old_file] [new_file]",
  1019. NULL,
  1020. };
  1021. static const struct option options[] = {
  1022. OPT_INCR('v', "verbose", &verbose,
  1023. "be more verbose (show symbol address, etc)"),
  1024. OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"),
  1025. OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
  1026. "Show only items with match in baseline"),
  1027. OPT_CALLBACK('c', "compute", &compute,
  1028. "delta,delta-abs,ratio,wdiff:w1,w2 (default delta-abs),cycles",
  1029. "Entries differential computation selection",
  1030. setup_compute),
  1031. OPT_BOOLEAN('p', "period", &show_period,
  1032. "Show period values."),
  1033. OPT_BOOLEAN('F', "formula", &show_formula,
  1034. "Show formula."),
  1035. OPT_BOOLEAN(0, "cycles-hist", &cycles_hist,
  1036. "Show cycles histogram and standard deviation "
  1037. "- WARNING: use only with -c cycles."),
  1038. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1039. "dump raw trace in ASCII"),
  1040. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  1041. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1042. "file", "kallsyms pathname"),
  1043. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  1044. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  1045. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  1046. "only consider symbols in these dsos"),
  1047. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1048. "only consider symbols in these comms"),
  1049. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1050. "only consider these symbols"),
  1051. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1052. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  1053. " Please refer the man page for the complete list."),
  1054. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  1055. "separator for columns, no spaces will be added between "
  1056. "columns '.' is reserved."),
  1057. OPT_CALLBACK(0, "symfs", NULL, "directory",
  1058. "Look for files with symbols relative to this directory",
  1059. symbol__config_symfs),
  1060. OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
  1061. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  1062. "How to display percentage of filtered entries", parse_filter_percentage),
  1063. OPT_STRING(0, "time", &pdiff.time_str, "str",
  1064. "Time span (time percent or absolute timestamp)"),
  1065. OPT_STRING(0, "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1066. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1067. "only consider symbols in these pids"),
  1068. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1069. "only consider symbols in these tids"),
  1070. OPT_BOOLEAN(0, "stream", &pdiff.stream,
  1071. "Enable hot streams comparison."),
  1072. OPT_END()
  1073. };
  1074. static double baseline_percent(struct hist_entry *he)
  1075. {
  1076. u64 total = hists__total_period(he->hists);
  1077. return 100.0 * he->stat.period / total;
  1078. }
  1079. static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
  1080. struct perf_hpp *hpp, struct hist_entry *he)
  1081. {
  1082. struct diff_hpp_fmt *dfmt =
  1083. container_of(fmt, struct diff_hpp_fmt, fmt);
  1084. double percent = baseline_percent(he);
  1085. char pfmt[20] = " ";
  1086. if (!he->dummy) {
  1087. scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
  1088. return percent_color_snprintf(hpp->buf, hpp->size,
  1089. pfmt, percent);
  1090. } else
  1091. return scnprintf(hpp->buf, hpp->size, "%*s",
  1092. dfmt->header_width, pfmt);
  1093. }
  1094. static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
  1095. {
  1096. double percent = baseline_percent(he);
  1097. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  1098. int ret = 0;
  1099. if (!he->dummy)
  1100. ret = scnprintf(buf, size, fmt, percent);
  1101. return ret;
  1102. }
  1103. static int cycles_printf(struct hist_entry *he, struct hist_entry *pair,
  1104. struct perf_hpp *hpp, int width)
  1105. {
  1106. struct block_hist *bh = container_of(he, struct block_hist, he);
  1107. struct block_hist *bh_pair = container_of(pair, struct block_hist, he);
  1108. struct hist_entry *block_he;
  1109. struct block_info *bi;
  1110. char buf[128];
  1111. char *start_line, *end_line;
  1112. block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx);
  1113. if (!block_he) {
  1114. hpp->skip = true;
  1115. return 0;
  1116. }
  1117. /*
  1118. * Avoid printing the warning "addr2line_init failed for ..."
  1119. */
  1120. symbol_conf.disable_add2line_warn = true;
  1121. bi = block_he->block_info;
  1122. start_line = map__srcline(he->ms.map, bi->sym->start + bi->start,
  1123. he->ms.sym);
  1124. end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
  1125. he->ms.sym);
  1126. if (start_line != SRCLINE_UNKNOWN &&
  1127. end_line != SRCLINE_UNKNOWN) {
  1128. scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld",
  1129. start_line, end_line, block_he->diff.cycles);
  1130. } else {
  1131. scnprintf(buf, sizeof(buf), "[%7lx -> %7lx] %4ld",
  1132. bi->start, bi->end, block_he->diff.cycles);
  1133. }
  1134. zfree_srcline(&start_line);
  1135. zfree_srcline(&end_line);
  1136. return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
  1137. }
  1138. static int __hpp__color_compare(struct perf_hpp_fmt *fmt,
  1139. struct perf_hpp *hpp, struct hist_entry *he,
  1140. int comparison_method)
  1141. {
  1142. struct diff_hpp_fmt *dfmt =
  1143. container_of(fmt, struct diff_hpp_fmt, fmt);
  1144. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  1145. double diff;
  1146. s64 wdiff;
  1147. char pfmt[20] = " ";
  1148. if (!pair) {
  1149. if (comparison_method == COMPUTE_CYCLES) {
  1150. struct block_hist *bh;
  1151. bh = container_of(he, struct block_hist, he);
  1152. if (bh->block_idx)
  1153. hpp->skip = true;
  1154. }
  1155. goto no_print;
  1156. }
  1157. switch (comparison_method) {
  1158. case COMPUTE_DELTA:
  1159. if (pair->diff.computed)
  1160. diff = pair->diff.period_ratio_delta;
  1161. else
  1162. diff = compute_delta(he, pair);
  1163. scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1);
  1164. return percent_color_snprintf(hpp->buf, hpp->size,
  1165. pfmt, diff);
  1166. case COMPUTE_RATIO:
  1167. if (he->dummy)
  1168. goto dummy_print;
  1169. if (pair->diff.computed)
  1170. diff = pair->diff.period_ratio;
  1171. else
  1172. diff = compute_ratio(he, pair);
  1173. scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width);
  1174. return value_color_snprintf(hpp->buf, hpp->size,
  1175. pfmt, diff);
  1176. case COMPUTE_WEIGHTED_DIFF:
  1177. if (he->dummy)
  1178. goto dummy_print;
  1179. if (pair->diff.computed)
  1180. wdiff = pair->diff.wdiff;
  1181. else
  1182. wdiff = compute_wdiff(he, pair);
  1183. scnprintf(pfmt, 20, "%%14ld", dfmt->header_width);
  1184. return color_snprintf(hpp->buf, hpp->size,
  1185. get_percent_color(wdiff),
  1186. pfmt, wdiff);
  1187. case COMPUTE_CYCLES:
  1188. return cycles_printf(he, pair, hpp, dfmt->header_width);
  1189. default:
  1190. BUG_ON(1);
  1191. }
  1192. dummy_print:
  1193. return scnprintf(hpp->buf, hpp->size, "%*s",
  1194. dfmt->header_width, "N/A");
  1195. no_print:
  1196. return scnprintf(hpp->buf, hpp->size, "%*s",
  1197. dfmt->header_width, pfmt);
  1198. }
  1199. static int hpp__color_delta(struct perf_hpp_fmt *fmt,
  1200. struct perf_hpp *hpp, struct hist_entry *he)
  1201. {
  1202. return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA);
  1203. }
  1204. static int hpp__color_ratio(struct perf_hpp_fmt *fmt,
  1205. struct perf_hpp *hpp, struct hist_entry *he)
  1206. {
  1207. return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO);
  1208. }
  1209. static int hpp__color_wdiff(struct perf_hpp_fmt *fmt,
  1210. struct perf_hpp *hpp, struct hist_entry *he)
  1211. {
  1212. return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF);
  1213. }
  1214. static int hpp__color_cycles(struct perf_hpp_fmt *fmt,
  1215. struct perf_hpp *hpp, struct hist_entry *he)
  1216. {
  1217. return __hpp__color_compare(fmt, hpp, he, COMPUTE_CYCLES);
  1218. }
  1219. static int all_zero(unsigned long *vals, int len)
  1220. {
  1221. int i;
  1222. for (i = 0; i < len; i++)
  1223. if (vals[i] != 0)
  1224. return 0;
  1225. return 1;
  1226. }
  1227. static int print_cycles_spark(char *bf, int size, unsigned long *svals, u64 n)
  1228. {
  1229. int printed;
  1230. if (n <= 1)
  1231. return 0;
  1232. if (n > NUM_SPARKS)
  1233. n = NUM_SPARKS;
  1234. if (all_zero(svals, n))
  1235. return 0;
  1236. printed = print_spark(bf, size, svals, n);
  1237. printed += scnprintf(bf + printed, size - printed, " ");
  1238. return printed;
  1239. }
  1240. static int hpp__color_cycles_hist(struct perf_hpp_fmt *fmt,
  1241. struct perf_hpp *hpp, struct hist_entry *he)
  1242. {
  1243. struct diff_hpp_fmt *dfmt =
  1244. container_of(fmt, struct diff_hpp_fmt, fmt);
  1245. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  1246. struct block_hist *bh = container_of(he, struct block_hist, he);
  1247. struct block_hist *bh_pair;
  1248. struct hist_entry *block_he;
  1249. char spark[32], buf[128];
  1250. double r;
  1251. int ret, pad;
  1252. if (!pair) {
  1253. if (bh->block_idx)
  1254. hpp->skip = true;
  1255. goto no_print;
  1256. }
  1257. bh_pair = container_of(pair, struct block_hist, he);
  1258. block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx);
  1259. if (!block_he) {
  1260. hpp->skip = true;
  1261. goto no_print;
  1262. }
  1263. ret = print_cycles_spark(spark, sizeof(spark), block_he->diff.svals,
  1264. block_he->diff.stats.n);
  1265. r = rel_stddev_stats(stddev_stats(&block_he->diff.stats),
  1266. avg_stats(&block_he->diff.stats));
  1267. if (ret) {
  1268. /*
  1269. * Padding spaces if number of sparks less than NUM_SPARKS
  1270. * otherwise the output is not aligned.
  1271. */
  1272. pad = NUM_SPARKS - ((ret - 1) / 3);
  1273. scnprintf(buf, sizeof(buf), "%s%5.1f%% %s", "\u00B1", r, spark);
  1274. ret = scnprintf(hpp->buf, hpp->size, "%*s",
  1275. dfmt->header_width, buf);
  1276. if (pad) {
  1277. ret += scnprintf(hpp->buf + ret, hpp->size - ret,
  1278. "%-*s", pad, " ");
  1279. }
  1280. return ret;
  1281. }
  1282. no_print:
  1283. return scnprintf(hpp->buf, hpp->size, "%*s",
  1284. dfmt->header_width, " ");
  1285. }
  1286. static void
  1287. hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
  1288. {
  1289. switch (idx) {
  1290. case PERF_HPP_DIFF__PERIOD_BASELINE:
  1291. scnprintf(buf, size, "%" PRIu64, he->stat.period);
  1292. break;
  1293. default:
  1294. break;
  1295. }
  1296. }
  1297. static void
  1298. hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
  1299. int idx, char *buf, size_t size)
  1300. {
  1301. double diff;
  1302. double ratio;
  1303. s64 wdiff;
  1304. switch (idx) {
  1305. case PERF_HPP_DIFF__DELTA:
  1306. case PERF_HPP_DIFF__DELTA_ABS:
  1307. if (pair->diff.computed)
  1308. diff = pair->diff.period_ratio_delta;
  1309. else
  1310. diff = compute_delta(he, pair);
  1311. scnprintf(buf, size, "%+4.2F%%", diff);
  1312. break;
  1313. case PERF_HPP_DIFF__RATIO:
  1314. /* No point for ratio number if we are dummy.. */
  1315. if (he->dummy) {
  1316. scnprintf(buf, size, "N/A");
  1317. break;
  1318. }
  1319. if (pair->diff.computed)
  1320. ratio = pair->diff.period_ratio;
  1321. else
  1322. ratio = compute_ratio(he, pair);
  1323. if (ratio > 0.0)
  1324. scnprintf(buf, size, "%14.6F", ratio);
  1325. break;
  1326. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  1327. /* No point for wdiff number if we are dummy.. */
  1328. if (he->dummy) {
  1329. scnprintf(buf, size, "N/A");
  1330. break;
  1331. }
  1332. if (pair->diff.computed)
  1333. wdiff = pair->diff.wdiff;
  1334. else
  1335. wdiff = compute_wdiff(he, pair);
  1336. if (wdiff != 0)
  1337. scnprintf(buf, size, "%14ld", wdiff);
  1338. break;
  1339. case PERF_HPP_DIFF__FORMULA:
  1340. formula_fprintf(he, pair, buf, size);
  1341. break;
  1342. case PERF_HPP_DIFF__PERIOD:
  1343. scnprintf(buf, size, "%" PRIu64, pair->stat.period);
  1344. break;
  1345. default:
  1346. BUG_ON(1);
  1347. }
  1348. }
  1349. static void
  1350. __hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
  1351. char *buf, size_t size)
  1352. {
  1353. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  1354. int idx = dfmt->idx;
  1355. /* baseline is special */
  1356. if (idx == PERF_HPP_DIFF__BASELINE)
  1357. hpp__entry_baseline(he, buf, size);
  1358. else {
  1359. if (pair)
  1360. hpp__entry_pair(he, pair, idx, buf, size);
  1361. else
  1362. hpp__entry_unpair(he, idx, buf, size);
  1363. }
  1364. }
  1365. static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
  1366. struct hist_entry *he)
  1367. {
  1368. struct diff_hpp_fmt *dfmt =
  1369. container_of(_fmt, struct diff_hpp_fmt, fmt);
  1370. char buf[MAX_COL_WIDTH] = " ";
  1371. __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
  1372. if (symbol_conf.field_sep)
  1373. return scnprintf(hpp->buf, hpp->size, "%s", buf);
  1374. else
  1375. return scnprintf(hpp->buf, hpp->size, "%*s",
  1376. dfmt->header_width, buf);
  1377. }
  1378. static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1379. struct hists *hists __maybe_unused,
  1380. int line __maybe_unused,
  1381. int *span __maybe_unused)
  1382. {
  1383. struct diff_hpp_fmt *dfmt =
  1384. container_of(fmt, struct diff_hpp_fmt, fmt);
  1385. BUG_ON(!dfmt->header);
  1386. return scnprintf(hpp->buf, hpp->size, dfmt->header);
  1387. }
  1388. static int hpp__width(struct perf_hpp_fmt *fmt,
  1389. struct perf_hpp *hpp __maybe_unused,
  1390. struct hists *hists __maybe_unused)
  1391. {
  1392. struct diff_hpp_fmt *dfmt =
  1393. container_of(fmt, struct diff_hpp_fmt, fmt);
  1394. BUG_ON(dfmt->header_width <= 0);
  1395. return dfmt->header_width;
  1396. }
  1397. static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
  1398. {
  1399. #define MAX_HEADER_NAME 100
  1400. char buf_indent[MAX_HEADER_NAME];
  1401. char buf[MAX_HEADER_NAME];
  1402. const char *header = NULL;
  1403. int width = 0;
  1404. BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
  1405. header = columns[dfmt->idx].name;
  1406. width = columns[dfmt->idx].width;
  1407. /* Only our defined HPP fmts should appear here. */
  1408. BUG_ON(!header);
  1409. if (data__files_cnt > 2)
  1410. scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
  1411. #define NAME (data__files_cnt > 2 ? buf : header)
  1412. dfmt->header_width = width;
  1413. width = (int) strlen(NAME);
  1414. if (dfmt->header_width < width)
  1415. dfmt->header_width = width;
  1416. scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
  1417. dfmt->header_width, NAME);
  1418. dfmt->header = strdup(buf_indent);
  1419. #undef MAX_HEADER_NAME
  1420. #undef NAME
  1421. }
  1422. static void data__hpp_register(struct data__file *d, int idx)
  1423. {
  1424. struct diff_hpp_fmt *dfmt = &d->fmt[idx];
  1425. struct perf_hpp_fmt *fmt = &dfmt->fmt;
  1426. dfmt->idx = idx;
  1427. fmt->header = hpp__header;
  1428. fmt->width = hpp__width;
  1429. fmt->entry = hpp__entry_global;
  1430. fmt->cmp = hist_entry__cmp_nop;
  1431. fmt->collapse = hist_entry__cmp_nop;
  1432. /* TODO more colors */
  1433. switch (idx) {
  1434. case PERF_HPP_DIFF__BASELINE:
  1435. fmt->color = hpp__color_baseline;
  1436. fmt->sort = hist_entry__cmp_baseline;
  1437. break;
  1438. case PERF_HPP_DIFF__DELTA:
  1439. fmt->color = hpp__color_delta;
  1440. fmt->sort = hist_entry__cmp_delta;
  1441. break;
  1442. case PERF_HPP_DIFF__RATIO:
  1443. fmt->color = hpp__color_ratio;
  1444. fmt->sort = hist_entry__cmp_ratio;
  1445. break;
  1446. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  1447. fmt->color = hpp__color_wdiff;
  1448. fmt->sort = hist_entry__cmp_wdiff;
  1449. break;
  1450. case PERF_HPP_DIFF__DELTA_ABS:
  1451. fmt->color = hpp__color_delta;
  1452. fmt->sort = hist_entry__cmp_delta_abs;
  1453. break;
  1454. case PERF_HPP_DIFF__CYCLES:
  1455. fmt->color = hpp__color_cycles;
  1456. fmt->sort = hist_entry__cmp_nop;
  1457. break;
  1458. case PERF_HPP_DIFF__CYCLES_HIST:
  1459. fmt->color = hpp__color_cycles_hist;
  1460. fmt->sort = hist_entry__cmp_nop;
  1461. break;
  1462. default:
  1463. fmt->sort = hist_entry__cmp_nop;
  1464. break;
  1465. }
  1466. init_header(d, dfmt);
  1467. perf_hpp__column_register(fmt);
  1468. perf_hpp__register_sort_field(fmt);
  1469. }
  1470. static int ui_init(void)
  1471. {
  1472. struct data__file *d;
  1473. struct perf_hpp_fmt *fmt;
  1474. int i;
  1475. data__for_each_file(i, d) {
  1476. /*
  1477. * Baseline or compute related columns:
  1478. *
  1479. * PERF_HPP_DIFF__BASELINE
  1480. * PERF_HPP_DIFF__DELTA
  1481. * PERF_HPP_DIFF__RATIO
  1482. * PERF_HPP_DIFF__WEIGHTED_DIFF
  1483. * PERF_HPP_DIFF__CYCLES
  1484. */
  1485. data__hpp_register(d, i ? compute_2_hpp[compute] :
  1486. PERF_HPP_DIFF__BASELINE);
  1487. if (cycles_hist && i)
  1488. data__hpp_register(d, PERF_HPP_DIFF__CYCLES_HIST);
  1489. /*
  1490. * And the rest:
  1491. *
  1492. * PERF_HPP_DIFF__FORMULA
  1493. * PERF_HPP_DIFF__PERIOD
  1494. * PERF_HPP_DIFF__PERIOD_BASELINE
  1495. */
  1496. if (show_formula && i)
  1497. data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
  1498. if (show_period)
  1499. data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
  1500. PERF_HPP_DIFF__PERIOD_BASELINE);
  1501. }
  1502. if (!sort_compute)
  1503. return 0;
  1504. /*
  1505. * Prepend an fmt to sort on columns at 'sort_compute' first.
  1506. * This fmt is added only to the sort list but not to the
  1507. * output fields list.
  1508. *
  1509. * Note that this column (data) can be compared twice - one
  1510. * for this 'sort_compute' fmt and another for the normal
  1511. * diff_hpp_fmt. But it shouldn't a problem as most entries
  1512. * will be sorted out by first try or baseline and comparing
  1513. * is not a costly operation.
  1514. */
  1515. fmt = zalloc(sizeof(*fmt));
  1516. if (fmt == NULL) {
  1517. pr_err("Memory allocation failed\n");
  1518. return -1;
  1519. }
  1520. fmt->cmp = hist_entry__cmp_nop;
  1521. fmt->collapse = hist_entry__cmp_nop;
  1522. switch (compute) {
  1523. case COMPUTE_DELTA:
  1524. fmt->sort = hist_entry__cmp_delta_idx;
  1525. break;
  1526. case COMPUTE_RATIO:
  1527. fmt->sort = hist_entry__cmp_ratio_idx;
  1528. break;
  1529. case COMPUTE_WEIGHTED_DIFF:
  1530. fmt->sort = hist_entry__cmp_wdiff_idx;
  1531. break;
  1532. case COMPUTE_DELTA_ABS:
  1533. fmt->sort = hist_entry__cmp_delta_abs_idx;
  1534. break;
  1535. case COMPUTE_CYCLES:
  1536. /*
  1537. * Should set since 'fmt->sort' is called without
  1538. * checking valid during sorting
  1539. */
  1540. fmt->sort = hist_entry__cmp_nop;
  1541. break;
  1542. default:
  1543. BUG_ON(1);
  1544. }
  1545. perf_hpp__prepend_sort_field(fmt);
  1546. return 0;
  1547. }
  1548. static int data_init(int argc, const char **argv)
  1549. {
  1550. struct data__file *d;
  1551. static const char *defaults[] = {
  1552. "perf.data.old",
  1553. "perf.data",
  1554. };
  1555. bool use_default = true;
  1556. int i;
  1557. data__files_cnt = 2;
  1558. if (argc) {
  1559. if (argc == 1)
  1560. defaults[1] = argv[0];
  1561. else {
  1562. data__files_cnt = argc;
  1563. use_default = false;
  1564. }
  1565. } else if (perf_guest) {
  1566. defaults[0] = "perf.data.host";
  1567. defaults[1] = "perf.data.guest";
  1568. }
  1569. if (sort_compute >= (unsigned int) data__files_cnt) {
  1570. pr_err("Order option out of limit.\n");
  1571. return -EINVAL;
  1572. }
  1573. data__files = zalloc(sizeof(*data__files) * data__files_cnt);
  1574. if (!data__files)
  1575. return -ENOMEM;
  1576. data__for_each_file(i, d) {
  1577. struct perf_data *data = &d->data;
  1578. data->path = use_default ? defaults[i] : argv[i];
  1579. data->mode = PERF_DATA_MODE_READ;
  1580. data->force = force;
  1581. d->idx = i;
  1582. }
  1583. return 0;
  1584. }
  1585. static int diff__config(const char *var, const char *value,
  1586. void *cb __maybe_unused)
  1587. {
  1588. if (!strcmp(var, "diff.order")) {
  1589. int ret;
  1590. if (perf_config_int(&ret, var, value) < 0)
  1591. return -1;
  1592. sort_compute = ret;
  1593. return 0;
  1594. }
  1595. if (!strcmp(var, "diff.compute")) {
  1596. if (!strcmp(value, "delta")) {
  1597. compute = COMPUTE_DELTA;
  1598. } else if (!strcmp(value, "delta-abs")) {
  1599. compute = COMPUTE_DELTA_ABS;
  1600. } else if (!strcmp(value, "ratio")) {
  1601. compute = COMPUTE_RATIO;
  1602. } else if (!strcmp(value, "wdiff")) {
  1603. compute = COMPUTE_WEIGHTED_DIFF;
  1604. } else {
  1605. pr_err("Invalid compute method: %s\n", value);
  1606. return -1;
  1607. }
  1608. }
  1609. return 0;
  1610. }
  1611. int cmd_diff(int argc, const char **argv)
  1612. {
  1613. int ret = hists__init();
  1614. if (ret < 0)
  1615. return ret;
  1616. perf_tool__init(&pdiff.tool, /*ordered_events=*/true);
  1617. pdiff.tool.sample = diff__process_sample_event;
  1618. pdiff.tool.mmap = perf_event__process_mmap;
  1619. pdiff.tool.mmap2 = perf_event__process_mmap2;
  1620. pdiff.tool.comm = perf_event__process_comm;
  1621. pdiff.tool.exit = perf_event__process_exit;
  1622. pdiff.tool.fork = perf_event__process_fork;
  1623. pdiff.tool.lost = perf_event__process_lost;
  1624. pdiff.tool.namespaces = perf_event__process_namespaces;
  1625. pdiff.tool.cgroup = perf_event__process_cgroup;
  1626. pdiff.tool.ordering_requires_timestamps = true;
  1627. perf_config(diff__config, NULL);
  1628. argc = parse_options(argc, argv, options, diff_usage, 0);
  1629. if (quiet)
  1630. perf_quiet_option();
  1631. if (cycles_hist && (compute != COMPUTE_CYCLES))
  1632. usage_with_options(diff_usage, options);
  1633. if (pdiff.stream)
  1634. compute = COMPUTE_STREAM;
  1635. symbol__annotation_init();
  1636. if (symbol__init(NULL) < 0)
  1637. return -1;
  1638. if (data_init(argc, argv) < 0)
  1639. return -1;
  1640. if (check_file_brstack() < 0)
  1641. return -1;
  1642. if ((compute == COMPUTE_CYCLES || compute == COMPUTE_STREAM)
  1643. && !pdiff.has_br_stack) {
  1644. return -1;
  1645. }
  1646. if (compute == COMPUTE_STREAM) {
  1647. symbol_conf.show_branchflag_count = true;
  1648. symbol_conf.disable_add2line_warn = true;
  1649. callchain_param.mode = CHAIN_FLAT;
  1650. callchain_param.key = CCKEY_SRCLINE;
  1651. callchain_param.branch_callstack = 1;
  1652. symbol_conf.use_callchain = true;
  1653. callchain_register_param(&callchain_param);
  1654. sort_order = "srcline,symbol,dso";
  1655. } else {
  1656. if (ui_init() < 0)
  1657. return -1;
  1658. sort__mode = SORT_MODE__DIFF;
  1659. }
  1660. if (setup_sorting(NULL) < 0)
  1661. usage_with_options(diff_usage, options);
  1662. setup_pager();
  1663. sort__setup_elide(NULL);
  1664. return __cmd_diff();
  1665. }