builtin-report.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 char default_pretty_printing_style[] = "normal";
  34. static char *pretty_printing_style = default_pretty_printing_style;
  35. static char callchain_default_opt[] = "fractal,0.5";
  36. static struct event_stat_id *get_stats(struct perf_session *self,
  37. u64 event_stream, u32 type, u64 config)
  38. {
  39. struct rb_node **p = &self->stats_by_id.rb_node;
  40. struct rb_node *parent = NULL;
  41. struct event_stat_id *iter, *new;
  42. while (*p != NULL) {
  43. parent = *p;
  44. iter = rb_entry(parent, struct event_stat_id, rb_node);
  45. if (iter->config == config)
  46. return iter;
  47. if (config > iter->config)
  48. p = &(*p)->rb_right;
  49. else
  50. p = &(*p)->rb_left;
  51. }
  52. new = malloc(sizeof(struct event_stat_id));
  53. if (new == NULL)
  54. return NULL;
  55. memset(new, 0, sizeof(struct event_stat_id));
  56. new->event_stream = event_stream;
  57. new->config = config;
  58. new->type = type;
  59. rb_link_node(&new->rb_node, parent, p);
  60. rb_insert_color(&new->rb_node, &self->stats_by_id);
  61. return new;
  62. }
  63. static int perf_session__add_hist_entry(struct perf_session *self,
  64. struct addr_location *al,
  65. struct sample_data *data)
  66. {
  67. struct map_symbol *syms = NULL;
  68. struct symbol *parent = NULL;
  69. int err = -ENOMEM;
  70. struct hist_entry *he;
  71. struct event_stat_id *stats;
  72. struct perf_event_attr *attr;
  73. if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain) {
  74. syms = perf_session__resolve_callchain(self, al->thread,
  75. data->callchain, &parent);
  76. if (syms == NULL)
  77. return -ENOMEM;
  78. }
  79. attr = perf_header__find_attr(data->id, &self->header);
  80. if (attr)
  81. stats = get_stats(self, data->id, attr->type, attr->config);
  82. else
  83. stats = get_stats(self, data->id, 0, 0);
  84. if (stats == NULL)
  85. goto out_free_syms;
  86. he = __perf_session__add_hist_entry(&stats->hists, al, parent,
  87. 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);
  93. out_free_syms:
  94. free(syms);
  95. return err;
  96. }
  97. static int add_event_total(struct perf_session *session,
  98. struct sample_data *data,
  99. struct perf_event_attr *attr)
  100. {
  101. struct event_stat_id *stats;
  102. if (attr)
  103. stats = get_stats(session, data->id, attr->type, attr->config);
  104. else
  105. stats = get_stats(session, data->id, 0, 0);
  106. if (!stats)
  107. return -ENOMEM;
  108. stats->stats.total += data->period;
  109. session->events_stats.total += data->period;
  110. return 0;
  111. }
  112. static int process_sample_event(event_t *event, struct perf_session *session)
  113. {
  114. struct sample_data data = { .period = 1, };
  115. struct addr_location al;
  116. struct perf_event_attr *attr;
  117. event__parse_sample(event, session->sample_type, &data);
  118. dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
  119. data.pid, data.tid, data.ip, data.period);
  120. if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
  121. unsigned int i;
  122. dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
  123. if (!ip_callchain__valid(data.callchain, event)) {
  124. pr_debug("call-chain problem with event, "
  125. "skipping it.\n");
  126. return 0;
  127. }
  128. if (dump_trace) {
  129. for (i = 0; i < data.callchain->nr; i++)
  130. dump_printf("..... %2d: %016Lx\n",
  131. i, data.callchain->ips[i]);
  132. }
  133. }
  134. if (event__preprocess_sample(event, session, &al, 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 count, 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 count\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 __attribute__((__unused__)))
  211. {
  212. session_done = 1;
  213. }
  214. static int __cmd_report(void)
  215. {
  216. int ret = -EINVAL;
  217. struct perf_session *session;
  218. struct rb_node *next;
  219. const char *help = "For a higher level overview, try: perf report --sort comm,dso";
  220. signal(SIGINT, sig_handler);
  221. session = perf_session__new(input_name, O_RDONLY, force, false);
  222. if (session == NULL)
  223. return -ENOMEM;
  224. if (show_threads)
  225. perf_read_values_init(&show_threads_values);
  226. ret = perf_session__setup_sample_type(session);
  227. if (ret)
  228. goto out_delete;
  229. ret = perf_session__process_events(session, &event_ops);
  230. if (ret)
  231. goto out_delete;
  232. if (dump_trace) {
  233. event__print_totals();
  234. goto out_delete;
  235. }
  236. if (verbose > 3)
  237. perf_session__fprintf(session, stdout);
  238. if (verbose > 2)
  239. perf_session__fprintf_dsos(session, stdout);
  240. next = rb_first(&session->stats_by_id);
  241. while (next) {
  242. struct event_stat_id *stats;
  243. u64 nr_hists;
  244. stats = rb_entry(next, struct event_stat_id, rb_node);
  245. perf_session__collapse_resort(&stats->hists);
  246. nr_hists = perf_session__output_resort(&stats->hists,
  247. stats->stats.total);
  248. if (use_browser)
  249. perf_session__browse_hists(&stats->hists, nr_hists,
  250. stats->stats.total, help,
  251. input_name);
  252. else {
  253. if (rb_first(&session->stats_by_id) ==
  254. rb_last(&session->stats_by_id))
  255. fprintf(stdout, "# Samples: %Ld\n#\n",
  256. stats->stats.total);
  257. else
  258. fprintf(stdout, "# Samples: %Ld %s\n#\n",
  259. stats->stats.total,
  260. __event_name(stats->type, stats->config));
  261. perf_session__fprintf_hists(&stats->hists, NULL, false, stdout,
  262. stats->stats.total);
  263. fprintf(stdout, "\n\n");
  264. }
  265. next = rb_next(&stats->rb_node);
  266. }
  267. if (!use_browser && sort_order == default_sort_order &&
  268. parent_pattern == default_parent_pattern) {
  269. fprintf(stdout, "#\n# (%s)\n#\n", help);
  270. if (show_threads) {
  271. bool style = !strcmp(pretty_printing_style, "raw");
  272. perf_read_values_display(stdout, &show_threads_values,
  273. style);
  274. perf_read_values_destroy(&show_threads_values);
  275. }
  276. }
  277. out_delete:
  278. perf_session__delete(session);
  279. return ret;
  280. }
  281. static int
  282. parse_callchain_opt(const struct option *opt __used, const char *arg,
  283. int unset)
  284. {
  285. char *tok;
  286. char *endptr;
  287. /*
  288. * --no-call-graph
  289. */
  290. if (unset) {
  291. dont_use_callchains = true;
  292. return 0;
  293. }
  294. symbol_conf.use_callchain = true;
  295. if (!arg)
  296. return 0;
  297. tok = strtok((char *)arg, ",");
  298. if (!tok)
  299. return -1;
  300. /* get the output mode */
  301. if (!strncmp(tok, "graph", strlen(arg)))
  302. callchain_param.mode = CHAIN_GRAPH_ABS;
  303. else if (!strncmp(tok, "flat", strlen(arg)))
  304. callchain_param.mode = CHAIN_FLAT;
  305. else if (!strncmp(tok, "fractal", strlen(arg)))
  306. callchain_param.mode = CHAIN_GRAPH_REL;
  307. else if (!strncmp(tok, "none", strlen(arg))) {
  308. callchain_param.mode = CHAIN_NONE;
  309. symbol_conf.use_callchain = false;
  310. return 0;
  311. }
  312. else
  313. return -1;
  314. /* get the min percentage */
  315. tok = strtok(NULL, ",");
  316. if (!tok)
  317. goto setup;
  318. callchain_param.min_percent = strtod(tok, &endptr);
  319. if (tok == endptr)
  320. return -1;
  321. setup:
  322. if (register_callchain_param(&callchain_param) < 0) {
  323. fprintf(stderr, "Can't register callchain params\n");
  324. return -1;
  325. }
  326. return 0;
  327. }
  328. static const char * const report_usage[] = {
  329. "perf report [<options>] <command>",
  330. NULL
  331. };
  332. static const struct option options[] = {
  333. OPT_STRING('i', "input", &input_name, "file",
  334. "input file name"),
  335. OPT_INCR('v', "verbose", &verbose,
  336. "be more verbose (show symbol address, etc)"),
  337. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  338. "dump raw trace in ASCII"),
  339. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  340. "file", "vmlinux pathname"),
  341. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  342. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  343. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  344. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  345. "Show a column with the number of samples"),
  346. OPT_BOOLEAN('T', "threads", &show_threads,
  347. "Show per-thread event counters"),
  348. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  349. "pretty printing style key: normal raw"),
  350. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  351. "sort by key(s): pid, comm, dso, symbol, parent"),
  352. OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
  353. "Don't shorten the pathnames taking into account the cwd"),
  354. OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  355. "Show sample percentage for different cpu modes"),
  356. OPT_STRING('p', "parent", &parent_pattern, "regex",
  357. "regex filter to identify parent, see: '--sort parent'"),
  358. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  359. "Only display entries with parent-match"),
  360. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  361. "Display callchains using output_type (graph, flat, fractal, or none) and min percent threshold. "
  362. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  363. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  364. "only consider symbols in these dsos"),
  365. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  366. "only consider symbols in these comms"),
  367. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  368. "only consider these symbols"),
  369. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  370. "width[,width...]",
  371. "don't try to adjust column width, use these fixed values"),
  372. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  373. "separator for columns, no spaces will be added between "
  374. "columns '.' is reserved."),
  375. OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
  376. "Only display entries resolved to a symbol"),
  377. OPT_END()
  378. };
  379. int cmd_report(int argc, const char **argv, const char *prefix __used)
  380. {
  381. argc = parse_options(argc, argv, options, report_usage, 0);
  382. if (strcmp(input_name, "-") != 0)
  383. setup_browser();
  384. if (symbol__init() < 0)
  385. return -1;
  386. setup_sorting(report_usage, options);
  387. if (parent_pattern != default_parent_pattern) {
  388. if (sort_dimension__add("parent") < 0)
  389. return -1;
  390. sort_parent.elide = 1;
  391. } else
  392. symbol_conf.exclude_other = false;
  393. /*
  394. * Any (unrecognized) arguments left?
  395. */
  396. if (argc)
  397. usage_with_options(report_usage, options);
  398. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
  399. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
  400. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
  401. return __cmd_report();
  402. }