builtin-report.c 30 KB

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