builtin-report.c 29 KB

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