builtin-report.c 15 KB

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