builtin-report.c 15 KB

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