builtin-report.c 10 KB

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