builtin-report.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/callchain.h"
  16. #include "util/strlist.h"
  17. #include "util/values.h"
  18. #include "perf.h"
  19. #include "util/debug.h"
  20. #include "util/header.h"
  21. #include "util/session.h"
  22. #include "util/parse-options.h"
  23. #include "util/parse-events.h"
  24. #include "util/thread.h"
  25. #include "util/sort.h"
  26. #include "util/hist.h"
  27. static char const *input_name = "perf.data";
  28. static bool force;
  29. static bool hide_unresolved;
  30. static bool dont_use_callchains;
  31. static bool show_threads;
  32. static struct perf_read_values show_threads_values;
  33. static char default_pretty_printing_style[] = "normal";
  34. static char *pretty_printing_style = default_pretty_printing_style;
  35. static char callchain_default_opt[] = "fractal,0.5";
  36. static struct hists *perf_session__hists_findnew(struct perf_session *self,
  37. u64 event_stream, u32 type,
  38. u64 config)
  39. {
  40. struct rb_node **p = &self->hists_tree.rb_node;
  41. struct rb_node *parent = NULL;
  42. struct hists *iter, *new;
  43. while (*p != NULL) {
  44. parent = *p;
  45. iter = rb_entry(parent, struct hists, 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 hists));
  54. if (new == NULL)
  55. return NULL;
  56. memset(new, 0, sizeof(struct hists));
  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->hists_tree);
  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. int err = -ENOMEM;
  71. struct hist_entry *he;
  72. struct hists *hists;
  73. struct perf_event_attr *attr;
  74. if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain) {
  75. syms = perf_session__resolve_callchain(self, al->thread,
  76. data->callchain, &parent);
  77. if (syms == NULL)
  78. return -ENOMEM;
  79. }
  80. attr = perf_header__find_attr(data->id, &self->header);
  81. if (attr)
  82. hists = perf_session__hists_findnew(self, data->id, attr->type, attr->config);
  83. else
  84. hists = perf_session__hists_findnew(self, data->id, 0, 0);
  85. if (hists == NULL)
  86. goto out_free_syms;
  87. he = __hists__add_entry(hists, al, parent, data->period);
  88. if (he == NULL)
  89. goto out_free_syms;
  90. err = 0;
  91. if (symbol_conf.use_callchain)
  92. err = append_chain(he->callchain, data->callchain, syms);
  93. out_free_syms:
  94. free(syms);
  95. return err;
  96. }
  97. static int add_event_total(struct perf_session *session,
  98. struct sample_data *data,
  99. struct perf_event_attr *attr)
  100. {
  101. struct hists *hists;
  102. if (attr)
  103. hists = perf_session__hists_findnew(session, data->id,
  104. attr->type, attr->config);
  105. else
  106. hists = perf_session__hists_findnew(session, data->id, 0, 0);
  107. if (!hists)
  108. return -ENOMEM;
  109. hists->stats.total += data->period;
  110. session->hists.stats.total += data->period;
  111. return 0;
  112. }
  113. static int process_sample_event(event_t *event, struct perf_session *session)
  114. {
  115. struct sample_data data = { .period = 1, };
  116. struct addr_location al;
  117. struct perf_event_attr *attr;
  118. event__parse_sample(event, session->sample_type, &data);
  119. dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
  120. data.pid, data.tid, data.ip, data.period);
  121. if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
  122. unsigned int i;
  123. dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
  124. if (!ip_callchain__valid(data.callchain, event)) {
  125. pr_debug("call-chain problem with event, "
  126. "skipping it.\n");
  127. return 0;
  128. }
  129. if (dump_trace) {
  130. for (i = 0; i < data.callchain->nr; i++)
  131. dump_printf("..... %2d: %016Lx\n",
  132. i, data.callchain->ips[i]);
  133. }
  134. }
  135. if (event__preprocess_sample(event, session, &al, NULL) < 0) {
  136. fprintf(stderr, "problem processing %d event, skipping it.\n",
  137. event->header.type);
  138. return -1;
  139. }
  140. if (al.filtered || (hide_unresolved && al.sym == NULL))
  141. return 0;
  142. if (perf_session__add_hist_entry(session, &al, &data)) {
  143. pr_debug("problem incrementing symbol count, skipping event\n");
  144. return -1;
  145. }
  146. attr = perf_header__find_attr(data.id, &session->header);
  147. if (add_event_total(session, &data, attr)) {
  148. pr_debug("problem adding event count\n");
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. static int process_read_event(event_t *event, struct perf_session *session __used)
  154. {
  155. struct perf_event_attr *attr;
  156. attr = perf_header__find_attr(event->read.id, &session->header);
  157. if (show_threads) {
  158. const char *name = attr ? __event_name(attr->type, attr->config)
  159. : "unknown";
  160. perf_read_values_add_value(&show_threads_values,
  161. event->read.pid, event->read.tid,
  162. event->read.id,
  163. name,
  164. event->read.value);
  165. }
  166. dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
  167. attr ? __event_name(attr->type, attr->config) : "FAIL",
  168. event->read.value);
  169. return 0;
  170. }
  171. static int perf_session__setup_sample_type(struct perf_session *self)
  172. {
  173. if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
  174. if (sort__has_parent) {
  175. fprintf(stderr, "selected --sort parent, but no"
  176. " callchain data. Did you call"
  177. " perf record without -g?\n");
  178. return -EINVAL;
  179. }
  180. if (symbol_conf.use_callchain) {
  181. fprintf(stderr, "selected -g but no callchain data."
  182. " Did you call perf record without"
  183. " -g?\n");
  184. return -1;
  185. }
  186. } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
  187. !symbol_conf.use_callchain) {
  188. symbol_conf.use_callchain = true;
  189. if (register_callchain_param(&callchain_param) < 0) {
  190. fprintf(stderr, "Can't register callchain"
  191. " params\n");
  192. return -EINVAL;
  193. }
  194. }
  195. return 0;
  196. }
  197. static struct perf_event_ops event_ops = {
  198. .sample = process_sample_event,
  199. .mmap = event__process_mmap,
  200. .comm = event__process_comm,
  201. .exit = event__process_task,
  202. .fork = event__process_task,
  203. .lost = event__process_lost,
  204. .read = process_read_event,
  205. .attr = event__process_attr,
  206. .event_type = event__process_event_type,
  207. .tracing_data = event__process_tracing_data,
  208. .build_id = event__process_build_id,
  209. };
  210. extern volatile int session_done;
  211. static void sig_handler(int sig __attribute__((__unused__)))
  212. {
  213. session_done = 1;
  214. }
  215. static int __cmd_report(void)
  216. {
  217. int ret = -EINVAL;
  218. struct perf_session *session;
  219. struct rb_node *next;
  220. const char *help = "For a higher level overview, try: perf report --sort comm,dso";
  221. signal(SIGINT, sig_handler);
  222. session = perf_session__new(input_name, O_RDONLY, force, false);
  223. if (session == NULL)
  224. return -ENOMEM;
  225. if (show_threads)
  226. perf_read_values_init(&show_threads_values);
  227. ret = perf_session__setup_sample_type(session);
  228. if (ret)
  229. goto out_delete;
  230. ret = perf_session__process_events(session, &event_ops);
  231. if (ret)
  232. goto out_delete;
  233. if (dump_trace) {
  234. event__print_totals();
  235. goto out_delete;
  236. }
  237. if (verbose > 3)
  238. perf_session__fprintf(session, stdout);
  239. if (verbose > 2)
  240. perf_session__fprintf_dsos(session, stdout);
  241. next = rb_first(&session->hists_tree);
  242. while (next) {
  243. struct hists *hists;
  244. u64 nr_hists;
  245. hists = rb_entry(next, struct hists, rb_node);
  246. hists__collapse_resort(hists);
  247. nr_hists = hists__output_resort(hists);
  248. if (use_browser)
  249. perf_session__browse_hists(&hists->entries, nr_hists,
  250. hists->stats.total, help,
  251. input_name);
  252. else {
  253. if (rb_first(&session->hists.entries) ==
  254. rb_last(&session->hists.entries))
  255. fprintf(stdout, "# Samples: %Ld\n#\n",
  256. hists->stats.total);
  257. else
  258. fprintf(stdout, "# Samples: %Ld %s\n#\n",
  259. hists->stats.total,
  260. __event_name(hists->type, hists->config));
  261. hists__fprintf(hists, NULL, false, stdout);
  262. fprintf(stdout, "\n\n");
  263. }
  264. next = rb_next(&hists->rb_node);
  265. }
  266. if (!use_browser && sort_order == default_sort_order &&
  267. parent_pattern == default_parent_pattern) {
  268. fprintf(stdout, "#\n# (%s)\n#\n", help);
  269. if (show_threads) {
  270. bool style = !strcmp(pretty_printing_style, "raw");
  271. perf_read_values_display(stdout, &show_threads_values,
  272. style);
  273. perf_read_values_destroy(&show_threads_values);
  274. }
  275. }
  276. out_delete:
  277. perf_session__delete(session);
  278. return ret;
  279. }
  280. static int
  281. parse_callchain_opt(const struct option *opt __used, const char *arg,
  282. int unset)
  283. {
  284. char *tok, *tok2;
  285. char *endptr;
  286. /*
  287. * --no-call-graph
  288. */
  289. if (unset) {
  290. dont_use_callchains = true;
  291. return 0;
  292. }
  293. symbol_conf.use_callchain = true;
  294. if (!arg)
  295. return 0;
  296. tok = strtok((char *)arg, ",");
  297. if (!tok)
  298. return -1;
  299. /* get the output mode */
  300. if (!strncmp(tok, "graph", strlen(arg)))
  301. callchain_param.mode = CHAIN_GRAPH_ABS;
  302. else if (!strncmp(tok, "flat", strlen(arg)))
  303. callchain_param.mode = CHAIN_FLAT;
  304. else if (!strncmp(tok, "fractal", strlen(arg)))
  305. callchain_param.mode = CHAIN_GRAPH_REL;
  306. else if (!strncmp(tok, "none", strlen(arg))) {
  307. callchain_param.mode = CHAIN_NONE;
  308. symbol_conf.use_callchain = false;
  309. return 0;
  310. }
  311. else
  312. return -1;
  313. /* get the min percentage */
  314. tok = strtok(NULL, ",");
  315. if (!tok)
  316. goto setup;
  317. tok2 = strtok(NULL, ",");
  318. callchain_param.min_percent = strtod(tok, &endptr);
  319. if (tok == endptr)
  320. return -1;
  321. if (tok2)
  322. callchain_param.print_limit = strtod(tok2, &endptr);
  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_INCR('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_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  357. "Show sample percentage for different cpu modes"),
  358. OPT_STRING('p', "parent", &parent_pattern, "regex",
  359. "regex filter to identify parent, see: '--sort parent'"),
  360. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  361. "Only display entries with parent-match"),
  362. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  363. "Display callchains using output_type (graph, flat, fractal, or none) and min percent threshold. "
  364. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  365. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  366. "only consider symbols in these dsos"),
  367. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  368. "only consider symbols in these comms"),
  369. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  370. "only consider these symbols"),
  371. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  372. "width[,width...]",
  373. "don't try to adjust column width, use these fixed values"),
  374. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  375. "separator for columns, no spaces will be added between "
  376. "columns '.' is reserved."),
  377. OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
  378. "Only display entries resolved to a symbol"),
  379. OPT_END()
  380. };
  381. int cmd_report(int argc, const char **argv, const char *prefix __used)
  382. {
  383. argc = parse_options(argc, argv, options, report_usage, 0);
  384. if (strcmp(input_name, "-") != 0)
  385. setup_browser();
  386. if (symbol__init() < 0)
  387. return -1;
  388. setup_sorting(report_usage, options);
  389. if (parent_pattern != default_parent_pattern) {
  390. if (sort_dimension__add("parent") < 0)
  391. return -1;
  392. sort_parent.elide = 1;
  393. } else
  394. symbol_conf.exclude_other = false;
  395. /*
  396. * Any (unrecognized) arguments left?
  397. */
  398. if (argc)
  399. usage_with_options(report_usage, options);
  400. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
  401. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
  402. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
  403. return __cmd_report();
  404. }