builtin-report.c 47 KB

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