builtin-report.c 15 KB

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