builtin-report.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 (quiet)
  279. return 0;
  280. if (symbol_conf.filter_relative) {
  281. nr_samples = hists->stats.nr_non_filtered_samples;
  282. nr_events = hists->stats.total_non_filtered_period;
  283. }
  284. if (perf_evsel__is_group_event(evsel)) {
  285. struct perf_evsel *pos;
  286. perf_evsel__group_desc(evsel, buf, size);
  287. evname = buf;
  288. for_each_group_member(pos, evsel) {
  289. const struct hists *pos_hists = evsel__hists(pos);
  290. if (symbol_conf.filter_relative) {
  291. nr_samples += pos_hists->stats.nr_non_filtered_samples;
  292. nr_events += pos_hists->stats.total_non_filtered_period;
  293. } else {
  294. nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
  295. nr_events += pos_hists->stats.total_period;
  296. }
  297. }
  298. }
  299. nr_samples = convert_unit(nr_samples, &unit);
  300. ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
  301. if (evname != NULL)
  302. ret += fprintf(fp, " of event '%s'", evname);
  303. if (symbol_conf.show_ref_callgraph &&
  304. strstr(evname, "call-graph=no")) {
  305. ret += fprintf(fp, ", show reference callgraph");
  306. }
  307. if (rep->mem_mode) {
  308. ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
  309. ret += fprintf(fp, "\n# Sort order : %s", sort_order ? : default_mem_sort_order);
  310. } else
  311. ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
  312. if (socked_id > -1)
  313. ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
  314. return ret + fprintf(fp, "\n#\n");
  315. }
  316. static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
  317. struct report *rep,
  318. const char *help)
  319. {
  320. struct perf_evsel *pos;
  321. if (!quiet) {
  322. fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n",
  323. evlist->stats.total_lost_samples);
  324. }
  325. evlist__for_each_entry(evlist, pos) {
  326. struct hists *hists = evsel__hists(pos);
  327. const char *evname = perf_evsel__name(pos);
  328. if (symbol_conf.event_group &&
  329. !perf_evsel__is_group_leader(pos))
  330. continue;
  331. hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
  332. hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout,
  333. symbol_conf.use_callchain);
  334. fprintf(stdout, "\n\n");
  335. }
  336. if (!quiet)
  337. fprintf(stdout, "#\n# (%s)\n#\n", help);
  338. if (rep->show_threads) {
  339. bool style = !strcmp(rep->pretty_printing_style, "raw");
  340. perf_read_values_display(stdout, &rep->show_threads_values,
  341. style);
  342. perf_read_values_destroy(&rep->show_threads_values);
  343. }
  344. return 0;
  345. }
  346. static void report__warn_kptr_restrict(const struct report *rep)
  347. {
  348. struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
  349. struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
  350. if (kernel_map == NULL ||
  351. (kernel_map->dso->hit &&
  352. (kernel_kmap->ref_reloc_sym == NULL ||
  353. kernel_kmap->ref_reloc_sym->addr == 0))) {
  354. const char *desc =
  355. "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
  356. "can't be resolved.";
  357. if (kernel_map) {
  358. const struct dso *kdso = kernel_map->dso;
  359. if (!RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION])) {
  360. desc = "If some relocation was applied (e.g. "
  361. "kexec) symbols may be misresolved.";
  362. }
  363. }
  364. ui__warning(
  365. "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
  366. "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
  367. "Samples in kernel modules can't be resolved as well.\n\n",
  368. desc);
  369. }
  370. }
  371. static int report__gtk_browse_hists(struct report *rep, const char *help)
  372. {
  373. int (*hist_browser)(struct perf_evlist *evlist, const char *help,
  374. struct hist_browser_timer *timer, float min_pcnt);
  375. hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
  376. if (hist_browser == NULL) {
  377. ui__error("GTK browser not found!\n");
  378. return -1;
  379. }
  380. return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
  381. }
  382. static int report__browse_hists(struct report *rep)
  383. {
  384. int ret;
  385. struct perf_session *session = rep->session;
  386. struct perf_evlist *evlist = session->evlist;
  387. const char *help = perf_tip(system_path(TIPDIR));
  388. if (help == NULL) {
  389. /* fallback for people who don't install perf ;-) */
  390. help = perf_tip(DOCDIR);
  391. if (help == NULL)
  392. help = "Cannot load tips.txt file, please install perf!";
  393. }
  394. switch (use_browser) {
  395. case 1:
  396. ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
  397. rep->min_percent,
  398. &session->header.env);
  399. /*
  400. * Usually "ret" is the last pressed key, and we only
  401. * care if the key notifies us to switch data file.
  402. */
  403. if (ret != K_SWITCH_INPUT_DATA)
  404. ret = 0;
  405. break;
  406. case 2:
  407. ret = report__gtk_browse_hists(rep, help);
  408. break;
  409. default:
  410. ret = perf_evlist__tty_browse_hists(evlist, rep, help);
  411. break;
  412. }
  413. return ret;
  414. }
  415. static int report__collapse_hists(struct report *rep)
  416. {
  417. struct ui_progress prog;
  418. struct perf_evsel *pos;
  419. int ret = 0;
  420. ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
  421. evlist__for_each_entry(rep->session->evlist, pos) {
  422. struct hists *hists = evsel__hists(pos);
  423. if (pos->idx == 0)
  424. hists->symbol_filter_str = rep->symbol_filter_str;
  425. hists->socket_filter = rep->socket_filter;
  426. ret = hists__collapse_resort(hists, &prog);
  427. if (ret < 0)
  428. break;
  429. /* Non-group events are considered as leader */
  430. if (symbol_conf.event_group &&
  431. !perf_evsel__is_group_leader(pos)) {
  432. struct hists *leader_hists = evsel__hists(pos->leader);
  433. hists__match(leader_hists, hists);
  434. hists__link(leader_hists, hists);
  435. }
  436. }
  437. ui_progress__finish();
  438. return ret;
  439. }
  440. static void report__output_resort(struct report *rep)
  441. {
  442. struct ui_progress prog;
  443. struct perf_evsel *pos;
  444. ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
  445. evlist__for_each_entry(rep->session->evlist, pos)
  446. perf_evsel__output_resort(pos, &prog);
  447. ui_progress__finish();
  448. }
  449. static int __cmd_report(struct report *rep)
  450. {
  451. int ret;
  452. struct perf_session *session = rep->session;
  453. struct perf_evsel *pos;
  454. struct perf_data_file *file = session->file;
  455. signal(SIGINT, sig_handler);
  456. if (rep->cpu_list) {
  457. ret = perf_session__cpu_bitmap(session, rep->cpu_list,
  458. rep->cpu_bitmap);
  459. if (ret) {
  460. ui__error("failed to set cpu bitmap\n");
  461. return ret;
  462. }
  463. }
  464. if (rep->show_threads) {
  465. ret = perf_read_values_init(&rep->show_threads_values);
  466. if (ret)
  467. return ret;
  468. }
  469. ret = report__setup_sample_type(rep);
  470. if (ret) {
  471. /* report__setup_sample_type() already showed error message */
  472. return ret;
  473. }
  474. ret = perf_session__process_events(session);
  475. if (ret) {
  476. ui__error("failed to process sample\n");
  477. return ret;
  478. }
  479. report__warn_kptr_restrict(rep);
  480. evlist__for_each_entry(session->evlist, pos)
  481. rep->nr_entries += evsel__hists(pos)->nr_entries;
  482. if (use_browser == 0) {
  483. if (verbose > 3)
  484. perf_session__fprintf(session, stdout);
  485. if (verbose > 2)
  486. perf_session__fprintf_dsos(session, stdout);
  487. if (dump_trace) {
  488. perf_session__fprintf_nr_events(session, stdout);
  489. perf_evlist__fprintf_nr_events(session->evlist, stdout);
  490. return 0;
  491. }
  492. }
  493. ret = report__collapse_hists(rep);
  494. if (ret) {
  495. ui__error("failed to process hist entry\n");
  496. return ret;
  497. }
  498. if (session_done())
  499. return 0;
  500. /*
  501. * recalculate number of entries after collapsing since it
  502. * might be changed during the collapse phase.
  503. */
  504. rep->nr_entries = 0;
  505. evlist__for_each_entry(session->evlist, pos)
  506. rep->nr_entries += evsel__hists(pos)->nr_entries;
  507. if (rep->nr_entries == 0) {
  508. ui__error("The %s file has no samples!\n", file->path);
  509. return 0;
  510. }
  511. report__output_resort(rep);
  512. return report__browse_hists(rep);
  513. }
  514. static int
  515. report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  516. {
  517. struct callchain_param *callchain = opt->value;
  518. callchain->enabled = !unset;
  519. /*
  520. * --no-call-graph
  521. */
  522. if (unset) {
  523. symbol_conf.use_callchain = false;
  524. callchain->mode = CHAIN_NONE;
  525. return 0;
  526. }
  527. return parse_callchain_report_opt(arg);
  528. }
  529. int
  530. report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
  531. const char *arg, int unset __maybe_unused)
  532. {
  533. if (arg) {
  534. int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
  535. if (err) {
  536. char buf[BUFSIZ];
  537. regerror(err, &ignore_callees_regex, buf, sizeof(buf));
  538. pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
  539. return -1;
  540. }
  541. have_ignore_callees = 1;
  542. }
  543. return 0;
  544. }
  545. static int
  546. parse_branch_mode(const struct option *opt,
  547. const char *str __maybe_unused, int unset)
  548. {
  549. int *branch_mode = opt->value;
  550. *branch_mode = !unset;
  551. return 0;
  552. }
  553. static int
  554. parse_percent_limit(const struct option *opt, const char *str,
  555. int unset __maybe_unused)
  556. {
  557. struct report *rep = opt->value;
  558. double pcnt = strtof(str, NULL);
  559. rep->min_percent = pcnt;
  560. callchain_param.min_percent = pcnt;
  561. return 0;
  562. }
  563. #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
  564. const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
  565. CALLCHAIN_REPORT_HELP
  566. "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
  567. int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
  568. {
  569. struct perf_session *session;
  570. struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
  571. struct stat st;
  572. bool has_br_stack = false;
  573. int branch_mode = -1;
  574. bool branch_call_mode = false;
  575. char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
  576. const char * const report_usage[] = {
  577. "perf report [<options>]",
  578. NULL
  579. };
  580. struct report report = {
  581. .tool = {
  582. .sample = process_sample_event,
  583. .mmap = perf_event__process_mmap,
  584. .mmap2 = perf_event__process_mmap2,
  585. .comm = perf_event__process_comm,
  586. .namespaces = perf_event__process_namespaces,
  587. .exit = perf_event__process_exit,
  588. .fork = perf_event__process_fork,
  589. .lost = perf_event__process_lost,
  590. .read = process_read_event,
  591. .attr = perf_event__process_attr,
  592. .tracing_data = perf_event__process_tracing_data,
  593. .build_id = perf_event__process_build_id,
  594. .id_index = perf_event__process_id_index,
  595. .auxtrace_info = perf_event__process_auxtrace_info,
  596. .auxtrace = perf_event__process_auxtrace,
  597. .ordered_events = true,
  598. .ordering_requires_timestamps = true,
  599. },
  600. .max_stack = PERF_MAX_STACK_DEPTH,
  601. .pretty_printing_style = "normal",
  602. .socket_filter = -1,
  603. };
  604. const struct option options[] = {
  605. OPT_STRING('i', "input", &input_name, "file",
  606. "input file name"),
  607. OPT_INCR('v', "verbose", &verbose,
  608. "be more verbose (show symbol address, etc)"),
  609. OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
  610. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  611. "dump raw trace in ASCII"),
  612. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  613. "file", "vmlinux pathname"),
  614. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  615. "file", "kallsyms pathname"),
  616. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  617. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  618. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  619. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  620. "Show a column with the number of samples"),
  621. OPT_BOOLEAN('T', "threads", &report.show_threads,
  622. "Show per-thread event counters"),
  623. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  624. "pretty printing style key: normal raw"),
  625. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  626. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  627. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  628. "Use the stdio interface"),
  629. OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
  630. OPT_BOOLEAN(0, "header-only", &report.header_only,
  631. "Show only data header."),
  632. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  633. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  634. " Please refer the man page for the complete list."),
  635. OPT_STRING('F', "fields", &field_order, "key[,keys...]",
  636. "output field(s): overhead, period, sample plus all of sort keys"),
  637. OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
  638. "Show sample percentage for different cpu modes"),
  639. OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  640. "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
  641. OPT_STRING('p', "parent", &parent_pattern, "regex",
  642. "regex filter to identify parent, see: '--sort parent'"),
  643. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  644. "Only display entries with parent-match"),
  645. OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
  646. "print_type,threshold[,print_limit],order,sort_key[,branch],value",
  647. report_callchain_help, &report_parse_callchain_opt,
  648. callchain_default_opt),
  649. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  650. "Accumulate callchains of children and show total overhead as well"),
  651. OPT_INTEGER(0, "max-stack", &report.max_stack,
  652. "Set the maximum stack depth when parsing the callchain, "
  653. "anything beyond the specified depth will be ignored. "
  654. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  655. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  656. "alias for inverted call graph"),
  657. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  658. "ignore callees of these functions in call graphs",
  659. report_parse_ignore_callees_opt),
  660. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  661. "only consider symbols in these dsos"),
  662. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  663. "only consider symbols in these comms"),
  664. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  665. "only consider symbols in these pids"),
  666. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  667. "only consider symbols in these tids"),
  668. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  669. "only consider these symbols"),
  670. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  671. "only show symbols that (partially) match with this filter"),
  672. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  673. "width[,width...]",
  674. "don't try to adjust column width, use these fixed values"),
  675. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  676. "separator for columns, no spaces will be added between "
  677. "columns '.' is reserved."),
  678. OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
  679. "Only display entries resolved to a symbol"),
  680. OPT_CALLBACK(0, "symfs", NULL, "directory",
  681. "Look for files with symbols relative to this directory",
  682. symbol__config_symfs),
  683. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  684. "list of cpus to profile"),
  685. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  686. "Display extended information about perf.data file"),
  687. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  688. "Interleave source code with assembly code (default)"),
  689. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  690. "Display raw encoding of assembly instructions (default)"),
  691. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  692. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  693. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  694. "Show a column with the sum of periods"),
  695. OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
  696. "Show event group information together"),
  697. OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
  698. "use branch records for per branch histogram filling",
  699. parse_branch_mode),
  700. OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
  701. "add last branch records to call history"),
  702. OPT_STRING(0, "objdump", &objdump_path, "path",
  703. "objdump binary to use for disassembly and annotations"),
  704. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  705. "Disable symbol demangling"),
  706. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  707. "Enable kernel symbol demangling"),
  708. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  709. OPT_CALLBACK(0, "percent-limit", &report, "percent",
  710. "Don't show entries under that percent", parse_percent_limit),
  711. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  712. "how to display percentage of filtered entries", parse_filter_percentage),
  713. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  714. "Instruction Tracing options",
  715. itrace_parse_synth_opts),
  716. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  717. "Show full source file name path for source lines"),
  718. OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
  719. "Show callgraph from reference event"),
  720. OPT_INTEGER(0, "socket-filter", &report.socket_filter,
  721. "only show processor socket that match with this filter"),
  722. OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
  723. "Show raw trace event output (do not use print fmt or plugins)"),
  724. OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
  725. "Show entries in a hierarchy"),
  726. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  727. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  728. stdio__config_color, "always"),
  729. OPT_STRING(0, "time", &report.time_str, "str",
  730. "Time span of interest (start,stop)"),
  731. OPT_END()
  732. };
  733. struct perf_data_file file = {
  734. .mode = PERF_DATA_MODE_READ,
  735. };
  736. int ret = hists__init();
  737. if (ret < 0)
  738. return ret;
  739. ret = perf_config(report__config, &report);
  740. if (ret)
  741. return ret;
  742. argc = parse_options(argc, argv, options, report_usage, 0);
  743. if (argc) {
  744. /*
  745. * Special case: if there's an argument left then assume that
  746. * it's a symbol filter:
  747. */
  748. if (argc > 1)
  749. usage_with_options(report_usage, options);
  750. report.symbol_filter_str = argv[0];
  751. }
  752. if (quiet)
  753. perf_quiet_option();
  754. if (symbol_conf.vmlinux_name &&
  755. access(symbol_conf.vmlinux_name, R_OK)) {
  756. pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
  757. return -EINVAL;
  758. }
  759. if (symbol_conf.kallsyms_name &&
  760. access(symbol_conf.kallsyms_name, R_OK)) {
  761. pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
  762. return -EINVAL;
  763. }
  764. if (report.use_stdio)
  765. use_browser = 0;
  766. else if (report.use_tui)
  767. use_browser = 1;
  768. else if (report.use_gtk)
  769. use_browser = 2;
  770. if (report.inverted_callchain)
  771. callchain_param.order = ORDER_CALLER;
  772. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  773. callchain_param.order = ORDER_CALLER;
  774. if (itrace_synth_opts.callchain &&
  775. (int)itrace_synth_opts.callchain_sz > report.max_stack)
  776. report.max_stack = itrace_synth_opts.callchain_sz;
  777. if (!input_name || !strlen(input_name)) {
  778. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  779. input_name = "-";
  780. else
  781. input_name = "perf.data";
  782. }
  783. file.path = input_name;
  784. file.force = symbol_conf.force;
  785. repeat:
  786. session = perf_session__new(&file, false, &report.tool);
  787. if (session == NULL)
  788. return -1;
  789. if (report.queue_size) {
  790. ordered_events__set_alloc_size(&session->ordered_events,
  791. report.queue_size);
  792. }
  793. session->itrace_synth_opts = &itrace_synth_opts;
  794. report.session = session;
  795. has_br_stack = perf_header__has_feat(&session->header,
  796. HEADER_BRANCH_STACK);
  797. if (itrace_synth_opts.last_branch)
  798. has_br_stack = true;
  799. if (has_br_stack && branch_call_mode)
  800. symbol_conf.show_branchflag_count = true;
  801. /*
  802. * Branch mode is a tristate:
  803. * -1 means default, so decide based on the file having branch data.
  804. * 0/1 means the user chose a mode.
  805. */
  806. if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
  807. !branch_call_mode) {
  808. sort__mode = SORT_MODE__BRANCH;
  809. symbol_conf.cumulate_callchain = false;
  810. }
  811. if (branch_call_mode) {
  812. callchain_param.key = CCKEY_ADDRESS;
  813. callchain_param.branch_callstack = 1;
  814. symbol_conf.use_callchain = true;
  815. callchain_register_param(&callchain_param);
  816. if (sort_order == NULL)
  817. sort_order = "srcline,symbol,dso";
  818. }
  819. if (report.mem_mode) {
  820. if (sort__mode == SORT_MODE__BRANCH) {
  821. pr_err("branch and mem mode incompatible\n");
  822. goto error;
  823. }
  824. sort__mode = SORT_MODE__MEMORY;
  825. symbol_conf.cumulate_callchain = false;
  826. }
  827. if (symbol_conf.report_hierarchy) {
  828. /* disable incompatible options */
  829. symbol_conf.cumulate_callchain = false;
  830. if (field_order) {
  831. pr_err("Error: --hierarchy and --fields options cannot be used together\n");
  832. parse_options_usage(report_usage, options, "F", 1);
  833. parse_options_usage(NULL, options, "hierarchy", 0);
  834. goto error;
  835. }
  836. perf_hpp_list.need_collapse = true;
  837. }
  838. /* Force tty output for header output and per-thread stat. */
  839. if (report.header || report.header_only || report.show_threads)
  840. use_browser = 0;
  841. if (strcmp(input_name, "-") != 0)
  842. setup_browser(true);
  843. else
  844. use_browser = 0;
  845. if (setup_sorting(session->evlist) < 0) {
  846. if (sort_order)
  847. parse_options_usage(report_usage, options, "s", 1);
  848. if (field_order)
  849. parse_options_usage(sort_order ? NULL : report_usage,
  850. options, "F", 1);
  851. goto error;
  852. }
  853. if ((report.header || report.header_only) && !quiet) {
  854. perf_session__fprintf_info(session, stdout,
  855. report.show_full_info);
  856. if (report.header_only) {
  857. ret = 0;
  858. goto error;
  859. }
  860. } else if (use_browser == 0 && !quiet) {
  861. fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
  862. stdout);
  863. }
  864. /*
  865. * Only in the TUI browser we are doing integrated annotation,
  866. * so don't allocate extra space that won't be used in the stdio
  867. * implementation.
  868. */
  869. if (ui__has_annotation()) {
  870. ret = symbol__annotation_init();
  871. if (ret < 0)
  872. goto error;
  873. /*
  874. * For searching by name on the "Browse map details".
  875. * providing it only in verbose mode not to bloat too
  876. * much struct symbol.
  877. */
  878. if (verbose > 0) {
  879. /*
  880. * XXX: Need to provide a less kludgy way to ask for
  881. * more space per symbol, the u32 is for the index on
  882. * the ui browser.
  883. * See symbol__browser_index.
  884. */
  885. symbol_conf.priv_size += sizeof(u32);
  886. symbol_conf.sort_by_name = true;
  887. }
  888. }
  889. if (symbol__init(&session->header.env) < 0)
  890. goto error;
  891. if (perf_time__parse_str(&report.ptime, report.time_str) != 0) {
  892. pr_err("Invalid time string\n");
  893. return -EINVAL;
  894. }
  895. sort__setup_elide(stdout);
  896. ret = __cmd_report(&report);
  897. if (ret == K_SWITCH_INPUT_DATA) {
  898. perf_session__delete(session);
  899. goto repeat;
  900. } else
  901. ret = 0;
  902. error:
  903. perf_session__delete(session);
  904. return ret;
  905. }