builtin-report.c 10 KB

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