builtin-report.c 26 KB

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