builtin-report.c 30 KB

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