builtin-report.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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/cache.h"
  11. #include "util/annotate.h"
  12. #include "util/color.h"
  13. #include <linux/list.h>
  14. #include <linux/rbtree.h>
  15. #include "util/symbol.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/evlist.h"
  22. #include "util/evsel.h"
  23. #include "util/header.h"
  24. #include "util/session.h"
  25. #include "util/tool.h"
  26. #include "util/parse-options.h"
  27. #include "util/parse-events.h"
  28. #include "util/thread.h"
  29. #include "util/sort.h"
  30. #include "util/hist.h"
  31. #include "util/data.h"
  32. #include "arch/common.h"
  33. #include "util/auxtrace.h"
  34. #include <dlfcn.h>
  35. #include <linux/bitmap.h>
  36. struct report {
  37. struct perf_tool tool;
  38. struct perf_session *session;
  39. bool force, use_tui, use_gtk, use_stdio;
  40. bool hide_unresolved;
  41. bool dont_use_callchains;
  42. bool show_full_info;
  43. bool show_threads;
  44. bool inverted_callchain;
  45. bool mem_mode;
  46. bool header;
  47. bool header_only;
  48. bool nonany_branch_mode;
  49. int max_stack;
  50. struct perf_read_values show_threads_values;
  51. const char *pretty_printing_style;
  52. const char *cpu_list;
  53. const char *symbol_filter_str;
  54. float min_percent;
  55. u64 nr_entries;
  56. u64 queue_size;
  57. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  58. };
  59. static int report__config(const char *var, const char *value, void *cb)
  60. {
  61. struct report *rep = cb;
  62. if (!strcmp(var, "report.group")) {
  63. symbol_conf.event_group = perf_config_bool(var, value);
  64. return 0;
  65. }
  66. if (!strcmp(var, "report.percent-limit")) {
  67. rep->min_percent = strtof(value, NULL);
  68. return 0;
  69. }
  70. if (!strcmp(var, "report.children")) {
  71. symbol_conf.cumulate_callchain = perf_config_bool(var, value);
  72. return 0;
  73. }
  74. if (!strcmp(var, "report.queue-size")) {
  75. rep->queue_size = perf_config_u64(var, value);
  76. return 0;
  77. }
  78. return perf_default_config(var, value, cb);
  79. }
  80. static int hist_iter__report_callback(struct hist_entry_iter *iter,
  81. struct addr_location *al, bool single,
  82. void *arg)
  83. {
  84. int err = 0;
  85. struct report *rep = arg;
  86. struct hist_entry *he = iter->he;
  87. struct perf_evsel *evsel = iter->evsel;
  88. struct mem_info *mi;
  89. struct branch_info *bi;
  90. if (!ui__has_annotation())
  91. return 0;
  92. if (sort__mode == SORT_MODE__BRANCH) {
  93. bi = he->branch_info;
  94. err = addr_map_symbol__inc_samples(&bi->from, evsel->idx);
  95. if (err)
  96. goto out;
  97. err = addr_map_symbol__inc_samples(&bi->to, evsel->idx);
  98. } else if (rep->mem_mode) {
  99. mi = he->mem_info;
  100. err = addr_map_symbol__inc_samples(&mi->daddr, evsel->idx);
  101. if (err)
  102. goto out;
  103. err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
  104. } else if (symbol_conf.cumulate_callchain) {
  105. if (single)
  106. err = hist_entry__inc_addr_samples(he, evsel->idx,
  107. al->addr);
  108. } else {
  109. err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
  110. }
  111. out:
  112. return err;
  113. }
  114. static int process_sample_event(struct perf_tool *tool,
  115. union perf_event *event,
  116. struct perf_sample *sample,
  117. struct perf_evsel *evsel,
  118. struct machine *machine)
  119. {
  120. struct report *rep = container_of(tool, struct report, tool);
  121. struct addr_location al;
  122. struct hist_entry_iter iter = {
  123. .evsel = evsel,
  124. .sample = sample,
  125. .hide_unresolved = rep->hide_unresolved,
  126. .add_entry_cb = hist_iter__report_callback,
  127. };
  128. int ret = 0;
  129. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
  130. pr_debug("problem processing %d event, skipping it.\n",
  131. event->header.type);
  132. return -1;
  133. }
  134. if (rep->hide_unresolved && al.sym == NULL)
  135. goto out_put;
  136. if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
  137. goto out_put;
  138. if (sort__mode == SORT_MODE__BRANCH)
  139. iter.ops = &hist_iter_branch;
  140. else if (rep->mem_mode)
  141. iter.ops = &hist_iter_mem;
  142. else if (symbol_conf.cumulate_callchain)
  143. iter.ops = &hist_iter_cumulative;
  144. else
  145. iter.ops = &hist_iter_normal;
  146. if (al.map != NULL)
  147. al.map->dso->hit = 1;
  148. ret = hist_entry_iter__add(&iter, &al, rep->max_stack, rep);
  149. if (ret < 0)
  150. pr_debug("problem adding hist entry, skipping event\n");
  151. out_put:
  152. addr_location__put(&al);
  153. return ret;
  154. }
  155. static int process_read_event(struct perf_tool *tool,
  156. union perf_event *event,
  157. struct perf_sample *sample __maybe_unused,
  158. struct perf_evsel *evsel,
  159. struct machine *machine __maybe_unused)
  160. {
  161. struct report *rep = container_of(tool, struct report, tool);
  162. if (rep->show_threads) {
  163. const char *name = evsel ? perf_evsel__name(evsel) : "unknown";
  164. perf_read_values_add_value(&rep->show_threads_values,
  165. event->read.pid, event->read.tid,
  166. event->read.id,
  167. name,
  168. event->read.value);
  169. }
  170. dump_printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid,
  171. evsel ? perf_evsel__name(evsel) : "FAIL",
  172. event->read.value);
  173. return 0;
  174. }
  175. /* For pipe mode, sample_type is not currently set */
  176. static int report__setup_sample_type(struct report *rep)
  177. {
  178. struct perf_session *session = rep->session;
  179. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  180. bool is_pipe = perf_data_file__is_pipe(session->file);
  181. if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  182. if (sort__has_parent) {
  183. ui__error("Selected --sort parent, but no "
  184. "callchain data. Did you call "
  185. "'perf record' without -g?\n");
  186. return -EINVAL;
  187. }
  188. if (symbol_conf.use_callchain) {
  189. ui__error("Selected -g or --branch-history but no "
  190. "callchain data. Did\n"
  191. "you call 'perf record' without -g?\n");
  192. return -1;
  193. }
  194. } else if (!rep->dont_use_callchains &&
  195. callchain_param.mode != CHAIN_NONE &&
  196. !symbol_conf.use_callchain) {
  197. symbol_conf.use_callchain = true;
  198. if (callchain_register_param(&callchain_param) < 0) {
  199. ui__error("Can't register callchain params.\n");
  200. return -EINVAL;
  201. }
  202. }
  203. if (symbol_conf.cumulate_callchain) {
  204. /* Silently ignore if callchain is missing */
  205. if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  206. symbol_conf.cumulate_callchain = false;
  207. perf_hpp__cancel_cumulate();
  208. }
  209. }
  210. if (sort__mode == SORT_MODE__BRANCH) {
  211. if (!is_pipe &&
  212. !(sample_type & PERF_SAMPLE_BRANCH_STACK)) {
  213. ui__error("Selected -b but no branch data. "
  214. "Did you call perf record without -b?\n");
  215. return -1;
  216. }
  217. }
  218. if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
  219. if ((sample_type & PERF_SAMPLE_REGS_USER) &&
  220. (sample_type & PERF_SAMPLE_STACK_USER))
  221. callchain_param.record_mode = CALLCHAIN_DWARF;
  222. else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
  223. callchain_param.record_mode = CALLCHAIN_LBR;
  224. else
  225. callchain_param.record_mode = CALLCHAIN_FP;
  226. }
  227. /* ??? handle more cases than just ANY? */
  228. if (!(perf_evlist__combined_branch_type(session->evlist) &
  229. PERF_SAMPLE_BRANCH_ANY))
  230. rep->nonany_branch_mode = true;
  231. return 0;
  232. }
  233. static void sig_handler(int sig __maybe_unused)
  234. {
  235. session_done = 1;
  236. }
  237. static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep,
  238. const char *evname, FILE *fp)
  239. {
  240. size_t ret;
  241. char unit;
  242. unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  243. u64 nr_events = hists->stats.total_period;
  244. struct perf_evsel *evsel = hists_to_evsel(hists);
  245. char buf[512];
  246. size_t size = sizeof(buf);
  247. if (symbol_conf.filter_relative) {
  248. nr_samples = hists->stats.nr_non_filtered_samples;
  249. nr_events = hists->stats.total_non_filtered_period;
  250. }
  251. if (perf_evsel__is_group_event(evsel)) {
  252. struct perf_evsel *pos;
  253. perf_evsel__group_desc(evsel, buf, size);
  254. evname = buf;
  255. for_each_group_member(pos, evsel) {
  256. const struct hists *pos_hists = evsel__hists(pos);
  257. if (symbol_conf.filter_relative) {
  258. nr_samples += pos_hists->stats.nr_non_filtered_samples;
  259. nr_events += pos_hists->stats.total_non_filtered_period;
  260. } else {
  261. nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
  262. nr_events += pos_hists->stats.total_period;
  263. }
  264. }
  265. }
  266. nr_samples = convert_unit(nr_samples, &unit);
  267. ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
  268. if (evname != NULL)
  269. ret += fprintf(fp, " of event '%s'", evname);
  270. if (rep->mem_mode) {
  271. ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
  272. ret += fprintf(fp, "\n# Sort order : %s", sort_order ? : default_mem_sort_order);
  273. } else
  274. ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
  275. return ret + fprintf(fp, "\n#\n");
  276. }
  277. static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
  278. struct report *rep,
  279. const char *help)
  280. {
  281. struct perf_evsel *pos;
  282. fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n", evlist->stats.total_lost_samples);
  283. evlist__for_each(evlist, pos) {
  284. struct hists *hists = evsel__hists(pos);
  285. const char *evname = perf_evsel__name(pos);
  286. if (symbol_conf.event_group &&
  287. !perf_evsel__is_group_leader(pos))
  288. continue;
  289. hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
  290. hists__fprintf(hists, true, 0, 0, rep->min_percent, stdout);
  291. fprintf(stdout, "\n\n");
  292. }
  293. if (sort_order == NULL &&
  294. parent_pattern == default_parent_pattern)
  295. fprintf(stdout, "#\n# (%s)\n#\n", help);
  296. if (rep->show_threads) {
  297. bool style = !strcmp(rep->pretty_printing_style, "raw");
  298. perf_read_values_display(stdout, &rep->show_threads_values,
  299. style);
  300. perf_read_values_destroy(&rep->show_threads_values);
  301. }
  302. return 0;
  303. }
  304. static void report__warn_kptr_restrict(const struct report *rep)
  305. {
  306. struct map *kernel_map = rep->session->machines.host.vmlinux_maps[MAP__FUNCTION];
  307. struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
  308. if (kernel_map == NULL ||
  309. (kernel_map->dso->hit &&
  310. (kernel_kmap->ref_reloc_sym == NULL ||
  311. kernel_kmap->ref_reloc_sym->addr == 0))) {
  312. const char *desc =
  313. "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
  314. "can't be resolved.";
  315. if (kernel_map) {
  316. const struct dso *kdso = kernel_map->dso;
  317. if (!RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION])) {
  318. desc = "If some relocation was applied (e.g. "
  319. "kexec) symbols may be misresolved.";
  320. }
  321. }
  322. ui__warning(
  323. "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
  324. "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
  325. "Samples in kernel modules can't be resolved as well.\n\n",
  326. desc);
  327. }
  328. }
  329. static int report__gtk_browse_hists(struct report *rep, const char *help)
  330. {
  331. int (*hist_browser)(struct perf_evlist *evlist, const char *help,
  332. struct hist_browser_timer *timer, float min_pcnt);
  333. hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
  334. if (hist_browser == NULL) {
  335. ui__error("GTK browser not found!\n");
  336. return -1;
  337. }
  338. return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
  339. }
  340. static int report__browse_hists(struct report *rep)
  341. {
  342. int ret;
  343. struct perf_session *session = rep->session;
  344. struct perf_evlist *evlist = session->evlist;
  345. const char *help = "For a higher level overview, try: perf report --sort comm,dso";
  346. switch (use_browser) {
  347. case 1:
  348. ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
  349. rep->min_percent,
  350. &session->header.env);
  351. /*
  352. * Usually "ret" is the last pressed key, and we only
  353. * care if the key notifies us to switch data file.
  354. */
  355. if (ret != K_SWITCH_INPUT_DATA)
  356. ret = 0;
  357. break;
  358. case 2:
  359. ret = report__gtk_browse_hists(rep, help);
  360. break;
  361. default:
  362. ret = perf_evlist__tty_browse_hists(evlist, rep, help);
  363. break;
  364. }
  365. return ret;
  366. }
  367. static void report__collapse_hists(struct report *rep)
  368. {
  369. struct ui_progress prog;
  370. struct perf_evsel *pos;
  371. ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
  372. evlist__for_each(rep->session->evlist, pos) {
  373. struct hists *hists = evsel__hists(pos);
  374. if (pos->idx == 0)
  375. hists->symbol_filter_str = rep->symbol_filter_str;
  376. hists__collapse_resort(hists, &prog);
  377. /* Non-group events are considered as leader */
  378. if (symbol_conf.event_group &&
  379. !perf_evsel__is_group_leader(pos)) {
  380. struct hists *leader_hists = evsel__hists(pos->leader);
  381. hists__match(leader_hists, hists);
  382. hists__link(leader_hists, hists);
  383. }
  384. }
  385. ui_progress__finish();
  386. }
  387. static void report__output_resort(struct report *rep)
  388. {
  389. struct ui_progress prog;
  390. struct perf_evsel *pos;
  391. ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
  392. evlist__for_each(rep->session->evlist, pos)
  393. hists__output_resort(evsel__hists(pos), &prog);
  394. ui_progress__finish();
  395. }
  396. static int __cmd_report(struct report *rep)
  397. {
  398. int ret;
  399. struct perf_session *session = rep->session;
  400. struct perf_evsel *pos;
  401. struct perf_data_file *file = session->file;
  402. signal(SIGINT, sig_handler);
  403. if (rep->cpu_list) {
  404. ret = perf_session__cpu_bitmap(session, rep->cpu_list,
  405. rep->cpu_bitmap);
  406. if (ret)
  407. return ret;
  408. }
  409. if (rep->show_threads)
  410. perf_read_values_init(&rep->show_threads_values);
  411. ret = report__setup_sample_type(rep);
  412. if (ret)
  413. return ret;
  414. ret = perf_session__process_events(session);
  415. if (ret)
  416. return ret;
  417. report__warn_kptr_restrict(rep);
  418. evlist__for_each(session->evlist, pos)
  419. rep->nr_entries += evsel__hists(pos)->nr_entries;
  420. if (use_browser == 0) {
  421. if (verbose > 3)
  422. perf_session__fprintf(session, stdout);
  423. if (verbose > 2)
  424. perf_session__fprintf_dsos(session, stdout);
  425. if (dump_trace) {
  426. perf_session__fprintf_nr_events(session, stdout);
  427. perf_evlist__fprintf_nr_events(session->evlist, stdout);
  428. return 0;
  429. }
  430. }
  431. report__collapse_hists(rep);
  432. if (session_done())
  433. return 0;
  434. /*
  435. * recalculate number of entries after collapsing since it
  436. * might be changed during the collapse phase.
  437. */
  438. rep->nr_entries = 0;
  439. evlist__for_each(session->evlist, pos)
  440. rep->nr_entries += evsel__hists(pos)->nr_entries;
  441. if (rep->nr_entries == 0) {
  442. ui__error("The %s file has no samples!\n", file->path);
  443. return 0;
  444. }
  445. report__output_resort(rep);
  446. return report__browse_hists(rep);
  447. }
  448. static int
  449. report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  450. {
  451. struct report *rep = (struct report *)opt->value;
  452. /*
  453. * --no-call-graph
  454. */
  455. if (unset) {
  456. rep->dont_use_callchains = true;
  457. return 0;
  458. }
  459. return parse_callchain_report_opt(arg);
  460. }
  461. int
  462. report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
  463. const char *arg, int unset __maybe_unused)
  464. {
  465. if (arg) {
  466. int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
  467. if (err) {
  468. char buf[BUFSIZ];
  469. regerror(err, &ignore_callees_regex, buf, sizeof(buf));
  470. pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
  471. return -1;
  472. }
  473. have_ignore_callees = 1;
  474. }
  475. return 0;
  476. }
  477. static int
  478. parse_branch_mode(const struct option *opt __maybe_unused,
  479. const char *str __maybe_unused, int unset)
  480. {
  481. int *branch_mode = opt->value;
  482. *branch_mode = !unset;
  483. return 0;
  484. }
  485. static int
  486. parse_percent_limit(const struct option *opt, const char *str,
  487. int unset __maybe_unused)
  488. {
  489. struct report *rep = opt->value;
  490. rep->min_percent = strtof(str, NULL);
  491. return 0;
  492. }
  493. int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
  494. {
  495. struct perf_session *session;
  496. struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
  497. struct stat st;
  498. bool has_br_stack = false;
  499. int branch_mode = -1;
  500. bool branch_call_mode = false;
  501. char callchain_default_opt[] = "fractal,0.5,callee";
  502. const char * const report_usage[] = {
  503. "perf report [<options>]",
  504. NULL
  505. };
  506. struct report report = {
  507. .tool = {
  508. .sample = process_sample_event,
  509. .mmap = perf_event__process_mmap,
  510. .mmap2 = perf_event__process_mmap2,
  511. .comm = perf_event__process_comm,
  512. .exit = perf_event__process_exit,
  513. .fork = perf_event__process_fork,
  514. .lost = perf_event__process_lost,
  515. .read = process_read_event,
  516. .attr = perf_event__process_attr,
  517. .tracing_data = perf_event__process_tracing_data,
  518. .build_id = perf_event__process_build_id,
  519. .id_index = perf_event__process_id_index,
  520. .auxtrace_info = perf_event__process_auxtrace_info,
  521. .auxtrace = perf_event__process_auxtrace,
  522. .ordered_events = true,
  523. .ordering_requires_timestamps = true,
  524. },
  525. .max_stack = PERF_MAX_STACK_DEPTH,
  526. .pretty_printing_style = "normal",
  527. };
  528. const struct option options[] = {
  529. OPT_STRING('i', "input", &input_name, "file",
  530. "input file name"),
  531. OPT_INCR('v', "verbose", &verbose,
  532. "be more verbose (show symbol address, etc)"),
  533. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  534. "dump raw trace in ASCII"),
  535. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  536. "file", "vmlinux pathname"),
  537. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  538. "file", "kallsyms pathname"),
  539. OPT_BOOLEAN('f', "force", &report.force, "don't complain, do it"),
  540. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  541. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  542. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  543. "Show a column with the number of samples"),
  544. OPT_BOOLEAN('T', "threads", &report.show_threads,
  545. "Show per-thread event counters"),
  546. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  547. "pretty printing style key: normal raw"),
  548. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  549. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  550. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  551. "Use the stdio interface"),
  552. OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
  553. OPT_BOOLEAN(0, "header-only", &report.header_only,
  554. "Show only data header."),
  555. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  556. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  557. " Please refer the man page for the complete list."),
  558. OPT_STRING('F', "fields", &field_order, "key[,keys...]",
  559. "output field(s): overhead, period, sample plus all of sort keys"),
  560. OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  561. "Show sample percentage for different cpu modes"),
  562. OPT_STRING('p', "parent", &parent_pattern, "regex",
  563. "regex filter to identify parent, see: '--sort parent'"),
  564. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  565. "Only display entries with parent-match"),
  566. OPT_CALLBACK_DEFAULT('g', "call-graph", &report, "output_type,min_percent[,print_limit],call_order[,branch]",
  567. "Display callchains using output_type (graph, flat, fractal, or none) , min percent threshold, optional print limit, callchain order, key (function or address), add branches. "
  568. "Default: fractal,0.5,callee,function", &report_parse_callchain_opt, callchain_default_opt),
  569. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  570. "Accumulate callchains of children and show total overhead as well"),
  571. OPT_INTEGER(0, "max-stack", &report.max_stack,
  572. "Set the maximum stack depth when parsing the callchain, "
  573. "anything beyond the specified depth will be ignored. "
  574. "Default: " __stringify(PERF_MAX_STACK_DEPTH)),
  575. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  576. "alias for inverted call graph"),
  577. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  578. "ignore callees of these functions in call graphs",
  579. report_parse_ignore_callees_opt),
  580. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  581. "only consider symbols in these dsos"),
  582. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  583. "only consider symbols in these comms"),
  584. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  585. "only consider symbols in these pids"),
  586. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  587. "only consider symbols in these tids"),
  588. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  589. "only consider these symbols"),
  590. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  591. "only show symbols that (partially) match with this filter"),
  592. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  593. "width[,width...]",
  594. "don't try to adjust column width, use these fixed values"),
  595. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  596. "separator for columns, no spaces will be added between "
  597. "columns '.' is reserved."),
  598. OPT_BOOLEAN('U', "hide-unresolved", &report.hide_unresolved,
  599. "Only display entries resolved to a symbol"),
  600. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  601. "Look for files with symbols relative to this directory"),
  602. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  603. "list of cpus to profile"),
  604. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  605. "Display extended information about perf.data file"),
  606. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  607. "Interleave source code with assembly code (default)"),
  608. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  609. "Display raw encoding of assembly instructions (default)"),
  610. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  611. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  612. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  613. "Show a column with the sum of periods"),
  614. OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
  615. "Show event group information together"),
  616. OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
  617. "use branch records for per branch histogram filling",
  618. parse_branch_mode),
  619. OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
  620. "add last branch records to call history"),
  621. OPT_STRING(0, "objdump", &objdump_path, "path",
  622. "objdump binary to use for disassembly and annotations"),
  623. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  624. "Disable symbol demangling"),
  625. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  626. "Enable kernel symbol demangling"),
  627. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  628. OPT_CALLBACK(0, "percent-limit", &report, "percent",
  629. "Don't show entries under that percent", parse_percent_limit),
  630. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  631. "how to display percentage of filtered entries", parse_filter_percentage),
  632. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  633. "Instruction Tracing options",
  634. itrace_parse_synth_opts),
  635. OPT_END()
  636. };
  637. struct perf_data_file file = {
  638. .mode = PERF_DATA_MODE_READ,
  639. };
  640. int ret = hists__init();
  641. if (ret < 0)
  642. return ret;
  643. perf_config(report__config, &report);
  644. argc = parse_options(argc, argv, options, report_usage, 0);
  645. if (symbol_conf.vmlinux_name &&
  646. access(symbol_conf.vmlinux_name, R_OK)) {
  647. pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
  648. return -EINVAL;
  649. }
  650. if (symbol_conf.kallsyms_name &&
  651. access(symbol_conf.kallsyms_name, R_OK)) {
  652. pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
  653. return -EINVAL;
  654. }
  655. if (report.use_stdio)
  656. use_browser = 0;
  657. else if (report.use_tui)
  658. use_browser = 1;
  659. else if (report.use_gtk)
  660. use_browser = 2;
  661. if (report.inverted_callchain)
  662. callchain_param.order = ORDER_CALLER;
  663. if (!input_name || !strlen(input_name)) {
  664. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  665. input_name = "-";
  666. else
  667. input_name = "perf.data";
  668. }
  669. file.path = input_name;
  670. file.force = report.force;
  671. repeat:
  672. session = perf_session__new(&file, false, &report.tool);
  673. if (session == NULL)
  674. return -1;
  675. if (report.queue_size) {
  676. ordered_events__set_alloc_size(&session->ordered_events,
  677. report.queue_size);
  678. }
  679. session->itrace_synth_opts = &itrace_synth_opts;
  680. report.session = session;
  681. has_br_stack = perf_header__has_feat(&session->header,
  682. HEADER_BRANCH_STACK);
  683. /*
  684. * Branch mode is a tristate:
  685. * -1 means default, so decide based on the file having branch data.
  686. * 0/1 means the user chose a mode.
  687. */
  688. if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
  689. !branch_call_mode) {
  690. sort__mode = SORT_MODE__BRANCH;
  691. symbol_conf.cumulate_callchain = false;
  692. }
  693. if (branch_call_mode) {
  694. callchain_param.key = CCKEY_ADDRESS;
  695. callchain_param.branch_callstack = 1;
  696. symbol_conf.use_callchain = true;
  697. callchain_register_param(&callchain_param);
  698. if (sort_order == NULL)
  699. sort_order = "srcline,symbol,dso";
  700. }
  701. if (report.mem_mode) {
  702. if (sort__mode == SORT_MODE__BRANCH) {
  703. pr_err("branch and mem mode incompatible\n");
  704. goto error;
  705. }
  706. sort__mode = SORT_MODE__MEMORY;
  707. symbol_conf.cumulate_callchain = false;
  708. }
  709. if (setup_sorting() < 0) {
  710. if (sort_order)
  711. parse_options_usage(report_usage, options, "s", 1);
  712. if (field_order)
  713. parse_options_usage(sort_order ? NULL : report_usage,
  714. options, "F", 1);
  715. goto error;
  716. }
  717. /* Force tty output for header output and per-thread stat. */
  718. if (report.header || report.header_only || report.show_threads)
  719. use_browser = 0;
  720. if (strcmp(input_name, "-") != 0)
  721. setup_browser(true);
  722. else
  723. use_browser = 0;
  724. if (report.header || report.header_only) {
  725. perf_session__fprintf_info(session, stdout,
  726. report.show_full_info);
  727. if (report.header_only) {
  728. ret = 0;
  729. goto error;
  730. }
  731. } else if (use_browser == 0) {
  732. fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
  733. stdout);
  734. }
  735. /*
  736. * Only in the TUI browser we are doing integrated annotation,
  737. * so don't allocate extra space that won't be used in the stdio
  738. * implementation.
  739. */
  740. if (ui__has_annotation()) {
  741. symbol_conf.priv_size = sizeof(struct annotation);
  742. machines__set_symbol_filter(&session->machines,
  743. symbol__annotate_init);
  744. /*
  745. * For searching by name on the "Browse map details".
  746. * providing it only in verbose mode not to bloat too
  747. * much struct symbol.
  748. */
  749. if (verbose) {
  750. /*
  751. * XXX: Need to provide a less kludgy way to ask for
  752. * more space per symbol, the u32 is for the index on
  753. * the ui browser.
  754. * See symbol__browser_index.
  755. */
  756. symbol_conf.priv_size += sizeof(u32);
  757. symbol_conf.sort_by_name = true;
  758. }
  759. }
  760. if (symbol__init(&session->header.env) < 0)
  761. goto error;
  762. if (argc) {
  763. /*
  764. * Special case: if there's an argument left then assume that
  765. * it's a symbol filter:
  766. */
  767. if (argc > 1)
  768. usage_with_options(report_usage, options);
  769. report.symbol_filter_str = argv[0];
  770. }
  771. sort__setup_elide(stdout);
  772. ret = __cmd_report(&report);
  773. if (ret == K_SWITCH_INPUT_DATA) {
  774. perf_session__delete(session);
  775. goto repeat;
  776. } else
  777. ret = 0;
  778. error:
  779. perf_session__delete(session);
  780. return ret;
  781. }