builtin-report.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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. return 0;
  332. }
  333. static void sig_handler(int sig __maybe_unused)
  334. {
  335. session_done = 1;
  336. }
  337. static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep,
  338. const char *evname, FILE *fp)
  339. {
  340. size_t ret;
  341. char unit;
  342. unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  343. u64 nr_events = hists->stats.total_period;
  344. struct perf_evsel *evsel = hists_to_evsel(hists);
  345. char buf[512];
  346. size_t size = sizeof(buf);
  347. int socked_id = hists->socket_filter;
  348. if (quiet)
  349. return 0;
  350. if (symbol_conf.filter_relative) {
  351. nr_samples = hists->stats.nr_non_filtered_samples;
  352. nr_events = hists->stats.total_non_filtered_period;
  353. }
  354. if (perf_evsel__is_group_event(evsel)) {
  355. struct perf_evsel *pos;
  356. perf_evsel__group_desc(evsel, buf, size);
  357. evname = buf;
  358. for_each_group_member(pos, evsel) {
  359. const struct hists *pos_hists = evsel__hists(pos);
  360. if (symbol_conf.filter_relative) {
  361. nr_samples += pos_hists->stats.nr_non_filtered_samples;
  362. nr_events += pos_hists->stats.total_non_filtered_period;
  363. } else {
  364. nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
  365. nr_events += pos_hists->stats.total_period;
  366. }
  367. }
  368. }
  369. nr_samples = convert_unit(nr_samples, &unit);
  370. ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
  371. if (evname != NULL) {
  372. ret += fprintf(fp, " of event%s '%s'",
  373. evsel->nr_members > 1 ? "s" : "", evname);
  374. }
  375. if (rep->time_str)
  376. ret += fprintf(fp, " (time slices: %s)", rep->time_str);
  377. if (symbol_conf.show_ref_callgraph &&
  378. strstr(evname, "call-graph=no")) {
  379. ret += fprintf(fp, ", show reference callgraph");
  380. }
  381. if (rep->mem_mode) {
  382. ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
  383. ret += fprintf(fp, "\n# Sort order : %s", sort_order ? : default_mem_sort_order);
  384. } else
  385. ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
  386. if (socked_id > -1)
  387. ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
  388. return ret + fprintf(fp, "\n#\n");
  389. }
  390. static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
  391. struct report *rep,
  392. const char *help)
  393. {
  394. struct perf_evsel *pos;
  395. if (!quiet) {
  396. fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n",
  397. evlist->stats.total_lost_samples);
  398. }
  399. evlist__for_each_entry(evlist, pos) {
  400. struct hists *hists = evsel__hists(pos);
  401. const char *evname = perf_evsel__name(pos);
  402. if (symbol_conf.event_group &&
  403. !perf_evsel__is_group_leader(pos))
  404. continue;
  405. hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
  406. hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout,
  407. !(symbol_conf.use_callchain ||
  408. symbol_conf.show_branchflag_count));
  409. fprintf(stdout, "\n\n");
  410. }
  411. if (!quiet)
  412. fprintf(stdout, "#\n# (%s)\n#\n", help);
  413. if (rep->show_threads) {
  414. bool style = !strcmp(rep->pretty_printing_style, "raw");
  415. perf_read_values_display(stdout, &rep->show_threads_values,
  416. style);
  417. perf_read_values_destroy(&rep->show_threads_values);
  418. }
  419. if (sort__mode == SORT_MODE__BRANCH)
  420. branch_type_stat_display(stdout, &rep->brtype_stat);
  421. return 0;
  422. }
  423. static void report__warn_kptr_restrict(const struct report *rep)
  424. {
  425. struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
  426. struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
  427. if (perf_evlist__exclude_kernel(rep->session->evlist))
  428. return;
  429. if (kernel_map == NULL ||
  430. (kernel_map->dso->hit &&
  431. (kernel_kmap->ref_reloc_sym == NULL ||
  432. kernel_kmap->ref_reloc_sym->addr == 0))) {
  433. const char *desc =
  434. "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
  435. "can't be resolved.";
  436. if (kernel_map && map__has_symbols(kernel_map)) {
  437. desc = "If some relocation was applied (e.g. "
  438. "kexec) symbols may be misresolved.";
  439. }
  440. ui__warning(
  441. "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
  442. "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
  443. "Samples in kernel modules can't be resolved as well.\n\n",
  444. desc);
  445. }
  446. }
  447. static int report__gtk_browse_hists(struct report *rep, const char *help)
  448. {
  449. int (*hist_browser)(struct perf_evlist *evlist, const char *help,
  450. struct hist_browser_timer *timer, float min_pcnt);
  451. hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
  452. if (hist_browser == NULL) {
  453. ui__error("GTK browser not found!\n");
  454. return -1;
  455. }
  456. return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
  457. }
  458. static int report__browse_hists(struct report *rep)
  459. {
  460. int ret;
  461. struct perf_session *session = rep->session;
  462. struct perf_evlist *evlist = session->evlist;
  463. const char *help = perf_tip(system_path(TIPDIR));
  464. if (help == NULL) {
  465. /* fallback for people who don't install perf ;-) */
  466. help = perf_tip(DOCDIR);
  467. if (help == NULL)
  468. help = "Cannot load tips.txt file, please install perf!";
  469. }
  470. switch (use_browser) {
  471. case 1:
  472. ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
  473. rep->min_percent,
  474. &session->header.env,
  475. true, &rep->annotation_opts);
  476. /*
  477. * Usually "ret" is the last pressed key, and we only
  478. * care if the key notifies us to switch data file.
  479. */
  480. if (ret != K_SWITCH_INPUT_DATA)
  481. ret = 0;
  482. break;
  483. case 2:
  484. ret = report__gtk_browse_hists(rep, help);
  485. break;
  486. default:
  487. ret = perf_evlist__tty_browse_hists(evlist, rep, help);
  488. break;
  489. }
  490. return ret;
  491. }
  492. static int report__collapse_hists(struct report *rep)
  493. {
  494. struct ui_progress prog;
  495. struct perf_evsel *pos;
  496. int ret = 0;
  497. ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
  498. evlist__for_each_entry(rep->session->evlist, pos) {
  499. struct hists *hists = evsel__hists(pos);
  500. if (pos->idx == 0)
  501. hists->symbol_filter_str = rep->symbol_filter_str;
  502. hists->socket_filter = rep->socket_filter;
  503. ret = hists__collapse_resort(hists, &prog);
  504. if (ret < 0)
  505. break;
  506. /* Non-group events are considered as leader */
  507. if (symbol_conf.event_group &&
  508. !perf_evsel__is_group_leader(pos)) {
  509. struct hists *leader_hists = evsel__hists(pos->leader);
  510. hists__match(leader_hists, hists);
  511. hists__link(leader_hists, hists);
  512. }
  513. }
  514. ui_progress__finish();
  515. return ret;
  516. }
  517. static void report__output_resort(struct report *rep)
  518. {
  519. struct ui_progress prog;
  520. struct perf_evsel *pos;
  521. ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
  522. evlist__for_each_entry(rep->session->evlist, pos)
  523. perf_evsel__output_resort(pos, &prog);
  524. ui_progress__finish();
  525. }
  526. static void stats_setup(struct report *rep)
  527. {
  528. memset(&rep->tool, 0, sizeof(rep->tool));
  529. rep->tool.no_warn = true;
  530. }
  531. static int stats_print(struct report *rep)
  532. {
  533. struct perf_session *session = rep->session;
  534. perf_session__fprintf_nr_events(session, stdout);
  535. return 0;
  536. }
  537. static void tasks_setup(struct report *rep)
  538. {
  539. memset(&rep->tool, 0, sizeof(rep->tool));
  540. rep->tool.ordered_events = true;
  541. if (rep->mmaps_mode) {
  542. rep->tool.mmap = perf_event__process_mmap;
  543. rep->tool.mmap2 = perf_event__process_mmap2;
  544. }
  545. rep->tool.comm = perf_event__process_comm;
  546. rep->tool.exit = perf_event__process_exit;
  547. rep->tool.fork = perf_event__process_fork;
  548. rep->tool.no_warn = true;
  549. }
  550. struct task {
  551. struct thread *thread;
  552. struct list_head list;
  553. struct list_head children;
  554. };
  555. static struct task *tasks_list(struct task *task, struct machine *machine)
  556. {
  557. struct thread *parent_thread, *thread = task->thread;
  558. struct task *parent_task;
  559. /* Already listed. */
  560. if (!list_empty(&task->list))
  561. return NULL;
  562. /* Last one in the chain. */
  563. if (thread->ppid == -1)
  564. return task;
  565. parent_thread = machine__find_thread(machine, -1, thread->ppid);
  566. if (!parent_thread)
  567. return ERR_PTR(-ENOENT);
  568. parent_task = thread__priv(parent_thread);
  569. list_add_tail(&task->list, &parent_task->children);
  570. return tasks_list(parent_task, machine);
  571. }
  572. static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
  573. {
  574. size_t printed = 0;
  575. struct rb_node *nd;
  576. for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
  577. struct map *map = rb_entry(nd, struct map, rb_node);
  578. printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n",
  579. indent, "", map->start, map->end,
  580. map->prot & PROT_READ ? 'r' : '-',
  581. map->prot & PROT_WRITE ? 'w' : '-',
  582. map->prot & PROT_EXEC ? 'x' : '-',
  583. map->flags & MAP_SHARED ? 's' : 'p',
  584. map->pgoff,
  585. map->ino, map->dso->name);
  586. }
  587. return printed;
  588. }
  589. static int map_groups__fprintf_task(struct map_groups *mg, int indent, FILE *fp)
  590. {
  591. return maps__fprintf_task(&mg->maps, indent, fp);
  592. }
  593. static void task__print_level(struct task *task, FILE *fp, int level)
  594. {
  595. struct thread *thread = task->thread;
  596. struct task *child;
  597. int comm_indent = fprintf(fp, " %8d %8d %8d |%*s",
  598. thread->pid_, thread->tid, thread->ppid,
  599. level, "");
  600. fprintf(fp, "%s\n", thread__comm_str(thread));
  601. map_groups__fprintf_task(thread->mg, comm_indent, fp);
  602. if (!list_empty(&task->children)) {
  603. list_for_each_entry(child, &task->children, list)
  604. task__print_level(child, fp, level + 1);
  605. }
  606. }
  607. static int tasks_print(struct report *rep, FILE *fp)
  608. {
  609. struct perf_session *session = rep->session;
  610. struct machine *machine = &session->machines.host;
  611. struct task *tasks, *task;
  612. unsigned int nr = 0, itask = 0, i;
  613. struct rb_node *nd;
  614. LIST_HEAD(list);
  615. /*
  616. * No locking needed while accessing machine->threads,
  617. * because --tasks is single threaded command.
  618. */
  619. /* Count all the threads. */
  620. for (i = 0; i < THREADS__TABLE_SIZE; i++)
  621. nr += machine->threads[i].nr;
  622. tasks = malloc(sizeof(*tasks) * nr);
  623. if (!tasks)
  624. return -ENOMEM;
  625. for (i = 0; i < THREADS__TABLE_SIZE; i++) {
  626. struct threads *threads = &machine->threads[i];
  627. for (nd = rb_first(&threads->entries); nd; nd = rb_next(nd)) {
  628. task = tasks + itask++;
  629. task->thread = rb_entry(nd, struct thread, rb_node);
  630. INIT_LIST_HEAD(&task->children);
  631. INIT_LIST_HEAD(&task->list);
  632. thread__set_priv(task->thread, task);
  633. }
  634. }
  635. /*
  636. * Iterate every task down to the unprocessed parent
  637. * and link all in task children list. Task with no
  638. * parent is added into 'list'.
  639. */
  640. for (itask = 0; itask < nr; itask++) {
  641. task = tasks + itask;
  642. if (!list_empty(&task->list))
  643. continue;
  644. task = tasks_list(task, machine);
  645. if (IS_ERR(task)) {
  646. pr_err("Error: failed to process tasks\n");
  647. free(tasks);
  648. return PTR_ERR(task);
  649. }
  650. if (task)
  651. list_add_tail(&task->list, &list);
  652. }
  653. fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm");
  654. list_for_each_entry(task, &list, list)
  655. task__print_level(task, fp, 0);
  656. free(tasks);
  657. return 0;
  658. }
  659. static int __cmd_report(struct report *rep)
  660. {
  661. int ret;
  662. struct perf_session *session = rep->session;
  663. struct perf_evsel *pos;
  664. struct perf_data *data = session->data;
  665. signal(SIGINT, sig_handler);
  666. if (rep->cpu_list) {
  667. ret = perf_session__cpu_bitmap(session, rep->cpu_list,
  668. rep->cpu_bitmap);
  669. if (ret) {
  670. ui__error("failed to set cpu bitmap\n");
  671. return ret;
  672. }
  673. session->itrace_synth_opts->cpu_bitmap = rep->cpu_bitmap;
  674. }
  675. if (rep->show_threads) {
  676. ret = perf_read_values_init(&rep->show_threads_values);
  677. if (ret)
  678. return ret;
  679. }
  680. ret = report__setup_sample_type(rep);
  681. if (ret) {
  682. /* report__setup_sample_type() already showed error message */
  683. return ret;
  684. }
  685. if (rep->stats_mode)
  686. stats_setup(rep);
  687. if (rep->tasks_mode)
  688. tasks_setup(rep);
  689. ret = perf_session__process_events(session);
  690. if (ret) {
  691. ui__error("failed to process sample\n");
  692. return ret;
  693. }
  694. if (rep->stats_mode)
  695. return stats_print(rep);
  696. if (rep->tasks_mode)
  697. return tasks_print(rep, stdout);
  698. report__warn_kptr_restrict(rep);
  699. evlist__for_each_entry(session->evlist, pos)
  700. rep->nr_entries += evsel__hists(pos)->nr_entries;
  701. if (use_browser == 0) {
  702. if (verbose > 3)
  703. perf_session__fprintf(session, stdout);
  704. if (verbose > 2)
  705. perf_session__fprintf_dsos(session, stdout);
  706. if (dump_trace) {
  707. perf_session__fprintf_nr_events(session, stdout);
  708. perf_evlist__fprintf_nr_events(session->evlist, stdout);
  709. return 0;
  710. }
  711. }
  712. ret = report__collapse_hists(rep);
  713. if (ret) {
  714. ui__error("failed to process hist entry\n");
  715. return ret;
  716. }
  717. if (session_done())
  718. return 0;
  719. /*
  720. * recalculate number of entries after collapsing since it
  721. * might be changed during the collapse phase.
  722. */
  723. rep->nr_entries = 0;
  724. evlist__for_each_entry(session->evlist, pos)
  725. rep->nr_entries += evsel__hists(pos)->nr_entries;
  726. if (rep->nr_entries == 0) {
  727. ui__error("The %s file has no samples!\n", data->file.path);
  728. return 0;
  729. }
  730. report__output_resort(rep);
  731. return report__browse_hists(rep);
  732. }
  733. static int
  734. report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  735. {
  736. struct callchain_param *callchain = opt->value;
  737. callchain->enabled = !unset;
  738. /*
  739. * --no-call-graph
  740. */
  741. if (unset) {
  742. symbol_conf.use_callchain = false;
  743. callchain->mode = CHAIN_NONE;
  744. return 0;
  745. }
  746. return parse_callchain_report_opt(arg);
  747. }
  748. int
  749. report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
  750. const char *arg, int unset __maybe_unused)
  751. {
  752. if (arg) {
  753. int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
  754. if (err) {
  755. char buf[BUFSIZ];
  756. regerror(err, &ignore_callees_regex, buf, sizeof(buf));
  757. pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
  758. return -1;
  759. }
  760. have_ignore_callees = 1;
  761. }
  762. return 0;
  763. }
  764. static int
  765. parse_branch_mode(const struct option *opt,
  766. const char *str __maybe_unused, int unset)
  767. {
  768. int *branch_mode = opt->value;
  769. *branch_mode = !unset;
  770. return 0;
  771. }
  772. static int
  773. parse_percent_limit(const struct option *opt, const char *str,
  774. int unset __maybe_unused)
  775. {
  776. struct report *rep = opt->value;
  777. double pcnt = strtof(str, NULL);
  778. rep->min_percent = pcnt;
  779. callchain_param.min_percent = pcnt;
  780. return 0;
  781. }
  782. int cmd_report(int argc, const char **argv)
  783. {
  784. struct perf_session *session;
  785. struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
  786. struct stat st;
  787. bool has_br_stack = false;
  788. int branch_mode = -1;
  789. bool branch_call_mode = false;
  790. #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
  791. const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
  792. CALLCHAIN_REPORT_HELP
  793. "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
  794. char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
  795. const char * const report_usage[] = {
  796. "perf report [<options>]",
  797. NULL
  798. };
  799. struct report report = {
  800. .tool = {
  801. .sample = process_sample_event,
  802. .mmap = perf_event__process_mmap,
  803. .mmap2 = perf_event__process_mmap2,
  804. .comm = perf_event__process_comm,
  805. .namespaces = perf_event__process_namespaces,
  806. .exit = perf_event__process_exit,
  807. .fork = perf_event__process_fork,
  808. .lost = perf_event__process_lost,
  809. .read = process_read_event,
  810. .attr = perf_event__process_attr,
  811. .tracing_data = perf_event__process_tracing_data,
  812. .build_id = perf_event__process_build_id,
  813. .id_index = perf_event__process_id_index,
  814. .auxtrace_info = perf_event__process_auxtrace_info,
  815. .auxtrace = perf_event__process_auxtrace,
  816. .feature = process_feature_event,
  817. .ordered_events = true,
  818. .ordering_requires_timestamps = true,
  819. },
  820. .max_stack = PERF_MAX_STACK_DEPTH,
  821. .pretty_printing_style = "normal",
  822. .socket_filter = -1,
  823. .annotation_opts = annotation__default_options,
  824. };
  825. const struct option options[] = {
  826. OPT_STRING('i', "input", &input_name, "file",
  827. "input file name"),
  828. OPT_INCR('v', "verbose", &verbose,
  829. "be more verbose (show symbol address, etc)"),
  830. OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
  831. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  832. "dump raw trace in ASCII"),
  833. OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"),
  834. OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"),
  835. OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"),
  836. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  837. "file", "vmlinux pathname"),
  838. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  839. "don't load vmlinux even if found"),
  840. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  841. "file", "kallsyms pathname"),
  842. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  843. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  844. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  845. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  846. "Show a column with the number of samples"),
  847. OPT_BOOLEAN('T', "threads", &report.show_threads,
  848. "Show per-thread event counters"),
  849. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  850. "pretty printing style key: normal raw"),
  851. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  852. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  853. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  854. "Use the stdio interface"),
  855. OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
  856. OPT_BOOLEAN(0, "header-only", &report.header_only,
  857. "Show only data header."),
  858. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  859. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  860. " Please refer the man page for the complete list."),
  861. OPT_STRING('F', "fields", &field_order, "key[,keys...]",
  862. "output field(s): overhead, period, sample plus all of sort keys"),
  863. OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
  864. "Show sample percentage for different cpu modes"),
  865. OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  866. "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
  867. OPT_STRING('p', "parent", &parent_pattern, "regex",
  868. "regex filter to identify parent, see: '--sort parent'"),
  869. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  870. "Only display entries with parent-match"),
  871. OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
  872. "print_type,threshold[,print_limit],order,sort_key[,branch],value",
  873. report_callchain_help, &report_parse_callchain_opt,
  874. callchain_default_opt),
  875. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  876. "Accumulate callchains of children and show total overhead as well"),
  877. OPT_INTEGER(0, "max-stack", &report.max_stack,
  878. "Set the maximum stack depth when parsing the callchain, "
  879. "anything beyond the specified depth will be ignored. "
  880. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  881. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  882. "alias for inverted call graph"),
  883. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  884. "ignore callees of these functions in call graphs",
  885. report_parse_ignore_callees_opt),
  886. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  887. "only consider symbols in these dsos"),
  888. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  889. "only consider symbols in these comms"),
  890. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  891. "only consider symbols in these pids"),
  892. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  893. "only consider symbols in these tids"),
  894. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  895. "only consider these symbols"),
  896. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  897. "only show symbols that (partially) match with this filter"),
  898. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  899. "width[,width...]",
  900. "don't try to adjust column width, use these fixed values"),
  901. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  902. "separator for columns, no spaces will be added between "
  903. "columns '.' is reserved."),
  904. OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
  905. "Only display entries resolved to a symbol"),
  906. OPT_CALLBACK(0, "symfs", NULL, "directory",
  907. "Look for files with symbols relative to this directory",
  908. symbol__config_symfs),
  909. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  910. "list of cpus to profile"),
  911. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  912. "Display extended information about perf.data file"),
  913. OPT_BOOLEAN(0, "source", &report.annotation_opts.annotate_src,
  914. "Interleave source code with assembly code (default)"),
  915. OPT_BOOLEAN(0, "asm-raw", &report.annotation_opts.show_asm_raw,
  916. "Display raw encoding of assembly instructions (default)"),
  917. OPT_STRING('M', "disassembler-style", &report.annotation_opts.disassembler_style, "disassembler style",
  918. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  919. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  920. "Show a column with the sum of periods"),
  921. OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set,
  922. "Show event group information together"),
  923. OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
  924. "use branch records for per branch histogram filling",
  925. parse_branch_mode),
  926. OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
  927. "add last branch records to call history"),
  928. OPT_STRING(0, "objdump", &report.annotation_opts.objdump_path, "path",
  929. "objdump binary to use for disassembly and annotations"),
  930. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  931. "Disable symbol demangling"),
  932. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  933. "Enable kernel symbol demangling"),
  934. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  935. OPT_CALLBACK(0, "percent-limit", &report, "percent",
  936. "Don't show entries under that percent", parse_percent_limit),
  937. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  938. "how to display percentage of filtered entries", parse_filter_percentage),
  939. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  940. "Instruction Tracing options",
  941. itrace_parse_synth_opts),
  942. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  943. "Show full source file name path for source lines"),
  944. OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
  945. "Show callgraph from reference event"),
  946. OPT_INTEGER(0, "socket-filter", &report.socket_filter,
  947. "only show processor socket that match with this filter"),
  948. OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
  949. "Show raw trace event output (do not use print fmt or plugins)"),
  950. OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
  951. "Show entries in a hierarchy"),
  952. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  953. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  954. stdio__config_color, "always"),
  955. OPT_STRING(0, "time", &report.time_str, "str",
  956. "Time span of interest (start,stop)"),
  957. OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
  958. "Show inline function"),
  959. OPT_CALLBACK(0, "percent-type", &report.annotation_opts, "local-period",
  960. "Set percent type local/global-period/hits",
  961. annotate_parse_percent_type),
  962. OPT_END()
  963. };
  964. struct perf_data data = {
  965. .mode = PERF_DATA_MODE_READ,
  966. };
  967. int ret = hists__init();
  968. if (ret < 0)
  969. return ret;
  970. ret = perf_config(report__config, &report);
  971. if (ret)
  972. return ret;
  973. argc = parse_options(argc, argv, options, report_usage, 0);
  974. if (argc) {
  975. /*
  976. * Special case: if there's an argument left then assume that
  977. * it's a symbol filter:
  978. */
  979. if (argc > 1)
  980. usage_with_options(report_usage, options);
  981. report.symbol_filter_str = argv[0];
  982. }
  983. if (report.mmaps_mode)
  984. report.tasks_mode = true;
  985. if (quiet)
  986. perf_quiet_option();
  987. if (symbol_conf.vmlinux_name &&
  988. access(symbol_conf.vmlinux_name, R_OK)) {
  989. pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
  990. return -EINVAL;
  991. }
  992. if (symbol_conf.kallsyms_name &&
  993. access(symbol_conf.kallsyms_name, R_OK)) {
  994. pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
  995. return -EINVAL;
  996. }
  997. if (report.inverted_callchain)
  998. callchain_param.order = ORDER_CALLER;
  999. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  1000. callchain_param.order = ORDER_CALLER;
  1001. if (itrace_synth_opts.callchain &&
  1002. (int)itrace_synth_opts.callchain_sz > report.max_stack)
  1003. report.max_stack = itrace_synth_opts.callchain_sz;
  1004. if (!input_name || !strlen(input_name)) {
  1005. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  1006. input_name = "-";
  1007. else
  1008. input_name = "perf.data";
  1009. }
  1010. data.file.path = input_name;
  1011. data.force = symbol_conf.force;
  1012. repeat:
  1013. session = perf_session__new(&data, false, &report.tool);
  1014. if (session == NULL)
  1015. return -1;
  1016. if (report.queue_size) {
  1017. ordered_events__set_alloc_size(&session->ordered_events,
  1018. report.queue_size);
  1019. }
  1020. session->itrace_synth_opts = &itrace_synth_opts;
  1021. report.session = session;
  1022. has_br_stack = perf_header__has_feat(&session->header,
  1023. HEADER_BRANCH_STACK);
  1024. setup_forced_leader(&report, session->evlist);
  1025. if (itrace_synth_opts.last_branch)
  1026. has_br_stack = true;
  1027. if (has_br_stack && branch_call_mode)
  1028. symbol_conf.show_branchflag_count = true;
  1029. memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
  1030. /*
  1031. * Branch mode is a tristate:
  1032. * -1 means default, so decide based on the file having branch data.
  1033. * 0/1 means the user chose a mode.
  1034. */
  1035. if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
  1036. !branch_call_mode) {
  1037. sort__mode = SORT_MODE__BRANCH;
  1038. symbol_conf.cumulate_callchain = false;
  1039. }
  1040. if (branch_call_mode) {
  1041. callchain_param.key = CCKEY_ADDRESS;
  1042. callchain_param.branch_callstack = 1;
  1043. symbol_conf.use_callchain = true;
  1044. callchain_register_param(&callchain_param);
  1045. if (sort_order == NULL)
  1046. sort_order = "srcline,symbol,dso";
  1047. }
  1048. if (report.mem_mode) {
  1049. if (sort__mode == SORT_MODE__BRANCH) {
  1050. pr_err("branch and mem mode incompatible\n");
  1051. goto error;
  1052. }
  1053. sort__mode = SORT_MODE__MEMORY;
  1054. symbol_conf.cumulate_callchain = false;
  1055. }
  1056. if (symbol_conf.report_hierarchy) {
  1057. /* disable incompatible options */
  1058. symbol_conf.cumulate_callchain = false;
  1059. if (field_order) {
  1060. pr_err("Error: --hierarchy and --fields options cannot be used together\n");
  1061. parse_options_usage(report_usage, options, "F", 1);
  1062. parse_options_usage(NULL, options, "hierarchy", 0);
  1063. goto error;
  1064. }
  1065. perf_hpp_list.need_collapse = true;
  1066. }
  1067. if (report.use_stdio)
  1068. use_browser = 0;
  1069. else if (report.use_tui)
  1070. use_browser = 1;
  1071. else if (report.use_gtk)
  1072. use_browser = 2;
  1073. /* Force tty output for header output and per-thread stat. */
  1074. if (report.header || report.header_only || report.show_threads)
  1075. use_browser = 0;
  1076. if (report.header || report.header_only)
  1077. report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
  1078. if (report.show_full_info)
  1079. report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
  1080. if (report.stats_mode || report.tasks_mode)
  1081. use_browser = 0;
  1082. if (report.stats_mode && report.tasks_mode) {
  1083. pr_err("Error: --tasks and --mmaps can't be used together with --stats\n");
  1084. goto error;
  1085. }
  1086. if (strcmp(input_name, "-") != 0)
  1087. setup_browser(true);
  1088. else
  1089. use_browser = 0;
  1090. if (setup_sorting(session->evlist) < 0) {
  1091. if (sort_order)
  1092. parse_options_usage(report_usage, options, "s", 1);
  1093. if (field_order)
  1094. parse_options_usage(sort_order ? NULL : report_usage,
  1095. options, "F", 1);
  1096. goto error;
  1097. }
  1098. if ((report.header || report.header_only) && !quiet) {
  1099. perf_session__fprintf_info(session, stdout,
  1100. report.show_full_info);
  1101. if (report.header_only) {
  1102. ret = 0;
  1103. goto error;
  1104. }
  1105. } else if (use_browser == 0 && !quiet &&
  1106. !report.stats_mode && !report.tasks_mode) {
  1107. fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
  1108. stdout);
  1109. }
  1110. /*
  1111. * Only in the TUI browser we are doing integrated annotation,
  1112. * so don't allocate extra space that won't be used in the stdio
  1113. * implementation.
  1114. */
  1115. if (ui__has_annotation()) {
  1116. ret = symbol__annotation_init();
  1117. if (ret < 0)
  1118. goto error;
  1119. /*
  1120. * For searching by name on the "Browse map details".
  1121. * providing it only in verbose mode not to bloat too
  1122. * much struct symbol.
  1123. */
  1124. if (verbose > 0) {
  1125. /*
  1126. * XXX: Need to provide a less kludgy way to ask for
  1127. * more space per symbol, the u32 is for the index on
  1128. * the ui browser.
  1129. * See symbol__browser_index.
  1130. */
  1131. symbol_conf.priv_size += sizeof(u32);
  1132. symbol_conf.sort_by_name = true;
  1133. }
  1134. annotation_config__init();
  1135. }
  1136. if (symbol__init(&session->header.env) < 0)
  1137. goto error;
  1138. report.ptime_range = perf_time__range_alloc(report.time_str,
  1139. &report.range_size);
  1140. if (!report.ptime_range) {
  1141. ret = -ENOMEM;
  1142. goto error;
  1143. }
  1144. if (perf_time__parse_str(report.ptime_range, report.time_str) != 0) {
  1145. if (session->evlist->first_sample_time == 0 &&
  1146. session->evlist->last_sample_time == 0) {
  1147. pr_err("HINT: no first/last sample time found in perf data.\n"
  1148. "Please use latest perf binary to execute 'perf record'\n"
  1149. "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
  1150. ret = -EINVAL;
  1151. goto error;
  1152. }
  1153. report.range_num = perf_time__percent_parse_str(
  1154. report.ptime_range, report.range_size,
  1155. report.time_str,
  1156. session->evlist->first_sample_time,
  1157. session->evlist->last_sample_time);
  1158. if (report.range_num < 0) {
  1159. pr_err("Invalid time string\n");
  1160. ret = -EINVAL;
  1161. goto error;
  1162. }
  1163. } else {
  1164. report.range_num = 1;
  1165. }
  1166. if (session->tevent.pevent &&
  1167. tep_set_function_resolver(session->tevent.pevent,
  1168. machine__resolve_kernel_addr,
  1169. &session->machines.host) < 0) {
  1170. pr_err("%s: failed to set libtraceevent function resolver\n",
  1171. __func__);
  1172. return -1;
  1173. }
  1174. sort__setup_elide(stdout);
  1175. ret = __cmd_report(&report);
  1176. if (ret == K_SWITCH_INPUT_DATA) {
  1177. perf_session__delete(session);
  1178. goto repeat;
  1179. } else
  1180. ret = 0;
  1181. error:
  1182. zfree(&report.ptime_range);
  1183. perf_session__delete(session);
  1184. return ret;
  1185. }