builtin-report.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 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. __perf_session__add_count(he, al, 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. .attr = event__process_attr,
  220. .event_type = event__process_event_type,
  221. .tracing_data = event__process_tracing_data,
  222. .build_id = event__process_build_id,
  223. };
  224. extern volatile int session_done;
  225. static void sig_handler(int sig __attribute__((__unused__)))
  226. {
  227. session_done = 1;
  228. }
  229. static int __cmd_report(void)
  230. {
  231. int ret = -EINVAL;
  232. struct perf_session *session;
  233. struct rb_node *next;
  234. const char *help = "For a higher level overview, try: perf report --sort comm,dso";
  235. signal(SIGINT, sig_handler);
  236. session = perf_session__new(input_name, O_RDONLY, force);
  237. if (session == NULL)
  238. return -ENOMEM;
  239. if (show_threads)
  240. perf_read_values_init(&show_threads_values);
  241. ret = perf_session__setup_sample_type(session);
  242. if (ret)
  243. goto out_delete;
  244. ret = perf_session__process_events(session, &event_ops);
  245. if (ret)
  246. goto out_delete;
  247. if (dump_trace) {
  248. event__print_totals();
  249. goto out_delete;
  250. }
  251. if (verbose > 3)
  252. perf_session__fprintf(session, stdout);
  253. if (verbose > 2)
  254. dsos__fprintf(&session->kerninfo_root, stdout);
  255. next = rb_first(&session->stats_by_id);
  256. while (next) {
  257. struct event_stat_id *stats;
  258. u64 nr_hists;
  259. stats = rb_entry(next, struct event_stat_id, rb_node);
  260. perf_session__collapse_resort(&stats->hists);
  261. nr_hists = perf_session__output_resort(&stats->hists,
  262. stats->stats.total);
  263. if (use_browser)
  264. perf_session__browse_hists(&stats->hists, nr_hists,
  265. stats->stats.total, help,
  266. input_name);
  267. else {
  268. if (rb_first(&session->stats_by_id) ==
  269. rb_last(&session->stats_by_id))
  270. fprintf(stdout, "# Samples: %Ld\n#\n",
  271. stats->stats.total);
  272. else
  273. fprintf(stdout, "# Samples: %Ld %s\n#\n",
  274. stats->stats.total,
  275. __event_name(stats->type, stats->config));
  276. perf_session__fprintf_hists(&stats->hists, NULL, false, stdout,
  277. stats->stats.total);
  278. fprintf(stdout, "\n\n");
  279. }
  280. next = rb_next(&stats->rb_node);
  281. }
  282. if (!use_browser && sort_order == default_sort_order &&
  283. parent_pattern == default_parent_pattern) {
  284. fprintf(stdout, "#\n# (%s)\n#\n", help);
  285. if (show_threads) {
  286. bool style = !strcmp(pretty_printing_style, "raw");
  287. perf_read_values_display(stdout, &show_threads_values,
  288. style);
  289. perf_read_values_destroy(&show_threads_values);
  290. }
  291. }
  292. out_delete:
  293. perf_session__delete(session);
  294. return ret;
  295. }
  296. static int
  297. parse_callchain_opt(const struct option *opt __used, const char *arg,
  298. int unset)
  299. {
  300. char *tok;
  301. char *endptr;
  302. /*
  303. * --no-call-graph
  304. */
  305. if (unset) {
  306. dont_use_callchains = true;
  307. return 0;
  308. }
  309. symbol_conf.use_callchain = true;
  310. if (!arg)
  311. return 0;
  312. tok = strtok((char *)arg, ",");
  313. if (!tok)
  314. return -1;
  315. /* get the output mode */
  316. if (!strncmp(tok, "graph", strlen(arg)))
  317. callchain_param.mode = CHAIN_GRAPH_ABS;
  318. else if (!strncmp(tok, "flat", strlen(arg)))
  319. callchain_param.mode = CHAIN_FLAT;
  320. else if (!strncmp(tok, "fractal", strlen(arg)))
  321. callchain_param.mode = CHAIN_GRAPH_REL;
  322. else if (!strncmp(tok, "none", strlen(arg))) {
  323. callchain_param.mode = CHAIN_NONE;
  324. symbol_conf.use_callchain = false;
  325. return 0;
  326. }
  327. else
  328. return -1;
  329. /* get the min percentage */
  330. tok = strtok(NULL, ",");
  331. if (!tok)
  332. goto setup;
  333. callchain_param.min_percent = strtod(tok, &endptr);
  334. if (tok == endptr)
  335. return -1;
  336. setup:
  337. if (register_callchain_param(&callchain_param) < 0) {
  338. fprintf(stderr, "Can't register callchain params\n");
  339. return -1;
  340. }
  341. return 0;
  342. }
  343. static const char * const report_usage[] = {
  344. "perf report [<options>] <command>",
  345. NULL
  346. };
  347. static const struct option options[] = {
  348. OPT_STRING('i', "input", &input_name, "file",
  349. "input file name"),
  350. OPT_INCR('v', "verbose", &verbose,
  351. "be more verbose (show symbol address, etc)"),
  352. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  353. "dump raw trace in ASCII"),
  354. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  355. "file", "vmlinux pathname"),
  356. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  357. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  358. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  359. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  360. "Show a column with the number of samples"),
  361. OPT_BOOLEAN('T', "threads", &show_threads,
  362. "Show per-thread event counters"),
  363. OPT_STRING(0, "pretty", &pretty_printing_style, "key",
  364. "pretty printing style key: normal raw"),
  365. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  366. "sort by key(s): pid, comm, dso, symbol, parent"),
  367. OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
  368. "Don't shorten the pathnames taking into account the cwd"),
  369. OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  370. "Show sample percentage for different cpu modes"),
  371. OPT_STRING('p', "parent", &parent_pattern, "regex",
  372. "regex filter to identify parent, see: '--sort parent'"),
  373. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  374. "Only display entries with parent-match"),
  375. OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
  376. "Display callchains using output_type and min percent threshold. "
  377. "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
  378. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  379. "only consider symbols in these dsos"),
  380. OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  381. "only consider symbols in these comms"),
  382. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  383. "only consider these symbols"),
  384. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  385. "width[,width...]",
  386. "don't try to adjust column width, use these fixed values"),
  387. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  388. "separator for columns, no spaces will be added between "
  389. "columns '.' is reserved."),
  390. OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
  391. "Only display entries resolved to a symbol"),
  392. OPT_END()
  393. };
  394. int cmd_report(int argc, const char **argv, const char *prefix __used)
  395. {
  396. argc = parse_options(argc, argv, options, report_usage, 0);
  397. if (strcmp(input_name, "-") != 0)
  398. setup_browser();
  399. if (symbol__init() < 0)
  400. return -1;
  401. setup_sorting(report_usage, options);
  402. if (parent_pattern != default_parent_pattern) {
  403. if (sort_dimension__add("parent") < 0)
  404. return -1;
  405. sort_parent.elide = 1;
  406. } else
  407. symbol_conf.exclude_other = false;
  408. /*
  409. * Any (unrecognized) arguments left?
  410. */
  411. if (argc)
  412. usage_with_options(report_usage, options);
  413. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
  414. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
  415. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
  416. return __cmd_report();
  417. }