builtin-report.c 13 KB

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