builtin-report.c 27 KB

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