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