builtin-report.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * builtin-report.c
  3. *
  4. * Builtin report command: Analyze the perf.data input file,
  5. * look up and read DSOs and symbol information and display
  6. * a histogram of results, along various sorting keys.
  7. */
  8. #include "builtin.h"
  9. #include "util/util.h"
  10. #include "util/config.h"
  11. #include "util/annotate.h"
  12. #include "util/color.h"
  13. #include <linux/list.h>
  14. #include <linux/rbtree.h>
  15. #include "util/symbol.h"
  16. #include "util/callchain.h"
  17. #include "util/strlist.h"
  18. #include "util/values.h"
  19. #include "perf.h"
  20. #include "util/debug.h"
  21. #include "util/evlist.h"
  22. #include "util/evsel.h"
  23. #include "util/header.h"
  24. #include "util/session.h"
  25. #include "util/tool.h"
  26. #include <subcmd/parse-options.h>
  27. #include <subcmd/exec-cmd.h>
  28. #include "util/parse-events.h"
  29. #include "util/thread.h"
  30. #include "util/sort.h"
  31. #include "util/hist.h"
  32. #include "util/data.h"
  33. #include "arch/common.h"
  34. #include "util/auxtrace.h"
  35. #include <dlfcn.h>
  36. #include <linux/bitmap.h>
  37. #include <linux/stringify.h>
  38. struct report {
  39. struct perf_tool tool;
  40. struct perf_session *session;
  41. bool use_tui, use_gtk, use_stdio;
  42. bool show_full_info;
  43. bool show_threads;
  44. bool inverted_callchain;
  45. bool mem_mode;
  46. bool header;
  47. bool header_only;
  48. bool nonany_branch_mode;
  49. int max_stack;
  50. struct perf_read_values show_threads_values;
  51. const char *pretty_printing_style;
  52. const char *cpu_list;
  53. const char *symbol_filter_str;
  54. float min_percent;
  55. u64 nr_entries;
  56. u64 queue_size;
  57. int socket_filter;
  58. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  59. };
  60. static int report__config(const char *var, const char *value, void *cb)
  61. {
  62. struct report *rep = cb;
  63. if (!strcmp(var, "report.group")) {
  64. symbol_conf.event_group = perf_config_bool(var, value);
  65. return 0;
  66. }
  67. if (!strcmp(var, "report.percent-limit")) {
  68. double pcnt = strtof(value, NULL);
  69. rep->min_percent = pcnt;
  70. callchain_param.min_percent = pcnt;
  71. return 0;
  72. }
  73. if (!strcmp(var, "report.children")) {
  74. symbol_conf.cumulate_callchain = perf_config_bool(var, value);
  75. return 0;
  76. }
  77. if (!strcmp(var, "report.queue-size")) {
  78. rep->queue_size = perf_config_u64(var, value);
  79. return 0;
  80. }
  81. if (!strcmp(var, "report.sort_order")) {
  82. default_sort_order = strdup(value);
  83. return 0;
  84. }
  85. return 0;
  86. }
  87. static int hist_iter__report_callback(struct hist_entry_iter *iter,
  88. struct addr_location *al, bool single,
  89. void *arg)
  90. {
  91. int err = 0;
  92. struct report *rep = arg;
  93. struct hist_entry *he = iter->he;
  94. struct perf_evsel *evsel = iter->evsel;
  95. struct mem_info *mi;
  96. struct branch_info *bi;
  97. if (!ui__has_annotation())
  98. return 0;
  99. hist__account_cycles(iter->sample->branch_stack, al, iter->sample,
  100. rep->nonany_branch_mode);
  101. if (sort__mode == SORT_MODE__BRANCH) {
  102. bi = he->branch_info;
  103. err = addr_map_symbol__inc_samples(&bi->from, evsel->idx);
  104. if (err)
  105. goto out;
  106. err = addr_map_symbol__inc_samples(&bi->to, evsel->idx);
  107. } else if (rep->mem_mode) {
  108. mi = he->mem_info;
  109. err = addr_map_symbol__inc_samples(&mi->daddr, evsel->idx);
  110. if (err)
  111. goto out;
  112. err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
  113. } else if (symbol_conf.cumulate_callchain) {
  114. if (single)
  115. err = hist_entry__inc_addr_samples(he, evsel->idx,
  116. al->addr);
  117. } else {
  118. err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
  119. }
  120. out:
  121. return err;
  122. }
  123. static int process_sample_event(struct perf_tool *tool,
  124. union perf_event *event,
  125. struct perf_sample *sample,
  126. struct perf_evsel *evsel,
  127. struct machine *machine)
  128. {
  129. struct report *rep = container_of(tool, struct report, tool);
  130. struct addr_location al;
  131. struct hist_entry_iter iter = {
  132. .evsel = evsel,
  133. .sample = sample,
  134. .hide_unresolved = symbol_conf.hide_unresolved,
  135. .add_entry_cb = hist_iter__report_callback,
  136. };
  137. int ret = 0;
  138. if (machine__resolve(machine, &al, sample) < 0) {
  139. pr_debug("problem processing %d event, skipping it.\n",
  140. event->header.type);
  141. return -1;
  142. }
  143. if (symbol_conf.hide_unresolved && al.sym == NULL)
  144. goto out_put;
  145. if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
  146. goto out_put;
  147. if (sort__mode == SORT_MODE__BRANCH) {
  148. /*
  149. * A non-synthesized event might not have a branch stack if
  150. * branch stacks have been synthesized (using itrace options).
  151. */
  152. if (!sample->branch_stack)
  153. goto out_put;
  154. iter.ops = &hist_iter_branch;
  155. } else if (rep->mem_mode) {
  156. iter.ops = &hist_iter_mem;
  157. } else if (symbol_conf.cumulate_callchain) {
  158. iter.ops = &hist_iter_cumulative;
  159. } else {
  160. iter.ops = &hist_iter_normal;
  161. }
  162. if (al.map != NULL)
  163. al.map->dso->hit = 1;
  164. ret = hist_entry_iter__add(&iter, &al, rep->max_stack, rep);
  165. if (ret < 0)
  166. pr_debug("problem adding hist entry, skipping event\n");
  167. out_put:
  168. addr_location__put(&al);
  169. return ret;
  170. }
  171. static int process_read_event(struct perf_tool *tool,
  172. union perf_event *event,
  173. struct perf_sample *sample __maybe_unused,
  174. struct perf_evsel *evsel,
  175. struct machine *machine __maybe_unused)
  176. {
  177. struct report *rep = container_of(tool, struct report, tool);
  178. if (rep->show_threads) {
  179. const char *name = evsel ? perf_evsel__name(evsel) : "unknown";
  180. perf_read_values_add_value(&rep->show_threads_values,
  181. event->read.pid, event->read.tid,
  182. event->read.id,
  183. name,
  184. event->read.value);
  185. }
  186. dump_printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid,
  187. evsel ? perf_evsel__name(evsel) : "FAIL",
  188. event->read.value);
  189. return 0;
  190. }
  191. /* For pipe mode, sample_type is not currently set */
  192. static int report__setup_sample_type(struct report *rep)
  193. {
  194. struct perf_session *session = rep->session;
  195. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  196. bool is_pipe = perf_data_file__is_pipe(session->file);
  197. if (session->itrace_synth_opts->callchain ||
  198. (!is_pipe &&
  199. perf_header__has_feat(&session->header, HEADER_AUXTRACE) &&
  200. !session->itrace_synth_opts->set))
  201. sample_type |= PERF_SAMPLE_CALLCHAIN;
  202. if (session->itrace_synth_opts->last_branch)
  203. sample_type |= PERF_SAMPLE_BRANCH_STACK;
  204. if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  205. if (perf_hpp_list.parent) {
  206. ui__error("Selected --sort parent, but no "
  207. "callchain data. Did you call "
  208. "'perf record' without -g?\n");
  209. return -EINVAL;
  210. }
  211. if (symbol_conf.use_callchain) {
  212. ui__error("Selected -g or --branch-history but no "
  213. "callchain data. Did\n"
  214. "you call 'perf record' without -g?\n");
  215. return -1;
  216. }
  217. } else if (!callchain_param.enabled &&
  218. callchain_param.mode != CHAIN_NONE &&
  219. !symbol_conf.use_callchain) {
  220. symbol_conf.use_callchain = true;
  221. if (callchain_register_param(&callchain_param) < 0) {
  222. ui__error("Can't register callchain params.\n");
  223. return -EINVAL;
  224. }
  225. }
  226. if (symbol_conf.cumulate_callchain) {
  227. /* Silently ignore if callchain is missing */
  228. if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  229. symbol_conf.cumulate_callchain = false;
  230. perf_hpp__cancel_cumulate();
  231. }
  232. }
  233. if (sort__mode == SORT_MODE__BRANCH) {
  234. if (!is_pipe &&
  235. !(sample_type & PERF_SAMPLE_BRANCH_STACK)) {
  236. ui__error("Selected -b but no branch data. "
  237. "Did you call perf record without -b?\n");
  238. return -1;
  239. }
  240. }
  241. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  242. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  243. (sample_type & PERF_SAMPLE_STACK_USER))
  244. callchain_param.record_mode = CALLCHAIN_DWARF;
  245. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  246. callchain_param.record_mode = CALLCHAIN_LBR;
  247. else
  248. callchain_param.record_mode = CALLCHAIN_FP;
  249. }
  250. /* ??? handle more cases than just ANY? */
  251. if (!(perf_evlist__combined_branch_type(session->evlist) &
  252. PERF_SAMPLE_BRANCH_ANY))
  253. rep->nonany_branch_mode = true;
  254. return 0;
  255. }
  256. static void sig_handler(int sig __maybe_unused)
  257. {
  258. session_done = 1;
  259. }
  260. static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep,
  261. const char *evname, FILE *fp)
  262. {
  263. size_t ret;
  264. char unit;
  265. unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  266. u64 nr_events = hists->stats.total_period;
  267. struct perf_evsel *evsel = hists_to_evsel(hists);
  268. char buf[512];
  269. size_t size = sizeof(buf);
  270. int socked_id = hists->socket_filter;
  271. if (symbol_conf.filter_relative) {
  272. nr_samples = hists->stats.nr_non_filtered_samples;
  273. nr_events = hists->stats.total_non_filtered_period;
  274. }
  275. if (perf_evsel__is_group_event(evsel)) {
  276. struct perf_evsel *pos;
  277. perf_evsel__group_desc(evsel, buf, size);
  278. evname = buf;
  279. for_each_group_member(pos, evsel) {
  280. const struct hists *pos_hists = evsel__hists(pos);
  281. if (symbol_conf.filter_relative) {
  282. nr_samples += pos_hists->stats.nr_non_filtered_samples;
  283. nr_events += pos_hists->stats.total_non_filtered_period;
  284. } else {
  285. nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
  286. nr_events += pos_hists->stats.total_period;
  287. }
  288. }
  289. }
  290. nr_samples = convert_unit(nr_samples, &unit);
  291. ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
  292. if (evname != NULL)
  293. ret += fprintf(fp, " of event '%s'", evname);
  294. if (symbol_conf.show_ref_callgraph &&
  295. strstr(evname, "call-graph=no")) {
  296. ret += fprintf(fp, ", show reference callgraph");
  297. }
  298. if (rep->mem_mode) {
  299. ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
  300. ret += fprintf(fp, "\n# Sort order : %s", sort_order ? : default_mem_sort_order);
  301. } else
  302. ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
  303. if (socked_id > -1)
  304. ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
  305. return ret + fprintf(fp, "\n#\n");
  306. }
  307. static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
  308. struct report *rep,
  309. const char *help)
  310. {
  311. struct perf_evsel *pos;
  312. fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n", evlist->stats.total_lost_samples);
  313. evlist__for_each_entry(evlist, pos) {
  314. struct hists *hists = evsel__hists(pos);
  315. const char *evname = perf_evsel__name(pos);
  316. if (symbol_conf.event_group &&
  317. !perf_evsel__is_group_leader(pos))
  318. continue;
  319. hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
  320. hists__fprintf(hists, true, 0, 0, rep->min_percent, stdout,
  321. symbol_conf.use_callchain);
  322. fprintf(stdout, "\n\n");
  323. }
  324. if (sort_order == NULL &&
  325. parent_pattern == default_parent_pattern)
  326. fprintf(stdout, "#\n# (%s)\n#\n", help);
  327. if (rep->show_threads) {
  328. bool style = !strcmp(rep->pretty_printing_style, "raw");
  329. perf_read_values_display(stdout, &rep->show_threads_values,
  330. style);
  331. perf_read_values_destroy(&rep->show_threads_values);
  332. }
  333. return 0;
  334. }
  335. static void report__warn_kptr_restrict(const struct report *rep)
  336. {
  337. struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
  338. struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
  339. if (kernel_map == NULL ||
  340. (kernel_map->dso->hit &&
  341. (kernel_kmap->ref_reloc_sym == NULL ||
  342. kernel_kmap->ref_reloc_sym->addr == 0))) {
  343. const char *desc =
  344. "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
  345. "can't be resolved.";
  346. if (kernel_map) {
  347. const struct dso *kdso = kernel_map->dso;
  348. if (!RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION])) {
  349. desc = "If some relocation was applied (e.g. "
  350. "kexec) symbols may be misresolved.";
  351. }
  352. }
  353. ui__warning(
  354. "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
  355. "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
  356. "Samples in kernel modules can't be resolved as well.\n\n",
  357. desc);
  358. }
  359. }
  360. static int report__gtk_browse_hists(struct report *rep, const char *help)
  361. {
  362. int (*hist_browser)(struct perf_evlist *evlist, const char *help,
  363. struct hist_browser_timer *timer, float min_pcnt);
  364. hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
  365. if (hist_browser == NULL) {
  366. ui__error("GTK browser not found!\n");
  367. return -1;
  368. }
  369. return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
  370. }
  371. static int report__browse_hists(struct report *rep)
  372. {
  373. int ret;
  374. struct perf_session *session = rep->session;
  375. struct perf_evlist *evlist = session->evlist;
  376. const char *help = perf_tip(system_path(TIPDIR));
  377. if (help == NULL) {
  378. /* fallback for people who don't install perf ;-) */
  379. help = perf_tip(DOCDIR);
  380. if (help == NULL)
  381. help = "Cannot load tips.txt file, please install perf!";
  382. }
  383. switch (use_browser) {
  384. case 1:
  385. ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
  386. rep->min_percent,
  387. &session->header.env);
  388. /*
  389. * Usually "ret" is the last pressed key, and we only
  390. * care if the key notifies us to switch data file.
  391. */
  392. if (ret != K_SWITCH_INPUT_DATA)
  393. ret = 0;
  394. break;
  395. case 2:
  396. ret = report__gtk_browse_hists(rep, help);
  397. break;
  398. default:
  399. ret = perf_evlist__tty_browse_hists(evlist, rep, help);
  400. break;
  401. }
  402. return ret;
  403. }
  404. static int report__collapse_hists(struct report *rep)
  405. {
  406. struct ui_progress prog;
  407. struct perf_evsel *pos;
  408. int ret = 0;
  409. ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
  410. evlist__for_each_entry(rep->session->evlist, pos) {
  411. struct hists *hists = evsel__hists(pos);
  412. if (pos->idx == 0)
  413. hists->symbol_filter_str = rep->symbol_filter_str;
  414. hists->socket_filter = rep->socket_filter;
  415. ret = hists__collapse_resort(hists, &prog);
  416. if (ret < 0)
  417. break;
  418. /* Non-group events are considered as leader */
  419. if (symbol_conf.event_group &&
  420. !perf_evsel__is_group_leader(pos)) {
  421. struct hists *leader_hists = evsel__hists(pos->leader);
  422. hists__match(leader_hists, hists);
  423. hists__link(leader_hists, hists);
  424. }
  425. }
  426. ui_progress__finish();
  427. return ret;
  428. }
  429. static void report__output_resort(struct report *rep)
  430. {
  431. struct ui_progress prog;
  432. struct perf_evsel *pos;
  433. ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
  434. evlist__for_each_entry(rep->session->evlist, pos)
  435. perf_evsel__output_resort(pos, &prog);
  436. ui_progress__finish();
  437. }
  438. static int __cmd_report(struct report *rep)
  439. {
  440. int ret;
  441. struct perf_session *session = rep->session;
  442. struct perf_evsel *pos;
  443. struct perf_data_file *file = session->file;
  444. signal(SIGINT, sig_handler);
  445. if (rep->cpu_list) {
  446. ret = perf_session__cpu_bitmap(session, rep->cpu_list,
  447. rep->cpu_bitmap);
  448. if (ret) {
  449. ui__error("failed to set cpu bitmap\n");
  450. return ret;
  451. }
  452. }
  453. if (rep->show_threads)
  454. perf_read_values_init(&rep->show_threads_values);
  455. ret = report__setup_sample_type(rep);
  456. if (ret) {
  457. /* report__setup_sample_type() already showed error message */
  458. return ret;
  459. }
  460. ret = perf_session__process_events(session);
  461. if (ret) {
  462. ui__error("failed to process sample\n");
  463. return ret;
  464. }
  465. report__warn_kptr_restrict(rep);
  466. evlist__for_each_entry(session->evlist, pos)
  467. rep->nr_entries += evsel__hists(pos)->nr_entries;
  468. if (use_browser == 0) {
  469. if (verbose > 3)
  470. perf_session__fprintf(session, stdout);
  471. if (verbose > 2)
  472. perf_session__fprintf_dsos(session, stdout);
  473. if (dump_trace) {
  474. perf_session__fprintf_nr_events(session, stdout);
  475. perf_evlist__fprintf_nr_events(session->evlist, stdout);
  476. return 0;
  477. }
  478. }
  479. ret = report__collapse_hists(rep);
  480. if (ret) {
  481. ui__error("failed to process hist entry\n");
  482. return ret;
  483. }
  484. if (session_done())
  485. return 0;
  486. /*
  487. * recalculate number of entries after collapsing since it
  488. * might be changed during the collapse phase.
  489. */
  490. rep->nr_entries = 0;
  491. evlist__for_each_entry(session->evlist, pos)
  492. rep->nr_entries += evsel__hists(pos)->nr_entries;
  493. if (rep->nr_entries == 0) {
  494. ui__error("The %s file has no samples!\n", file->path);
  495. return 0;
  496. }
  497. report__output_resort(rep);
  498. return report__browse_hists(rep);
  499. }
  500. static int
  501. report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  502. {
  503. struct callchain_param *callchain = opt->value;
  504. callchain->enabled = !unset;
  505. /*
  506. * --no-call-graph
  507. */
  508. if (unset) {
  509. symbol_conf.use_callchain = false;
  510. callchain->mode = CHAIN_NONE;
  511. return 0;
  512. }
  513. return parse_callchain_report_opt(arg);
  514. }
  515. int
  516. report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
  517. const char *arg, int unset __maybe_unused)
  518. {
  519. if (arg) {
  520. int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
  521. if (err) {
  522. char buf[BUFSIZ];
  523. regerror(err, &ignore_callees_regex, buf, sizeof(buf));
  524. pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
  525. return -1;
  526. }
  527. have_ignore_callees = 1;
  528. }
  529. return 0;
  530. }
  531. static int
  532. parse_branch_mode(const struct option *opt __maybe_unused,
  533. const char *str __maybe_unused, int unset)
  534. {
  535. int *branch_mode = opt->value;
  536. *branch_mode = !unset;
  537. return 0;
  538. }
  539. static int
  540. parse_percent_limit(const struct option *opt, const char *str,
  541. int unset __maybe_unused)
  542. {
  543. struct report *rep = opt->value;
  544. double pcnt = strtof(str, NULL);
  545. rep->min_percent = pcnt;
  546. callchain_param.min_percent = pcnt;
  547. return 0;
  548. }
  549. #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
  550. const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
  551. CALLCHAIN_REPORT_HELP
  552. "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
  553. int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
  554. {
  555. struct perf_session *session;
  556. struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
  557. struct stat st;
  558. bool has_br_stack = false;
  559. int branch_mode = -1;
  560. bool branch_call_mode = false;
  561. char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
  562. const char * const report_usage[] = {
  563. "perf report [<options>]",
  564. NULL
  565. };
  566. struct report report = {
  567. .tool = {
  568. .sample = process_sample_event,
  569. .mmap = perf_event__process_mmap,
  570. .mmap2 = perf_event__process_mmap2,
  571. .comm = perf_event__process_comm,
  572. .exit = perf_event__process_exit,
  573. .fork = perf_event__process_fork,
  574. .lost = perf_event__process_lost,
  575. .read = process_read_event,
  576. .attr = perf_event__process_attr,
  577. .tracing_data = perf_event__process_tracing_data,
  578. .build_id = perf_event__process_build_id,
  579. .id_index = perf_event__process_id_index,
  580. .auxtrace_info = perf_event__process_auxtrace_info,
  581. .auxtrace = perf_event__process_auxtrace,
  582. .ordered_events = true,
  583. .ordering_requires_timestamps = true,
  584. },
  585. .max_stack = PERF_MAX_STACK_DEPTH,
  586. .pretty_printing_style = "normal",
  587. .socket_filter = -1,
  588. };
  589. const struct option options[] = {
  590. OPT_STRING('i', "input", &input_name, "file",
  591. "input file name"),
  592. OPT_INCR('v', "verbose", &verbose,
  593. "be more verbose (show symbol address, etc)"),
  594. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  595. "dump raw trace in ASCII"),
  596. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  597. "file", "vmlinux pathname"),
  598. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  599. "file", "kallsyms pathname"),
  600. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  601. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  602. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  603. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  604. "Show a column with the number of samples"),
  605. OPT_BOOLEAN('T', "threads", &report.show_threads,
  606. "Show per-thread event counters"),
  607. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  608. "pretty printing style key: normal raw"),
  609. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  610. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  611. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  612. "Use the stdio interface"),
  613. OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
  614. OPT_BOOLEAN(0, "header-only", &report.header_only,
  615. "Show only data header."),
  616. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  617. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  618. " Please refer the man page for the complete list."),
  619. OPT_STRING('F', "fields", &field_order, "key[,keys...]",
  620. "output field(s): overhead, period, sample plus all of sort keys"),
  621. OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
  622. "Show sample percentage for different cpu modes"),
  623. OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  624. "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
  625. OPT_STRING('p', "parent", &parent_pattern, "regex",
  626. "regex filter to identify parent, see: '--sort parent'"),
  627. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  628. "Only display entries with parent-match"),
  629. OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
  630. "print_type,threshold[,print_limit],order,sort_key[,branch],value",
  631. report_callchain_help, &report_parse_callchain_opt,
  632. callchain_default_opt),
  633. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  634. "Accumulate callchains of children and show total overhead as well"),
  635. OPT_INTEGER(0, "max-stack", &report.max_stack,
  636. "Set the maximum stack depth when parsing the callchain, "
  637. "anything beyond the specified depth will be ignored. "
  638. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  639. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  640. "alias for inverted call graph"),
  641. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  642. "ignore callees of these functions in call graphs",
  643. report_parse_ignore_callees_opt),
  644. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  645. "only consider symbols in these dsos"),
  646. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  647. "only consider symbols in these comms"),
  648. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  649. "only consider symbols in these pids"),
  650. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  651. "only consider symbols in these tids"),
  652. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  653. "only consider these symbols"),
  654. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  655. "only show symbols that (partially) match with this filter"),
  656. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  657. "width[,width...]",
  658. "don't try to adjust column width, use these fixed values"),
  659. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  660. "separator for columns, no spaces will be added between "
  661. "columns '.' is reserved."),
  662. OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
  663. "Only display entries resolved to a symbol"),
  664. OPT_CALLBACK(0, "symfs", NULL, "directory",
  665. "Look for files with symbols relative to this directory",
  666. symbol__config_symfs),
  667. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  668. "list of cpus to profile"),
  669. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  670. "Display extended information about perf.data file"),
  671. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  672. "Interleave source code with assembly code (default)"),
  673. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  674. "Display raw encoding of assembly instructions (default)"),
  675. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  676. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  677. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  678. "Show a column with the sum of periods"),
  679. OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
  680. "Show event group information together"),
  681. OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
  682. "use branch records for per branch histogram filling",
  683. parse_branch_mode),
  684. OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
  685. "add last branch records to call history"),
  686. OPT_STRING(0, "objdump", &objdump_path, "path",
  687. "objdump binary to use for disassembly and annotations"),
  688. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  689. "Disable symbol demangling"),
  690. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  691. "Enable kernel symbol demangling"),
  692. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  693. OPT_CALLBACK(0, "percent-limit", &report, "percent",
  694. "Don't show entries under that percent", parse_percent_limit),
  695. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  696. "how to display percentage of filtered entries", parse_filter_percentage),
  697. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  698. "Instruction Tracing options",
  699. itrace_parse_synth_opts),
  700. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  701. "Show full source file name path for source lines"),
  702. OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
  703. "Show callgraph from reference event"),
  704. OPT_INTEGER(0, "socket-filter", &report.socket_filter,
  705. "only show processor socket that match with this filter"),
  706. OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
  707. "Show raw trace event output (do not use print fmt or plugins)"),
  708. OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
  709. "Show entries in a hierarchy"),
  710. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  711. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  712. stdio__config_color, "always"),
  713. OPT_END()
  714. };
  715. struct perf_data_file file = {
  716. .mode = PERF_DATA_MODE_READ,
  717. };
  718. int ret = hists__init();
  719. if (ret < 0)
  720. return ret;
  721. perf_config(report__config, &report);
  722. argc = parse_options(argc, argv, options, report_usage, 0);
  723. if (argc) {
  724. /*
  725. * Special case: if there's an argument left then assume that
  726. * it's a symbol filter:
  727. */
  728. if (argc > 1)
  729. usage_with_options(report_usage, options);
  730. report.symbol_filter_str = argv[0];
  731. }
  732. if (symbol_conf.vmlinux_name &&
  733. access(symbol_conf.vmlinux_name, R_OK)) {
  734. pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
  735. return -EINVAL;
  736. }
  737. if (symbol_conf.kallsyms_name &&
  738. access(symbol_conf.kallsyms_name, R_OK)) {
  739. pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
  740. return -EINVAL;
  741. }
  742. if (report.use_stdio)
  743. use_browser = 0;
  744. else if (report.use_tui)
  745. use_browser = 1;
  746. else if (report.use_gtk)
  747. use_browser = 2;
  748. if (report.inverted_callchain)
  749. callchain_param.order = ORDER_CALLER;
  750. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  751. callchain_param.order = ORDER_CALLER;
  752. if (itrace_synth_opts.callchain &&
  753. (int)itrace_synth_opts.callchain_sz > report.max_stack)
  754. report.max_stack = itrace_synth_opts.callchain_sz;
  755. if (!input_name || !strlen(input_name)) {
  756. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  757. input_name = "-";
  758. else
  759. input_name = "perf.data";
  760. }
  761. file.path = input_name;
  762. file.force = symbol_conf.force;
  763. repeat:
  764. session = perf_session__new(&file, false, &report.tool);
  765. if (session == NULL)
  766. return -1;
  767. if (report.queue_size) {
  768. ordered_events__set_alloc_size(&session->ordered_events,
  769. report.queue_size);
  770. }
  771. session->itrace_synth_opts = &itrace_synth_opts;
  772. report.session = session;
  773. has_br_stack = perf_header__has_feat(&session->header,
  774. HEADER_BRANCH_STACK);
  775. if (itrace_synth_opts.last_branch)
  776. has_br_stack = true;
  777. /*
  778. * Branch mode is a tristate:
  779. * -1 means default, so decide based on the file having branch data.
  780. * 0/1 means the user chose a mode.
  781. */
  782. if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
  783. !branch_call_mode) {
  784. sort__mode = SORT_MODE__BRANCH;
  785. symbol_conf.cumulate_callchain = false;
  786. }
  787. if (branch_call_mode) {
  788. callchain_param.key = CCKEY_ADDRESS;
  789. callchain_param.branch_callstack = 1;
  790. symbol_conf.use_callchain = true;
  791. callchain_register_param(&callchain_param);
  792. if (sort_order == NULL)
  793. sort_order = "srcline,symbol,dso";
  794. }
  795. if (report.mem_mode) {
  796. if (sort__mode == SORT_MODE__BRANCH) {
  797. pr_err("branch and mem mode incompatible\n");
  798. goto error;
  799. }
  800. sort__mode = SORT_MODE__MEMORY;
  801. symbol_conf.cumulate_callchain = false;
  802. }
  803. if (symbol_conf.report_hierarchy) {
  804. /* disable incompatible options */
  805. symbol_conf.cumulate_callchain = false;
  806. if (field_order) {
  807. pr_err("Error: --hierarchy and --fields options cannot be used together\n");
  808. parse_options_usage(report_usage, options, "F", 1);
  809. parse_options_usage(NULL, options, "hierarchy", 0);
  810. goto error;
  811. }
  812. perf_hpp_list.need_collapse = true;
  813. }
  814. /* Force tty output for header output and per-thread stat. */
  815. if (report.header || report.header_only || report.show_threads)
  816. use_browser = 0;
  817. if (strcmp(input_name, "-") != 0)
  818. setup_browser(true);
  819. else
  820. use_browser = 0;
  821. if (setup_sorting(session->evlist) < 0) {
  822. if (sort_order)
  823. parse_options_usage(report_usage, options, "s", 1);
  824. if (field_order)
  825. parse_options_usage(sort_order ? NULL : report_usage,
  826. options, "F", 1);
  827. goto error;
  828. }
  829. if (report.header || report.header_only) {
  830. perf_session__fprintf_info(session, stdout,
  831. report.show_full_info);
  832. if (report.header_only) {
  833. ret = 0;
  834. goto error;
  835. }
  836. } else if (use_browser == 0) {
  837. fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
  838. stdout);
  839. }
  840. /*
  841. * Only in the TUI browser we are doing integrated annotation,
  842. * so don't allocate extra space that won't be used in the stdio
  843. * implementation.
  844. */
  845. if (ui__has_annotation()) {
  846. ret = symbol__annotation_init();
  847. if (ret < 0)
  848. goto error;
  849. /*
  850. * For searching by name on the "Browse map details".
  851. * providing it only in verbose mode not to bloat too
  852. * much struct symbol.
  853. */
  854. if (verbose) {
  855. /*
  856. * XXX: Need to provide a less kludgy way to ask for
  857. * more space per symbol, the u32 is for the index on
  858. * the ui browser.
  859. * See symbol__browser_index.
  860. */
  861. symbol_conf.priv_size += sizeof(u32);
  862. symbol_conf.sort_by_name = true;
  863. }
  864. }
  865. if (symbol__init(&session->header.env) < 0)
  866. goto error;
  867. sort__setup_elide(stdout);
  868. ret = __cmd_report(&report);
  869. if (ret == K_SWITCH_INPUT_DATA) {
  870. perf_session__delete(session);
  871. goto repeat;
  872. } else
  873. ret = 0;
  874. error:
  875. perf_session__delete(session);
  876. return ret;
  877. }