builtin-report.c 13 KB

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