builtin-report.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. 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_entry(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. symbol_conf.use_callchain);
  318. fprintf(stdout, "\n\n");
  319. }
  320. if (sort_order == NULL &&
  321. parent_pattern == default_parent_pattern)
  322. fprintf(stdout, "#\n# (%s)\n#\n", help);
  323. if (rep->show_threads) {
  324. bool style = !strcmp(rep->pretty_printing_style, "raw");
  325. perf_read_values_display(stdout, &rep->show_threads_values,
  326. style);
  327. perf_read_values_destroy(&rep->show_threads_values);
  328. }
  329. return 0;
  330. }
  331. static void report__warn_kptr_restrict(const struct report *rep)
  332. {
  333. struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
  334. struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
  335. if (kernel_map == NULL ||
  336. (kernel_map->dso->hit &&
  337. (kernel_kmap->ref_reloc_sym == NULL ||
  338. kernel_kmap->ref_reloc_sym->addr == 0))) {
  339. const char *desc =
  340. "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
  341. "can't be resolved.";
  342. if (kernel_map) {
  343. const struct dso *kdso = kernel_map->dso;
  344. if (!RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION])) {
  345. desc = "If some relocation was applied (e.g. "
  346. "kexec) symbols may be misresolved.";
  347. }
  348. }
  349. ui__warning(
  350. "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
  351. "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
  352. "Samples in kernel modules can't be resolved as well.\n\n",
  353. desc);
  354. }
  355. }
  356. static int report__gtk_browse_hists(struct report *rep, const char *help)
  357. {
  358. int (*hist_browser)(struct perf_evlist *evlist, const char *help,
  359. struct hist_browser_timer *timer, float min_pcnt);
  360. hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
  361. if (hist_browser == NULL) {
  362. ui__error("GTK browser not found!\n");
  363. return -1;
  364. }
  365. return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
  366. }
  367. static int report__browse_hists(struct report *rep)
  368. {
  369. int ret;
  370. struct perf_session *session = rep->session;
  371. struct perf_evlist *evlist = session->evlist;
  372. const char *help = perf_tip(system_path(TIPDIR));
  373. if (help == NULL) {
  374. /* fallback for people who don't install perf ;-) */
  375. help = perf_tip(DOCDIR);
  376. if (help == NULL)
  377. help = "Cannot load tips.txt file, please install perf!";
  378. }
  379. switch (use_browser) {
  380. case 1:
  381. ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
  382. rep->min_percent,
  383. &session->header.env);
  384. /*
  385. * Usually "ret" is the last pressed key, and we only
  386. * care if the key notifies us to switch data file.
  387. */
  388. if (ret != K_SWITCH_INPUT_DATA)
  389. ret = 0;
  390. break;
  391. case 2:
  392. ret = report__gtk_browse_hists(rep, help);
  393. break;
  394. default:
  395. ret = perf_evlist__tty_browse_hists(evlist, rep, help);
  396. break;
  397. }
  398. return ret;
  399. }
  400. static int report__collapse_hists(struct report *rep)
  401. {
  402. struct ui_progress prog;
  403. struct perf_evsel *pos;
  404. int ret = 0;
  405. ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
  406. evlist__for_each_entry(rep->session->evlist, pos) {
  407. struct hists *hists = evsel__hists(pos);
  408. if (pos->idx == 0)
  409. hists->symbol_filter_str = rep->symbol_filter_str;
  410. hists->socket_filter = rep->socket_filter;
  411. ret = hists__collapse_resort(hists, &prog);
  412. if (ret < 0)
  413. break;
  414. /* Non-group events are considered as leader */
  415. if (symbol_conf.event_group &&
  416. !perf_evsel__is_group_leader(pos)) {
  417. struct hists *leader_hists = evsel__hists(pos->leader);
  418. hists__match(leader_hists, hists);
  419. hists__link(leader_hists, hists);
  420. }
  421. }
  422. ui_progress__finish();
  423. return ret;
  424. }
  425. static void report__output_resort(struct report *rep)
  426. {
  427. struct ui_progress prog;
  428. struct perf_evsel *pos;
  429. ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
  430. evlist__for_each_entry(rep->session->evlist, pos)
  431. perf_evsel__output_resort(pos, &prog);
  432. ui_progress__finish();
  433. }
  434. static int __cmd_report(struct report *rep)
  435. {
  436. int ret;
  437. struct perf_session *session = rep->session;
  438. struct perf_evsel *pos;
  439. struct perf_data_file *file = session->file;
  440. signal(SIGINT, sig_handler);
  441. if (rep->cpu_list) {
  442. ret = perf_session__cpu_bitmap(session, rep->cpu_list,
  443. rep->cpu_bitmap);
  444. if (ret) {
  445. ui__error("failed to set cpu bitmap\n");
  446. return ret;
  447. }
  448. }
  449. if (rep->show_threads)
  450. perf_read_values_init(&rep->show_threads_values);
  451. ret = report__setup_sample_type(rep);
  452. if (ret) {
  453. /* report__setup_sample_type() already showed error message */
  454. return ret;
  455. }
  456. ret = perf_session__process_events(session);
  457. if (ret) {
  458. ui__error("failed to process sample\n");
  459. return ret;
  460. }
  461. report__warn_kptr_restrict(rep);
  462. evlist__for_each_entry(session->evlist, pos)
  463. rep->nr_entries += evsel__hists(pos)->nr_entries;
  464. if (use_browser == 0) {
  465. if (verbose > 3)
  466. perf_session__fprintf(session, stdout);
  467. if (verbose > 2)
  468. perf_session__fprintf_dsos(session, stdout);
  469. if (dump_trace) {
  470. perf_session__fprintf_nr_events(session, stdout);
  471. perf_evlist__fprintf_nr_events(session->evlist, stdout);
  472. return 0;
  473. }
  474. }
  475. ret = report__collapse_hists(rep);
  476. if (ret) {
  477. ui__error("failed to process hist entry\n");
  478. return ret;
  479. }
  480. if (session_done())
  481. return 0;
  482. /*
  483. * recalculate number of entries after collapsing since it
  484. * might be changed during the collapse phase.
  485. */
  486. rep->nr_entries = 0;
  487. evlist__for_each_entry(session->evlist, pos)
  488. rep->nr_entries += evsel__hists(pos)->nr_entries;
  489. if (rep->nr_entries == 0) {
  490. ui__error("The %s file has no samples!\n", file->path);
  491. return 0;
  492. }
  493. report__output_resort(rep);
  494. return report__browse_hists(rep);
  495. }
  496. static int
  497. report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  498. {
  499. struct callchain_param *callchain = opt->value;
  500. callchain->enabled = !unset;
  501. /*
  502. * --no-call-graph
  503. */
  504. if (unset) {
  505. symbol_conf.use_callchain = false;
  506. callchain->mode = CHAIN_NONE;
  507. return 0;
  508. }
  509. return parse_callchain_report_opt(arg);
  510. }
  511. int
  512. report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
  513. const char *arg, int unset __maybe_unused)
  514. {
  515. if (arg) {
  516. int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
  517. if (err) {
  518. char buf[BUFSIZ];
  519. regerror(err, &ignore_callees_regex, buf, sizeof(buf));
  520. pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
  521. return -1;
  522. }
  523. have_ignore_callees = 1;
  524. }
  525. return 0;
  526. }
  527. static int
  528. parse_branch_mode(const struct option *opt __maybe_unused,
  529. const char *str __maybe_unused, int unset)
  530. {
  531. int *branch_mode = opt->value;
  532. *branch_mode = !unset;
  533. return 0;
  534. }
  535. static int
  536. parse_percent_limit(const struct option *opt, const char *str,
  537. int unset __maybe_unused)
  538. {
  539. struct report *rep = opt->value;
  540. double pcnt = strtof(str, NULL);
  541. rep->min_percent = pcnt;
  542. callchain_param.min_percent = pcnt;
  543. return 0;
  544. }
  545. #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
  546. const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
  547. CALLCHAIN_REPORT_HELP
  548. "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
  549. int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
  550. {
  551. struct perf_session *session;
  552. struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
  553. struct stat st;
  554. bool has_br_stack = false;
  555. int branch_mode = -1;
  556. bool branch_call_mode = false;
  557. char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
  558. const char * const report_usage[] = {
  559. "perf report [<options>]",
  560. NULL
  561. };
  562. struct report report = {
  563. .tool = {
  564. .sample = process_sample_event,
  565. .mmap = perf_event__process_mmap,
  566. .mmap2 = perf_event__process_mmap2,
  567. .comm = perf_event__process_comm,
  568. .exit = perf_event__process_exit,
  569. .fork = perf_event__process_fork,
  570. .lost = perf_event__process_lost,
  571. .read = process_read_event,
  572. .attr = perf_event__process_attr,
  573. .tracing_data = perf_event__process_tracing_data,
  574. .build_id = perf_event__process_build_id,
  575. .id_index = perf_event__process_id_index,
  576. .auxtrace_info = perf_event__process_auxtrace_info,
  577. .auxtrace = perf_event__process_auxtrace,
  578. .ordered_events = true,
  579. .ordering_requires_timestamps = true,
  580. },
  581. .max_stack = PERF_MAX_STACK_DEPTH,
  582. .pretty_printing_style = "normal",
  583. .socket_filter = -1,
  584. };
  585. const struct option options[] = {
  586. OPT_STRING('i', "input", &input_name, "file",
  587. "input file name"),
  588. OPT_INCR('v', "verbose", &verbose,
  589. "be more verbose (show symbol address, etc)"),
  590. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  591. "dump raw trace in ASCII"),
  592. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  593. "file", "vmlinux pathname"),
  594. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  595. "file", "kallsyms pathname"),
  596. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  597. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  598. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  599. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  600. "Show a column with the number of samples"),
  601. OPT_BOOLEAN('T', "threads", &report.show_threads,
  602. "Show per-thread event counters"),
  603. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  604. "pretty printing style key: normal raw"),
  605. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  606. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  607. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  608. "Use the stdio interface"),
  609. OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
  610. OPT_BOOLEAN(0, "header-only", &report.header_only,
  611. "Show only data header."),
  612. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  613. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  614. " Please refer the man page for the complete list."),
  615. OPT_STRING('F', "fields", &field_order, "key[,keys...]",
  616. "output field(s): overhead, period, sample plus all of sort keys"),
  617. OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
  618. "Show sample percentage for different cpu modes"),
  619. OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  620. "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
  621. OPT_STRING('p', "parent", &parent_pattern, "regex",
  622. "regex filter to identify parent, see: '--sort parent'"),
  623. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  624. "Only display entries with parent-match"),
  625. OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
  626. "print_type,threshold[,print_limit],order,sort_key[,branch],value",
  627. report_callchain_help, &report_parse_callchain_opt,
  628. callchain_default_opt),
  629. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  630. "Accumulate callchains of children and show total overhead as well"),
  631. OPT_INTEGER(0, "max-stack", &report.max_stack,
  632. "Set the maximum stack depth when parsing the callchain, "
  633. "anything beyond the specified depth will be ignored. "
  634. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  635. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  636. "alias for inverted call graph"),
  637. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  638. "ignore callees of these functions in call graphs",
  639. report_parse_ignore_callees_opt),
  640. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  641. "only consider symbols in these dsos"),
  642. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  643. "only consider symbols in these comms"),
  644. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  645. "only consider symbols in these pids"),
  646. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  647. "only consider symbols in these tids"),
  648. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  649. "only consider these symbols"),
  650. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  651. "only show symbols that (partially) match with this filter"),
  652. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  653. "width[,width...]",
  654. "don't try to adjust column width, use these fixed values"),
  655. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  656. "separator for columns, no spaces will be added between "
  657. "columns '.' is reserved."),
  658. OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
  659. "Only display entries resolved to a symbol"),
  660. OPT_CALLBACK(0, "symfs", NULL, "directory",
  661. "Look for files with symbols relative to this directory",
  662. symbol__config_symfs),
  663. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  664. "list of cpus to profile"),
  665. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  666. "Display extended information about perf.data file"),
  667. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  668. "Interleave source code with assembly code (default)"),
  669. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  670. "Display raw encoding of assembly instructions (default)"),
  671. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  672. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  673. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  674. "Show a column with the sum of periods"),
  675. OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
  676. "Show event group information together"),
  677. OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
  678. "use branch records for per branch histogram filling",
  679. parse_branch_mode),
  680. OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
  681. "add last branch records to call history"),
  682. OPT_STRING(0, "objdump", &objdump_path, "path",
  683. "objdump binary to use for disassembly and annotations"),
  684. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  685. "Disable symbol demangling"),
  686. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  687. "Enable kernel symbol demangling"),
  688. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  689. OPT_CALLBACK(0, "percent-limit", &report, "percent",
  690. "Don't show entries under that percent", parse_percent_limit),
  691. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  692. "how to display percentage of filtered entries", parse_filter_percentage),
  693. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  694. "Instruction Tracing options",
  695. itrace_parse_synth_opts),
  696. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  697. "Show full source file name path for source lines"),
  698. OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
  699. "Show callgraph from reference event"),
  700. OPT_INTEGER(0, "socket-filter", &report.socket_filter,
  701. "only show processor socket that match with this filter"),
  702. OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
  703. "Show raw trace event output (do not use print fmt or plugins)"),
  704. OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
  705. "Show entries in a hierarchy"),
  706. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  707. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  708. stdio__config_color, "always"),
  709. OPT_END()
  710. };
  711. struct perf_data_file file = {
  712. .mode = PERF_DATA_MODE_READ,
  713. };
  714. int ret = hists__init();
  715. if (ret < 0)
  716. return ret;
  717. perf_config(report__config, &report);
  718. argc = parse_options(argc, argv, options, report_usage, 0);
  719. if (argc) {
  720. /*
  721. * Special case: if there's an argument left then assume that
  722. * it's a symbol filter:
  723. */
  724. if (argc > 1)
  725. usage_with_options(report_usage, options);
  726. report.symbol_filter_str = argv[0];
  727. }
  728. if (symbol_conf.vmlinux_name &&
  729. access(symbol_conf.vmlinux_name, R_OK)) {
  730. pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
  731. return -EINVAL;
  732. }
  733. if (symbol_conf.kallsyms_name &&
  734. access(symbol_conf.kallsyms_name, R_OK)) {
  735. pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
  736. return -EINVAL;
  737. }
  738. if (report.use_stdio)
  739. use_browser = 0;
  740. else if (report.use_tui)
  741. use_browser = 1;
  742. else if (report.use_gtk)
  743. use_browser = 2;
  744. if (report.inverted_callchain)
  745. callchain_param.order = ORDER_CALLER;
  746. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  747. callchain_param.order = ORDER_CALLER;
  748. if (itrace_synth_opts.callchain &&
  749. (int)itrace_synth_opts.callchain_sz > report.max_stack)
  750. report.max_stack = itrace_synth_opts.callchain_sz;
  751. if (!input_name || !strlen(input_name)) {
  752. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  753. input_name = "-";
  754. else
  755. input_name = "perf.data";
  756. }
  757. file.path = input_name;
  758. file.force = symbol_conf.force;
  759. repeat:
  760. session = perf_session__new(&file, false, &report.tool);
  761. if (session == NULL)
  762. return -1;
  763. if (report.queue_size) {
  764. ordered_events__set_alloc_size(&session->ordered_events,
  765. report.queue_size);
  766. }
  767. session->itrace_synth_opts = &itrace_synth_opts;
  768. report.session = session;
  769. has_br_stack = perf_header__has_feat(&session->header,
  770. HEADER_BRANCH_STACK);
  771. if (itrace_synth_opts.last_branch)
  772. has_br_stack = true;
  773. /*
  774. * Branch mode is a tristate:
  775. * -1 means default, so decide based on the file having branch data.
  776. * 0/1 means the user chose a mode.
  777. */
  778. if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
  779. !branch_call_mode) {
  780. sort__mode = SORT_MODE__BRANCH;
  781. symbol_conf.cumulate_callchain = false;
  782. }
  783. if (branch_call_mode) {
  784. callchain_param.key = CCKEY_ADDRESS;
  785. callchain_param.branch_callstack = 1;
  786. symbol_conf.use_callchain = true;
  787. callchain_register_param(&callchain_param);
  788. if (sort_order == NULL)
  789. sort_order = "srcline,symbol,dso";
  790. }
  791. if (report.mem_mode) {
  792. if (sort__mode == SORT_MODE__BRANCH) {
  793. pr_err("branch and mem mode incompatible\n");
  794. goto error;
  795. }
  796. sort__mode = SORT_MODE__MEMORY;
  797. symbol_conf.cumulate_callchain = false;
  798. }
  799. if (symbol_conf.report_hierarchy) {
  800. /* disable incompatible options */
  801. symbol_conf.event_group = false;
  802. symbol_conf.cumulate_callchain = false;
  803. if (field_order) {
  804. pr_err("Error: --hierarchy and --fields options cannot be used together\n");
  805. parse_options_usage(report_usage, options, "F", 1);
  806. parse_options_usage(NULL, options, "hierarchy", 0);
  807. goto error;
  808. }
  809. perf_hpp_list.need_collapse = true;
  810. }
  811. /* Force tty output for header output and per-thread stat. */
  812. if (report.header || report.header_only || report.show_threads)
  813. use_browser = 0;
  814. if (strcmp(input_name, "-") != 0)
  815. setup_browser(true);
  816. else
  817. use_browser = 0;
  818. if (setup_sorting(session->evlist) < 0) {
  819. if (sort_order)
  820. parse_options_usage(report_usage, options, "s", 1);
  821. if (field_order)
  822. parse_options_usage(sort_order ? NULL : report_usage,
  823. options, "F", 1);
  824. goto error;
  825. }
  826. if (report.header || report.header_only) {
  827. perf_session__fprintf_info(session, stdout,
  828. report.show_full_info);
  829. if (report.header_only) {
  830. ret = 0;
  831. goto error;
  832. }
  833. } else if (use_browser == 0) {
  834. fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
  835. stdout);
  836. }
  837. /*
  838. * Only in the TUI browser we are doing integrated annotation,
  839. * so don't allocate extra space that won't be used in the stdio
  840. * implementation.
  841. */
  842. if (ui__has_annotation()) {
  843. symbol_conf.priv_size = sizeof(struct annotation);
  844. machines__set_symbol_filter(&session->machines,
  845. symbol__annotate_init);
  846. /*
  847. * For searching by name on the "Browse map details".
  848. * providing it only in verbose mode not to bloat too
  849. * much struct symbol.
  850. */
  851. if (verbose) {
  852. /*
  853. * XXX: Need to provide a less kludgy way to ask for
  854. * more space per symbol, the u32 is for the index on
  855. * the ui browser.
  856. * See symbol__browser_index.
  857. */
  858. symbol_conf.priv_size += sizeof(u32);
  859. symbol_conf.sort_by_name = true;
  860. }
  861. }
  862. if (symbol__init(&session->header.env) < 0)
  863. goto error;
  864. sort__setup_elide(stdout);
  865. ret = __cmd_report(&report);
  866. if (ret == K_SWITCH_INPUT_DATA) {
  867. perf_session__delete(session);
  868. goto repeat;
  869. } else
  870. ret = 0;
  871. error:
  872. perf_session__delete(session);
  873. return ret;
  874. }