builtin-report.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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/annotate.h"
  11. #include "util/color.h"
  12. #include <linux/list.h>
  13. #include "util/cache.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 "util/parse-options.h"
  27. #include "util/parse-events.h"
  28. #include "util/thread.h"
  29. #include "util/sort.h"
  30. #include "util/hist.h"
  31. #include <linux/bitmap.h>
  32. struct perf_report {
  33. struct perf_tool tool;
  34. struct perf_session *session;
  35. char const *input_name;
  36. bool force, use_tui, use_stdio;
  37. bool hide_unresolved;
  38. bool dont_use_callchains;
  39. bool show_full_info;
  40. bool show_threads;
  41. bool inverted_callchain;
  42. struct perf_read_values show_threads_values;
  43. const char *pretty_printing_style;
  44. symbol_filter_t annotate_init;
  45. const char *cpu_list;
  46. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  47. };
  48. static int perf_evsel__add_hist_entry(struct perf_evsel *evsel,
  49. struct addr_location *al,
  50. struct perf_sample *sample,
  51. struct machine *machine)
  52. {
  53. struct symbol *parent = NULL;
  54. int err = 0;
  55. struct hist_entry *he;
  56. if ((sort__has_parent || symbol_conf.use_callchain) && sample->callchain) {
  57. err = machine__resolve_callchain(machine, evsel, al->thread,
  58. sample->callchain, &parent);
  59. if (err)
  60. return err;
  61. }
  62. he = __hists__add_entry(&evsel->hists, al, parent, sample->period);
  63. if (he == NULL)
  64. return -ENOMEM;
  65. if (symbol_conf.use_callchain) {
  66. err = callchain_append(he->callchain,
  67. &evsel->hists.callchain_cursor,
  68. sample->period);
  69. if (err)
  70. return err;
  71. }
  72. /*
  73. * Only in the newt browser we are doing integrated annotation,
  74. * so we don't allocated the extra space needed because the stdio
  75. * code will not use it.
  76. */
  77. if (al->sym != NULL && use_browser > 0) {
  78. struct annotation *notes = symbol__annotation(he->ms.sym);
  79. assert(evsel != NULL);
  80. err = -ENOMEM;
  81. if (notes->src == NULL && symbol__alloc_hist(he->ms.sym) < 0)
  82. goto out;
  83. err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
  84. }
  85. evsel->hists.stats.total_period += sample->period;
  86. hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
  87. out:
  88. return err;
  89. }
  90. static int process_sample_event(struct perf_tool *tool,
  91. union perf_event *event,
  92. struct perf_sample *sample,
  93. struct perf_evsel *evsel,
  94. struct machine *machine)
  95. {
  96. struct perf_report *rep = container_of(tool, struct perf_report, tool);
  97. struct addr_location al;
  98. if (perf_event__preprocess_sample(event, machine, &al, sample,
  99. rep->annotate_init) < 0) {
  100. fprintf(stderr, "problem processing %d event, skipping it.\n",
  101. event->header.type);
  102. return -1;
  103. }
  104. if (al.filtered || (rep->hide_unresolved && al.sym == NULL))
  105. return 0;
  106. if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
  107. return 0;
  108. if (al.map != NULL)
  109. al.map->dso->hit = 1;
  110. if (perf_evsel__add_hist_entry(evsel, &al, sample, machine)) {
  111. pr_debug("problem incrementing symbol period, skipping event\n");
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. static int process_read_event(struct perf_tool *tool,
  117. union perf_event *event,
  118. struct perf_sample *sample __used,
  119. struct perf_evsel *evsel,
  120. struct machine *machine __used)
  121. {
  122. struct perf_report *rep = container_of(tool, struct perf_report, tool);
  123. if (rep->show_threads) {
  124. const char *name = evsel ? event_name(evsel) : "unknown";
  125. perf_read_values_add_value(&rep->show_threads_values,
  126. event->read.pid, event->read.tid,
  127. event->read.id,
  128. name,
  129. event->read.value);
  130. }
  131. dump_printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid,
  132. evsel ? event_name(evsel) : "FAIL",
  133. event->read.value);
  134. return 0;
  135. }
  136. static int perf_report__setup_sample_type(struct perf_report *rep)
  137. {
  138. struct perf_session *self = rep->session;
  139. if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
  140. if (sort__has_parent) {
  141. ui__warning("Selected --sort parent, but no "
  142. "callchain data. Did you call "
  143. "'perf record' without -g?\n");
  144. return -EINVAL;
  145. }
  146. if (symbol_conf.use_callchain) {
  147. ui__warning("Selected -g but no callchain data. Did "
  148. "you call 'perf record' without -g?\n");
  149. return -1;
  150. }
  151. } else if (!rep->dont_use_callchains &&
  152. callchain_param.mode != CHAIN_NONE &&
  153. !symbol_conf.use_callchain) {
  154. symbol_conf.use_callchain = true;
  155. if (callchain_register_param(&callchain_param) < 0) {
  156. ui__warning("Can't register callchain "
  157. "params.\n");
  158. return -EINVAL;
  159. }
  160. }
  161. return 0;
  162. }
  163. extern volatile int session_done;
  164. static void sig_handler(int sig __used)
  165. {
  166. session_done = 1;
  167. }
  168. static size_t hists__fprintf_nr_sample_events(struct hists *self,
  169. const char *evname, FILE *fp)
  170. {
  171. size_t ret;
  172. char unit;
  173. unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
  174. nr_events = convert_unit(nr_events, &unit);
  175. ret = fprintf(fp, "# Events: %lu%c", nr_events, unit);
  176. if (evname != NULL)
  177. ret += fprintf(fp, " %s", evname);
  178. return ret + fprintf(fp, "\n#\n");
  179. }
  180. static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
  181. struct perf_report *rep,
  182. const char *help)
  183. {
  184. struct perf_evsel *pos;
  185. list_for_each_entry(pos, &evlist->entries, node) {
  186. struct hists *hists = &pos->hists;
  187. const char *evname = event_name(pos);
  188. hists__fprintf_nr_sample_events(hists, evname, stdout);
  189. hists__fprintf(hists, NULL, false, true, 0, 0, stdout);
  190. fprintf(stdout, "\n\n");
  191. }
  192. if (sort_order == default_sort_order &&
  193. parent_pattern == default_parent_pattern) {
  194. fprintf(stdout, "#\n# (%s)\n#\n", help);
  195. if (rep->show_threads) {
  196. bool style = !strcmp(rep->pretty_printing_style, "raw");
  197. perf_read_values_display(stdout, &rep->show_threads_values,
  198. style);
  199. perf_read_values_destroy(&rep->show_threads_values);
  200. }
  201. }
  202. return 0;
  203. }
  204. static int __cmd_report(struct perf_report *rep)
  205. {
  206. int ret = -EINVAL;
  207. u64 nr_samples;
  208. struct perf_session *session;
  209. struct perf_evsel *pos;
  210. struct map *kernel_map;
  211. struct kmap *kernel_kmap;
  212. const char *help = "For a higher level overview, try: perf report --sort comm,dso";
  213. signal(SIGINT, sig_handler);
  214. session = perf_session__new(rep->input_name, O_RDONLY,
  215. rep->force, false, &rep->tool);
  216. if (session == NULL)
  217. return -ENOMEM;
  218. rep->session = session;
  219. if (rep->cpu_list) {
  220. ret = perf_session__cpu_bitmap(session, rep->cpu_list,
  221. rep->cpu_bitmap);
  222. if (ret)
  223. goto out_delete;
  224. }
  225. if (use_browser <= 0)
  226. perf_session__fprintf_info(session, stdout, rep->show_full_info);
  227. if (rep->show_threads)
  228. perf_read_values_init(&rep->show_threads_values);
  229. ret = perf_report__setup_sample_type(rep);
  230. if (ret)
  231. goto out_delete;
  232. ret = perf_session__process_events(session, &rep->tool);
  233. if (ret)
  234. goto out_delete;
  235. kernel_map = session->host_machine.vmlinux_maps[MAP__FUNCTION];
  236. kernel_kmap = map__kmap(kernel_map);
  237. if (kernel_map == NULL ||
  238. (kernel_map->dso->hit &&
  239. (kernel_kmap->ref_reloc_sym == NULL ||
  240. kernel_kmap->ref_reloc_sym->addr == 0))) {
  241. const struct dso *kdso = kernel_map->dso;
  242. ui__warning(
  243. "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
  244. "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
  245. "Samples in kernel modules can't be resolved as well.\n\n",
  246. RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION]) ?
  247. "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
  248. "can't be resolved." :
  249. "If some relocation was applied (e.g. kexec) symbols may be misresolved.");
  250. }
  251. if (dump_trace) {
  252. perf_session__fprintf_nr_events(session, stdout);
  253. goto out_delete;
  254. }
  255. if (verbose > 3)
  256. perf_session__fprintf(session, stdout);
  257. if (verbose > 2)
  258. perf_session__fprintf_dsos(session, stdout);
  259. nr_samples = 0;
  260. list_for_each_entry(pos, &session->evlist->entries, node) {
  261. struct hists *hists = &pos->hists;
  262. hists__collapse_resort(hists);
  263. hists__output_resort(hists);
  264. nr_samples += hists->stats.nr_events[PERF_RECORD_SAMPLE];
  265. }
  266. if (nr_samples == 0) {
  267. ui__warning("The %s file has no samples!\n", session->filename);
  268. goto out_delete;
  269. }
  270. if (use_browser > 0) {
  271. perf_evlist__tui_browse_hists(session->evlist, help,
  272. NULL, NULL, 0);
  273. } else
  274. perf_evlist__tty_browse_hists(session->evlist, rep, help);
  275. out_delete:
  276. /*
  277. * Speed up the exit process, for large files this can
  278. * take quite a while.
  279. *
  280. * XXX Enable this when using valgrind or if we ever
  281. * librarize this command.
  282. *
  283. * Also experiment with obstacks to see how much speed
  284. * up we'll get here.
  285. *
  286. * perf_session__delete(session);
  287. */
  288. return ret;
  289. }
  290. static int
  291. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  292. {
  293. struct perf_report *rep = (struct perf_report *)opt->value;
  294. char *tok, *tok2;
  295. char *endptr;
  296. /*
  297. * --no-call-graph
  298. */
  299. if (unset) {
  300. rep->dont_use_callchains = true;
  301. return 0;
  302. }
  303. symbol_conf.use_callchain = true;
  304. if (!arg)
  305. return 0;
  306. tok = strtok((char *)arg, ",");
  307. if (!tok)
  308. return -1;
  309. /* get the output mode */
  310. if (!strncmp(tok, "graph", strlen(arg)))
  311. callchain_param.mode = CHAIN_GRAPH_ABS;
  312. else if (!strncmp(tok, "flat", strlen(arg)))
  313. callchain_param.mode = CHAIN_FLAT;
  314. else if (!strncmp(tok, "fractal", strlen(arg)))
  315. callchain_param.mode = CHAIN_GRAPH_REL;
  316. else if (!strncmp(tok, "none", strlen(arg))) {
  317. callchain_param.mode = CHAIN_NONE;
  318. symbol_conf.use_callchain = false;
  319. return 0;
  320. }
  321. else
  322. return -1;
  323. /* get the min percentage */
  324. tok = strtok(NULL, ",");
  325. if (!tok)
  326. goto setup;
  327. callchain_param.min_percent = strtod(tok, &endptr);
  328. if (tok == endptr)
  329. return -1;
  330. /* get the print limit */
  331. tok2 = strtok(NULL, ",");
  332. if (!tok2)
  333. goto setup;
  334. if (tok2[0] != 'c') {
  335. callchain_param.print_limit = strtoul(tok2, &endptr, 0);
  336. tok2 = strtok(NULL, ",");
  337. if (!tok2)
  338. goto setup;
  339. }
  340. /* get the call chain order */
  341. if (!strcmp(tok2, "caller"))
  342. callchain_param.order = ORDER_CALLER;
  343. else if (!strcmp(tok2, "callee"))
  344. callchain_param.order = ORDER_CALLEE;
  345. else
  346. return -1;
  347. setup:
  348. if (callchain_register_param(&callchain_param) < 0) {
  349. fprintf(stderr, "Can't register callchain params\n");
  350. return -1;
  351. }
  352. return 0;
  353. }
  354. int cmd_report(int argc, const char **argv, const char *prefix __used)
  355. {
  356. struct stat st;
  357. char callchain_default_opt[] = "fractal,0.5,callee";
  358. const char * const report_usage[] = {
  359. "perf report [<options>]",
  360. NULL
  361. };
  362. struct perf_report report = {
  363. .tool = {
  364. .sample = process_sample_event,
  365. .mmap = perf_event__process_mmap,
  366. .comm = perf_event__process_comm,
  367. .exit = perf_event__process_task,
  368. .fork = perf_event__process_task,
  369. .lost = perf_event__process_lost,
  370. .read = process_read_event,
  371. .attr = perf_event__process_attr,
  372. .event_type = perf_event__process_event_type,
  373. .tracing_data = perf_event__process_tracing_data,
  374. .build_id = perf_event__process_build_id,
  375. .ordered_samples = true,
  376. .ordering_requires_timestamps = true,
  377. },
  378. .pretty_printing_style = "normal",
  379. };
  380. const struct option options[] = {
  381. OPT_STRING('i', "input", &report.input_name, "file",
  382. "input file name"),
  383. OPT_INCR('v', "verbose", &verbose,
  384. "be more verbose (show symbol address, etc)"),
  385. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  386. "dump raw trace in ASCII"),
  387. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  388. "file", "vmlinux pathname"),
  389. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  390. "file", "kallsyms pathname"),
  391. OPT_BOOLEAN('f', "force", &report.force, "don't complain, do it"),
  392. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  393. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  394. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  395. "Show a column with the number of samples"),
  396. OPT_BOOLEAN('T', "threads", &report.show_threads,
  397. "Show per-thread event counters"),
  398. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  399. "pretty printing style key: normal raw"),
  400. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  401. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  402. "Use the stdio interface"),
  403. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  404. "sort by key(s): pid, comm, dso, symbol, parent"),
  405. OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  406. "Show sample percentage for different cpu modes"),
  407. OPT_STRING('p', "parent", &parent_pattern, "regex",
  408. "regex filter to identify parent, see: '--sort parent'"),
  409. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  410. "Only display entries with parent-match"),
  411. OPT_CALLBACK_DEFAULT('g', "call-graph", &report, "output_type,min_percent[,print_limit],call_order",
  412. "Display callchains using output_type (graph, flat, fractal, or none) , min percent threshold, optional print limit and callchain order. "
  413. "Default: fractal,0.5,callee", &parse_callchain_opt, callchain_default_opt),
  414. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  415. "alias for inverted call graph"),
  416. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  417. "only consider symbols in these dsos"),
  418. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  419. "only consider symbols in these comms"),
  420. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  421. "only consider these symbols"),
  422. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  423. "width[,width...]",
  424. "don't try to adjust column width, use these fixed values"),
  425. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  426. "separator for columns, no spaces will be added between "
  427. "columns '.' is reserved."),
  428. OPT_BOOLEAN('U', "hide-unresolved", &report.hide_unresolved,
  429. "Only display entries resolved to a symbol"),
  430. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  431. "Look for files with symbols relative to this directory"),
  432. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  433. "list of cpus to profile"),
  434. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  435. "Display extended information about perf.data file"),
  436. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  437. "Interleave source code with assembly code (default)"),
  438. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  439. "Display raw encoding of assembly instructions (default)"),
  440. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  441. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  442. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  443. "Show a column with the sum of periods"),
  444. OPT_END()
  445. };
  446. argc = parse_options(argc, argv, options, report_usage, 0);
  447. if (report.use_stdio)
  448. use_browser = 0;
  449. else if (report.use_tui)
  450. use_browser = 1;
  451. if (report.inverted_callchain)
  452. callchain_param.order = ORDER_CALLER;
  453. if (!report.input_name || !strlen(report.input_name)) {
  454. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  455. report.input_name = "-";
  456. else
  457. report.input_name = "perf.data";
  458. }
  459. if (strcmp(report.input_name, "-") != 0)
  460. setup_browser(true);
  461. else
  462. use_browser = 0;
  463. /*
  464. * Only in the newt browser we are doing integrated annotation,
  465. * so don't allocate extra space that won't be used in the stdio
  466. * implementation.
  467. */
  468. if (use_browser > 0) {
  469. symbol_conf.priv_size = sizeof(struct annotation);
  470. report.annotate_init = symbol__annotate_init;
  471. /*
  472. * For searching by name on the "Browse map details".
  473. * providing it only in verbose mode not to bloat too
  474. * much struct symbol.
  475. */
  476. if (verbose) {
  477. /*
  478. * XXX: Need to provide a less kludgy way to ask for
  479. * more space per symbol, the u32 is for the index on
  480. * the ui browser.
  481. * See symbol__browser_index.
  482. */
  483. symbol_conf.priv_size += sizeof(u32);
  484. symbol_conf.sort_by_name = true;
  485. }
  486. }
  487. if (symbol__init() < 0)
  488. return -1;
  489. setup_sorting(report_usage, options);
  490. if (parent_pattern != default_parent_pattern) {
  491. if (sort_dimension__add("parent") < 0)
  492. return -1;
  493. /*
  494. * Only show the parent fields if we explicitly
  495. * sort that way. If we only use parent machinery
  496. * for filtering, we don't want it.
  497. */
  498. if (!strstr(sort_order, "parent"))
  499. sort_parent.elide = 1;
  500. } else
  501. symbol_conf.exclude_other = false;
  502. /*
  503. * Any (unrecognized) arguments left?
  504. */
  505. if (argc)
  506. usage_with_options(report_usage, options);
  507. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
  508. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
  509. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
  510. return __cmd_report(&report);
  511. }