builtin-report.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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 *session = rep->session;
  323. u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
  324. bool is_pipe = perf_data_file__is_pipe(session->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 *hists,
  362. const char *evname, FILE *fp)
  363. {
  364. size_t ret;
  365. char unit;
  366. unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  367. u64 nr_events = hists->stats.total_period;
  368. struct perf_evsel *evsel = hists_to_evsel(hists);
  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 ui_progress prog;
  428. struct perf_data_file *file = session->file;
  429. signal(SIGINT, sig_handler);
  430. if (rep->cpu_list) {
  431. ret = perf_session__cpu_bitmap(session, rep->cpu_list,
  432. rep->cpu_bitmap);
  433. if (ret)
  434. return ret;
  435. }
  436. if (use_browser <= 0)
  437. perf_session__fprintf_info(session, stdout, rep->show_full_info);
  438. if (rep->show_threads)
  439. perf_read_values_init(&rep->show_threads_values);
  440. ret = perf_report__setup_sample_type(rep);
  441. if (ret)
  442. return ret;
  443. ret = perf_session__process_events(session, &rep->tool);
  444. if (ret)
  445. return ret;
  446. kernel_map = session->machines.host.vmlinux_maps[MAP__FUNCTION];
  447. kernel_kmap = map__kmap(kernel_map);
  448. if (kernel_map == NULL ||
  449. (kernel_map->dso->hit &&
  450. (kernel_kmap->ref_reloc_sym == NULL ||
  451. kernel_kmap->ref_reloc_sym->addr == 0))) {
  452. const char *desc =
  453. "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
  454. "can't be resolved.";
  455. if (kernel_map) {
  456. const struct dso *kdso = kernel_map->dso;
  457. if (!RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION])) {
  458. desc = "If some relocation was applied (e.g. "
  459. "kexec) symbols may be misresolved.";
  460. }
  461. }
  462. ui__warning(
  463. "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
  464. "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
  465. "Samples in kernel modules can't be resolved as well.\n\n",
  466. desc);
  467. }
  468. if (verbose > 3)
  469. perf_session__fprintf(session, stdout);
  470. if (verbose > 2)
  471. perf_session__fprintf_dsos(session, stdout);
  472. if (dump_trace) {
  473. perf_session__fprintf_nr_events(session, stdout);
  474. return 0;
  475. }
  476. nr_samples = 0;
  477. list_for_each_entry(pos, &session->evlist->entries, node)
  478. nr_samples += pos->hists.nr_entries;
  479. ui_progress__init(&prog, nr_samples, "Merging related events...");
  480. nr_samples = 0;
  481. list_for_each_entry(pos, &session->evlist->entries, node) {
  482. struct hists *hists = &pos->hists;
  483. if (pos->idx == 0)
  484. hists->symbol_filter_str = rep->symbol_filter_str;
  485. hists__collapse_resort(hists, &prog);
  486. nr_samples += hists->stats.nr_events[PERF_RECORD_SAMPLE];
  487. /* Non-group events are considered as leader */
  488. if (symbol_conf.event_group &&
  489. !perf_evsel__is_group_leader(pos)) {
  490. struct hists *leader_hists = &pos->leader->hists;
  491. hists__match(leader_hists, hists);
  492. hists__link(leader_hists, hists);
  493. }
  494. }
  495. ui_progress__finish();
  496. if (session_done())
  497. return 0;
  498. if (nr_samples == 0) {
  499. ui__error("The %s file has no samples!\n", file->path);
  500. return 0;
  501. }
  502. list_for_each_entry(pos, &session->evlist->entries, node)
  503. hists__output_resort(&pos->hists);
  504. if (use_browser > 0) {
  505. if (use_browser == 1) {
  506. ret = perf_evlist__tui_browse_hists(session->evlist,
  507. help, NULL,
  508. rep->min_percent,
  509. &session->header.env);
  510. /*
  511. * Usually "ret" is the last pressed key, and we only
  512. * care if the key notifies us to switch data file.
  513. */
  514. if (ret != K_SWITCH_INPUT_DATA)
  515. ret = 0;
  516. } else if (use_browser == 2) {
  517. int (*hist_browser)(struct perf_evlist *,
  518. const char *,
  519. struct hist_browser_timer *,
  520. float min_pcnt);
  521. hist_browser = dlsym(perf_gtk_handle,
  522. "perf_evlist__gtk_browse_hists");
  523. if (hist_browser == NULL) {
  524. ui__error("GTK browser not found!\n");
  525. return ret;
  526. }
  527. hist_browser(session->evlist, help, NULL,
  528. rep->min_percent);
  529. }
  530. } else
  531. perf_evlist__tty_browse_hists(session->evlist, rep, help);
  532. return ret;
  533. }
  534. static int
  535. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  536. {
  537. struct perf_report *rep = (struct perf_report *)opt->value;
  538. char *tok, *tok2;
  539. char *endptr;
  540. /*
  541. * --no-call-graph
  542. */
  543. if (unset) {
  544. rep->dont_use_callchains = true;
  545. return 0;
  546. }
  547. symbol_conf.use_callchain = true;
  548. if (!arg)
  549. return 0;
  550. tok = strtok((char *)arg, ",");
  551. if (!tok)
  552. return -1;
  553. /* get the output mode */
  554. if (!strncmp(tok, "graph", strlen(arg)))
  555. callchain_param.mode = CHAIN_GRAPH_ABS;
  556. else if (!strncmp(tok, "flat", strlen(arg)))
  557. callchain_param.mode = CHAIN_FLAT;
  558. else if (!strncmp(tok, "fractal", strlen(arg)))
  559. callchain_param.mode = CHAIN_GRAPH_REL;
  560. else if (!strncmp(tok, "none", strlen(arg))) {
  561. callchain_param.mode = CHAIN_NONE;
  562. symbol_conf.use_callchain = false;
  563. return 0;
  564. }
  565. else
  566. return -1;
  567. /* get the min percentage */
  568. tok = strtok(NULL, ",");
  569. if (!tok)
  570. goto setup;
  571. callchain_param.min_percent = strtod(tok, &endptr);
  572. if (tok == endptr)
  573. return -1;
  574. /* get the print limit */
  575. tok2 = strtok(NULL, ",");
  576. if (!tok2)
  577. goto setup;
  578. if (tok2[0] != 'c') {
  579. callchain_param.print_limit = strtoul(tok2, &endptr, 0);
  580. tok2 = strtok(NULL, ",");
  581. if (!tok2)
  582. goto setup;
  583. }
  584. /* get the call chain order */
  585. if (!strncmp(tok2, "caller", strlen("caller")))
  586. callchain_param.order = ORDER_CALLER;
  587. else if (!strncmp(tok2, "callee", strlen("callee")))
  588. callchain_param.order = ORDER_CALLEE;
  589. else
  590. return -1;
  591. /* Get the sort key */
  592. tok2 = strtok(NULL, ",");
  593. if (!tok2)
  594. goto setup;
  595. if (!strncmp(tok2, "function", strlen("function")))
  596. callchain_param.key = CCKEY_FUNCTION;
  597. else if (!strncmp(tok2, "address", strlen("address")))
  598. callchain_param.key = CCKEY_ADDRESS;
  599. else
  600. return -1;
  601. setup:
  602. if (callchain_register_param(&callchain_param) < 0) {
  603. fprintf(stderr, "Can't register callchain params\n");
  604. return -1;
  605. }
  606. return 0;
  607. }
  608. int
  609. report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
  610. const char *arg, int unset __maybe_unused)
  611. {
  612. if (arg) {
  613. int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
  614. if (err) {
  615. char buf[BUFSIZ];
  616. regerror(err, &ignore_callees_regex, buf, sizeof(buf));
  617. pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
  618. return -1;
  619. }
  620. have_ignore_callees = 1;
  621. }
  622. return 0;
  623. }
  624. static int
  625. parse_branch_mode(const struct option *opt __maybe_unused,
  626. const char *str __maybe_unused, int unset)
  627. {
  628. int *branch_mode = opt->value;
  629. *branch_mode = !unset;
  630. return 0;
  631. }
  632. static int
  633. parse_percent_limit(const struct option *opt, const char *str,
  634. int unset __maybe_unused)
  635. {
  636. struct perf_report *rep = opt->value;
  637. rep->min_percent = strtof(str, NULL);
  638. return 0;
  639. }
  640. int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
  641. {
  642. struct perf_session *session;
  643. struct stat st;
  644. bool has_br_stack = false;
  645. int branch_mode = -1;
  646. int ret = -1;
  647. char callchain_default_opt[] = "fractal,0.5,callee";
  648. const char * const report_usage[] = {
  649. "perf report [<options>]",
  650. NULL
  651. };
  652. struct perf_report report = {
  653. .tool = {
  654. .sample = process_sample_event,
  655. .mmap = perf_event__process_mmap,
  656. .mmap2 = perf_event__process_mmap2,
  657. .comm = perf_event__process_comm,
  658. .exit = perf_event__process_exit,
  659. .fork = perf_event__process_fork,
  660. .lost = perf_event__process_lost,
  661. .read = process_read_event,
  662. .attr = perf_event__process_attr,
  663. .tracing_data = perf_event__process_tracing_data,
  664. .build_id = perf_event__process_build_id,
  665. .ordered_samples = true,
  666. .ordering_requires_timestamps = true,
  667. },
  668. .max_stack = PERF_MAX_STACK_DEPTH,
  669. .pretty_printing_style = "normal",
  670. };
  671. const struct option options[] = {
  672. OPT_STRING('i', "input", &input_name, "file",
  673. "input file name"),
  674. OPT_INCR('v', "verbose", &verbose,
  675. "be more verbose (show symbol address, etc)"),
  676. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  677. "dump raw trace in ASCII"),
  678. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  679. "file", "vmlinux pathname"),
  680. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  681. "file", "kallsyms pathname"),
  682. OPT_BOOLEAN('f', "force", &report.force, "don't complain, do it"),
  683. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  684. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  685. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  686. "Show a column with the number of samples"),
  687. OPT_BOOLEAN('T', "threads", &report.show_threads,
  688. "Show per-thread event counters"),
  689. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  690. "pretty printing style key: normal raw"),
  691. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  692. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  693. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  694. "Use the stdio interface"),
  695. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  696. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline,"
  697. " dso_to, dso_from, symbol_to, symbol_from, mispredict,"
  698. " weight, local_weight, mem, symbol_daddr, dso_daddr, tlb, "
  699. "snoop, locked, abort, in_tx, transaction"),
  700. OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  701. "Show sample percentage for different cpu modes"),
  702. OPT_STRING('p', "parent", &parent_pattern, "regex",
  703. "regex filter to identify parent, see: '--sort parent'"),
  704. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  705. "Only display entries with parent-match"),
  706. OPT_CALLBACK_DEFAULT('g', "call-graph", &report, "output_type,min_percent[,print_limit],call_order",
  707. "Display callchains using output_type (graph, flat, fractal, or none) , min percent threshold, optional print limit, callchain order, key (function or address). "
  708. "Default: fractal,0.5,callee,function", &parse_callchain_opt, callchain_default_opt),
  709. OPT_INTEGER(0, "max-stack", &report.max_stack,
  710. "Set the maximum stack depth when parsing the callchain, "
  711. "anything beyond the specified depth will be ignored. "
  712. "Default: " __stringify(PERF_MAX_STACK_DEPTH)),
  713. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  714. "alias for inverted call graph"),
  715. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  716. "ignore callees of these functions in call graphs",
  717. report_parse_ignore_callees_opt),
  718. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  719. "only consider symbols in these dsos"),
  720. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  721. "only consider symbols in these comms"),
  722. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  723. "only consider these symbols"),
  724. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  725. "only show symbols that (partially) match with this filter"),
  726. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  727. "width[,width...]",
  728. "don't try to adjust column width, use these fixed values"),
  729. OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
  730. "separator for columns, no spaces will be added between "
  731. "columns '.' is reserved."),
  732. OPT_BOOLEAN('U', "hide-unresolved", &report.hide_unresolved,
  733. "Only display entries resolved to a symbol"),
  734. OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
  735. "Look for files with symbols relative to this directory"),
  736. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  737. "list of cpus to profile"),
  738. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  739. "Display extended information about perf.data file"),
  740. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  741. "Interleave source code with assembly code (default)"),
  742. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  743. "Display raw encoding of assembly instructions (default)"),
  744. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  745. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  746. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  747. "Show a column with the sum of periods"),
  748. OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
  749. "Show event group information together"),
  750. OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
  751. "use branch records for histogram filling", parse_branch_mode),
  752. OPT_STRING(0, "objdump", &objdump_path, "path",
  753. "objdump binary to use for disassembly and annotations"),
  754. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  755. "Disable symbol demangling"),
  756. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  757. OPT_CALLBACK(0, "percent-limit", &report, "percent",
  758. "Don't show entries under that percent", parse_percent_limit),
  759. OPT_END()
  760. };
  761. struct perf_data_file file = {
  762. .mode = PERF_DATA_MODE_READ,
  763. };
  764. perf_config(perf_report_config, &report);
  765. argc = parse_options(argc, argv, options, report_usage, 0);
  766. if (report.use_stdio)
  767. use_browser = 0;
  768. else if (report.use_tui)
  769. use_browser = 1;
  770. else if (report.use_gtk)
  771. use_browser = 2;
  772. if (report.inverted_callchain)
  773. callchain_param.order = ORDER_CALLER;
  774. if (!input_name || !strlen(input_name)) {
  775. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  776. input_name = "-";
  777. else
  778. input_name = "perf.data";
  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. if (parent_pattern != default_parent_pattern) {
  817. if (sort_dimension__add("parent") < 0)
  818. goto error;
  819. }
  820. if (strcmp(input_name, "-") != 0)
  821. setup_browser(true);
  822. else {
  823. use_browser = 0;
  824. perf_hpp__init();
  825. }
  826. /*
  827. * Only in the TUI browser we are doing integrated annotation,
  828. * so don't allocate extra space that won't be used in the stdio
  829. * implementation.
  830. */
  831. if (use_browser == 1 && sort__has_sym) {
  832. symbol_conf.priv_size = sizeof(struct annotation);
  833. machines__set_symbol_filter(&session->machines,
  834. symbol__annotate_init);
  835. /*
  836. * For searching by name on the "Browse map details".
  837. * providing it only in verbose mode not to bloat too
  838. * much struct symbol.
  839. */
  840. if (verbose) {
  841. /*
  842. * XXX: Need to provide a less kludgy way to ask for
  843. * more space per symbol, the u32 is for the index on
  844. * the ui browser.
  845. * See symbol__browser_index.
  846. */
  847. symbol_conf.priv_size += sizeof(u32);
  848. symbol_conf.sort_by_name = true;
  849. }
  850. }
  851. if (symbol__init() < 0)
  852. goto error;
  853. if (argc) {
  854. /*
  855. * Special case: if there's an argument left then assume that
  856. * it's a symbol filter:
  857. */
  858. if (argc > 1)
  859. usage_with_options(report_usage, options);
  860. report.symbol_filter_str = argv[0];
  861. }
  862. sort__setup_elide(stdout);
  863. ret = __cmd_report(&report);
  864. if (ret == K_SWITCH_INPUT_DATA) {
  865. perf_session__delete(session);
  866. goto repeat;
  867. } else
  868. ret = 0;
  869. error:
  870. perf_session__delete(session);
  871. return ret;
  872. }