builtin-report.c 42 KB

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