builtin-report.c 10 KB

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