builtin-report.c 28 KB

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