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