builtin-report.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * builtin-report.c
  4. *
  5. * Builtin report command: Analyze the perf.data input file,
  6. * look up and read DSOs and symbol information and display
  7. * a histogram of results, along various sorting keys.
  8. */
  9. #include "builtin.h"
  10. #include "util/config.h"
  11. #include "util/annotate.h"
  12. #include "util/color.h"
  13. #include "util/dso.h"
  14. #include <linux/list.h>
  15. #include <linux/rbtree.h>
  16. #include <linux/err.h>
  17. #include <linux/zalloc.h>
  18. #include "util/map.h"
  19. #include "util/symbol.h"
  20. #include "util/map_symbol.h"
  21. #include "util/mem-events.h"
  22. #include "util/branch.h"
  23. #include "util/callchain.h"
  24. #include "util/values.h"
  25. #include "perf.h"
  26. #include "util/debug.h"
  27. #include "util/evlist.h"
  28. #include "util/evsel.h"
  29. #include "util/evswitch.h"
  30. #include "util/header.h"
  31. #include "util/mem-info.h"
  32. #include "util/session.h"
  33. #include "util/srcline.h"
  34. #include "util/tool.h"
  35. #include <subcmd/parse-options.h>
  36. #include <subcmd/exec-cmd.h>
  37. #include "util/parse-events.h"
  38. #include "util/thread.h"
  39. #include "util/sort.h"
  40. #include "util/hist.h"
  41. #include "util/data.h"
  42. #include "arch/common.h"
  43. #include "util/time-utils.h"
  44. #include "util/auxtrace.h"
  45. #include "util/units.h"
  46. #include "util/util.h" // perf_tip()
  47. #include "ui/ui.h"
  48. #include "ui/progress.h"
  49. #include "util/block-info.h"
  50. #include <dlfcn.h>
  51. #include <errno.h>
  52. #include <inttypes.h>
  53. #include <regex.h>
  54. #include <linux/ctype.h>
  55. #include <signal.h>
  56. #include <linux/bitmap.h>
  57. #include <linux/list_sort.h>
  58. #include <linux/string.h>
  59. #include <linux/stringify.h>
  60. #include <linux/time64.h>
  61. #include <sys/types.h>
  62. #include <sys/stat.h>
  63. #include <unistd.h>
  64. #include <linux/mman.h>
  65. #ifdef HAVE_LIBTRACEEVENT
  66. #include <event-parse.h>
  67. #endif
  68. struct report {
  69. struct perf_tool tool;
  70. struct perf_session *session;
  71. struct evswitch evswitch;
  72. #ifdef HAVE_SLANG_SUPPORT
  73. bool use_tui;
  74. #endif
  75. #ifdef HAVE_GTK2_SUPPORT
  76. bool use_gtk;
  77. #endif
  78. bool use_stdio;
  79. bool show_full_info;
  80. bool show_threads;
  81. bool inverted_callchain;
  82. bool mem_mode;
  83. bool stats_mode;
  84. bool tasks_mode;
  85. bool mmaps_mode;
  86. bool header;
  87. bool header_only;
  88. bool nonany_branch_mode;
  89. bool group_set;
  90. bool stitch_lbr;
  91. bool disable_order;
  92. bool skip_empty;
  93. bool data_type;
  94. int max_stack;
  95. struct perf_read_values show_threads_values;
  96. const char *pretty_printing_style;
  97. const char *cpu_list;
  98. const char *symbol_filter_str;
  99. const char *time_str;
  100. struct perf_time_interval *ptime_range;
  101. int range_size;
  102. int range_num;
  103. float min_percent;
  104. u64 nr_entries;
  105. u64 queue_size;
  106. u64 total_cycles;
  107. u64 total_samples;
  108. u64 singlethreaded_samples;
  109. int socket_filter;
  110. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  111. struct branch_type_stat brtype_stat;
  112. bool symbol_ipc;
  113. bool total_cycles_mode;
  114. struct block_report *block_reports;
  115. int nr_block_reports;
  116. };
  117. static int report__config(const char *var, const char *value, void *cb)
  118. {
  119. struct report *rep = cb;
  120. if (!strcmp(var, "report.group")) {
  121. symbol_conf.event_group = perf_config_bool(var, value);
  122. return 0;
  123. }
  124. if (!strcmp(var, "report.percent-limit")) {
  125. double pcnt = strtof(value, NULL);
  126. rep->min_percent = pcnt;
  127. callchain_param.min_percent = pcnt;
  128. return 0;
  129. }
  130. if (!strcmp(var, "report.children")) {
  131. symbol_conf.cumulate_callchain = perf_config_bool(var, value);
  132. return 0;
  133. }
  134. if (!strcmp(var, "report.queue-size"))
  135. return perf_config_u64(&rep->queue_size, var, value);
  136. if (!strcmp(var, "report.sort_order")) {
  137. default_sort_order = strdup(value);
  138. if (!default_sort_order) {
  139. pr_err("Not enough memory for report.sort_order\n");
  140. return -1;
  141. }
  142. return 0;
  143. }
  144. if (!strcmp(var, "report.skip-empty")) {
  145. rep->skip_empty = perf_config_bool(var, value);
  146. return 0;
  147. }
  148. pr_debug("%s variable unknown, ignoring...", var);
  149. return 0;
  150. }
  151. static int hist_iter__report_callback(struct hist_entry_iter *iter,
  152. struct addr_location *al, bool single,
  153. void *arg)
  154. {
  155. int err = 0;
  156. struct report *rep = arg;
  157. struct hist_entry *he = iter->he;
  158. struct evsel *evsel = iter->evsel;
  159. struct perf_sample *sample = iter->sample;
  160. struct mem_info *mi;
  161. struct branch_info *bi;
  162. if (!ui__has_annotation() && !rep->symbol_ipc)
  163. return 0;
  164. if (sort__mode == SORT_MODE__BRANCH) {
  165. bi = he->branch_info;
  166. err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
  167. if (err)
  168. goto out;
  169. err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
  170. } else if (rep->mem_mode) {
  171. mi = he->mem_info;
  172. err = addr_map_symbol__inc_samples(mem_info__daddr(mi), sample, evsel);
  173. if (err)
  174. goto out;
  175. err = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
  176. } else if (symbol_conf.cumulate_callchain) {
  177. if (single)
  178. err = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
  179. } else {
  180. err = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
  181. }
  182. out:
  183. return err;
  184. }
  185. static int hist_iter__branch_callback(struct hist_entry_iter *iter,
  186. struct addr_location *al __maybe_unused,
  187. bool single __maybe_unused,
  188. void *arg)
  189. {
  190. struct hist_entry *he = iter->he;
  191. struct report *rep = arg;
  192. struct branch_info *bi = he->branch_info;
  193. struct perf_sample *sample = iter->sample;
  194. struct evsel *evsel = iter->evsel;
  195. int err;
  196. branch_type_count(&rep->brtype_stat, &bi->flags,
  197. bi->from.addr, bi->to.addr);
  198. if (!ui__has_annotation() && !rep->symbol_ipc)
  199. return 0;
  200. err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
  201. if (err)
  202. goto out;
  203. err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
  204. out:
  205. return err;
  206. }
  207. static void setup_forced_leader(struct report *report,
  208. struct evlist *evlist)
  209. {
  210. if (report->group_set)
  211. evlist__force_leader(evlist);
  212. }
  213. static int process_feature_event(const struct perf_tool *tool,
  214. struct perf_session *session,
  215. union perf_event *event)
  216. {
  217. struct report *rep = container_of(tool, struct report, tool);
  218. if (event->feat.feat_id < HEADER_LAST_FEATURE)
  219. return perf_event__process_feature(session, event);
  220. if (event->feat.feat_id != HEADER_LAST_FEATURE) {
  221. pr_err("failed: wrong feature ID: %" PRI_lu64 "\n",
  222. event->feat.feat_id);
  223. return -1;
  224. } else if (rep->header_only) {
  225. session_done = 1;
  226. }
  227. /*
  228. * (feat_id = HEADER_LAST_FEATURE) is the end marker which
  229. * means all features are received, now we can force the
  230. * group if needed.
  231. */
  232. setup_forced_leader(rep, session->evlist);
  233. return 0;
  234. }
  235. static int process_sample_event(const struct perf_tool *tool,
  236. union perf_event *event,
  237. struct perf_sample *sample,
  238. struct evsel *evsel,
  239. struct machine *machine)
  240. {
  241. struct report *rep = container_of(tool, struct report, tool);
  242. struct addr_location al;
  243. struct hist_entry_iter iter = {
  244. .evsel = evsel,
  245. .sample = sample,
  246. .hide_unresolved = symbol_conf.hide_unresolved,
  247. .add_entry_cb = hist_iter__report_callback,
  248. };
  249. int ret = 0;
  250. if (perf_time__ranges_skip_sample(rep->ptime_range, rep->range_num,
  251. sample->time)) {
  252. return 0;
  253. }
  254. if (evswitch__discard(&rep->evswitch, evsel))
  255. return 0;
  256. addr_location__init(&al);
  257. if (machine__resolve(machine, &al, sample) < 0) {
  258. pr_debug("problem processing %d event, skipping it.\n",
  259. event->header.type);
  260. ret = -1;
  261. goto out_put;
  262. }
  263. if (rep->stitch_lbr)
  264. thread__set_lbr_stitch_enable(al.thread, true);
  265. if (symbol_conf.hide_unresolved && al.sym == NULL)
  266. goto out_put;
  267. if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
  268. goto out_put;
  269. if (sort__mode == SORT_MODE__BRANCH) {
  270. /*
  271. * A non-synthesized event might not have a branch stack if
  272. * branch stacks have been synthesized (using itrace options).
  273. */
  274. if (!sample->branch_stack)
  275. goto out_put;
  276. iter.add_entry_cb = hist_iter__branch_callback;
  277. iter.ops = &hist_iter_branch;
  278. } else if (rep->mem_mode) {
  279. iter.ops = &hist_iter_mem;
  280. } else if (symbol_conf.cumulate_callchain) {
  281. iter.ops = &hist_iter_cumulative;
  282. } else {
  283. iter.ops = &hist_iter_normal;
  284. }
  285. if (al.map != NULL)
  286. dso__set_hit(map__dso(al.map));
  287. if (ui__has_annotation() || rep->symbol_ipc || rep->total_cycles_mode) {
  288. hist__account_cycles(sample->branch_stack, &al, sample,
  289. rep->nonany_branch_mode,
  290. &rep->total_cycles, evsel);
  291. }
  292. rep->total_samples++;
  293. if (al.parallelism == 1)
  294. rep->singlethreaded_samples++;
  295. ret = hist_entry_iter__add(&iter, &al, rep->max_stack, rep);
  296. if (ret < 0)
  297. pr_debug("problem adding hist entry, skipping event\n");
  298. out_put:
  299. addr_location__exit(&al);
  300. return ret;
  301. }
  302. static int process_read_event(const struct perf_tool *tool,
  303. union perf_event *event,
  304. struct perf_sample *sample __maybe_unused,
  305. struct evsel *evsel,
  306. struct machine *machine __maybe_unused)
  307. {
  308. struct report *rep = container_of(tool, struct report, tool);
  309. if (rep->show_threads) {
  310. int err = perf_read_values_add_value(&rep->show_threads_values,
  311. event->read.pid, event->read.tid,
  312. evsel,
  313. event->read.value);
  314. if (err)
  315. return err;
  316. }
  317. return 0;
  318. }
  319. /* For pipe mode, sample_type is not currently set */
  320. static int report__setup_sample_type(struct report *rep)
  321. {
  322. struct perf_session *session = rep->session;
  323. u64 sample_type = evlist__combined_sample_type(session->evlist);
  324. bool is_pipe = perf_data__is_pipe(session->data);
  325. struct evsel *evsel;
  326. if (session->itrace_synth_opts->callchain ||
  327. session->itrace_synth_opts->add_callchain ||
  328. (!is_pipe &&
  329. perf_header__has_feat(&session->header, HEADER_AUXTRACE) &&
  330. !session->itrace_synth_opts->set))
  331. sample_type |= PERF_SAMPLE_CALLCHAIN;
  332. if (session->itrace_synth_opts->last_branch ||
  333. session->itrace_synth_opts->add_last_branch)
  334. sample_type |= PERF_SAMPLE_BRANCH_STACK;
  335. if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  336. if (perf_hpp_list.parent) {
  337. ui__error("Selected --sort parent, but no "
  338. "callchain data. Did you call "
  339. "'perf record' without -g?\n");
  340. return -EINVAL;
  341. }
  342. if (symbol_conf.use_callchain &&
  343. !symbol_conf.show_branchflag_count) {
  344. ui__error("Selected -g or --branch-history.\n"
  345. "But no callchain or branch data.\n"
  346. "Did you call 'perf record' without -g or -b?\n");
  347. return -1;
  348. }
  349. } else if (!callchain_param.enabled &&
  350. callchain_param.mode != CHAIN_NONE &&
  351. !symbol_conf.use_callchain) {
  352. symbol_conf.use_callchain = true;
  353. if (callchain_register_param(&callchain_param) < 0) {
  354. ui__error("Can't register callchain params.\n");
  355. return -EINVAL;
  356. }
  357. }
  358. if (symbol_conf.cumulate_callchain) {
  359. /* Silently ignore if callchain is missing */
  360. if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  361. symbol_conf.cumulate_callchain = false;
  362. perf_hpp__cancel_cumulate(session->evlist);
  363. }
  364. }
  365. if (sort__mode == SORT_MODE__BRANCH) {
  366. if (!is_pipe &&
  367. !(sample_type & PERF_SAMPLE_BRANCH_STACK)) {
  368. ui__error("Selected -b but no branch data. "
  369. "Did you call perf record without -b?\n");
  370. return -1;
  371. }
  372. }
  373. if (sort__mode == SORT_MODE__MEMORY) {
  374. /*
  375. * FIXUP: prior to kernel 5.18, Arm SPE missed to set
  376. * PERF_SAMPLE_DATA_SRC bit in sample type. For backward
  377. * compatibility, set the bit if it's an old perf data file.
  378. */
  379. evlist__for_each_entry(session->evlist, evsel) {
  380. if (strstr(evsel__name(evsel), "arm_spe") &&
  381. !(sample_type & PERF_SAMPLE_DATA_SRC)) {
  382. evsel->core.attr.sample_type |= PERF_SAMPLE_DATA_SRC;
  383. sample_type |= PERF_SAMPLE_DATA_SRC;
  384. }
  385. }
  386. if (!is_pipe && !(sample_type & PERF_SAMPLE_DATA_SRC)) {
  387. ui__error("Selected --mem-mode but no mem data. "
  388. "Did you call perf record without -d?\n");
  389. return -1;
  390. }
  391. }
  392. callchain_param_setup(sample_type, perf_session__e_machine(session, /*e_flags=*/NULL));
  393. if (rep->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) {
  394. ui__warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
  395. "Please apply --call-graph lbr when recording.\n");
  396. rep->stitch_lbr = false;
  397. }
  398. /* ??? handle more cases than just ANY? */
  399. if (!(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY))
  400. rep->nonany_branch_mode = true;
  401. #if !defined(HAVE_LIBUNWIND_SUPPORT) && !defined(HAVE_LIBDW_SUPPORT)
  402. if (dwarf_callchain_users) {
  403. ui__warning("Please install libunwind or libdw "
  404. "development packages during the perf build.\n");
  405. }
  406. #endif
  407. return 0;
  408. }
  409. static void sig_handler(int sig __maybe_unused)
  410. {
  411. session_done = 1;
  412. }
  413. static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep,
  414. const char *evname, FILE *fp)
  415. {
  416. size_t ret;
  417. char unit;
  418. unsigned long nr_samples = hists->stats.nr_samples;
  419. u64 nr_events = hists->stats.total_period;
  420. struct evsel *evsel = hists_to_evsel(hists);
  421. char buf[512];
  422. size_t size = sizeof(buf);
  423. int socked_id = hists->socket_filter;
  424. if (quiet)
  425. return 0;
  426. if (symbol_conf.filter_relative) {
  427. nr_samples = hists->stats.nr_non_filtered_samples;
  428. nr_events = hists->stats.total_non_filtered_period;
  429. }
  430. if (evsel__is_group_event(evsel)) {
  431. struct evsel *pos;
  432. evsel__group_desc(evsel, buf, size);
  433. evname = buf;
  434. for_each_group_member(pos, evsel) {
  435. const struct hists *pos_hists = evsel__hists(pos);
  436. if (symbol_conf.filter_relative) {
  437. nr_samples += pos_hists->stats.nr_non_filtered_samples;
  438. nr_events += pos_hists->stats.total_non_filtered_period;
  439. } else {
  440. nr_samples += pos_hists->stats.nr_samples;
  441. nr_events += pos_hists->stats.total_period;
  442. }
  443. }
  444. }
  445. nr_samples = convert_unit(nr_samples, &unit);
  446. ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
  447. if (evname != NULL) {
  448. ret += fprintf(fp, " of event%s '%s'",
  449. evsel->core.nr_members > 1 ? "s" : "", evname);
  450. }
  451. if (rep->time_str)
  452. ret += fprintf(fp, " (time slices: %s)", rep->time_str);
  453. if (symbol_conf.show_ref_callgraph && evname && strstr(evname, "call-graph=no")) {
  454. ret += fprintf(fp, ", show reference callgraph");
  455. }
  456. if (rep->mem_mode) {
  457. ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
  458. if (sort_order || !field_order) {
  459. ret += fprintf(fp, "\n# Sort order : %s",
  460. sort_order ? : default_mem_sort_order);
  461. }
  462. } else
  463. ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
  464. if (socked_id > -1)
  465. ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
  466. return ret + fprintf(fp, "\n#\n");
  467. }
  468. static int evlist__tui_block_hists_browse(struct evlist *evlist, struct report *rep)
  469. {
  470. struct evsel *pos;
  471. int i = 0, ret;
  472. evlist__for_each_entry(evlist, pos) {
  473. ret = report__browse_block_hists(&rep->block_reports[i++].hist,
  474. rep->min_percent, pos,
  475. perf_session__env(rep->session));
  476. if (ret != 0)
  477. return ret;
  478. }
  479. return 0;
  480. }
  481. static int evlist__tty_browse_hists(struct evlist *evlist, struct report *rep, const char *help)
  482. {
  483. struct evsel *pos;
  484. int i = 0;
  485. if (!quiet) {
  486. fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n",
  487. evlist->stats.total_lost_samples);
  488. }
  489. evlist__for_each_entry(evlist, pos) {
  490. struct hists *hists = evsel__hists(pos);
  491. const char *evname = evsel__name(pos);
  492. i++;
  493. if (symbol_conf.event_group && !evsel__is_group_leader(pos))
  494. continue;
  495. if (rep->skip_empty && !hists->stats.nr_samples)
  496. continue;
  497. hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
  498. if (rep->total_cycles_mode) {
  499. char *buf;
  500. if (!annotation_br_cntr_abbr_list(&buf, pos, true)) {
  501. fprintf(stdout, "%s", buf);
  502. fprintf(stdout, "#\n");
  503. free(buf);
  504. }
  505. report__browse_block_hists(&rep->block_reports[i - 1].hist,
  506. rep->min_percent, pos, NULL);
  507. continue;
  508. }
  509. hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout,
  510. !(symbol_conf.use_callchain ||
  511. symbol_conf.show_branchflag_count));
  512. fprintf(stdout, "\n\n");
  513. }
  514. if (!quiet)
  515. fprintf(stdout, "#\n# (%s)\n#\n", help);
  516. if (rep->show_threads) {
  517. bool style = !strcmp(rep->pretty_printing_style, "raw");
  518. perf_read_values_display(stdout, &rep->show_threads_values,
  519. style);
  520. perf_read_values_destroy(&rep->show_threads_values);
  521. }
  522. if (sort__mode == SORT_MODE__BRANCH)
  523. branch_type_stat_display(stdout, &rep->brtype_stat);
  524. return 0;
  525. }
  526. static void report__warn_kptr_restrict(const struct report *rep)
  527. {
  528. struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
  529. struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
  530. if (evlist__exclude_kernel(rep->session->evlist))
  531. return;
  532. if (kernel_map == NULL ||
  533. (dso__hit(map__dso(kernel_map)) &&
  534. (kernel_kmap->ref_reloc_sym == NULL ||
  535. kernel_kmap->ref_reloc_sym->addr == 0))) {
  536. const char *desc =
  537. "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
  538. "can't be resolved.";
  539. if (kernel_map && map__has_symbols(kernel_map)) {
  540. desc = "If some relocation was applied (e.g. "
  541. "kexec) symbols may be misresolved.";
  542. }
  543. ui__warning(
  544. "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
  545. "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
  546. "Samples in kernel modules can't be resolved as well.\n\n",
  547. desc);
  548. }
  549. }
  550. static int report__gtk_browse_hists(struct report *rep, const char *help)
  551. {
  552. int (*hist_browser)(struct evlist *evlist, const char *help,
  553. struct hist_browser_timer *timer, float min_pcnt);
  554. hist_browser = dlsym(perf_gtk_handle, "evlist__gtk_browse_hists");
  555. if (hist_browser == NULL) {
  556. ui__error("GTK browser not found!\n");
  557. return -1;
  558. }
  559. return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
  560. }
  561. static int report__browse_hists(struct report *rep)
  562. {
  563. int ret;
  564. struct perf_session *session = rep->session;
  565. struct evlist *evlist = session->evlist;
  566. char *help = NULL, *path = NULL;
  567. path = system_path(TIPDIR);
  568. if (perf_tip(&help, path) || help == NULL) {
  569. /* fallback for people who don't install perf ;-) */
  570. free(path);
  571. path = system_path(DOCDIR);
  572. if (perf_tip(&help, path) || help == NULL)
  573. help = strdup("Cannot load tips.txt file, please install perf!");
  574. }
  575. free(path);
  576. switch (use_browser) {
  577. case 1:
  578. if (rep->total_cycles_mode) {
  579. ret = evlist__tui_block_hists_browse(evlist, rep);
  580. break;
  581. }
  582. ret = evlist__tui_browse_hists(evlist, help, NULL, rep->min_percent,
  583. perf_session__env(session), true);
  584. /*
  585. * Usually "ret" is the last pressed key, and we only
  586. * care if the key notifies us to switch data file.
  587. */
  588. if (ret != K_SWITCH_INPUT_DATA && ret != K_RELOAD)
  589. ret = 0;
  590. break;
  591. case 2:
  592. ret = report__gtk_browse_hists(rep, help);
  593. break;
  594. default:
  595. ret = evlist__tty_browse_hists(evlist, rep, help);
  596. break;
  597. }
  598. free(help);
  599. return ret;
  600. }
  601. static int report__collapse_hists(struct report *rep)
  602. {
  603. struct perf_session *session = rep->session;
  604. struct evlist *evlist = session->evlist;
  605. struct ui_progress prog;
  606. struct evsel *pos;
  607. int ret = 0;
  608. /*
  609. * The pipe data needs to setup hierarchy hpp formats now, because it
  610. * cannot know about evsels in the data before reading the data. The
  611. * normal file data saves the event (attribute) info in the header
  612. * section, but pipe does not have the luxury.
  613. */
  614. if (perf_data__is_pipe(session->data)) {
  615. if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0) {
  616. ui__error("Failed to setup hierarchy output formats\n");
  617. return -1;
  618. }
  619. }
  620. ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
  621. evlist__for_each_entry(rep->session->evlist, pos) {
  622. struct hists *hists = evsel__hists(pos);
  623. if (pos->core.idx == 0)
  624. hists->symbol_filter_str = rep->symbol_filter_str;
  625. hists->socket_filter = rep->socket_filter;
  626. ret = hists__collapse_resort(hists, &prog);
  627. if (ret < 0)
  628. break;
  629. /* Non-group events are considered as leader */
  630. if (symbol_conf.event_group && !evsel__is_group_leader(pos)) {
  631. struct hists *leader_hists = evsel__hists(evsel__leader(pos));
  632. hists__match(leader_hists, hists);
  633. hists__link(leader_hists, hists);
  634. }
  635. }
  636. ui_progress__finish();
  637. return ret;
  638. }
  639. static int hists__resort_cb(struct hist_entry *he, void *arg)
  640. {
  641. struct report *rep = arg;
  642. struct symbol *sym = he->ms.sym;
  643. if (rep->symbol_ipc && sym && !sym->annotate2) {
  644. struct evsel *evsel = hists_to_evsel(he->hists);
  645. symbol__annotate2(&he->ms, evsel, NULL);
  646. }
  647. return 0;
  648. }
  649. static void report__output_resort(struct report *rep)
  650. {
  651. struct ui_progress prog;
  652. struct evsel *pos;
  653. ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
  654. evlist__for_each_entry(rep->session->evlist, pos) {
  655. evsel__output_resort_cb(pos, &prog, hists__resort_cb, rep);
  656. }
  657. ui_progress__finish();
  658. }
  659. static int count_sample_event(const struct perf_tool *tool __maybe_unused,
  660. union perf_event *event __maybe_unused,
  661. struct perf_sample *sample __maybe_unused,
  662. struct evsel *evsel,
  663. struct machine *machine __maybe_unused)
  664. {
  665. struct hists *hists = evsel__hists(evsel);
  666. hists__inc_nr_events(hists);
  667. return 0;
  668. }
  669. static int count_lost_samples_event(const struct perf_tool *tool,
  670. union perf_event *event,
  671. struct perf_sample *sample,
  672. struct machine *machine __maybe_unused)
  673. {
  674. struct report *rep = container_of(tool, struct report, tool);
  675. struct evsel *evsel;
  676. evsel = evlist__id2evsel(rep->session->evlist, sample->id);
  677. if (evsel) {
  678. struct hists *hists = evsel__hists(evsel);
  679. u32 count = event->lost_samples.lost;
  680. if (event->header.misc & PERF_RECORD_MISC_LOST_SAMPLES_BPF)
  681. hists__inc_nr_dropped_samples(hists, count);
  682. else
  683. hists__inc_nr_lost_samples(hists, count);
  684. }
  685. return 0;
  686. }
  687. static int process_attr(const struct perf_tool *tool __maybe_unused,
  688. union perf_event *event,
  689. struct evlist **pevlist);
  690. static void stats_setup(struct report *rep)
  691. {
  692. perf_tool__init(&rep->tool, /*ordered_events=*/false);
  693. rep->tool.attr = process_attr;
  694. rep->tool.sample = count_sample_event;
  695. rep->tool.lost_samples = count_lost_samples_event;
  696. rep->tool.event_update = perf_event__process_event_update;
  697. rep->tool.no_warn = true;
  698. }
  699. static int stats_print(struct report *rep)
  700. {
  701. struct perf_session *session = rep->session;
  702. perf_session__fprintf_nr_events(session, stdout);
  703. evlist__fprintf_nr_events(session->evlist, stdout);
  704. return 0;
  705. }
  706. static void tasks_setup(struct report *rep)
  707. {
  708. perf_tool__init(&rep->tool, /*ordered_events=*/true);
  709. if (rep->mmaps_mode) {
  710. rep->tool.mmap = perf_event__process_mmap;
  711. rep->tool.mmap2 = perf_event__process_mmap2;
  712. }
  713. rep->tool.attr = process_attr;
  714. rep->tool.comm = perf_event__process_comm;
  715. rep->tool.exit = perf_event__process_exit;
  716. rep->tool.fork = perf_event__process_fork;
  717. rep->tool.no_warn = true;
  718. }
  719. struct maps__fprintf_task_args {
  720. int indent;
  721. FILE *fp;
  722. size_t printed;
  723. };
  724. static int maps__fprintf_task_cb(struct map *map, void *data)
  725. {
  726. struct maps__fprintf_task_args *args = data;
  727. const struct dso *dso = map__dso(map);
  728. u32 prot = map__prot(map);
  729. const struct dso_id *dso_id = dso__id_const(dso);
  730. int ret;
  731. char buf[SBUILD_ID_SIZE];
  732. if (dso_id->mmap2_valid)
  733. snprintf(buf, sizeof(buf), "%" PRIu64, dso_id->ino);
  734. else
  735. build_id__snprintf(&dso_id->build_id, buf, sizeof(buf));
  736. ret = fprintf(args->fp,
  737. "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %s %s\n",
  738. args->indent, "", map__start(map), map__end(map),
  739. prot & PROT_READ ? 'r' : '-',
  740. prot & PROT_WRITE ? 'w' : '-',
  741. prot & PROT_EXEC ? 'x' : '-',
  742. map__flags(map) ? 's' : 'p',
  743. map__pgoff(map),
  744. buf, dso__name(dso));
  745. if (ret < 0)
  746. return ret;
  747. args->printed += ret;
  748. return 0;
  749. }
  750. static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
  751. {
  752. struct maps__fprintf_task_args args = {
  753. .indent = indent,
  754. .fp = fp,
  755. .printed = 0,
  756. };
  757. maps__for_each_map(maps, maps__fprintf_task_cb, &args);
  758. return args.printed;
  759. }
  760. static int thread_level(struct machine *machine, const struct thread *thread)
  761. {
  762. struct thread *parent_thread;
  763. int res;
  764. if (thread__tid(thread) <= 0)
  765. return 0;
  766. if (thread__ppid(thread) <= 0)
  767. return 1;
  768. parent_thread = machine__find_thread(machine, -1, thread__ppid(thread));
  769. if (!parent_thread) {
  770. pr_err("Missing parent thread of %d\n", thread__tid(thread));
  771. return 0;
  772. }
  773. res = 1 + thread_level(machine, parent_thread);
  774. thread__put(parent_thread);
  775. return res;
  776. }
  777. static void task__print_level(struct machine *machine, struct thread *thread, FILE *fp)
  778. {
  779. int level = thread_level(machine, thread);
  780. int comm_indent = fprintf(fp, " %8d %8d %8d |%*s",
  781. thread__pid(thread), thread__tid(thread),
  782. thread__ppid(thread), level, "");
  783. fprintf(fp, "%s\n", thread__comm_str(thread));
  784. maps__fprintf_task(thread__maps(thread), comm_indent, fp);
  785. }
  786. /*
  787. * Sort two thread list nodes such that they form a tree. The first node is the
  788. * root of the tree, its children are ordered numerically after it. If a child
  789. * has children itself then they appear immediately after their parent. For
  790. * example, the 4 threads in the order they'd appear in the list:
  791. * - init with a TID 1 and a parent of 0
  792. * - systemd with a TID 3000 and a parent of init/1
  793. * - systemd child thread with TID 4000, the parent is 3000
  794. * - NetworkManager is a child of init with a TID of 3500.
  795. */
  796. static int task_list_cmp(void *priv, const struct list_head *la, const struct list_head *lb)
  797. {
  798. struct machine *machine = priv;
  799. struct thread_list *task_a = list_entry(la, struct thread_list, list);
  800. struct thread_list *task_b = list_entry(lb, struct thread_list, list);
  801. struct thread *a = task_a->thread;
  802. struct thread *b = task_b->thread;
  803. int level_a, level_b, res;
  804. /* Same thread? */
  805. if (thread__tid(a) == thread__tid(b))
  806. return 0;
  807. /* Compare a and b to root. */
  808. if (thread__tid(a) == 0)
  809. return -1;
  810. if (thread__tid(b) == 0)
  811. return 1;
  812. /* If parents match sort by tid. */
  813. if (thread__ppid(a) == thread__ppid(b))
  814. return thread__tid(a) < thread__tid(b) ? -1 : 1;
  815. /*
  816. * Find a and b such that if they are a child of each other a and b's
  817. * tid's match, otherwise a and b have a common parent and distinct
  818. * tid's to sort by. First make the depths of the threads match.
  819. */
  820. level_a = thread_level(machine, a);
  821. level_b = thread_level(machine, b);
  822. a = thread__get(a);
  823. b = thread__get(b);
  824. for (int i = level_a; i > level_b; i--) {
  825. struct thread *parent = machine__find_thread(machine, -1, thread__ppid(a));
  826. thread__put(a);
  827. if (!parent) {
  828. pr_err("Missing parent thread of %d\n", thread__tid(a));
  829. thread__put(b);
  830. return -1;
  831. }
  832. a = parent;
  833. }
  834. for (int i = level_b; i > level_a; i--) {
  835. struct thread *parent = machine__find_thread(machine, -1, thread__ppid(b));
  836. thread__put(b);
  837. if (!parent) {
  838. pr_err("Missing parent thread of %d\n", thread__tid(b));
  839. thread__put(a);
  840. return 1;
  841. }
  842. b = parent;
  843. }
  844. /* Search up to a common parent. */
  845. while (thread__ppid(a) != thread__ppid(b)) {
  846. struct thread *parent;
  847. parent = machine__find_thread(machine, -1, thread__ppid(a));
  848. thread__put(a);
  849. if (!parent)
  850. pr_err("Missing parent thread of %d\n", thread__tid(a));
  851. a = parent;
  852. parent = machine__find_thread(machine, -1, thread__ppid(b));
  853. thread__put(b);
  854. if (!parent)
  855. pr_err("Missing parent thread of %d\n", thread__tid(b));
  856. b = parent;
  857. if (!a || !b) {
  858. /* Handle missing parent (unexpected) with some sanity. */
  859. thread__put(a);
  860. thread__put(b);
  861. return !a && !b ? 0 : (!a ? -1 : 1);
  862. }
  863. }
  864. if (thread__tid(a) == thread__tid(b)) {
  865. /* a is a child of b or vice-versa, deeper levels appear later. */
  866. res = level_a < level_b ? -1 : (level_a > level_b ? 1 : 0);
  867. } else {
  868. /* Sort by tid now the parent is the same. */
  869. res = thread__tid(a) < thread__tid(b) ? -1 : 1;
  870. }
  871. thread__put(a);
  872. thread__put(b);
  873. return res;
  874. }
  875. static int tasks_print(struct report *rep, FILE *fp)
  876. {
  877. struct machine *machine = &rep->session->machines.host;
  878. LIST_HEAD(tasks);
  879. int ret;
  880. ret = machine__thread_list(machine, &tasks);
  881. if (!ret) {
  882. struct thread_list *task;
  883. list_sort(machine, &tasks, task_list_cmp);
  884. fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm");
  885. list_for_each_entry(task, &tasks, list)
  886. task__print_level(machine, task->thread, fp);
  887. }
  888. thread_list__delete(&tasks);
  889. return ret;
  890. }
  891. static int __cmd_report(struct report *rep)
  892. {
  893. int ret;
  894. struct perf_session *session = rep->session;
  895. struct evsel *pos;
  896. struct perf_data *data = session->data;
  897. signal(SIGINT, sig_handler);
  898. if (rep->cpu_list) {
  899. ret = perf_session__cpu_bitmap(session, rep->cpu_list,
  900. rep->cpu_bitmap);
  901. if (ret) {
  902. ui__error("failed to set cpu bitmap\n");
  903. return ret;
  904. }
  905. session->itrace_synth_opts->cpu_bitmap = rep->cpu_bitmap;
  906. }
  907. if (rep->show_threads) {
  908. ret = perf_read_values_init(&rep->show_threads_values);
  909. if (ret)
  910. return ret;
  911. }
  912. ret = report__setup_sample_type(rep);
  913. if (ret) {
  914. /* report__setup_sample_type() already showed error message */
  915. return ret;
  916. }
  917. if (rep->stats_mode)
  918. stats_setup(rep);
  919. if (rep->tasks_mode)
  920. tasks_setup(rep);
  921. ret = perf_session__process_events(session);
  922. if (ret) {
  923. ui__error("failed to process sample\n");
  924. return ret;
  925. }
  926. /* Don't show Latency column for non-parallel profiles by default. */
  927. if (!symbol_conf.prefer_latency && rep->total_samples &&
  928. rep->singlethreaded_samples * 100 / rep->total_samples >= 99)
  929. perf_hpp__cancel_latency(session->evlist);
  930. evlist__check_mem_load_aux(session->evlist);
  931. if (rep->stats_mode)
  932. return stats_print(rep);
  933. if (rep->tasks_mode)
  934. return tasks_print(rep, stdout);
  935. report__warn_kptr_restrict(rep);
  936. evlist__for_each_entry(session->evlist, pos)
  937. rep->nr_entries += evsel__hists(pos)->nr_entries;
  938. if (use_browser == 0) {
  939. if (verbose > 3)
  940. perf_session__fprintf(session, stdout);
  941. if (verbose > 2)
  942. perf_session__fprintf_dsos(session, stdout);
  943. if (dump_trace) {
  944. stats_print(rep);
  945. return 0;
  946. }
  947. }
  948. ret = report__collapse_hists(rep);
  949. if (ret) {
  950. ui__error("failed to process hist entry\n");
  951. return ret;
  952. }
  953. if (session_done())
  954. return 0;
  955. /*
  956. * recalculate number of entries after collapsing since it
  957. * might be changed during the collapse phase.
  958. */
  959. rep->nr_entries = 0;
  960. evlist__for_each_entry(session->evlist, pos)
  961. rep->nr_entries += evsel__hists(pos)->nr_entries;
  962. if (rep->nr_entries == 0) {
  963. ui__error("The %s data has no samples!\n", data->path);
  964. return 0;
  965. }
  966. report__output_resort(rep);
  967. if (rep->total_cycles_mode) {
  968. int nr_hpps = 4;
  969. int block_hpps[PERF_HPP_REPORT__BLOCK_MAX_INDEX] = {
  970. PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT,
  971. PERF_HPP_REPORT__BLOCK_LBR_CYCLES,
  972. PERF_HPP_REPORT__BLOCK_CYCLES_PCT,
  973. PERF_HPP_REPORT__BLOCK_AVG_CYCLES,
  974. };
  975. if (session->evlist->nr_br_cntr > 0)
  976. block_hpps[nr_hpps++] = PERF_HPP_REPORT__BLOCK_BRANCH_COUNTER;
  977. block_hpps[nr_hpps++] = PERF_HPP_REPORT__BLOCK_RANGE;
  978. block_hpps[nr_hpps++] = PERF_HPP_REPORT__BLOCK_DSO;
  979. rep->block_reports = block_info__create_report(session->evlist,
  980. rep->total_cycles,
  981. block_hpps, nr_hpps,
  982. &rep->nr_block_reports);
  983. if (!rep->block_reports)
  984. return -1;
  985. }
  986. return report__browse_hists(rep);
  987. }
  988. static int
  989. report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  990. {
  991. struct callchain_param *callchain = opt->value;
  992. callchain->enabled = !unset;
  993. /*
  994. * --no-call-graph
  995. */
  996. if (unset) {
  997. symbol_conf.use_callchain = false;
  998. callchain->mode = CHAIN_NONE;
  999. return 0;
  1000. }
  1001. return parse_callchain_report_opt(arg);
  1002. }
  1003. static int
  1004. parse_time_quantum(const struct option *opt, const char *arg,
  1005. int unset __maybe_unused)
  1006. {
  1007. unsigned long *time_q = opt->value;
  1008. char *end;
  1009. *time_q = strtoul(arg, &end, 0);
  1010. if (end == arg)
  1011. goto parse_err;
  1012. if (*time_q == 0) {
  1013. pr_err("time quantum cannot be 0");
  1014. return -1;
  1015. }
  1016. end = skip_spaces(end);
  1017. if (*end == 0)
  1018. return 0;
  1019. if (!strcmp(end, "s")) {
  1020. *time_q *= NSEC_PER_SEC;
  1021. return 0;
  1022. }
  1023. if (!strcmp(end, "ms")) {
  1024. *time_q *= NSEC_PER_MSEC;
  1025. return 0;
  1026. }
  1027. if (!strcmp(end, "us")) {
  1028. *time_q *= NSEC_PER_USEC;
  1029. return 0;
  1030. }
  1031. if (!strcmp(end, "ns"))
  1032. return 0;
  1033. parse_err:
  1034. pr_err("Cannot parse time quantum `%s'\n", arg);
  1035. return -1;
  1036. }
  1037. int
  1038. report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
  1039. const char *arg, int unset __maybe_unused)
  1040. {
  1041. if (arg) {
  1042. int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
  1043. if (err) {
  1044. char buf[BUFSIZ];
  1045. regerror(err, &ignore_callees_regex, buf, sizeof(buf));
  1046. pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
  1047. return -1;
  1048. }
  1049. have_ignore_callees = 1;
  1050. }
  1051. return 0;
  1052. }
  1053. static int
  1054. parse_branch_mode(const struct option *opt,
  1055. const char *str __maybe_unused, int unset)
  1056. {
  1057. int *branch_mode = opt->value;
  1058. *branch_mode = !unset;
  1059. return 0;
  1060. }
  1061. static int
  1062. parse_percent_limit(const struct option *opt, const char *str,
  1063. int unset __maybe_unused)
  1064. {
  1065. struct report *rep = opt->value;
  1066. double pcnt = strtof(str, NULL);
  1067. rep->min_percent = pcnt;
  1068. callchain_param.min_percent = pcnt;
  1069. return 0;
  1070. }
  1071. static int
  1072. report_parse_addr2line_config(const struct option *opt __maybe_unused,
  1073. const char *arg, int unset __maybe_unused)
  1074. {
  1075. return addr2line_configure("addr2line.style", arg, NULL);
  1076. }
  1077. static int process_attr(const struct perf_tool *tool __maybe_unused,
  1078. union perf_event *event,
  1079. struct evlist **pevlist)
  1080. {
  1081. struct perf_session *session;
  1082. u64 sample_type;
  1083. int err;
  1084. err = perf_event__process_attr(tool, event, pevlist);
  1085. if (err)
  1086. return err;
  1087. /*
  1088. * Check if we need to enable callchains based
  1089. * on events sample_type.
  1090. */
  1091. sample_type = evlist__combined_sample_type(*pevlist);
  1092. session = (*pevlist)->session;
  1093. callchain_param_setup(sample_type, perf_session__e_machine(session, /*e_flags=*/NULL));
  1094. return 0;
  1095. }
  1096. #define CALLCHAIN_BRANCH_SORT_ORDER \
  1097. "srcline,symbol,dso,callchain_branch_predicted," \
  1098. "callchain_branch_abort,callchain_branch_cycles"
  1099. int cmd_report(int argc, const char **argv)
  1100. {
  1101. struct perf_session *session;
  1102. struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
  1103. struct stat st;
  1104. bool has_br_stack = false;
  1105. int branch_mode = -1;
  1106. int last_key = 0;
  1107. bool branch_call_mode = false;
  1108. #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
  1109. static const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
  1110. CALLCHAIN_REPORT_HELP
  1111. "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
  1112. char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
  1113. const char * const report_usage[] = {
  1114. "perf report [<options>]",
  1115. NULL
  1116. };
  1117. struct report report = {
  1118. .max_stack = PERF_MAX_STACK_DEPTH,
  1119. .pretty_printing_style = "normal",
  1120. .socket_filter = -1,
  1121. .skip_empty = true,
  1122. };
  1123. char *sort_order_help = sort_help("sort by key(s):", SORT_MODE__NORMAL);
  1124. char *field_order_help = sort_help("output field(s):", SORT_MODE__NORMAL);
  1125. const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
  1126. const struct option options[] = {
  1127. OPT_STRING('i', "input", &input_name, "file",
  1128. "input file name"),
  1129. OPT_INCR('v', "verbose", &verbose,
  1130. "be more verbose (show symbol address, etc)"),
  1131. OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"),
  1132. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1133. "dump raw trace in ASCII"),
  1134. OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"),
  1135. OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"),
  1136. OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"),
  1137. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1138. "file", "vmlinux pathname"),
  1139. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  1140. "don't load vmlinux even if found"),
  1141. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  1142. "file", "kallsyms pathname"),
  1143. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  1144. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  1145. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  1146. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  1147. "Show a column with the number of samples"),
  1148. OPT_BOOLEAN('T', "threads", &report.show_threads,
  1149. "Show per-thread event counters"),
  1150. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  1151. "pretty printing style key: normal raw"),
  1152. #ifdef HAVE_SLANG_SUPPORT
  1153. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  1154. #endif
  1155. #ifdef HAVE_GTK2_SUPPORT
  1156. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  1157. #endif
  1158. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  1159. "Use the stdio interface"),
  1160. OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
  1161. OPT_BOOLEAN(0, "header-only", &report.header_only,
  1162. "Show only data header."),
  1163. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1164. sort_order_help),
  1165. OPT_STRING('F', "fields", &field_order, "key[,keys...]",
  1166. field_order_help),
  1167. OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
  1168. "Show sample percentage for different cpu modes"),
  1169. OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  1170. "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
  1171. OPT_STRING('p', "parent", &parent_pattern, "regex",
  1172. "regex filter to identify parent, see: '--sort parent'"),
  1173. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  1174. "Only display entries with parent-match"),
  1175. OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
  1176. "print_type,threshold[,print_limit],order,sort_key[,branch],value",
  1177. report_callchain_help, &report_parse_callchain_opt,
  1178. callchain_default_opt),
  1179. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  1180. "Accumulate callchains of children and show total overhead as well. "
  1181. "Enabled by default, use --no-children to disable."),
  1182. OPT_INTEGER(0, "max-stack", &report.max_stack,
  1183. "Set the maximum stack depth when parsing the callchain, "
  1184. "anything beyond the specified depth will be ignored. "
  1185. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  1186. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  1187. "alias for inverted call graph"),
  1188. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  1189. "ignore callees of these functions in call graphs",
  1190. report_parse_ignore_callees_opt),
  1191. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  1192. "only consider symbols in these dsos"),
  1193. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1194. "only consider symbols in these comms"),
  1195. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  1196. "only consider symbols in these pids"),
  1197. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  1198. "only consider symbols in these tids"),
  1199. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1200. "only consider these symbols"),
  1201. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  1202. "only show symbols that (partially) match with this filter"),
  1203. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  1204. "width[,width...]",
  1205. "don't try to adjust column width, use these fixed values"),
  1206. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  1207. "separator for columns, no spaces will be added between "
  1208. "columns '.' is reserved."),
  1209. OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
  1210. "Only display entries resolved to a symbol"),
  1211. OPT_CALLBACK(0, "symfs", NULL, "directory",
  1212. "Look for files with symbols relative to this directory",
  1213. symbol__config_symfs),
  1214. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  1215. "list of cpus to profile"),
  1216. OPT_STRING(0, "parallelism", &symbol_conf.parallelism_list_str, "parallelism",
  1217. "only consider these parallelism levels (cpu set format)"),
  1218. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  1219. "Display extended information about perf.data file"),
  1220. OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src,
  1221. "Interleave source code with assembly code (default)"),
  1222. OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw,
  1223. "Display raw encoding of assembly instructions (default)"),
  1224. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  1225. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  1226. OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix",
  1227. "Add prefix to source file path names in programs (with --prefix-strip)"),
  1228. OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N",
  1229. "Strip first N entries of source file path name in programs (with --prefix)"),
  1230. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  1231. "Show a column with the sum of periods"),
  1232. OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set,
  1233. "Show event group information together"),
  1234. OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx,
  1235. "Sort the output by the event at the index n in group. "
  1236. "If n is invalid, sort by the first event. "
  1237. "WARNING: should be used on grouped events."),
  1238. OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
  1239. "use branch records for per branch histogram filling",
  1240. parse_branch_mode),
  1241. OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
  1242. "add last branch records to call history"),
  1243. OPT_STRING(0, "objdump", &objdump_path, "path",
  1244. "objdump binary to use for disassembly and annotations"),
  1245. OPT_STRING(0, "addr2line", &addr2line_path, "path",
  1246. "addr2line binary to use for line numbers"),
  1247. OPT_CALLBACK(0, "addr2line-style", NULL, "addr2line style",
  1248. "addr2line styles (libdw,llvm,libbfd,addr2line)",
  1249. report_parse_addr2line_config),
  1250. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  1251. "Symbol demangling. Enabled by default, use --no-demangle to disable."),
  1252. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1253. "Enable kernel symbol demangling"),
  1254. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  1255. OPT_INTEGER(0, "samples", &symbol_conf.res_sample,
  1256. "Number of samples to save per histogram entry for individual browsing"),
  1257. OPT_CALLBACK(0, "percent-limit", &report, "percent",
  1258. "Don't show entries under that percent", parse_percent_limit),
  1259. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  1260. "how to display percentage of filtered entries", parse_filter_percentage),
  1261. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  1262. "Instruction Tracing options\n" ITRACE_HELP,
  1263. itrace_parse_synth_opts),
  1264. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  1265. "Show full source file name path for source lines"),
  1266. OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
  1267. "Show callgraph from reference event"),
  1268. OPT_BOOLEAN(0, "stitch-lbr", &report.stitch_lbr,
  1269. "Enable LBR callgraph stitching approach"),
  1270. OPT_INTEGER(0, "socket-filter", &report.socket_filter,
  1271. "only show processor socket that match with this filter"),
  1272. OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
  1273. "Show raw trace event output (do not use print fmt or plugins)"),
  1274. OPT_BOOLEAN('H', "hierarchy", &symbol_conf.report_hierarchy,
  1275. "Show entries in a hierarchy"),
  1276. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  1277. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  1278. stdio__config_color, "always"),
  1279. OPT_STRING(0, "time", &report.time_str, "str",
  1280. "Time span of interest (start,stop)"),
  1281. OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
  1282. "Show inline function"),
  1283. OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period",
  1284. "Set percent type local/global-period/hits",
  1285. annotate_parse_percent_type),
  1286. OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs, "Show times in nanosecs"),
  1287. OPT_CALLBACK(0, "time-quantum", &symbol_conf.time_quantum, "time (ms|us|ns|s)",
  1288. "Set time quantum for time sort key (default 100ms)",
  1289. parse_time_quantum),
  1290. OPTS_EVSWITCH(&report.evswitch),
  1291. OPT_BOOLEAN(0, "total-cycles", &report.total_cycles_mode,
  1292. "Sort all blocks by 'Sampled Cycles%'"),
  1293. OPT_BOOLEAN(0, "disable-order", &report.disable_order,
  1294. "Disable raw trace ordering"),
  1295. OPT_BOOLEAN(0, "skip-empty", &report.skip_empty,
  1296. "Do not display empty (or dummy) events in the output"),
  1297. OPT_BOOLEAN(0, "latency", &symbol_conf.prefer_latency,
  1298. "Show latency-centric profile rather than the default\n"
  1299. "\t\t\t CPU-consumption-centric profile\n"
  1300. "\t\t\t (requires perf record --latency flag)."),
  1301. OPT_END()
  1302. };
  1303. struct perf_data data = {
  1304. .mode = PERF_DATA_MODE_READ,
  1305. };
  1306. int ret = hists__init();
  1307. char sort_tmp[128];
  1308. bool ordered_events = true;
  1309. if (ret < 0)
  1310. goto exit;
  1311. /*
  1312. * tasks_mode require access to exited threads to list those that are in
  1313. * the data file. Off-cpu events are synthesized after other events and
  1314. * reference exited threads.
  1315. */
  1316. symbol_conf.keep_exited_threads = true;
  1317. annotation_options__init();
  1318. ret = perf_config(report__config, &report);
  1319. if (ret)
  1320. goto exit;
  1321. argc = parse_options(argc, argv, options, report_usage, 0);
  1322. if (argc) {
  1323. /*
  1324. * Special case: if there's an argument left then assume that
  1325. * it's a symbol filter:
  1326. */
  1327. if (argc > 1)
  1328. usage_with_options(report_usage, options);
  1329. report.symbol_filter_str = argv[0];
  1330. }
  1331. if (disassembler_style) {
  1332. annotate_opts.disassembler_style = strdup(disassembler_style);
  1333. if (!annotate_opts.disassembler_style)
  1334. return -ENOMEM;
  1335. }
  1336. if (objdump_path) {
  1337. annotate_opts.objdump_path = strdup(objdump_path);
  1338. if (!annotate_opts.objdump_path)
  1339. return -ENOMEM;
  1340. }
  1341. if (addr2line_path) {
  1342. symbol_conf.addr2line_path = strdup(addr2line_path);
  1343. if (!symbol_conf.addr2line_path)
  1344. return -ENOMEM;
  1345. }
  1346. if (annotate_check_args() < 0) {
  1347. ret = -EINVAL;
  1348. goto exit;
  1349. }
  1350. if (report.mmaps_mode)
  1351. report.tasks_mode = true;
  1352. if (dump_trace && report.disable_order)
  1353. ordered_events = false;
  1354. if (quiet)
  1355. perf_quiet_option();
  1356. ret = symbol__validate_sym_arguments();
  1357. if (ret)
  1358. goto exit;
  1359. if (report.inverted_callchain)
  1360. callchain_param.order = ORDER_CALLER;
  1361. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  1362. callchain_param.order = ORDER_CALLER;
  1363. if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
  1364. (int)itrace_synth_opts.callchain_sz > report.max_stack)
  1365. report.max_stack = itrace_synth_opts.callchain_sz;
  1366. if (!input_name || !strlen(input_name)) {
  1367. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  1368. input_name = "-";
  1369. else
  1370. input_name = "perf.data";
  1371. }
  1372. repeat:
  1373. data.path = input_name;
  1374. data.force = symbol_conf.force;
  1375. symbol_conf.skip_empty = report.skip_empty;
  1376. perf_tool__init(&report.tool, ordered_events);
  1377. report.tool.sample = process_sample_event;
  1378. report.tool.mmap = perf_event__process_mmap;
  1379. report.tool.mmap2 = perf_event__process_mmap2;
  1380. report.tool.comm = perf_event__process_comm;
  1381. report.tool.namespaces = perf_event__process_namespaces;
  1382. report.tool.cgroup = perf_event__process_cgroup;
  1383. report.tool.exit = perf_event__process_exit;
  1384. report.tool.fork = perf_event__process_fork;
  1385. report.tool.context_switch = perf_event__process_switch;
  1386. report.tool.lost = perf_event__process_lost;
  1387. report.tool.read = process_read_event;
  1388. report.tool.attr = process_attr;
  1389. #ifdef HAVE_LIBTRACEEVENT
  1390. report.tool.tracing_data = perf_event__process_tracing_data;
  1391. #endif
  1392. report.tool.build_id = perf_event__process_build_id;
  1393. report.tool.id_index = perf_event__process_id_index;
  1394. report.tool.auxtrace_info = perf_event__process_auxtrace_info;
  1395. report.tool.auxtrace = perf_event__process_auxtrace;
  1396. report.tool.event_update = perf_event__process_event_update;
  1397. report.tool.feature = process_feature_event;
  1398. report.tool.ordering_requires_timestamps = true;
  1399. report.tool.merge_deferred_callchains = !dump_trace;
  1400. session = perf_session__new(&data, &report.tool);
  1401. if (IS_ERR(session)) {
  1402. ret = PTR_ERR(session);
  1403. goto exit;
  1404. }
  1405. ret = evswitch__init(&report.evswitch, session->evlist, stderr);
  1406. if (ret)
  1407. goto exit;
  1408. if (zstd_init(&(session->zstd_data), 0) < 0)
  1409. pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
  1410. if (report.queue_size) {
  1411. ordered_events__set_alloc_size(&session->ordered_events,
  1412. report.queue_size);
  1413. }
  1414. session->itrace_synth_opts = &itrace_synth_opts;
  1415. report.session = session;
  1416. has_br_stack = perf_header__has_feat(&session->header,
  1417. HEADER_BRANCH_STACK);
  1418. if (evlist__combined_sample_type(session->evlist) & PERF_SAMPLE_STACK_USER)
  1419. has_br_stack = false;
  1420. setup_forced_leader(&report, session->evlist);
  1421. if (symbol_conf.group_sort_idx && evlist__nr_groups(session->evlist) == 0) {
  1422. parse_options_usage(NULL, options, "group-sort-idx", 0);
  1423. ret = -EINVAL;
  1424. goto error;
  1425. }
  1426. if (itrace_synth_opts.last_branch || itrace_synth_opts.add_last_branch)
  1427. has_br_stack = true;
  1428. if (has_br_stack && branch_call_mode)
  1429. symbol_conf.show_branchflag_count = true;
  1430. memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
  1431. /*
  1432. * Branch mode is a tristate:
  1433. * -1 means default, so decide based on the file having branch data.
  1434. * 0/1 means the user chose a mode.
  1435. */
  1436. if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
  1437. !branch_call_mode) {
  1438. sort__mode = SORT_MODE__BRANCH;
  1439. symbol_conf.cumulate_callchain = false;
  1440. }
  1441. if (branch_call_mode) {
  1442. callchain_param.key = CCKEY_ADDRESS;
  1443. callchain_param.branch_callstack = true;
  1444. symbol_conf.use_callchain = true;
  1445. callchain_register_param(&callchain_param);
  1446. if (sort_order == NULL)
  1447. sort_order = CALLCHAIN_BRANCH_SORT_ORDER;
  1448. }
  1449. if (report.mem_mode) {
  1450. if (sort__mode == SORT_MODE__BRANCH) {
  1451. pr_err("branch and mem mode incompatible\n");
  1452. goto error;
  1453. }
  1454. sort__mode = SORT_MODE__MEMORY;
  1455. symbol_conf.cumulate_callchain = false;
  1456. }
  1457. if (symbol_conf.report_hierarchy) {
  1458. /*
  1459. * The hist entries in hierarchy are added during the collpase
  1460. * phase. Let's enable it even if no sort keys require it.
  1461. */
  1462. perf_hpp_list.need_collapse = true;
  1463. }
  1464. if (report.use_stdio)
  1465. use_browser = 0;
  1466. #ifdef HAVE_SLANG_SUPPORT
  1467. else if (report.use_tui)
  1468. use_browser = 1;
  1469. #endif
  1470. #ifdef HAVE_GTK2_SUPPORT
  1471. else if (report.use_gtk)
  1472. use_browser = 2;
  1473. #endif
  1474. /* Force tty output for header output and per-thread stat. */
  1475. if (report.header || report.header_only || report.show_threads)
  1476. use_browser = 0;
  1477. if (report.header || report.header_only)
  1478. report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
  1479. if (report.show_full_info)
  1480. report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
  1481. if (report.stats_mode || report.tasks_mode)
  1482. use_browser = 0;
  1483. if (report.stats_mode && report.tasks_mode) {
  1484. pr_err("Error: --tasks and --mmaps can't be used together with --stats\n");
  1485. goto error;
  1486. }
  1487. if (report.total_cycles_mode) {
  1488. if (sort__mode != SORT_MODE__BRANCH)
  1489. report.total_cycles_mode = false;
  1490. else
  1491. sort_order = NULL;
  1492. }
  1493. if ((sort_order && strstr(sort_order, "type")) ||
  1494. (field_order && strstr(field_order, "type"))) {
  1495. report.data_type = true;
  1496. annotate_opts.annotate_src = false;
  1497. /* disable incompatible options */
  1498. symbol_conf.cumulate_callchain = false;
  1499. #ifndef HAVE_LIBDW_SUPPORT
  1500. pr_err("Error: Data type profiling is disabled due to missing DWARF support\n");
  1501. goto error;
  1502. #endif
  1503. }
  1504. if (strcmp(input_name, "-") != 0)
  1505. setup_browser(true);
  1506. else
  1507. use_browser = 0;
  1508. if (report.data_type && use_browser == 1) {
  1509. symbol_conf.annotate_data_member = true;
  1510. symbol_conf.annotate_data_sample = true;
  1511. }
  1512. symbol_conf.enable_latency = true;
  1513. if (report.disable_order || !perf_session__has_switch_events(session)) {
  1514. if (symbol_conf.parallelism_list_str ||
  1515. symbol_conf.prefer_latency ||
  1516. (sort_order && (strstr(sort_order, "latency") ||
  1517. strstr(sort_order, "parallelism"))) ||
  1518. (field_order && (strstr(field_order, "latency") ||
  1519. strstr(field_order, "parallelism")))) {
  1520. if (report.disable_order)
  1521. ui__error("Use of latency profile or parallelism is incompatible with --disable-order.\n");
  1522. else
  1523. ui__error("Use of latency profile or parallelism requires --latency flag during record.\n");
  1524. return -1;
  1525. }
  1526. /*
  1527. * If user did not ask for anything related to
  1528. * latency/parallelism explicitly, just don't show it.
  1529. */
  1530. symbol_conf.enable_latency = false;
  1531. }
  1532. if (last_key != K_SWITCH_INPUT_DATA) {
  1533. if (sort_order && strstr(sort_order, "ipc")) {
  1534. parse_options_usage(report_usage, options, "s", 1);
  1535. goto error;
  1536. }
  1537. if (sort_order && strstr(sort_order, "symbol")) {
  1538. if (sort__mode == SORT_MODE__BRANCH) {
  1539. snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s",
  1540. sort_order, "ipc_lbr");
  1541. report.symbol_ipc = true;
  1542. } else {
  1543. snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s",
  1544. sort_order, "ipc_null");
  1545. }
  1546. sort_order = sort_tmp;
  1547. }
  1548. }
  1549. if ((last_key != K_SWITCH_INPUT_DATA && last_key != K_RELOAD) &&
  1550. (setup_sorting(session->evlist, perf_session__env(session)) < 0)) {
  1551. if (sort_order)
  1552. parse_options_usage(report_usage, options, "s", 1);
  1553. if (field_order)
  1554. parse_options_usage(sort_order ? NULL : report_usage,
  1555. options, "F", 1);
  1556. goto error;
  1557. }
  1558. if ((report.header || report.header_only) && !quiet) {
  1559. perf_session__fprintf_info(session, stdout,
  1560. report.show_full_info);
  1561. if (report.header_only) {
  1562. if (data.is_pipe) {
  1563. /*
  1564. * we need to process first few records
  1565. * which contains PERF_RECORD_HEADER_FEATURE.
  1566. */
  1567. perf_session__process_events(session);
  1568. }
  1569. ret = 0;
  1570. goto error;
  1571. }
  1572. } else if (use_browser == 0 && !quiet &&
  1573. !report.stats_mode && !report.tasks_mode) {
  1574. fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
  1575. stdout);
  1576. }
  1577. /*
  1578. * Only in the TUI browser we are doing integrated annotation,
  1579. * so don't allocate extra space that won't be used in the stdio
  1580. * implementation.
  1581. */
  1582. if (ui__has_annotation() || report.symbol_ipc || report.data_type ||
  1583. report.total_cycles_mode) {
  1584. ret = symbol__annotation_init();
  1585. if (ret < 0)
  1586. goto error;
  1587. /*
  1588. * For searching by name on the "Browse map details".
  1589. * providing it only in verbose mode not to bloat too
  1590. * much struct symbol.
  1591. */
  1592. if (verbose > 0) {
  1593. /*
  1594. * XXX: Need to provide a less kludgy way to ask for
  1595. * more space per symbol, the u32 is for the index on
  1596. * the ui browser.
  1597. * See symbol__browser_index.
  1598. */
  1599. symbol_conf.priv_size += sizeof(u32);
  1600. }
  1601. annotation_config__init();
  1602. }
  1603. if (symbol__init(perf_session__env(session)) < 0)
  1604. goto error;
  1605. if (report.time_str) {
  1606. ret = perf_time__parse_for_ranges(report.time_str, session,
  1607. &report.ptime_range,
  1608. &report.range_size,
  1609. &report.range_num);
  1610. if (ret < 0)
  1611. goto error;
  1612. itrace_synth_opts__set_time_range(&itrace_synth_opts,
  1613. report.ptime_range,
  1614. report.range_num);
  1615. }
  1616. #ifdef HAVE_LIBTRACEEVENT
  1617. if (session->tevent.pevent &&
  1618. tep_set_function_resolver(session->tevent.pevent,
  1619. machine__resolve_kernel_addr,
  1620. &session->machines.host) < 0) {
  1621. pr_err("%s: failed to set libtraceevent function resolver\n",
  1622. __func__);
  1623. return -1;
  1624. }
  1625. #endif
  1626. sort__setup_elide(stdout);
  1627. ret = __cmd_report(&report);
  1628. if (ret == K_SWITCH_INPUT_DATA || ret == K_RELOAD) {
  1629. perf_session__delete(session);
  1630. last_key = K_SWITCH_INPUT_DATA;
  1631. /*
  1632. * To support switching between data with and without callchains.
  1633. * report__setup_sample_type() will update it properly.
  1634. */
  1635. symbol_conf.use_callchain = false;
  1636. goto repeat;
  1637. } else
  1638. ret = 0;
  1639. if (!use_browser && (verbose > 2 || debug_kmaps))
  1640. perf_session__dump_kmaps(session);
  1641. error:
  1642. if (report.ptime_range) {
  1643. itrace_synth_opts__clear_time_range(&itrace_synth_opts);
  1644. zfree(&report.ptime_range);
  1645. }
  1646. if (report.block_reports) {
  1647. block_info__free_report(report.block_reports,
  1648. report.nr_block_reports);
  1649. report.block_reports = NULL;
  1650. }
  1651. zstd_fini(&(session->zstd_data));
  1652. perf_session__delete(session);
  1653. exit:
  1654. annotation_options__exit();
  1655. free(sort_order_help);
  1656. free(field_order_help);
  1657. return ret;
  1658. }