builtin-report.c 15 KB

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