builtin-report.c 29 KB

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