builtin-report.c 28 KB

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