builtin-report.c 14 KB

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