PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/perf/builtin-report.c

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