builtin-report.c 13 KB

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