builtin-report.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. bool hit;
  70. int err;
  71. struct hist_entry *he;
  72. struct event_stat_id *stats;
  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. stats = get_stats(self, data->id, attr->type, attr->config);
  83. else
  84. stats = get_stats(self, data->id, 0, 0);
  85. if (stats == NULL)
  86. return -ENOMEM;
  87. he = __perf_session__add_hist_entry(&stats->hists, al, parent,
  88. data->period, &hit);
  89. if (he == NULL)
  90. return -ENOMEM;
  91. if (hit)
  92. he->count += data->period;
  93. if (symbol_conf.use_callchain) {
  94. if (!hit)
  95. callchain_init(he->callchain);
  96. err = append_chain(he->callchain, data->callchain, syms);
  97. free(syms);
  98. if (err)
  99. return err;
  100. }
  101. return 0;
  102. }
  103. static int validate_chain(struct ip_callchain *chain, event_t *event)
  104. {
  105. unsigned int chain_size;
  106. chain_size = event->header.size;
  107. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  108. if (chain->nr*sizeof(u64) > chain_size)
  109. return -1;
  110. return 0;
  111. }
  112. static int add_event_total(struct perf_session *session,
  113. struct sample_data *data,
  114. struct perf_event_attr *attr)
  115. {
  116. struct event_stat_id *stats;
  117. if (attr)
  118. stats = get_stats(session, data->id, attr->type, attr->config);
  119. else
  120. stats = get_stats(session, data->id, 0, 0);
  121. if (!stats)
  122. return -ENOMEM;
  123. stats->stats.total += data->period;
  124. session->events_stats.total += data->period;
  125. return 0;
  126. }
  127. static int process_sample_event(event_t *event, struct perf_session *session)
  128. {
  129. struct sample_data data = { .period = 1, };
  130. struct addr_location al;
  131. struct perf_event_attr *attr;
  132. event__parse_sample(event, session->sample_type, &data);
  133. dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
  134. data.pid, data.tid, data.ip, data.period);
  135. if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
  136. unsigned int i;
  137. dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
  138. if (validate_chain(data.callchain, event) < 0) {
  139. pr_debug("call-chain problem with event, "
  140. "skipping it.\n");
  141. return 0;
  142. }
  143. if (dump_trace) {
  144. for (i = 0; i < data.callchain->nr; i++)
  145. dump_printf("..... %2d: %016Lx\n",
  146. i, data.callchain->ips[i]);
  147. }
  148. }
  149. if (event__preprocess_sample(event, session, &al, NULL) < 0) {
  150. fprintf(stderr, "problem processing %d event, skipping it.\n",
  151. event->header.type);
  152. return -1;
  153. }
  154. if (al.filtered || (hide_unresolved && al.sym == NULL))
  155. return 0;
  156. if (perf_session__add_hist_entry(session, &al, &data)) {
  157. pr_debug("problem incrementing symbol count, skipping event\n");
  158. return -1;
  159. }
  160. attr = perf_header__find_attr(data.id, &session->header);
  161. if (add_event_total(session, &data, attr)) {
  162. pr_debug("problem adding event count\n");
  163. return -1;
  164. }
  165. return 0;
  166. }
  167. static int process_read_event(event_t *event, struct perf_session *session __used)
  168. {
  169. struct perf_event_attr *attr;
  170. attr = perf_header__find_attr(event->read.id, &session->header);
  171. if (show_threads) {
  172. const char *name = attr ? __event_name(attr->type, attr->config)
  173. : "unknown";
  174. perf_read_values_add_value(&show_threads_values,
  175. event->read.pid, event->read.tid,
  176. event->read.id,
  177. name,
  178. event->read.value);
  179. }
  180. dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
  181. attr ? __event_name(attr->type, attr->config) : "FAIL",
  182. event->read.value);
  183. return 0;
  184. }
  185. static int perf_session__setup_sample_type(struct perf_session *self)
  186. {
  187. if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
  188. if (sort__has_parent) {
  189. fprintf(stderr, "selected --sort parent, but no"
  190. " callchain data. Did you call"
  191. " perf record without -g?\n");
  192. return -EINVAL;
  193. }
  194. if (symbol_conf.use_callchain) {
  195. fprintf(stderr, "selected -g but no callchain data."
  196. " Did you call perf record without"
  197. " -g?\n");
  198. return -1;
  199. }
  200. } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
  201. !symbol_conf.use_callchain) {
  202. symbol_conf.use_callchain = true;
  203. if (register_callchain_param(&callchain_param) < 0) {
  204. fprintf(stderr, "Can't register callchain"
  205. " params\n");
  206. return -EINVAL;
  207. }
  208. }
  209. return 0;
  210. }
  211. static struct perf_event_ops event_ops = {
  212. .sample = process_sample_event,
  213. .mmap = event__process_mmap,
  214. .comm = event__process_comm,
  215. .exit = event__process_task,
  216. .fork = event__process_task,
  217. .lost = event__process_lost,
  218. .read = process_read_event,
  219. .attr = event__process_attr,
  220. };
  221. extern volatile int session_done;
  222. static void sig_handler(int sig __attribute__((__unused__)))
  223. {
  224. session_done = 1;
  225. }
  226. static int __cmd_report(void)
  227. {
  228. int ret = -EINVAL;
  229. struct perf_session *session;
  230. struct rb_node *next;
  231. const char *help = "For a higher level overview, try: perf report --sort comm,dso";
  232. signal(SIGINT, sig_handler);
  233. session = perf_session__new(input_name, O_RDONLY, force);
  234. if (session == NULL)
  235. return -ENOMEM;
  236. if (show_threads)
  237. perf_read_values_init(&show_threads_values);
  238. ret = perf_session__setup_sample_type(session);
  239. if (ret)
  240. goto out_delete;
  241. ret = perf_session__process_events(session, &event_ops);
  242. if (ret)
  243. goto out_delete;
  244. if (dump_trace) {
  245. event__print_totals();
  246. goto out_delete;
  247. }
  248. if (verbose > 3)
  249. perf_session__fprintf(session, stdout);
  250. if (verbose > 2)
  251. dsos__fprintf(stdout);
  252. next = rb_first(&session->stats_by_id);
  253. while (next) {
  254. struct event_stat_id *stats;
  255. u64 nr_hists;
  256. stats = rb_entry(next, struct event_stat_id, rb_node);
  257. perf_session__collapse_resort(&stats->hists);
  258. nr_hists = perf_session__output_resort(&stats->hists,
  259. stats->stats.total);
  260. if (use_browser)
  261. perf_session__browse_hists(&stats->hists, nr_hists,
  262. stats->stats.total, help,
  263. input_name);
  264. else {
  265. if (rb_first(&session->stats_by_id) ==
  266. rb_last(&session->stats_by_id))
  267. fprintf(stdout, "# Samples: %Ld\n#\n",
  268. stats->stats.total);
  269. else
  270. fprintf(stdout, "# Samples: %Ld %s\n#\n",
  271. stats->stats.total,
  272. __event_name(stats->type, stats->config));
  273. perf_session__fprintf_hists(&stats->hists, NULL, false, stdout,
  274. stats->stats.total);
  275. fprintf(stdout, "\n\n");
  276. }
  277. next = rb_next(&stats->rb_node);
  278. }
  279. if (!use_browser && sort_order == default_sort_order &&
  280. parent_pattern == default_parent_pattern) {
  281. fprintf(stdout, "#\n# (%s)\n#\n", help);
  282. if (show_threads) {
  283. bool style = !strcmp(pretty_printing_style, "raw");
  284. perf_read_values_display(stdout, &show_threads_values,
  285. style);
  286. perf_read_values_destroy(&show_threads_values);
  287. }
  288. }
  289. out_delete:
  290. perf_session__delete(session);
  291. return ret;
  292. }
  293. static int
  294. parse_callchain_opt(const struct option *opt __used, const char *arg,
  295. int unset)
  296. {
  297. char *tok;
  298. char *endptr;
  299. /*
  300. * --no-call-graph
  301. */
  302. if (unset) {
  303. dont_use_callchains = true;
  304. return 0;
  305. }
  306. symbol_conf.use_callchain = true;
  307. if (!arg)
  308. return 0;
  309. tok = strtok((char *)arg, ",");
  310. if (!tok)
  311. return -1;
  312. /* get the output mode */
  313. if (!strncmp(tok, "graph", strlen(arg)))
  314. callchain_param.mode = CHAIN_GRAPH_ABS;
  315. else if (!strncmp(tok, "flat", strlen(arg)))
  316. callchain_param.mode = CHAIN_FLAT;
  317. else if (!strncmp(tok, "fractal", strlen(arg)))
  318. callchain_param.mode = CHAIN_GRAPH_REL;
  319. else if (!strncmp(tok, "none", strlen(arg))) {
  320. callchain_param.mode = CHAIN_NONE;
  321. symbol_conf.use_callchain = false;
  322. return 0;
  323. }
  324. else
  325. return -1;
  326. /* get the min percentage */
  327. tok = strtok(NULL, ",");
  328. if (!tok)
  329. goto setup;
  330. callchain_param.min_percent = strtod(tok, &endptr);
  331. if (tok == endptr)
  332. return -1;
  333. setup:
  334. if (register_callchain_param(&callchain_param) < 0) {
  335. fprintf(stderr, "Can't register callchain params\n");
  336. return -1;
  337. }
  338. return 0;
  339. }
  340. static const char * const report_usage[] = {
  341. "perf report [<options>] <command>",
  342. NULL
  343. };
  344. static const struct option options[] = {
  345. OPT_STRING('i', "input", &input_name, "file",
  346. "input file name"),
  347. OPT_INCR('v', "verbose", &verbose,
  348. "be more verbose (show symbol address, etc)"),
  349. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  350. "dump raw trace in ASCII"),
  351. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  352. "file", "vmlinux pathname"),
  353. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  354. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  355. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  356. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  357. "Show a column with the number of samples"),
  358. OPT_BOOLEAN('T', "threads", &show_threads,
  359. "Show per-thread event counters"),
  360. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  361. "pretty printing style key: normal raw"),
  362. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  363. "sort by key(s): pid, comm, dso, symbol, parent"),
  364. OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
  365. "Don't shorten the pathnames taking into account the cwd"),
  366. OPT_STRING('p', "parent", &parent_pattern, "regex",
  367. "regex filter to identify parent, see: '--sort parent'"),
  368. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  369. "Only display entries with parent-match"),
  370. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  371. "Display callchains using output_type and min percent threshold. "
  372. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  373. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  374. "only consider symbols in these dsos"),
  375. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  376. "only consider symbols in these comms"),
  377. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  378. "only consider these symbols"),
  379. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  380. "width[,width...]",
  381. "don't try to adjust column width, use these fixed values"),
  382. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  383. "separator for columns, no spaces will be added between "
  384. "columns '.' is reserved."),
  385. OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
  386. "Only display entries resolved to a symbol"),
  387. OPT_END()
  388. };
  389. int cmd_report(int argc, const char **argv, const char *prefix __used)
  390. {
  391. argc = parse_options(argc, argv, options, report_usage, 0);
  392. if (strcmp(input_name, "-") != 0)
  393. setup_browser();
  394. if (symbol__init() < 0)
  395. return -1;
  396. setup_sorting(report_usage, options);
  397. if (parent_pattern != default_parent_pattern) {
  398. if (sort_dimension__add("parent") < 0)
  399. return -1;
  400. sort_parent.elide = 1;
  401. } else
  402. symbol_conf.exclude_other = false;
  403. /*
  404. * Any (unrecognized) arguments left?
  405. */
  406. if (argc)
  407. usage_with_options(report_usage, options);
  408. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
  409. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
  410. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
  411. return __cmd_report();
  412. }