builtin-report.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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. .event_update = perf_event__process_event_update,
  817. .feature = process_feature_event,
  818. .ordered_events = true,
  819. .ordering_requires_timestamps = true,
  820. },
  821. .max_stack = PERF_MAX_STACK_DEPTH,
  822. .pretty_printing_style = "normal",
  823. .socket_filter = -1,
  824. .annotation_opts = annotation__default_options,
  825. };
  826. const struct option options[] = {
  827. OPT_STRING('i', "input", &input_name, "file",
  828. "input file name"),
  829. OPT_INCR('v', "verbose", &verbose,
  830. "be more verbose (show symbol address, etc)"),
  831. OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
  832. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  833. "dump raw trace in ASCII"),
  834. OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"),
  835. OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"),
  836. OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"),
  837. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  838. "file", "vmlinux pathname"),
  839. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  840. "don't load vmlinux even if found"),
  841. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  842. "file", "kallsyms pathname"),
  843. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  844. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  845. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  846. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  847. "Show a column with the number of samples"),
  848. OPT_BOOLEAN('T', "threads", &report.show_threads,
  849. "Show per-thread event counters"),
  850. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  851. "pretty printing style key: normal raw"),
  852. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  853. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  854. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  855. "Use the stdio interface"),
  856. OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
  857. OPT_BOOLEAN(0, "header-only", &report.header_only,
  858. "Show only data header."),
  859. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  860. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  861. " Please refer the man page for the complete list."),
  862. OPT_STRING('F', "fields", &field_order, "key[,keys...]",
  863. "output field(s): overhead, period, sample plus all of sort keys"),
  864. OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
  865. "Show sample percentage for different cpu modes"),
  866. OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  867. "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
  868. OPT_STRING('p', "parent", &parent_pattern, "regex",
  869. "regex filter to identify parent, see: '--sort parent'"),
  870. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  871. "Only display entries with parent-match"),
  872. OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
  873. "print_type,threshold[,print_limit],order,sort_key[,branch],value",
  874. report_callchain_help, &report_parse_callchain_opt,
  875. callchain_default_opt),
  876. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  877. "Accumulate callchains of children and show total overhead as well"),
  878. OPT_INTEGER(0, "max-stack", &report.max_stack,
  879. "Set the maximum stack depth when parsing the callchain, "
  880. "anything beyond the specified depth will be ignored. "
  881. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  882. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  883. "alias for inverted call graph"),
  884. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  885. "ignore callees of these functions in call graphs",
  886. report_parse_ignore_callees_opt),
  887. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  888. "only consider symbols in these dsos"),
  889. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  890. "only consider symbols in these comms"),
  891. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  892. "only consider symbols in these pids"),
  893. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  894. "only consider symbols in these tids"),
  895. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  896. "only consider these symbols"),
  897. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  898. "only show symbols that (partially) match with this filter"),
  899. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  900. "width[,width...]",
  901. "don't try to adjust column width, use these fixed values"),
  902. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  903. "separator for columns, no spaces will be added between "
  904. "columns '.' is reserved."),
  905. OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
  906. "Only display entries resolved to a symbol"),
  907. OPT_CALLBACK(0, "symfs", NULL, "directory",
  908. "Look for files with symbols relative to this directory",
  909. symbol__config_symfs),
  910. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  911. "list of cpus to profile"),
  912. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  913. "Display extended information about perf.data file"),
  914. OPT_BOOLEAN(0, "source", &report.annotation_opts.annotate_src,
  915. "Interleave source code with assembly code (default)"),
  916. OPT_BOOLEAN(0, "asm-raw", &report.annotation_opts.show_asm_raw,
  917. "Display raw encoding of assembly instructions (default)"),
  918. OPT_STRING('M', "disassembler-style", &report.annotation_opts.disassembler_style, "disassembler style",
  919. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  920. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  921. "Show a column with the sum of periods"),
  922. OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set,
  923. "Show event group information together"),
  924. OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
  925. "use branch records for per branch histogram filling",
  926. parse_branch_mode),
  927. OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
  928. "add last branch records to call history"),
  929. OPT_STRING(0, "objdump", &report.annotation_opts.objdump_path, "path",
  930. "objdump binary to use for disassembly and annotations"),
  931. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  932. "Disable symbol demangling"),
  933. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  934. "Enable kernel symbol demangling"),
  935. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  936. OPT_CALLBACK(0, "percent-limit", &report, "percent",
  937. "Don't show entries under that percent", parse_percent_limit),
  938. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  939. "how to display percentage of filtered entries", parse_filter_percentage),
  940. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  941. "Instruction Tracing options",
  942. itrace_parse_synth_opts),
  943. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  944. "Show full source file name path for source lines"),
  945. OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
  946. "Show callgraph from reference event"),
  947. OPT_INTEGER(0, "socket-filter", &report.socket_filter,
  948. "only show processor socket that match with this filter"),
  949. OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
  950. "Show raw trace event output (do not use print fmt or plugins)"),
  951. OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
  952. "Show entries in a hierarchy"),
  953. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  954. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  955. stdio__config_color, "always"),
  956. OPT_STRING(0, "time", &report.time_str, "str",
  957. "Time span of interest (start,stop)"),
  958. OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
  959. "Show inline function"),
  960. OPT_CALLBACK(0, "percent-type", &report.annotation_opts, "local-period",
  961. "Set percent type local/global-period/hits",
  962. annotate_parse_percent_type),
  963. OPT_END()
  964. };
  965. struct perf_data data = {
  966. .mode = PERF_DATA_MODE_READ,
  967. };
  968. int ret = hists__init();
  969. if (ret < 0)
  970. return ret;
  971. ret = perf_config(report__config, &report);
  972. if (ret)
  973. return ret;
  974. argc = parse_options(argc, argv, options, report_usage, 0);
  975. if (argc) {
  976. /*
  977. * Special case: if there's an argument left then assume that
  978. * it's a symbol filter:
  979. */
  980. if (argc > 1)
  981. usage_with_options(report_usage, options);
  982. report.symbol_filter_str = argv[0];
  983. }
  984. if (report.mmaps_mode)
  985. report.tasks_mode = true;
  986. if (quiet)
  987. perf_quiet_option();
  988. if (symbol_conf.vmlinux_name &&
  989. access(symbol_conf.vmlinux_name, R_OK)) {
  990. pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
  991. return -EINVAL;
  992. }
  993. if (symbol_conf.kallsyms_name &&
  994. access(symbol_conf.kallsyms_name, R_OK)) {
  995. pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
  996. return -EINVAL;
  997. }
  998. if (report.inverted_callchain)
  999. callchain_param.order = ORDER_CALLER;
  1000. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  1001. callchain_param.order = ORDER_CALLER;
  1002. if (itrace_synth_opts.callchain &&
  1003. (int)itrace_synth_opts.callchain_sz > report.max_stack)
  1004. report.max_stack = itrace_synth_opts.callchain_sz;
  1005. if (!input_name || !strlen(input_name)) {
  1006. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  1007. input_name = "-";
  1008. else
  1009. input_name = "perf.data";
  1010. }
  1011. data.file.path = input_name;
  1012. data.force = symbol_conf.force;
  1013. repeat:
  1014. session = perf_session__new(&data, false, &report.tool);
  1015. if (session == NULL)
  1016. return -1;
  1017. if (report.queue_size) {
  1018. ordered_events__set_alloc_size(&session->ordered_events,
  1019. report.queue_size);
  1020. }
  1021. session->itrace_synth_opts = &itrace_synth_opts;
  1022. report.session = session;
  1023. has_br_stack = perf_header__has_feat(&session->header,
  1024. HEADER_BRANCH_STACK);
  1025. setup_forced_leader(&report, session->evlist);
  1026. if (itrace_synth_opts.last_branch)
  1027. has_br_stack = true;
  1028. if (has_br_stack && branch_call_mode)
  1029. symbol_conf.show_branchflag_count = true;
  1030. memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
  1031. /*
  1032. * Branch mode is a tristate:
  1033. * -1 means default, so decide based on the file having branch data.
  1034. * 0/1 means the user chose a mode.
  1035. */
  1036. if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
  1037. !branch_call_mode) {
  1038. sort__mode = SORT_MODE__BRANCH;
  1039. symbol_conf.cumulate_callchain = false;
  1040. }
  1041. if (branch_call_mode) {
  1042. callchain_param.key = CCKEY_ADDRESS;
  1043. callchain_param.branch_callstack = 1;
  1044. symbol_conf.use_callchain = true;
  1045. callchain_register_param(&callchain_param);
  1046. if (sort_order == NULL)
  1047. sort_order = "srcline,symbol,dso";
  1048. }
  1049. if (report.mem_mode) {
  1050. if (sort__mode == SORT_MODE__BRANCH) {
  1051. pr_err("branch and mem mode incompatible\n");
  1052. goto error;
  1053. }
  1054. sort__mode = SORT_MODE__MEMORY;
  1055. symbol_conf.cumulate_callchain = false;
  1056. }
  1057. if (symbol_conf.report_hierarchy) {
  1058. /* disable incompatible options */
  1059. symbol_conf.cumulate_callchain = false;
  1060. if (field_order) {
  1061. pr_err("Error: --hierarchy and --fields options cannot be used together\n");
  1062. parse_options_usage(report_usage, options, "F", 1);
  1063. parse_options_usage(NULL, options, "hierarchy", 0);
  1064. goto error;
  1065. }
  1066. perf_hpp_list.need_collapse = true;
  1067. }
  1068. if (report.use_stdio)
  1069. use_browser = 0;
  1070. else if (report.use_tui)
  1071. use_browser = 1;
  1072. else if (report.use_gtk)
  1073. use_browser = 2;
  1074. /* Force tty output for header output and per-thread stat. */
  1075. if (report.header || report.header_only || report.show_threads)
  1076. use_browser = 0;
  1077. if (report.header || report.header_only)
  1078. report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
  1079. if (report.show_full_info)
  1080. report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
  1081. if (report.stats_mode || report.tasks_mode)
  1082. use_browser = 0;
  1083. if (report.stats_mode && report.tasks_mode) {
  1084. pr_err("Error: --tasks and --mmaps can't be used together with --stats\n");
  1085. goto error;
  1086. }
  1087. if (strcmp(input_name, "-") != 0)
  1088. setup_browser(true);
  1089. else
  1090. use_browser = 0;
  1091. if (setup_sorting(session->evlist) < 0) {
  1092. if (sort_order)
  1093. parse_options_usage(report_usage, options, "s", 1);
  1094. if (field_order)
  1095. parse_options_usage(sort_order ? NULL : report_usage,
  1096. options, "F", 1);
  1097. goto error;
  1098. }
  1099. if ((report.header || report.header_only) && !quiet) {
  1100. perf_session__fprintf_info(session, stdout,
  1101. report.show_full_info);
  1102. if (report.header_only) {
  1103. ret = 0;
  1104. goto error;
  1105. }
  1106. } else if (use_browser == 0 && !quiet &&
  1107. !report.stats_mode && !report.tasks_mode) {
  1108. fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
  1109. stdout);
  1110. }
  1111. /*
  1112. * Only in the TUI browser we are doing integrated annotation,
  1113. * so don't allocate extra space that won't be used in the stdio
  1114. * implementation.
  1115. */
  1116. if (ui__has_annotation()) {
  1117. ret = symbol__annotation_init();
  1118. if (ret < 0)
  1119. goto error;
  1120. /*
  1121. * For searching by name on the "Browse map details".
  1122. * providing it only in verbose mode not to bloat too
  1123. * much struct symbol.
  1124. */
  1125. if (verbose > 0) {
  1126. /*
  1127. * XXX: Need to provide a less kludgy way to ask for
  1128. * more space per symbol, the u32 is for the index on
  1129. * the ui browser.
  1130. * See symbol__browser_index.
  1131. */
  1132. symbol_conf.priv_size += sizeof(u32);
  1133. symbol_conf.sort_by_name = true;
  1134. }
  1135. annotation_config__init();
  1136. }
  1137. if (symbol__init(&session->header.env) < 0)
  1138. goto error;
  1139. report.ptime_range = perf_time__range_alloc(report.time_str,
  1140. &report.range_size);
  1141. if (!report.ptime_range) {
  1142. ret = -ENOMEM;
  1143. goto error;
  1144. }
  1145. if (perf_time__parse_str(report.ptime_range, report.time_str) != 0) {
  1146. if (session->evlist->first_sample_time == 0 &&
  1147. session->evlist->last_sample_time == 0) {
  1148. pr_err("HINT: no first/last sample time found in perf data.\n"
  1149. "Please use latest perf binary to execute 'perf record'\n"
  1150. "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
  1151. ret = -EINVAL;
  1152. goto error;
  1153. }
  1154. report.range_num = perf_time__percent_parse_str(
  1155. report.ptime_range, report.range_size,
  1156. report.time_str,
  1157. session->evlist->first_sample_time,
  1158. session->evlist->last_sample_time);
  1159. if (report.range_num < 0) {
  1160. pr_err("Invalid time string\n");
  1161. ret = -EINVAL;
  1162. goto error;
  1163. }
  1164. } else {
  1165. report.range_num = 1;
  1166. }
  1167. if (session->tevent.pevent &&
  1168. tep_set_function_resolver(session->tevent.pevent,
  1169. machine__resolve_kernel_addr,
  1170. &session->machines.host) < 0) {
  1171. pr_err("%s: failed to set libtraceevent function resolver\n",
  1172. __func__);
  1173. return -1;
  1174. }
  1175. sort__setup_elide(stdout);
  1176. ret = __cmd_report(&report);
  1177. if (ret == K_SWITCH_INPUT_DATA) {
  1178. perf_session__delete(session);
  1179. goto repeat;
  1180. } else
  1181. ret = 0;
  1182. error:
  1183. zfree(&report.ptime_range);
  1184. perf_session__delete(session);
  1185. return ret;
  1186. }