builtin-report.c 32 KB

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