builtin-report.c 30 KB

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