builtin-report.c 29 KB

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