builtin-diff.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  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 perf_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->idx);
  852. if (!es_base)
  853. return -1;
  854. es_pair = evsel_streams__entry(data_pair->evlist_streams,
  855. evsel_pair->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, false, &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, false, &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. perf_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. perf_session__delete(d->session);
  1019. data__free(d);
  1020. }
  1021. free(data__files);
  1022. if (pdiff.ptime_range)
  1023. zfree(&pdiff.ptime_range);
  1024. if (abstime_ostr)
  1025. free(abstime_ostr);
  1026. return ret;
  1027. }
  1028. static const char * const diff_usage[] = {
  1029. "perf diff [<options>] [old_file] [new_file]",
  1030. NULL,
  1031. };
  1032. static const struct option options[] = {
  1033. OPT_INCR('v', "verbose", &verbose,
  1034. "be more verbose (show symbol address, etc)"),
  1035. OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
  1036. OPT_BOOLEAN('b', "baseline-only", &show_baseline_only,
  1037. "Show only items with match in baseline"),
  1038. OPT_CALLBACK('c', "compute", &compute,
  1039. "delta,delta-abs,ratio,wdiff:w1,w2 (default delta-abs),cycles",
  1040. "Entries differential computation selection",
  1041. setup_compute),
  1042. OPT_BOOLEAN('p', "period", &show_period,
  1043. "Show period values."),
  1044. OPT_BOOLEAN('F', "formula", &show_formula,
  1045. "Show formula."),
  1046. OPT_BOOLEAN(0, "cycles-hist", &cycles_hist,
  1047. "Show cycles histogram and standard deviation "
  1048. "- WARNING: use only with -c cycles."),
  1049. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1050. "dump raw trace in ASCII"),
  1051. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  1052. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1053. "file", "kallsyms pathname"),
  1054. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  1055. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  1056. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  1057. "only consider symbols in these dsos"),
  1058. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1059. "only consider symbols in these comms"),
  1060. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1061. "only consider these symbols"),
  1062. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1063. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  1064. " Please refer the man page for the complete list."),
  1065. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  1066. "separator for columns, no spaces will be added between "
  1067. "columns '.' is reserved."),
  1068. OPT_CALLBACK(0, "symfs", NULL, "directory",
  1069. "Look for files with symbols relative to this directory",
  1070. symbol__config_symfs),
  1071. OPT_UINTEGER('o', "order", &sort_compute, "Specify compute sorting."),
  1072. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  1073. "How to display percentage of filtered entries", parse_filter_percentage),
  1074. OPT_STRING(0, "time", &pdiff.time_str, "str",
  1075. "Time span (time percent or absolute timestamp)"),
  1076. OPT_STRING(0, "cpu", &cpu_list, "cpu", "list of cpus to profile"),
  1077. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1078. "only consider symbols in these pids"),
  1079. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1080. "only consider symbols in these tids"),
  1081. OPT_BOOLEAN(0, "stream", &pdiff.stream,
  1082. "Enable hot streams comparison."),
  1083. OPT_END()
  1084. };
  1085. static double baseline_percent(struct hist_entry *he)
  1086. {
  1087. u64 total = hists__total_period(he->hists);
  1088. return 100.0 * he->stat.period / total;
  1089. }
  1090. static int hpp__color_baseline(struct perf_hpp_fmt *fmt,
  1091. struct perf_hpp *hpp, struct hist_entry *he)
  1092. {
  1093. struct diff_hpp_fmt *dfmt =
  1094. container_of(fmt, struct diff_hpp_fmt, fmt);
  1095. double percent = baseline_percent(he);
  1096. char pfmt[20] = " ";
  1097. if (!he->dummy) {
  1098. scnprintf(pfmt, 20, "%%%d.2f%%%%", dfmt->header_width - 1);
  1099. return percent_color_snprintf(hpp->buf, hpp->size,
  1100. pfmt, percent);
  1101. } else
  1102. return scnprintf(hpp->buf, hpp->size, "%*s",
  1103. dfmt->header_width, pfmt);
  1104. }
  1105. static int hpp__entry_baseline(struct hist_entry *he, char *buf, size_t size)
  1106. {
  1107. double percent = baseline_percent(he);
  1108. const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
  1109. int ret = 0;
  1110. if (!he->dummy)
  1111. ret = scnprintf(buf, size, fmt, percent);
  1112. return ret;
  1113. }
  1114. static int cycles_printf(struct hist_entry *he, struct hist_entry *pair,
  1115. struct perf_hpp *hpp, int width)
  1116. {
  1117. struct block_hist *bh = container_of(he, struct block_hist, he);
  1118. struct block_hist *bh_pair = container_of(pair, struct block_hist, he);
  1119. struct hist_entry *block_he;
  1120. struct block_info *bi;
  1121. char buf[128];
  1122. char *start_line, *end_line;
  1123. block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx);
  1124. if (!block_he) {
  1125. hpp->skip = true;
  1126. return 0;
  1127. }
  1128. /*
  1129. * Avoid printing the warning "addr2line_init failed for ..."
  1130. */
  1131. symbol_conf.disable_add2line_warn = true;
  1132. bi = block_he->block_info;
  1133. start_line = map__srcline(he->ms.map, bi->sym->start + bi->start,
  1134. he->ms.sym);
  1135. end_line = map__srcline(he->ms.map, bi->sym->start + bi->end,
  1136. he->ms.sym);
  1137. if ((strncmp(start_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0) &&
  1138. (strncmp(end_line, SRCLINE_UNKNOWN, strlen(SRCLINE_UNKNOWN)) != 0)) {
  1139. scnprintf(buf, sizeof(buf), "[%s -> %s] %4ld",
  1140. start_line, end_line, block_he->diff.cycles);
  1141. } else {
  1142. scnprintf(buf, sizeof(buf), "[%7lx -> %7lx] %4ld",
  1143. bi->start, bi->end, block_he->diff.cycles);
  1144. }
  1145. free_srcline(start_line);
  1146. free_srcline(end_line);
  1147. return scnprintf(hpp->buf, hpp->size, "%*s", width, buf);
  1148. }
  1149. static int __hpp__color_compare(struct perf_hpp_fmt *fmt,
  1150. struct perf_hpp *hpp, struct hist_entry *he,
  1151. int comparison_method)
  1152. {
  1153. struct diff_hpp_fmt *dfmt =
  1154. container_of(fmt, struct diff_hpp_fmt, fmt);
  1155. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  1156. double diff;
  1157. s64 wdiff;
  1158. char pfmt[20] = " ";
  1159. if (!pair) {
  1160. if (comparison_method == COMPUTE_CYCLES) {
  1161. struct block_hist *bh;
  1162. bh = container_of(he, struct block_hist, he);
  1163. if (bh->block_idx)
  1164. hpp->skip = true;
  1165. }
  1166. goto no_print;
  1167. }
  1168. switch (comparison_method) {
  1169. case COMPUTE_DELTA:
  1170. if (pair->diff.computed)
  1171. diff = pair->diff.period_ratio_delta;
  1172. else
  1173. diff = compute_delta(he, pair);
  1174. scnprintf(pfmt, 20, "%%%+d.2f%%%%", dfmt->header_width - 1);
  1175. return percent_color_snprintf(hpp->buf, hpp->size,
  1176. pfmt, diff);
  1177. case COMPUTE_RATIO:
  1178. if (he->dummy)
  1179. goto dummy_print;
  1180. if (pair->diff.computed)
  1181. diff = pair->diff.period_ratio;
  1182. else
  1183. diff = compute_ratio(he, pair);
  1184. scnprintf(pfmt, 20, "%%%d.6f", dfmt->header_width);
  1185. return value_color_snprintf(hpp->buf, hpp->size,
  1186. pfmt, diff);
  1187. case COMPUTE_WEIGHTED_DIFF:
  1188. if (he->dummy)
  1189. goto dummy_print;
  1190. if (pair->diff.computed)
  1191. wdiff = pair->diff.wdiff;
  1192. else
  1193. wdiff = compute_wdiff(he, pair);
  1194. scnprintf(pfmt, 20, "%%14ld", dfmt->header_width);
  1195. return color_snprintf(hpp->buf, hpp->size,
  1196. get_percent_color(wdiff),
  1197. pfmt, wdiff);
  1198. case COMPUTE_CYCLES:
  1199. return cycles_printf(he, pair, hpp, dfmt->header_width);
  1200. default:
  1201. BUG_ON(1);
  1202. }
  1203. dummy_print:
  1204. return scnprintf(hpp->buf, hpp->size, "%*s",
  1205. dfmt->header_width, "N/A");
  1206. no_print:
  1207. return scnprintf(hpp->buf, hpp->size, "%*s",
  1208. dfmt->header_width, pfmt);
  1209. }
  1210. static int hpp__color_delta(struct perf_hpp_fmt *fmt,
  1211. struct perf_hpp *hpp, struct hist_entry *he)
  1212. {
  1213. return __hpp__color_compare(fmt, hpp, he, COMPUTE_DELTA);
  1214. }
  1215. static int hpp__color_ratio(struct perf_hpp_fmt *fmt,
  1216. struct perf_hpp *hpp, struct hist_entry *he)
  1217. {
  1218. return __hpp__color_compare(fmt, hpp, he, COMPUTE_RATIO);
  1219. }
  1220. static int hpp__color_wdiff(struct perf_hpp_fmt *fmt,
  1221. struct perf_hpp *hpp, struct hist_entry *he)
  1222. {
  1223. return __hpp__color_compare(fmt, hpp, he, COMPUTE_WEIGHTED_DIFF);
  1224. }
  1225. static int hpp__color_cycles(struct perf_hpp_fmt *fmt,
  1226. struct perf_hpp *hpp, struct hist_entry *he)
  1227. {
  1228. return __hpp__color_compare(fmt, hpp, he, COMPUTE_CYCLES);
  1229. }
  1230. static int all_zero(unsigned long *vals, int len)
  1231. {
  1232. int i;
  1233. for (i = 0; i < len; i++)
  1234. if (vals[i] != 0)
  1235. return 0;
  1236. return 1;
  1237. }
  1238. static int print_cycles_spark(char *bf, int size, unsigned long *svals, u64 n)
  1239. {
  1240. int printed;
  1241. if (n <= 1)
  1242. return 0;
  1243. if (n > NUM_SPARKS)
  1244. n = NUM_SPARKS;
  1245. if (all_zero(svals, n))
  1246. return 0;
  1247. printed = print_spark(bf, size, svals, n);
  1248. printed += scnprintf(bf + printed, size - printed, " ");
  1249. return printed;
  1250. }
  1251. static int hpp__color_cycles_hist(struct perf_hpp_fmt *fmt,
  1252. struct perf_hpp *hpp, struct hist_entry *he)
  1253. {
  1254. struct diff_hpp_fmt *dfmt =
  1255. container_of(fmt, struct diff_hpp_fmt, fmt);
  1256. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  1257. struct block_hist *bh = container_of(he, struct block_hist, he);
  1258. struct block_hist *bh_pair;
  1259. struct hist_entry *block_he;
  1260. char spark[32], buf[128];
  1261. double r;
  1262. int ret, pad;
  1263. if (!pair) {
  1264. if (bh->block_idx)
  1265. hpp->skip = true;
  1266. goto no_print;
  1267. }
  1268. bh_pair = container_of(pair, struct block_hist, he);
  1269. block_he = hists__get_entry(&bh_pair->block_hists, bh->block_idx);
  1270. if (!block_he) {
  1271. hpp->skip = true;
  1272. goto no_print;
  1273. }
  1274. ret = print_cycles_spark(spark, sizeof(spark), block_he->diff.svals,
  1275. block_he->diff.stats.n);
  1276. r = rel_stddev_stats(stddev_stats(&block_he->diff.stats),
  1277. avg_stats(&block_he->diff.stats));
  1278. if (ret) {
  1279. /*
  1280. * Padding spaces if number of sparks less than NUM_SPARKS
  1281. * otherwise the output is not aligned.
  1282. */
  1283. pad = NUM_SPARKS - ((ret - 1) / 3);
  1284. scnprintf(buf, sizeof(buf), "%s%5.1f%% %s", "\u00B1", r, spark);
  1285. ret = scnprintf(hpp->buf, hpp->size, "%*s",
  1286. dfmt->header_width, buf);
  1287. if (pad) {
  1288. ret += scnprintf(hpp->buf + ret, hpp->size - ret,
  1289. "%-*s", pad, " ");
  1290. }
  1291. return ret;
  1292. }
  1293. no_print:
  1294. return scnprintf(hpp->buf, hpp->size, "%*s",
  1295. dfmt->header_width, " ");
  1296. }
  1297. static void
  1298. hpp__entry_unpair(struct hist_entry *he, int idx, char *buf, size_t size)
  1299. {
  1300. switch (idx) {
  1301. case PERF_HPP_DIFF__PERIOD_BASELINE:
  1302. scnprintf(buf, size, "%" PRIu64, he->stat.period);
  1303. break;
  1304. default:
  1305. break;
  1306. }
  1307. }
  1308. static void
  1309. hpp__entry_pair(struct hist_entry *he, struct hist_entry *pair,
  1310. int idx, char *buf, size_t size)
  1311. {
  1312. double diff;
  1313. double ratio;
  1314. s64 wdiff;
  1315. switch (idx) {
  1316. case PERF_HPP_DIFF__DELTA:
  1317. case PERF_HPP_DIFF__DELTA_ABS:
  1318. if (pair->diff.computed)
  1319. diff = pair->diff.period_ratio_delta;
  1320. else
  1321. diff = compute_delta(he, pair);
  1322. scnprintf(buf, size, "%+4.2F%%", diff);
  1323. break;
  1324. case PERF_HPP_DIFF__RATIO:
  1325. /* No point for ratio number if we are dummy.. */
  1326. if (he->dummy) {
  1327. scnprintf(buf, size, "N/A");
  1328. break;
  1329. }
  1330. if (pair->diff.computed)
  1331. ratio = pair->diff.period_ratio;
  1332. else
  1333. ratio = compute_ratio(he, pair);
  1334. if (ratio > 0.0)
  1335. scnprintf(buf, size, "%14.6F", ratio);
  1336. break;
  1337. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  1338. /* No point for wdiff number if we are dummy.. */
  1339. if (he->dummy) {
  1340. scnprintf(buf, size, "N/A");
  1341. break;
  1342. }
  1343. if (pair->diff.computed)
  1344. wdiff = pair->diff.wdiff;
  1345. else
  1346. wdiff = compute_wdiff(he, pair);
  1347. if (wdiff != 0)
  1348. scnprintf(buf, size, "%14ld", wdiff);
  1349. break;
  1350. case PERF_HPP_DIFF__FORMULA:
  1351. formula_fprintf(he, pair, buf, size);
  1352. break;
  1353. case PERF_HPP_DIFF__PERIOD:
  1354. scnprintf(buf, size, "%" PRIu64, pair->stat.period);
  1355. break;
  1356. default:
  1357. BUG_ON(1);
  1358. }
  1359. }
  1360. static void
  1361. __hpp__entry_global(struct hist_entry *he, struct diff_hpp_fmt *dfmt,
  1362. char *buf, size_t size)
  1363. {
  1364. struct hist_entry *pair = get_pair_fmt(he, dfmt);
  1365. int idx = dfmt->idx;
  1366. /* baseline is special */
  1367. if (idx == PERF_HPP_DIFF__BASELINE)
  1368. hpp__entry_baseline(he, buf, size);
  1369. else {
  1370. if (pair)
  1371. hpp__entry_pair(he, pair, idx, buf, size);
  1372. else
  1373. hpp__entry_unpair(he, idx, buf, size);
  1374. }
  1375. }
  1376. static int hpp__entry_global(struct perf_hpp_fmt *_fmt, struct perf_hpp *hpp,
  1377. struct hist_entry *he)
  1378. {
  1379. struct diff_hpp_fmt *dfmt =
  1380. container_of(_fmt, struct diff_hpp_fmt, fmt);
  1381. char buf[MAX_COL_WIDTH] = " ";
  1382. __hpp__entry_global(he, dfmt, buf, MAX_COL_WIDTH);
  1383. if (symbol_conf.field_sep)
  1384. return scnprintf(hpp->buf, hpp->size, "%s", buf);
  1385. else
  1386. return scnprintf(hpp->buf, hpp->size, "%*s",
  1387. dfmt->header_width, buf);
  1388. }
  1389. static int hpp__header(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  1390. struct hists *hists __maybe_unused,
  1391. int line __maybe_unused,
  1392. int *span __maybe_unused)
  1393. {
  1394. struct diff_hpp_fmt *dfmt =
  1395. container_of(fmt, struct diff_hpp_fmt, fmt);
  1396. BUG_ON(!dfmt->header);
  1397. return scnprintf(hpp->buf, hpp->size, dfmt->header);
  1398. }
  1399. static int hpp__width(struct perf_hpp_fmt *fmt,
  1400. struct perf_hpp *hpp __maybe_unused,
  1401. struct hists *hists __maybe_unused)
  1402. {
  1403. struct diff_hpp_fmt *dfmt =
  1404. container_of(fmt, struct diff_hpp_fmt, fmt);
  1405. BUG_ON(dfmt->header_width <= 0);
  1406. return dfmt->header_width;
  1407. }
  1408. static void init_header(struct data__file *d, struct diff_hpp_fmt *dfmt)
  1409. {
  1410. #define MAX_HEADER_NAME 100
  1411. char buf_indent[MAX_HEADER_NAME];
  1412. char buf[MAX_HEADER_NAME];
  1413. const char *header = NULL;
  1414. int width = 0;
  1415. BUG_ON(dfmt->idx >= PERF_HPP_DIFF__MAX_INDEX);
  1416. header = columns[dfmt->idx].name;
  1417. width = columns[dfmt->idx].width;
  1418. /* Only our defined HPP fmts should appear here. */
  1419. BUG_ON(!header);
  1420. if (data__files_cnt > 2)
  1421. scnprintf(buf, MAX_HEADER_NAME, "%s/%d", header, d->idx);
  1422. #define NAME (data__files_cnt > 2 ? buf : header)
  1423. dfmt->header_width = width;
  1424. width = (int) strlen(NAME);
  1425. if (dfmt->header_width < width)
  1426. dfmt->header_width = width;
  1427. scnprintf(buf_indent, MAX_HEADER_NAME, "%*s",
  1428. dfmt->header_width, NAME);
  1429. dfmt->header = strdup(buf_indent);
  1430. #undef MAX_HEADER_NAME
  1431. #undef NAME
  1432. }
  1433. static void data__hpp_register(struct data__file *d, int idx)
  1434. {
  1435. struct diff_hpp_fmt *dfmt = &d->fmt[idx];
  1436. struct perf_hpp_fmt *fmt = &dfmt->fmt;
  1437. dfmt->idx = idx;
  1438. fmt->header = hpp__header;
  1439. fmt->width = hpp__width;
  1440. fmt->entry = hpp__entry_global;
  1441. fmt->cmp = hist_entry__cmp_nop;
  1442. fmt->collapse = hist_entry__cmp_nop;
  1443. /* TODO more colors */
  1444. switch (idx) {
  1445. case PERF_HPP_DIFF__BASELINE:
  1446. fmt->color = hpp__color_baseline;
  1447. fmt->sort = hist_entry__cmp_baseline;
  1448. break;
  1449. case PERF_HPP_DIFF__DELTA:
  1450. fmt->color = hpp__color_delta;
  1451. fmt->sort = hist_entry__cmp_delta;
  1452. break;
  1453. case PERF_HPP_DIFF__RATIO:
  1454. fmt->color = hpp__color_ratio;
  1455. fmt->sort = hist_entry__cmp_ratio;
  1456. break;
  1457. case PERF_HPP_DIFF__WEIGHTED_DIFF:
  1458. fmt->color = hpp__color_wdiff;
  1459. fmt->sort = hist_entry__cmp_wdiff;
  1460. break;
  1461. case PERF_HPP_DIFF__DELTA_ABS:
  1462. fmt->color = hpp__color_delta;
  1463. fmt->sort = hist_entry__cmp_delta_abs;
  1464. break;
  1465. case PERF_HPP_DIFF__CYCLES:
  1466. fmt->color = hpp__color_cycles;
  1467. fmt->sort = hist_entry__cmp_nop;
  1468. break;
  1469. case PERF_HPP_DIFF__CYCLES_HIST:
  1470. fmt->color = hpp__color_cycles_hist;
  1471. fmt->sort = hist_entry__cmp_nop;
  1472. break;
  1473. default:
  1474. fmt->sort = hist_entry__cmp_nop;
  1475. break;
  1476. }
  1477. init_header(d, dfmt);
  1478. perf_hpp__column_register(fmt);
  1479. perf_hpp__register_sort_field(fmt);
  1480. }
  1481. static int ui_init(void)
  1482. {
  1483. struct data__file *d;
  1484. struct perf_hpp_fmt *fmt;
  1485. int i;
  1486. data__for_each_file(i, d) {
  1487. /*
  1488. * Baseline or compute realted columns:
  1489. *
  1490. * PERF_HPP_DIFF__BASELINE
  1491. * PERF_HPP_DIFF__DELTA
  1492. * PERF_HPP_DIFF__RATIO
  1493. * PERF_HPP_DIFF__WEIGHTED_DIFF
  1494. * PERF_HPP_DIFF__CYCLES
  1495. */
  1496. data__hpp_register(d, i ? compute_2_hpp[compute] :
  1497. PERF_HPP_DIFF__BASELINE);
  1498. if (cycles_hist && i)
  1499. data__hpp_register(d, PERF_HPP_DIFF__CYCLES_HIST);
  1500. /*
  1501. * And the rest:
  1502. *
  1503. * PERF_HPP_DIFF__FORMULA
  1504. * PERF_HPP_DIFF__PERIOD
  1505. * PERF_HPP_DIFF__PERIOD_BASELINE
  1506. */
  1507. if (show_formula && i)
  1508. data__hpp_register(d, PERF_HPP_DIFF__FORMULA);
  1509. if (show_period)
  1510. data__hpp_register(d, i ? PERF_HPP_DIFF__PERIOD :
  1511. PERF_HPP_DIFF__PERIOD_BASELINE);
  1512. }
  1513. if (!sort_compute)
  1514. return 0;
  1515. /*
  1516. * Prepend an fmt to sort on columns at 'sort_compute' first.
  1517. * This fmt is added only to the sort list but not to the
  1518. * output fields list.
  1519. *
  1520. * Note that this column (data) can be compared twice - one
  1521. * for this 'sort_compute' fmt and another for the normal
  1522. * diff_hpp_fmt. But it shouldn't a problem as most entries
  1523. * will be sorted out by first try or baseline and comparing
  1524. * is not a costly operation.
  1525. */
  1526. fmt = zalloc(sizeof(*fmt));
  1527. if (fmt == NULL) {
  1528. pr_err("Memory allocation failed\n");
  1529. return -1;
  1530. }
  1531. fmt->cmp = hist_entry__cmp_nop;
  1532. fmt->collapse = hist_entry__cmp_nop;
  1533. switch (compute) {
  1534. case COMPUTE_DELTA:
  1535. fmt->sort = hist_entry__cmp_delta_idx;
  1536. break;
  1537. case COMPUTE_RATIO:
  1538. fmt->sort = hist_entry__cmp_ratio_idx;
  1539. break;
  1540. case COMPUTE_WEIGHTED_DIFF:
  1541. fmt->sort = hist_entry__cmp_wdiff_idx;
  1542. break;
  1543. case COMPUTE_DELTA_ABS:
  1544. fmt->sort = hist_entry__cmp_delta_abs_idx;
  1545. break;
  1546. case COMPUTE_CYCLES:
  1547. /*
  1548. * Should set since 'fmt->sort' is called without
  1549. * checking valid during sorting
  1550. */
  1551. fmt->sort = hist_entry__cmp_nop;
  1552. break;
  1553. default:
  1554. BUG_ON(1);
  1555. }
  1556. perf_hpp__prepend_sort_field(fmt);
  1557. return 0;
  1558. }
  1559. static int data_init(int argc, const char **argv)
  1560. {
  1561. struct data__file *d;
  1562. static const char *defaults[] = {
  1563. "perf.data.old",
  1564. "perf.data",
  1565. };
  1566. bool use_default = true;
  1567. int i;
  1568. data__files_cnt = 2;
  1569. if (argc) {
  1570. if (argc == 1)
  1571. defaults[1] = argv[0];
  1572. else {
  1573. data__files_cnt = argc;
  1574. use_default = false;
  1575. }
  1576. } else if (perf_guest) {
  1577. defaults[0] = "perf.data.host";
  1578. defaults[1] = "perf.data.guest";
  1579. }
  1580. if (sort_compute >= (unsigned int) data__files_cnt) {
  1581. pr_err("Order option out of limit.\n");
  1582. return -EINVAL;
  1583. }
  1584. data__files = zalloc(sizeof(*data__files) * data__files_cnt);
  1585. if (!data__files)
  1586. return -ENOMEM;
  1587. data__for_each_file(i, d) {
  1588. struct perf_data *data = &d->data;
  1589. data->path = use_default ? defaults[i] : argv[i];
  1590. data->mode = PERF_DATA_MODE_READ,
  1591. data->force = force,
  1592. d->idx = i;
  1593. }
  1594. return 0;
  1595. }
  1596. static int diff__config(const char *var, const char *value,
  1597. void *cb __maybe_unused)
  1598. {
  1599. if (!strcmp(var, "diff.order")) {
  1600. int ret;
  1601. if (perf_config_int(&ret, var, value) < 0)
  1602. return -1;
  1603. sort_compute = ret;
  1604. return 0;
  1605. }
  1606. if (!strcmp(var, "diff.compute")) {
  1607. if (!strcmp(value, "delta")) {
  1608. compute = COMPUTE_DELTA;
  1609. } else if (!strcmp(value, "delta-abs")) {
  1610. compute = COMPUTE_DELTA_ABS;
  1611. } else if (!strcmp(value, "ratio")) {
  1612. compute = COMPUTE_RATIO;
  1613. } else if (!strcmp(value, "wdiff")) {
  1614. compute = COMPUTE_WEIGHTED_DIFF;
  1615. } else {
  1616. pr_err("Invalid compute method: %s\n", value);
  1617. return -1;
  1618. }
  1619. }
  1620. return 0;
  1621. }
  1622. int cmd_diff(int argc, const char **argv)
  1623. {
  1624. int ret = hists__init();
  1625. if (ret < 0)
  1626. return ret;
  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. }