builtin-report.c 12 KB

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