builtin-report.c 26 KB

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