builtin-report.c 40 KB

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