builtin-report.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * builtin-report.c
  3. *
  4. * Builtin report command: Analyze the perf.data input file,
  5. * look up and read DSOs and symbol information and display
  6. * a histogram of results, along various sorting keys.
  7. */
  8. #include "builtin.h"
  9. #include "util/util.h"
  10. #include "util/cache.h"
  11. #include "util/annotate.h"
  12. #include "util/color.h"
  13. #include <linux/list.h>
  14. #include <linux/rbtree.h>
  15. #include "util/symbol.h"
  16. #include "util/callchain.h"
  17. #include "util/strlist.h"
  18. #include "util/values.h"
  19. #include "perf.h"
  20. #include "util/debug.h"
  21. #include "util/evlist.h"
  22. #include "util/evsel.h"
  23. #include "util/header.h"
  24. #include "util/session.h"
  25. #include "util/tool.h"
  26. #include "util/parse-options.h"
  27. #include "util/parse-events.h"
  28. #include "util/thread.h"
  29. #include "util/sort.h"
  30. #include "util/hist.h"
  31. #include "arch/common.h"
  32. #include <linux/bitmap.h>
  33. struct perf_report {
  34. struct perf_tool tool;
  35. struct perf_session *session;
  36. bool force, use_tui, use_gtk, use_stdio;
  37. bool hide_unresolved;
  38. bool dont_use_callchains;
  39. bool show_full_info;
  40. bool show_threads;
  41. bool inverted_callchain;
  42. bool mem_mode;
  43. struct perf_read_values show_threads_values;
  44. const char *pretty_printing_style;
  45. symbol_filter_t annotate_init;
  46. const char *cpu_list;
  47. const char *symbol_filter_str;
  48. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  49. };
  50. static int perf_report_config(const char *var, const char *value, void *cb)
  51. {
  52. if (!strcmp(var, "report.group")) {
  53. symbol_conf.event_group = perf_config_bool(var, value);
  54. return 0;
  55. }
  56. return perf_default_config(var, value, cb);
  57. }
  58. static int perf_report__add_mem_hist_entry(struct perf_tool *tool,
  59. struct addr_location *al,
  60. struct perf_sample *sample,
  61. struct perf_evsel *evsel,
  62. struct machine *machine,
  63. union perf_event *event)
  64. {
  65. struct perf_report *rep = container_of(tool, struct perf_report, tool);
  66. struct symbol *parent = NULL;
  67. u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  68. int err = 0;
  69. struct hist_entry *he;
  70. struct mem_info *mi, *mx;
  71. uint64_t cost;
  72. if ((sort__has_parent || symbol_conf.use_callchain) &&
  73. sample->callchain) {
  74. err = machine__resolve_callchain(machine, evsel, al->thread,
  75. sample, &parent);
  76. if (err)
  77. return err;
  78. }
  79. mi = machine__resolve_mem(machine, al->thread, sample, cpumode);
  80. if (!mi)
  81. return -ENOMEM;
  82. if (rep->hide_unresolved && !al->sym)
  83. return 0;
  84. cost = sample->weight;
  85. if (!cost)
  86. cost = 1;
  87. /*
  88. * must pass period=weight in order to get the correct
  89. * sorting from hists__collapse_resort() which is solely
  90. * based on periods. We want sorting be done on nr_events * weight
  91. * and this is indirectly achieved by passing period=weight here
  92. * and the he_stat__add_period() function.
  93. */
  94. he = __hists__add_mem_entry(&evsel->hists, al, parent, mi, cost, cost);
  95. if (!he)
  96. return -ENOMEM;
  97. /*
  98. * In the TUI browser, we are doing integrated annotation,
  99. * so we don't allocate the extra space needed because the stdio
  100. * code will not use it.
  101. */
  102. if (sort__has_sym && he->ms.sym && use_browser > 0) {
  103. struct annotation *notes = symbol__annotation(he->ms.sym);
  104. assert(evsel != NULL);
  105. if (notes->src == NULL && symbol__alloc_hist(he->ms.sym) < 0)
  106. goto out;
  107. err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
  108. if (err)
  109. goto out;
  110. }
  111. if (sort__has_sym && he->mem_info->daddr.sym && use_browser > 0) {
  112. struct annotation *notes;
  113. mx = he->mem_info;
  114. notes = symbol__annotation(mx->daddr.sym);
  115. if (notes->src == NULL && symbol__alloc_hist(mx->daddr.sym) < 0)
  116. goto out;
  117. err = symbol__inc_addr_samples(mx->daddr.sym,
  118. mx->daddr.map,
  119. evsel->idx,
  120. mx->daddr.al_addr);
  121. if (err)
  122. goto out;
  123. }
  124. evsel->hists.stats.total_period += cost;
  125. hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
  126. err = 0;
  127. if (symbol_conf.use_callchain) {
  128. err = callchain_append(he->callchain,
  129. &callchain_cursor,
  130. sample->period);
  131. }
  132. out:
  133. return err;
  134. }
  135. static int perf_report__add_branch_hist_entry(struct perf_tool *tool,
  136. struct addr_location *al,
  137. struct perf_sample *sample,
  138. struct perf_evsel *evsel,
  139. struct machine *machine)
  140. {
  141. struct perf_report *rep = container_of(tool, struct perf_report, tool);
  142. struct symbol *parent = NULL;
  143. int err = 0;
  144. unsigned i;
  145. struct hist_entry *he;
  146. struct branch_info *bi, *bx;
  147. if ((sort__has_parent || symbol_conf.use_callchain)
  148. && sample->callchain) {
  149. err = machine__resolve_callchain(machine, evsel, al->thread,
  150. sample, &parent);
  151. if (err)
  152. return err;
  153. }
  154. bi = machine__resolve_bstack(machine, al->thread,
  155. sample->branch_stack);
  156. if (!bi)
  157. return -ENOMEM;
  158. for (i = 0; i < sample->branch_stack->nr; i++) {
  159. if (rep->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
  160. continue;
  161. err = -ENOMEM;
  162. /*
  163. * The report shows the percentage of total branches captured
  164. * and not events sampled. Thus we use a pseudo period of 1.
  165. */
  166. he = __hists__add_branch_entry(&evsel->hists, al, parent,
  167. &bi[i], 1, 1);
  168. if (he) {
  169. struct annotation *notes;
  170. bx = he->branch_info;
  171. if (bx->from.sym && use_browser == 1 && sort__has_sym) {
  172. notes = symbol__annotation(bx->from.sym);
  173. if (!notes->src
  174. && symbol__alloc_hist(bx->from.sym) < 0)
  175. goto out;
  176. err = symbol__inc_addr_samples(bx->from.sym,
  177. bx->from.map,
  178. evsel->idx,
  179. bx->from.al_addr);
  180. if (err)
  181. goto out;
  182. }
  183. if (bx->to.sym && use_browser == 1 && sort__has_sym) {
  184. notes = symbol__annotation(bx->to.sym);
  185. if (!notes->src
  186. && symbol__alloc_hist(bx->to.sym) < 0)
  187. goto out;
  188. err = symbol__inc_addr_samples(bx->to.sym,
  189. bx->to.map,
  190. evsel->idx,
  191. bx->to.al_addr);
  192. if (err)
  193. goto out;
  194. }
  195. evsel->hists.stats.total_period += 1;
  196. hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
  197. } else
  198. goto out;
  199. }
  200. err = 0;
  201. out:
  202. free(bi);
  203. return err;
  204. }
  205. static int perf_evsel__add_hist_entry(struct perf_evsel *evsel,
  206. struct addr_location *al,
  207. struct perf_sample *sample,
  208. struct machine *machine)
  209. {
  210. struct symbol *parent = NULL;
  211. int err = 0;
  212. struct hist_entry *he;
  213. if ((sort__has_parent || symbol_conf.use_callchain) && sample->callchain) {
  214. err = machine__resolve_callchain(machine, evsel, al->thread,
  215. sample, &parent);
  216. if (err)
  217. return err;
  218. }
  219. he = __hists__add_entry(&evsel->hists, al, parent, sample->period,
  220. sample->weight);
  221. if (he == NULL)
  222. return -ENOMEM;
  223. if (symbol_conf.use_callchain) {
  224. err = callchain_append(he->callchain,
  225. &callchain_cursor,
  226. sample->period);
  227. if (err)
  228. return err;
  229. }
  230. /*
  231. * Only in the TUI browser we are doing integrated annotation,
  232. * so we don't allocated the extra space needed because the stdio
  233. * code will not use it.
  234. */
  235. if (he->ms.sym != NULL && use_browser == 1 && sort__has_sym) {
  236. struct annotation *notes = symbol__annotation(he->ms.sym);
  237. assert(evsel != NULL);
  238. err = -ENOMEM;
  239. if (notes->src == NULL && symbol__alloc_hist(he->ms.sym) < 0)
  240. goto out;
  241. err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
  242. }
  243. evsel->hists.stats.total_period += sample->period;
  244. hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
  245. out:
  246. return err;
  247. }
  248. static int process_sample_event(struct perf_tool *tool,
  249. union perf_event *event,
  250. struct perf_sample *sample,
  251. struct perf_evsel *evsel,
  252. struct machine *machine)
  253. {
  254. struct perf_report *rep = container_of(tool, struct perf_report, tool);
  255. struct addr_location al;
  256. if (perf_event__preprocess_sample(event, machine, &al, sample,
  257. rep->annotate_init) < 0) {
  258. fprintf(stderr, "problem processing %d event, skipping it.\n",
  259. event->header.type);
  260. return -1;
  261. }
  262. if (al.filtered || (rep->hide_unresolved && al.sym == NULL))
  263. return 0;
  264. if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
  265. return 0;
  266. if (sort__branch_mode == 1) {
  267. if (perf_report__add_branch_hist_entry(tool, &al, sample,
  268. evsel, machine)) {
  269. pr_debug("problem adding lbr entry, skipping event\n");
  270. return -1;
  271. }
  272. } else if (rep->mem_mode == 1) {
  273. if (perf_report__add_mem_hist_entry(tool, &al, sample,
  274. evsel, machine, event)) {
  275. pr_debug("problem adding mem entry, skipping event\n");
  276. return -1;
  277. }
  278. } else {
  279. if (al.map != NULL)
  280. al.map->dso->hit = 1;
  281. if (perf_evsel__add_hist_entry(evsel, &al, sample, machine)) {
  282. pr_debug("problem incrementing symbol period, skipping event\n");
  283. return -1;
  284. }
  285. }
  286. return 0;
  287. }
  288. static int process_read_event(struct perf_tool *tool,
  289. union perf_event *event,
  290. struct perf_sample *sample __maybe_unused,
  291. struct perf_evsel *evsel,
  292. struct machine *machine __maybe_unused)
  293. {
  294. struct perf_report *rep = container_of(tool, struct perf_report, tool);
  295. if (rep->show_threads) {
  296. const char *name = evsel ? perf_evsel__name(evsel) : "unknown";
  297. perf_read_values_add_value(&rep->show_threads_values,
  298. event->read.pid, event->read.tid,
  299. event->read.id,
  300. name,
  301. event->read.value);
  302. }
  303. dump_printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid,
  304. evsel ? perf_evsel__name(evsel) : "FAIL",
  305. event->read.value);
  306. return 0;
  307. }
  308. /* For pipe mode, sample_type is not currently set */
  309. static int perf_report__setup_sample_type(struct perf_report *rep)
  310. {
  311. struct perf_session *self = rep->session;
  312. u64 sample_type = perf_evlist__sample_type(self->evlist);
  313. if (!self->fd_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  314. if (sort__has_parent) {
  315. ui__error("Selected --sort parent, but no "
  316. "callchain data. Did you call "
  317. "'perf record' without -g?\n");
  318. return -EINVAL;
  319. }
  320. if (symbol_conf.use_callchain) {
  321. ui__error("Selected -g but no callchain data. Did "
  322. "you call 'perf record' without -g?\n");
  323. return -1;
  324. }
  325. } else if (!rep->dont_use_callchains &&
  326. callchain_param.mode != CHAIN_NONE &&
  327. !symbol_conf.use_callchain) {
  328. symbol_conf.use_callchain = true;
  329. if (callchain_register_param(&callchain_param) < 0) {
  330. ui__error("Can't register callchain params.\n");
  331. return -EINVAL;
  332. }
  333. }
  334. if (sort__branch_mode == 1) {
  335. if (!self->fd_pipe &&
  336. !(sample_type & PERF_SAMPLE_BRANCH_STACK)) {
  337. ui__error("Selected -b but no branch data. "
  338. "Did you call perf record without -b?\n");
  339. return -1;
  340. }
  341. }
  342. return 0;
  343. }
  344. extern volatile int session_done;
  345. static void sig_handler(int sig __maybe_unused)
  346. {
  347. session_done = 1;
  348. }
  349. static size_t hists__fprintf_nr_sample_events(struct perf_report *rep,
  350. struct hists *self,
  351. const char *evname, FILE *fp)
  352. {
  353. size_t ret;
  354. char unit;
  355. unsigned long nr_samples = self->stats.nr_events[PERF_RECORD_SAMPLE];
  356. u64 nr_events = self->stats.total_period;
  357. struct perf_evsel *evsel = hists_to_evsel(self);
  358. char buf[512];
  359. size_t size = sizeof(buf);
  360. if (perf_evsel__is_group_event(evsel)) {
  361. struct perf_evsel *pos;
  362. perf_evsel__group_desc(evsel, buf, size);
  363. evname = buf;
  364. for_each_group_member(pos, evsel) {
  365. nr_samples += pos->hists.stats.nr_events[PERF_RECORD_SAMPLE];
  366. nr_events += pos->hists.stats.total_period;
  367. }
  368. }
  369. nr_samples = convert_unit(nr_samples, &unit);
  370. ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
  371. if (evname != NULL)
  372. ret += fprintf(fp, " of event '%s'", evname);
  373. if (rep->mem_mode) {
  374. ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
  375. ret += fprintf(fp, "\n# Sort order : %s", sort_order);
  376. } else
  377. ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
  378. return ret + fprintf(fp, "\n#\n");
  379. }
  380. static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
  381. struct perf_report *rep,
  382. const char *help)
  383. {
  384. struct perf_evsel *pos;
  385. list_for_each_entry(pos, &evlist->entries, node) {
  386. struct hists *hists = &pos->hists;
  387. const char *evname = perf_evsel__name(pos);
  388. if (symbol_conf.event_group &&
  389. !perf_evsel__is_group_leader(pos))
  390. continue;
  391. hists__fprintf_nr_sample_events(rep, hists, evname, stdout);
  392. hists__fprintf(hists, true, 0, 0, stdout);
  393. fprintf(stdout, "\n\n");
  394. }
  395. if (sort_order == default_sort_order &&
  396. parent_pattern == default_parent_pattern) {
  397. fprintf(stdout, "#\n# (%s)\n#\n", help);
  398. if (rep->show_threads) {
  399. bool style = !strcmp(rep->pretty_printing_style, "raw");
  400. perf_read_values_display(stdout, &rep->show_threads_values,
  401. style);
  402. perf_read_values_destroy(&rep->show_threads_values);
  403. }
  404. }
  405. return 0;
  406. }
  407. static int __cmd_report(struct perf_report *rep)
  408. {
  409. int ret = -EINVAL;
  410. u64 nr_samples;
  411. struct perf_session *session = rep->session;
  412. struct perf_evsel *pos;
  413. struct map *kernel_map;
  414. struct kmap *kernel_kmap;
  415. const char *help = "For a higher level overview, try: perf report --sort comm,dso";
  416. signal(SIGINT, sig_handler);
  417. if (rep->cpu_list) {
  418. ret = perf_session__cpu_bitmap(session, rep->cpu_list,
  419. rep->cpu_bitmap);
  420. if (ret)
  421. goto out_delete;
  422. }
  423. if (use_browser <= 0)
  424. perf_session__fprintf_info(session, stdout, rep->show_full_info);
  425. if (rep->show_threads)
  426. perf_read_values_init(&rep->show_threads_values);
  427. ret = perf_report__setup_sample_type(rep);
  428. if (ret)
  429. goto out_delete;
  430. ret = perf_session__process_events(session, &rep->tool);
  431. if (ret)
  432. goto out_delete;
  433. kernel_map = session->machines.host.vmlinux_maps[MAP__FUNCTION];
  434. kernel_kmap = map__kmap(kernel_map);
  435. if (kernel_map == NULL ||
  436. (kernel_map->dso->hit &&
  437. (kernel_kmap->ref_reloc_sym == NULL ||
  438. kernel_kmap->ref_reloc_sym->addr == 0))) {
  439. const char *desc =
  440. "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
  441. "can't be resolved.";
  442. if (kernel_map) {
  443. const struct dso *kdso = kernel_map->dso;
  444. if (!RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION])) {
  445. desc = "If some relocation was applied (e.g. "
  446. "kexec) symbols may be misresolved.";
  447. }
  448. }
  449. ui__warning(
  450. "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
  451. "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
  452. "Samples in kernel modules can't be resolved as well.\n\n",
  453. desc);
  454. }
  455. if (verbose > 3)
  456. perf_session__fprintf(session, stdout);
  457. if (verbose > 2)
  458. perf_session__fprintf_dsos(session, stdout);
  459. if (dump_trace) {
  460. perf_session__fprintf_nr_events(session, stdout);
  461. goto out_delete;
  462. }
  463. nr_samples = 0;
  464. list_for_each_entry(pos, &session->evlist->entries, node) {
  465. struct hists *hists = &pos->hists;
  466. if (pos->idx == 0)
  467. hists->symbol_filter_str = rep->symbol_filter_str;
  468. hists__collapse_resort(hists);
  469. nr_samples += hists->stats.nr_events[PERF_RECORD_SAMPLE];
  470. /* Non-group events are considered as leader */
  471. if (symbol_conf.event_group &&
  472. !perf_evsel__is_group_leader(pos)) {
  473. struct hists *leader_hists = &pos->leader->hists;
  474. hists__match(leader_hists, hists);
  475. hists__link(leader_hists, hists);
  476. }
  477. }
  478. if (nr_samples == 0) {
  479. ui__error("The %s file has no samples!\n", session->filename);
  480. goto out_delete;
  481. }
  482. list_for_each_entry(pos, &session->evlist->entries, node)
  483. hists__output_resort(&pos->hists);
  484. if (use_browser > 0) {
  485. if (use_browser == 1) {
  486. ret = perf_evlist__tui_browse_hists(session->evlist,
  487. help,
  488. NULL,
  489. &session->header.env);
  490. /*
  491. * Usually "ret" is the last pressed key, and we only
  492. * care if the key notifies us to switch data file.
  493. */
  494. if (ret != K_SWITCH_INPUT_DATA)
  495. ret = 0;
  496. } else if (use_browser == 2) {
  497. perf_evlist__gtk_browse_hists(session->evlist, help,
  498. NULL);
  499. }
  500. } else
  501. perf_evlist__tty_browse_hists(session->evlist, rep, help);
  502. out_delete:
  503. /*
  504. * Speed up the exit process, for large files this can
  505. * take quite a while.
  506. *
  507. * XXX Enable this when using valgrind or if we ever
  508. * librarize this command.
  509. *
  510. * Also experiment with obstacks to see how much speed
  511. * up we'll get here.
  512. *
  513. * perf_session__delete(session);
  514. */
  515. return ret;
  516. }
  517. static int
  518. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  519. {
  520. struct perf_report *rep = (struct perf_report *)opt->value;
  521. char *tok, *tok2;
  522. char *endptr;
  523. /*
  524. * --no-call-graph
  525. */
  526. if (unset) {
  527. rep->dont_use_callchains = true;
  528. return 0;
  529. }
  530. symbol_conf.use_callchain = true;
  531. if (!arg)
  532. return 0;
  533. tok = strtok((char *)arg, ",");
  534. if (!tok)
  535. return -1;
  536. /* get the output mode */
  537. if (!strncmp(tok, "graph", strlen(arg)))
  538. callchain_param.mode = CHAIN_GRAPH_ABS;
  539. else if (!strncmp(tok, "flat", strlen(arg)))
  540. callchain_param.mode = CHAIN_FLAT;
  541. else if (!strncmp(tok, "fractal", strlen(arg)))
  542. callchain_param.mode = CHAIN_GRAPH_REL;
  543. else if (!strncmp(tok, "none", strlen(arg))) {
  544. callchain_param.mode = CHAIN_NONE;
  545. symbol_conf.use_callchain = false;
  546. return 0;
  547. }
  548. else
  549. return -1;
  550. /* get the min percentage */
  551. tok = strtok(NULL, ",");
  552. if (!tok)
  553. goto setup;
  554. callchain_param.min_percent = strtod(tok, &endptr);
  555. if (tok == endptr)
  556. return -1;
  557. /* get the print limit */
  558. tok2 = strtok(NULL, ",");
  559. if (!tok2)
  560. goto setup;
  561. if (tok2[0] != 'c') {
  562. callchain_param.print_limit = strtoul(tok2, &endptr, 0);
  563. tok2 = strtok(NULL, ",");
  564. if (!tok2)
  565. goto setup;
  566. }
  567. /* get the call chain order */
  568. if (!strcmp(tok2, "caller"))
  569. callchain_param.order = ORDER_CALLER;
  570. else if (!strcmp(tok2, "callee"))
  571. callchain_param.order = ORDER_CALLEE;
  572. else
  573. return -1;
  574. setup:
  575. if (callchain_register_param(&callchain_param) < 0) {
  576. fprintf(stderr, "Can't register callchain params\n");
  577. return -1;
  578. }
  579. return 0;
  580. }
  581. static int
  582. parse_branch_mode(const struct option *opt __maybe_unused,
  583. const char *str __maybe_unused, int unset)
  584. {
  585. sort__branch_mode = !unset;
  586. return 0;
  587. }
  588. int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
  589. {
  590. struct perf_session *session;
  591. struct stat st;
  592. bool has_br_stack = false;
  593. int ret = -1;
  594. char callchain_default_opt[] = "fractal,0.5,callee";
  595. const char * const report_usage[] = {
  596. "perf report [<options>]",
  597. NULL
  598. };
  599. struct perf_report report = {
  600. .tool = {
  601. .sample = process_sample_event,
  602. .mmap = perf_event__process_mmap,
  603. .comm = perf_event__process_comm,
  604. .exit = perf_event__process_exit,
  605. .fork = perf_event__process_fork,
  606. .lost = perf_event__process_lost,
  607. .read = process_read_event,
  608. .attr = perf_event__process_attr,
  609. .event_type = perf_event__process_event_type,
  610. .tracing_data = perf_event__process_tracing_data,
  611. .build_id = perf_event__process_build_id,
  612. .ordered_samples = true,
  613. .ordering_requires_timestamps = true,
  614. },
  615. .pretty_printing_style = "normal",
  616. };
  617. const struct option options[] = {
  618. OPT_STRING('i', "input", &input_name, "file",
  619. "input file name"),
  620. OPT_INCR('v', "verbose", &verbose,
  621. "be more verbose (show symbol address, etc)"),
  622. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  623. "dump raw trace in ASCII"),
  624. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  625. "file", "vmlinux pathname"),
  626. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  627. "file", "kallsyms pathname"),
  628. OPT_BOOLEAN('f', "force", &report.force, "don't complain, do it"),
  629. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  630. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  631. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  632. "Show a column with the number of samples"),
  633. OPT_BOOLEAN('T', "threads", &report.show_threads,
  634. "Show per-thread event counters"),
  635. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  636. "pretty printing style key: normal raw"),
  637. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  638. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  639. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  640. "Use the stdio interface"),
  641. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  642. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline,"
  643. " dso_to, dso_from, symbol_to, symbol_from, mispredict,"
  644. " weight, local_weight, mem, symbol_daddr, dso_daddr, tlb, "
  645. "snoop, locked"),
  646. OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  647. "Show sample percentage for different cpu modes"),
  648. OPT_STRING('p', "parent", &parent_pattern, "regex",
  649. "regex filter to identify parent, see: '--sort parent'"),
  650. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  651. "Only display entries with parent-match"),
  652. OPT_CALLBACK_DEFAULT('g', "call-graph", &report, "output_type,min_percent[,print_limit],call_order",
  653. "Display callchains using output_type (graph, flat, fractal, or none) , min percent threshold, optional print limit and callchain order. "
  654. "Default: fractal,0.5,callee", &parse_callchain_opt, callchain_default_opt),
  655. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  656. "alias for inverted call graph"),
  657. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  658. "only consider symbols in these dsos"),
  659. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  660. "only consider symbols in these comms"),
  661. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  662. "only consider these symbols"),
  663. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  664. "only show symbols that (partially) match with this filter"),
  665. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  666. "width[,width...]",
  667. "don't try to adjust column width, use these fixed values"),
  668. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  669. "separator for columns, no spaces will be added between "
  670. "columns '.' is reserved."),
  671. OPT_BOOLEAN('U', "hide-unresolved", &report.hide_unresolved,
  672. "Only display entries resolved to a symbol"),
  673. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  674. "Look for files with symbols relative to this directory"),
  675. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  676. "list of cpus to profile"),
  677. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  678. "Display extended information about perf.data file"),
  679. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  680. "Interleave source code with assembly code (default)"),
  681. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  682. "Display raw encoding of assembly instructions (default)"),
  683. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  684. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  685. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  686. "Show a column with the sum of periods"),
  687. OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
  688. "Show event group information together"),
  689. OPT_CALLBACK_NOOPT('b', "branch-stack", &sort__branch_mode, "",
  690. "use branch records for histogram filling", parse_branch_mode),
  691. OPT_STRING(0, "objdump", &objdump_path, "path",
  692. "objdump binary to use for disassembly and annotations"),
  693. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  694. "Disable symbol demangling"),
  695. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  696. OPT_END()
  697. };
  698. perf_config(perf_report_config, NULL);
  699. argc = parse_options(argc, argv, options, report_usage, 0);
  700. if (report.use_stdio)
  701. use_browser = 0;
  702. else if (report.use_tui)
  703. use_browser = 1;
  704. else if (report.use_gtk)
  705. use_browser = 2;
  706. if (report.inverted_callchain)
  707. callchain_param.order = ORDER_CALLER;
  708. if (!input_name || !strlen(input_name)) {
  709. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  710. input_name = "-";
  711. else
  712. input_name = "perf.data";
  713. }
  714. if (strcmp(input_name, "-") != 0)
  715. setup_browser(true);
  716. else {
  717. use_browser = 0;
  718. perf_hpp__column_enable(PERF_HPP__OVERHEAD);
  719. perf_hpp__init();
  720. }
  721. repeat:
  722. session = perf_session__new(input_name, O_RDONLY,
  723. report.force, false, &report.tool);
  724. if (session == NULL)
  725. return -ENOMEM;
  726. report.session = session;
  727. has_br_stack = perf_header__has_feat(&session->header,
  728. HEADER_BRANCH_STACK);
  729. if (sort__branch_mode == -1 && has_br_stack)
  730. sort__branch_mode = 1;
  731. /* sort__branch_mode could be 0 if --no-branch-stack */
  732. if (sort__branch_mode == 1) {
  733. /*
  734. * if no sort_order is provided, then specify
  735. * branch-mode specific order
  736. */
  737. if (sort_order == default_sort_order)
  738. sort_order = "comm,dso_from,symbol_from,"
  739. "dso_to,symbol_to";
  740. }
  741. if (report.mem_mode) {
  742. if (sort__branch_mode == 1) {
  743. fprintf(stderr, "branch and mem mode incompatible\n");
  744. goto error;
  745. }
  746. /*
  747. * if no sort_order is provided, then specify
  748. * branch-mode specific order
  749. */
  750. if (sort_order == default_sort_order)
  751. sort_order = "local_weight,mem,sym,dso,symbol_daddr,dso_daddr,snoop,tlb,locked";
  752. }
  753. if (setup_sorting() < 0)
  754. usage_with_options(report_usage, options);
  755. /*
  756. * Only in the TUI browser we are doing integrated annotation,
  757. * so don't allocate extra space that won't be used in the stdio
  758. * implementation.
  759. */
  760. if (use_browser == 1 && sort__has_sym) {
  761. symbol_conf.priv_size = sizeof(struct annotation);
  762. report.annotate_init = symbol__annotate_init;
  763. /*
  764. * For searching by name on the "Browse map details".
  765. * providing it only in verbose mode not to bloat too
  766. * much struct symbol.
  767. */
  768. if (verbose) {
  769. /*
  770. * XXX: Need to provide a less kludgy way to ask for
  771. * more space per symbol, the u32 is for the index on
  772. * the ui browser.
  773. * See symbol__browser_index.
  774. */
  775. symbol_conf.priv_size += sizeof(u32);
  776. symbol_conf.sort_by_name = true;
  777. }
  778. }
  779. if (symbol__init() < 0)
  780. goto error;
  781. if (parent_pattern != default_parent_pattern) {
  782. if (sort_dimension__add("parent") < 0)
  783. goto error;
  784. /*
  785. * Only show the parent fields if we explicitly
  786. * sort that way. If we only use parent machinery
  787. * for filtering, we don't want it.
  788. */
  789. if (!strstr(sort_order, "parent"))
  790. sort_parent.elide = 1;
  791. } else
  792. symbol_conf.exclude_other = false;
  793. if (argc) {
  794. /*
  795. * Special case: if there's an argument left then assume that
  796. * it's a symbol filter:
  797. */
  798. if (argc > 1)
  799. usage_with_options(report_usage, options);
  800. report.symbol_filter_str = argv[0];
  801. }
  802. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
  803. if (sort__branch_mode == 1) {
  804. sort_entry__setup_elide(&sort_dso_from, symbol_conf.dso_from_list, "dso_from", stdout);
  805. sort_entry__setup_elide(&sort_dso_to, symbol_conf.dso_to_list, "dso_to", stdout);
  806. sort_entry__setup_elide(&sort_sym_from, symbol_conf.sym_from_list, "sym_from", stdout);
  807. sort_entry__setup_elide(&sort_sym_to, symbol_conf.sym_to_list, "sym_to", stdout);
  808. } else {
  809. if (report.mem_mode) {
  810. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "symbol_daddr", stdout);
  811. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso_daddr", stdout);
  812. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "mem", stdout);
  813. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "local_weight", stdout);
  814. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "tlb", stdout);
  815. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "snoop", stdout);
  816. }
  817. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
  818. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
  819. }
  820. ret = __cmd_report(&report);
  821. if (ret == K_SWITCH_INPUT_DATA) {
  822. perf_session__delete(session);
  823. goto repeat;
  824. } else
  825. ret = 0;
  826. error:
  827. perf_session__delete(session);
  828. return ret;
  829. }