builtin-top.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /*
  2. * builtin-top.c
  3. *
  4. * Builtin top command: Display a continuously updated profile of
  5. * any workload, CPU or specific PID.
  6. *
  7. * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  8. * 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Improvements and fixes by:
  11. *
  12. * Arjan van de Ven <arjan@linux.intel.com>
  13. * Yanmin Zhang <yanmin.zhang@intel.com>
  14. * Wu Fengguang <fengguang.wu@intel.com>
  15. * Mike Galbraith <efault@gmx.de>
  16. * Paul Mackerras <paulus@samba.org>
  17. *
  18. * Released under the GPL v2. (and only v2, not any later version)
  19. */
  20. #include "builtin.h"
  21. #include "perf.h"
  22. #include "util/annotate.h"
  23. #include "util/cache.h"
  24. #include "util/color.h"
  25. #include "util/evlist.h"
  26. #include "util/evsel.h"
  27. #include "util/machine.h"
  28. #include "util/session.h"
  29. #include "util/symbol.h"
  30. #include "util/thread.h"
  31. #include "util/thread_map.h"
  32. #include "util/top.h"
  33. #include "util/util.h"
  34. #include <linux/rbtree.h>
  35. #include "util/parse-options.h"
  36. #include "util/parse-events.h"
  37. #include "util/cpumap.h"
  38. #include "util/xyarray.h"
  39. #include "util/sort.h"
  40. #include "util/intlist.h"
  41. #include "util/parse-branch-options.h"
  42. #include "arch/common.h"
  43. #include "util/debug.h"
  44. #include <assert.h>
  45. #include <elf.h>
  46. #include <fcntl.h>
  47. #include <stdio.h>
  48. #include <termios.h>
  49. #include <unistd.h>
  50. #include <inttypes.h>
  51. #include <errno.h>
  52. #include <time.h>
  53. #include <sched.h>
  54. #include <sys/syscall.h>
  55. #include <sys/ioctl.h>
  56. #include <poll.h>
  57. #include <sys/prctl.h>
  58. #include <sys/wait.h>
  59. #include <sys/uio.h>
  60. #include <sys/utsname.h>
  61. #include <sys/mman.h>
  62. #include <linux/types.h>
  63. static volatile int done;
  64. static volatile int resize;
  65. #define HEADER_LINE_NR 5
  66. static void perf_top__update_print_entries(struct perf_top *top)
  67. {
  68. top->print_entries = top->winsize.ws_row - HEADER_LINE_NR;
  69. }
  70. static void perf_top__sig_winch(int sig __maybe_unused,
  71. siginfo_t *info __maybe_unused, void *arg __maybe_unused)
  72. {
  73. resize = 1;
  74. }
  75. static void perf_top__resize(struct perf_top *top)
  76. {
  77. get_term_dimensions(&top->winsize);
  78. perf_top__update_print_entries(top);
  79. }
  80. static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
  81. {
  82. struct symbol *sym;
  83. struct annotation *notes;
  84. struct map *map;
  85. int err = -1;
  86. if (!he || !he->ms.sym)
  87. return -1;
  88. sym = he->ms.sym;
  89. map = he->ms.map;
  90. /*
  91. * We can't annotate with just /proc/kallsyms
  92. */
  93. if (map->dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  94. !dso__is_kcore(map->dso)) {
  95. pr_err("Can't annotate %s: No vmlinux file was found in the "
  96. "path\n", sym->name);
  97. sleep(1);
  98. return -1;
  99. }
  100. notes = symbol__annotation(sym);
  101. if (notes->src != NULL) {
  102. pthread_mutex_lock(&notes->lock);
  103. goto out_assign;
  104. }
  105. pthread_mutex_lock(&notes->lock);
  106. if (symbol__alloc_hist(sym) < 0) {
  107. pthread_mutex_unlock(&notes->lock);
  108. pr_err("Not enough memory for annotating '%s' symbol!\n",
  109. sym->name);
  110. sleep(1);
  111. return err;
  112. }
  113. err = symbol__annotate(sym, map, 0);
  114. if (err == 0) {
  115. out_assign:
  116. top->sym_filter_entry = he;
  117. }
  118. pthread_mutex_unlock(&notes->lock);
  119. return err;
  120. }
  121. static void __zero_source_counters(struct hist_entry *he)
  122. {
  123. struct symbol *sym = he->ms.sym;
  124. symbol__annotate_zero_histograms(sym);
  125. }
  126. static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
  127. {
  128. struct utsname uts;
  129. int err = uname(&uts);
  130. ui__warning("Out of bounds address found:\n\n"
  131. "Addr: %" PRIx64 "\n"
  132. "DSO: %s %c\n"
  133. "Map: %" PRIx64 "-%" PRIx64 "\n"
  134. "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n"
  135. "Arch: %s\n"
  136. "Kernel: %s\n"
  137. "Tools: %s\n\n"
  138. "Not all samples will be on the annotation output.\n\n"
  139. "Please report to linux-kernel@vger.kernel.org\n",
  140. ip, map->dso->long_name, dso__symtab_origin(map->dso),
  141. map->start, map->end, sym->start, sym->end,
  142. sym->binding == STB_GLOBAL ? 'g' :
  143. sym->binding == STB_LOCAL ? 'l' : 'w', sym->name,
  144. err ? "[unknown]" : uts.machine,
  145. err ? "[unknown]" : uts.release, perf_version_string);
  146. if (use_browser <= 0)
  147. sleep(5);
  148. map->erange_warned = true;
  149. }
  150. static void perf_top__record_precise_ip(struct perf_top *top,
  151. struct hist_entry *he,
  152. int counter, u64 ip)
  153. {
  154. struct annotation *notes;
  155. struct symbol *sym;
  156. int err = 0;
  157. if (he == NULL || he->ms.sym == NULL ||
  158. ((top->sym_filter_entry == NULL ||
  159. top->sym_filter_entry->ms.sym != he->ms.sym) && use_browser != 1))
  160. return;
  161. sym = he->ms.sym;
  162. notes = symbol__annotation(sym);
  163. if (pthread_mutex_trylock(&notes->lock))
  164. return;
  165. ip = he->ms.map->map_ip(he->ms.map, ip);
  166. if (ui__has_annotation())
  167. err = hist_entry__inc_addr_samples(he, counter, ip);
  168. pthread_mutex_unlock(&notes->lock);
  169. /*
  170. * This function is now called with he->hists->lock held.
  171. * Release it before going to sleep.
  172. */
  173. pthread_mutex_unlock(&he->hists->lock);
  174. if (err == -ERANGE && !he->ms.map->erange_warned)
  175. ui__warn_map_erange(he->ms.map, sym, ip);
  176. else if (err == -ENOMEM) {
  177. pr_err("Not enough memory for annotating '%s' symbol!\n",
  178. sym->name);
  179. sleep(1);
  180. }
  181. pthread_mutex_lock(&he->hists->lock);
  182. }
  183. static void perf_top__show_details(struct perf_top *top)
  184. {
  185. struct hist_entry *he = top->sym_filter_entry;
  186. struct annotation *notes;
  187. struct symbol *symbol;
  188. int more;
  189. if (!he)
  190. return;
  191. symbol = he->ms.sym;
  192. notes = symbol__annotation(symbol);
  193. pthread_mutex_lock(&notes->lock);
  194. if (notes->src == NULL)
  195. goto out_unlock;
  196. printf("Showing %s for %s\n", perf_evsel__name(top->sym_evsel), symbol->name);
  197. printf(" Events Pcnt (>=%d%%)\n", top->sym_pcnt_filter);
  198. more = symbol__annotate_printf(symbol, he->ms.map, top->sym_evsel,
  199. 0, top->sym_pcnt_filter, top->print_entries, 4);
  200. if (top->evlist->enabled) {
  201. if (top->zero)
  202. symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx);
  203. else
  204. symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx);
  205. }
  206. if (more != 0)
  207. printf("%d lines not displayed, maybe increase display entries [e]\n", more);
  208. out_unlock:
  209. pthread_mutex_unlock(&notes->lock);
  210. }
  211. static void perf_top__print_sym_table(struct perf_top *top)
  212. {
  213. char bf[160];
  214. int printed = 0;
  215. const int win_width = top->winsize.ws_col - 1;
  216. struct hists *hists = evsel__hists(top->sym_evsel);
  217. puts(CONSOLE_CLEAR);
  218. perf_top__header_snprintf(top, bf, sizeof(bf));
  219. printf("%s\n", bf);
  220. perf_top__reset_sample_counters(top);
  221. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  222. if (hists->stats.nr_lost_warned !=
  223. hists->stats.nr_events[PERF_RECORD_LOST]) {
  224. hists->stats.nr_lost_warned =
  225. hists->stats.nr_events[PERF_RECORD_LOST];
  226. color_fprintf(stdout, PERF_COLOR_RED,
  227. "WARNING: LOST %d chunks, Check IO/CPU overload",
  228. hists->stats.nr_lost_warned);
  229. ++printed;
  230. }
  231. if (top->sym_filter_entry) {
  232. perf_top__show_details(top);
  233. return;
  234. }
  235. if (top->evlist->enabled) {
  236. if (top->zero) {
  237. hists__delete_entries(hists);
  238. } else {
  239. hists__decay_entries(hists, top->hide_user_symbols,
  240. top->hide_kernel_symbols);
  241. }
  242. }
  243. hists__collapse_resort(hists, NULL);
  244. hists__output_resort(hists, NULL);
  245. hists__output_recalc_col_len(hists, top->print_entries - printed);
  246. putchar('\n');
  247. hists__fprintf(hists, false, top->print_entries - printed, win_width,
  248. top->min_percent, stdout);
  249. }
  250. static void prompt_integer(int *target, const char *msg)
  251. {
  252. char *buf = malloc(0), *p;
  253. size_t dummy = 0;
  254. int tmp;
  255. fprintf(stdout, "\n%s: ", msg);
  256. if (getline(&buf, &dummy, stdin) < 0)
  257. return;
  258. p = strchr(buf, '\n');
  259. if (p)
  260. *p = 0;
  261. p = buf;
  262. while(*p) {
  263. if (!isdigit(*p))
  264. goto out_free;
  265. p++;
  266. }
  267. tmp = strtoul(buf, NULL, 10);
  268. *target = tmp;
  269. out_free:
  270. free(buf);
  271. }
  272. static void prompt_percent(int *target, const char *msg)
  273. {
  274. int tmp = 0;
  275. prompt_integer(&tmp, msg);
  276. if (tmp >= 0 && tmp <= 100)
  277. *target = tmp;
  278. }
  279. static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
  280. {
  281. char *buf = malloc(0), *p;
  282. struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
  283. struct hists *hists = evsel__hists(top->sym_evsel);
  284. struct rb_node *next;
  285. size_t dummy = 0;
  286. /* zero counters of active symbol */
  287. if (syme) {
  288. __zero_source_counters(syme);
  289. top->sym_filter_entry = NULL;
  290. }
  291. fprintf(stdout, "\n%s: ", msg);
  292. if (getline(&buf, &dummy, stdin) < 0)
  293. goto out_free;
  294. p = strchr(buf, '\n');
  295. if (p)
  296. *p = 0;
  297. next = rb_first(&hists->entries);
  298. while (next) {
  299. n = rb_entry(next, struct hist_entry, rb_node);
  300. if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
  301. found = n;
  302. break;
  303. }
  304. next = rb_next(&n->rb_node);
  305. }
  306. if (!found) {
  307. fprintf(stderr, "Sorry, %s is not active.\n", buf);
  308. sleep(1);
  309. } else
  310. perf_top__parse_source(top, found);
  311. out_free:
  312. free(buf);
  313. }
  314. static void perf_top__print_mapped_keys(struct perf_top *top)
  315. {
  316. char *name = NULL;
  317. if (top->sym_filter_entry) {
  318. struct symbol *sym = top->sym_filter_entry->ms.sym;
  319. name = sym->name;
  320. }
  321. fprintf(stdout, "\nMapped keys:\n");
  322. fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs);
  323. fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries);
  324. if (top->evlist->nr_entries > 1)
  325. fprintf(stdout, "\t[E] active event counter. \t(%s)\n", perf_evsel__name(top->sym_evsel));
  326. fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter);
  327. fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->sym_pcnt_filter);
  328. fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
  329. fprintf(stdout, "\t[S] stop annotation.\n");
  330. fprintf(stdout,
  331. "\t[K] hide kernel_symbols symbols. \t(%s)\n",
  332. top->hide_kernel_symbols ? "yes" : "no");
  333. fprintf(stdout,
  334. "\t[U] hide user symbols. \t(%s)\n",
  335. top->hide_user_symbols ? "yes" : "no");
  336. fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0);
  337. fprintf(stdout, "\t[qQ] quit.\n");
  338. }
  339. static int perf_top__key_mapped(struct perf_top *top, int c)
  340. {
  341. switch (c) {
  342. case 'd':
  343. case 'e':
  344. case 'f':
  345. case 'z':
  346. case 'q':
  347. case 'Q':
  348. case 'K':
  349. case 'U':
  350. case 'F':
  351. case 's':
  352. case 'S':
  353. return 1;
  354. case 'E':
  355. return top->evlist->nr_entries > 1 ? 1 : 0;
  356. default:
  357. break;
  358. }
  359. return 0;
  360. }
  361. static bool perf_top__handle_keypress(struct perf_top *top, int c)
  362. {
  363. bool ret = true;
  364. if (!perf_top__key_mapped(top, c)) {
  365. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  366. struct termios save;
  367. perf_top__print_mapped_keys(top);
  368. fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
  369. fflush(stdout);
  370. set_term_quiet_input(&save);
  371. poll(&stdin_poll, 1, -1);
  372. c = getc(stdin);
  373. tcsetattr(0, TCSAFLUSH, &save);
  374. if (!perf_top__key_mapped(top, c))
  375. return ret;
  376. }
  377. switch (c) {
  378. case 'd':
  379. prompt_integer(&top->delay_secs, "Enter display delay");
  380. if (top->delay_secs < 1)
  381. top->delay_secs = 1;
  382. break;
  383. case 'e':
  384. prompt_integer(&top->print_entries, "Enter display entries (lines)");
  385. if (top->print_entries == 0) {
  386. struct sigaction act = {
  387. .sa_sigaction = perf_top__sig_winch,
  388. .sa_flags = SA_SIGINFO,
  389. };
  390. perf_top__resize(top);
  391. sigaction(SIGWINCH, &act, NULL);
  392. } else {
  393. signal(SIGWINCH, SIG_DFL);
  394. }
  395. break;
  396. case 'E':
  397. if (top->evlist->nr_entries > 1) {
  398. /* Select 0 as the default event: */
  399. int counter = 0;
  400. fprintf(stderr, "\nAvailable events:");
  401. evlist__for_each(top->evlist, top->sym_evsel)
  402. fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, perf_evsel__name(top->sym_evsel));
  403. prompt_integer(&counter, "Enter details event counter");
  404. if (counter >= top->evlist->nr_entries) {
  405. top->sym_evsel = perf_evlist__first(top->evlist);
  406. fprintf(stderr, "Sorry, no such event, using %s.\n", perf_evsel__name(top->sym_evsel));
  407. sleep(1);
  408. break;
  409. }
  410. evlist__for_each(top->evlist, top->sym_evsel)
  411. if (top->sym_evsel->idx == counter)
  412. break;
  413. } else
  414. top->sym_evsel = perf_evlist__first(top->evlist);
  415. break;
  416. case 'f':
  417. prompt_integer(&top->count_filter, "Enter display event count filter");
  418. break;
  419. case 'F':
  420. prompt_percent(&top->sym_pcnt_filter,
  421. "Enter details display event filter (percent)");
  422. break;
  423. case 'K':
  424. top->hide_kernel_symbols = !top->hide_kernel_symbols;
  425. break;
  426. case 'q':
  427. case 'Q':
  428. printf("exiting.\n");
  429. if (top->dump_symtab)
  430. perf_session__fprintf_dsos(top->session, stderr);
  431. ret = false;
  432. break;
  433. case 's':
  434. perf_top__prompt_symbol(top, "Enter details symbol");
  435. break;
  436. case 'S':
  437. if (!top->sym_filter_entry)
  438. break;
  439. else {
  440. struct hist_entry *syme = top->sym_filter_entry;
  441. top->sym_filter_entry = NULL;
  442. __zero_source_counters(syme);
  443. }
  444. break;
  445. case 'U':
  446. top->hide_user_symbols = !top->hide_user_symbols;
  447. break;
  448. case 'z':
  449. top->zero = !top->zero;
  450. break;
  451. default:
  452. break;
  453. }
  454. return ret;
  455. }
  456. static void perf_top__sort_new_samples(void *arg)
  457. {
  458. struct perf_top *t = arg;
  459. struct hists *hists;
  460. perf_top__reset_sample_counters(t);
  461. if (t->evlist->selected != NULL)
  462. t->sym_evsel = t->evlist->selected;
  463. hists = evsel__hists(t->sym_evsel);
  464. if (t->evlist->enabled) {
  465. if (t->zero) {
  466. hists__delete_entries(hists);
  467. } else {
  468. hists__decay_entries(hists, t->hide_user_symbols,
  469. t->hide_kernel_symbols);
  470. }
  471. }
  472. hists__collapse_resort(hists, NULL);
  473. hists__output_resort(hists, NULL);
  474. }
  475. static void *display_thread_tui(void *arg)
  476. {
  477. struct perf_evsel *pos;
  478. struct perf_top *top = arg;
  479. const char *help = "For a higher level overview, try: perf top --sort comm,dso";
  480. struct hist_browser_timer hbt = {
  481. .timer = perf_top__sort_new_samples,
  482. .arg = top,
  483. .refresh = top->delay_secs,
  484. };
  485. perf_top__sort_new_samples(top);
  486. /*
  487. * Initialize the uid_filter_str, in the future the TUI will allow
  488. * Zooming in/out UIDs. For now juse use whatever the user passed
  489. * via --uid.
  490. */
  491. evlist__for_each(top->evlist, pos) {
  492. struct hists *hists = evsel__hists(pos);
  493. hists->uid_filter_str = top->record_opts.target.uid_str;
  494. }
  495. perf_evlist__tui_browse_hists(top->evlist, help, &hbt,
  496. top->min_percent,
  497. &top->session->header.env);
  498. done = 1;
  499. return NULL;
  500. }
  501. static void display_sig(int sig __maybe_unused)
  502. {
  503. done = 1;
  504. }
  505. static void display_setup_sig(void)
  506. {
  507. signal(SIGSEGV, sighandler_dump_stack);
  508. signal(SIGFPE, sighandler_dump_stack);
  509. signal(SIGINT, display_sig);
  510. signal(SIGQUIT, display_sig);
  511. signal(SIGTERM, display_sig);
  512. }
  513. static void *display_thread(void *arg)
  514. {
  515. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  516. struct termios save;
  517. struct perf_top *top = arg;
  518. int delay_msecs, c;
  519. display_setup_sig();
  520. pthread__unblock_sigwinch();
  521. repeat:
  522. delay_msecs = top->delay_secs * 1000;
  523. set_term_quiet_input(&save);
  524. /* trash return*/
  525. getc(stdin);
  526. while (!done) {
  527. perf_top__print_sym_table(top);
  528. /*
  529. * Either timeout expired or we got an EINTR due to SIGWINCH,
  530. * refresh screen in both cases.
  531. */
  532. switch (poll(&stdin_poll, 1, delay_msecs)) {
  533. case 0:
  534. continue;
  535. case -1:
  536. if (errno == EINTR)
  537. continue;
  538. __fallthrough;
  539. default:
  540. c = getc(stdin);
  541. tcsetattr(0, TCSAFLUSH, &save);
  542. if (perf_top__handle_keypress(top, c))
  543. goto repeat;
  544. done = 1;
  545. }
  546. }
  547. tcsetattr(0, TCSAFLUSH, &save);
  548. return NULL;
  549. }
  550. static int symbol_filter(struct map *map, struct symbol *sym)
  551. {
  552. const char *name = sym->name;
  553. if (!__map__is_kernel(map))
  554. return 0;
  555. /*
  556. * ppc64 uses function descriptors and appends a '.' to the
  557. * start of every instruction address. Remove it.
  558. */
  559. if (name[0] == '.')
  560. name++;
  561. if (!strcmp(name, "_text") ||
  562. !strcmp(name, "_etext") ||
  563. !strcmp(name, "_sinittext") ||
  564. !strncmp("init_module", name, 11) ||
  565. !strncmp("cleanup_module", name, 14) ||
  566. strstr(name, "_text_start") ||
  567. strstr(name, "_text_end"))
  568. return 1;
  569. if (symbol__is_idle(sym))
  570. sym->ignore = true;
  571. return 0;
  572. }
  573. static int hist_iter__top_callback(struct hist_entry_iter *iter,
  574. struct addr_location *al, bool single,
  575. void *arg)
  576. {
  577. struct perf_top *top = arg;
  578. struct hist_entry *he = iter->he;
  579. struct perf_evsel *evsel = iter->evsel;
  580. if (sort__has_sym && single) {
  581. u64 ip = al->addr;
  582. if (al->map)
  583. ip = al->map->unmap_ip(al->map, ip);
  584. perf_top__record_precise_ip(top, he, evsel->idx, ip);
  585. }
  586. hist__account_cycles(iter->sample->branch_stack, al, iter->sample,
  587. !(top->record_opts.branch_stack & PERF_SAMPLE_BRANCH_ANY));
  588. return 0;
  589. }
  590. static void perf_event__process_sample(struct perf_tool *tool,
  591. const union perf_event *event,
  592. struct perf_evsel *evsel,
  593. struct perf_sample *sample,
  594. struct machine *machine)
  595. {
  596. struct perf_top *top = container_of(tool, struct perf_top, tool);
  597. struct addr_location al;
  598. int err;
  599. if (!machine && perf_guest) {
  600. static struct intlist *seen;
  601. if (!seen)
  602. seen = intlist__new(NULL);
  603. if (!intlist__has_entry(seen, sample->pid)) {
  604. pr_err("Can't find guest [%d]'s kernel information\n",
  605. sample->pid);
  606. intlist__add(seen, sample->pid);
  607. }
  608. return;
  609. }
  610. if (!machine) {
  611. pr_err("%u unprocessable samples recorded.\r",
  612. top->session->evlist->stats.nr_unprocessable_samples++);
  613. return;
  614. }
  615. if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
  616. top->exact_samples++;
  617. if (perf_event__preprocess_sample(event, machine, &al, sample) < 0)
  618. return;
  619. if (!top->kptr_restrict_warned &&
  620. symbol_conf.kptr_restrict &&
  621. al.cpumode == PERF_RECORD_MISC_KERNEL) {
  622. ui__warning(
  623. "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  624. "Check /proc/sys/kernel/kptr_restrict.\n\n"
  625. "Kernel%s samples will not be resolved.\n",
  626. al.map && !RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION]) ?
  627. " modules" : "");
  628. if (use_browser <= 0)
  629. sleep(5);
  630. top->kptr_restrict_warned = true;
  631. }
  632. if (al.sym == NULL) {
  633. const char *msg = "Kernel samples will not be resolved.\n";
  634. /*
  635. * As we do lazy loading of symtabs we only will know if the
  636. * specified vmlinux file is invalid when we actually have a
  637. * hit in kernel space and then try to load it. So if we get
  638. * here and there are _no_ symbols in the DSO backing the
  639. * kernel map, bail out.
  640. *
  641. * We may never get here, for instance, if we use -K/
  642. * --hide-kernel-symbols, even if the user specifies an
  643. * invalid --vmlinux ;-)
  644. */
  645. if (!top->kptr_restrict_warned && !top->vmlinux_warned &&
  646. al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
  647. RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
  648. if (symbol_conf.vmlinux_name) {
  649. char serr[256];
  650. dso__strerror_load(al.map->dso, serr, sizeof(serr));
  651. ui__warning("The %s file can't be used: %s\n%s",
  652. symbol_conf.vmlinux_name, serr, msg);
  653. } else {
  654. ui__warning("A vmlinux file was not found.\n%s",
  655. msg);
  656. }
  657. if (use_browser <= 0)
  658. sleep(5);
  659. top->vmlinux_warned = true;
  660. }
  661. }
  662. if (al.sym == NULL || !al.sym->ignore) {
  663. struct hists *hists = evsel__hists(evsel);
  664. struct hist_entry_iter iter = {
  665. .evsel = evsel,
  666. .sample = sample,
  667. .add_entry_cb = hist_iter__top_callback,
  668. };
  669. if (symbol_conf.cumulate_callchain)
  670. iter.ops = &hist_iter_cumulative;
  671. else
  672. iter.ops = &hist_iter_normal;
  673. pthread_mutex_lock(&hists->lock);
  674. err = hist_entry_iter__add(&iter, &al, top->max_stack, top);
  675. if (err < 0)
  676. pr_err("Problem incrementing symbol period, skipping event\n");
  677. pthread_mutex_unlock(&hists->lock);
  678. }
  679. addr_location__put(&al);
  680. }
  681. static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
  682. {
  683. struct perf_sample sample;
  684. struct perf_evsel *evsel;
  685. struct perf_session *session = top->session;
  686. union perf_event *event;
  687. struct machine *machine;
  688. u8 origin;
  689. int ret;
  690. while ((event = perf_evlist__mmap_read(top->evlist, idx)) != NULL) {
  691. ret = perf_evlist__parse_sample(top->evlist, event, &sample);
  692. if (ret) {
  693. pr_err("Can't parse sample, err = %d\n", ret);
  694. goto next_event;
  695. }
  696. evsel = perf_evlist__id2evsel(session->evlist, sample.id);
  697. assert(evsel != NULL);
  698. origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  699. if (event->header.type == PERF_RECORD_SAMPLE)
  700. ++top->samples;
  701. switch (origin) {
  702. case PERF_RECORD_MISC_USER:
  703. ++top->us_samples;
  704. if (top->hide_user_symbols)
  705. goto next_event;
  706. machine = &session->machines.host;
  707. break;
  708. case PERF_RECORD_MISC_KERNEL:
  709. ++top->kernel_samples;
  710. if (top->hide_kernel_symbols)
  711. goto next_event;
  712. machine = &session->machines.host;
  713. break;
  714. case PERF_RECORD_MISC_GUEST_KERNEL:
  715. ++top->guest_kernel_samples;
  716. machine = perf_session__find_machine(session,
  717. sample.pid);
  718. break;
  719. case PERF_RECORD_MISC_GUEST_USER:
  720. ++top->guest_us_samples;
  721. /*
  722. * TODO: we don't process guest user from host side
  723. * except simple counting.
  724. */
  725. goto next_event;
  726. default:
  727. if (event->header.type == PERF_RECORD_SAMPLE)
  728. goto next_event;
  729. machine = &session->machines.host;
  730. break;
  731. }
  732. if (event->header.type == PERF_RECORD_SAMPLE) {
  733. perf_event__process_sample(&top->tool, event, evsel,
  734. &sample, machine);
  735. } else if (event->header.type < PERF_RECORD_MAX) {
  736. hists__inc_nr_events(evsel__hists(evsel), event->header.type);
  737. machine__process_event(machine, event, &sample);
  738. } else
  739. ++session->evlist->stats.nr_unknown_events;
  740. next_event:
  741. perf_evlist__mmap_consume(top->evlist, idx);
  742. }
  743. }
  744. static void perf_top__mmap_read(struct perf_top *top)
  745. {
  746. int i;
  747. for (i = 0; i < top->evlist->nr_mmaps; i++)
  748. perf_top__mmap_read_idx(top, i);
  749. }
  750. static int perf_top__start_counters(struct perf_top *top)
  751. {
  752. char msg[512];
  753. struct perf_evsel *counter;
  754. struct perf_evlist *evlist = top->evlist;
  755. struct record_opts *opts = &top->record_opts;
  756. perf_evlist__config(evlist, opts);
  757. evlist__for_each(evlist, counter) {
  758. try_again:
  759. if (perf_evsel__open(counter, top->evlist->cpus,
  760. top->evlist->threads) < 0) {
  761. if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
  762. if (verbose)
  763. ui__warning("%s\n", msg);
  764. goto try_again;
  765. }
  766. perf_evsel__open_strerror(counter, &opts->target,
  767. errno, msg, sizeof(msg));
  768. ui__error("%s\n", msg);
  769. goto out_err;
  770. }
  771. }
  772. if (perf_evlist__mmap(evlist, opts->mmap_pages, false) < 0) {
  773. ui__error("Failed to mmap with %d (%s)\n",
  774. errno, strerror_r(errno, msg, sizeof(msg)));
  775. goto out_err;
  776. }
  777. return 0;
  778. out_err:
  779. return -1;
  780. }
  781. static int perf_top__setup_sample_type(struct perf_top *top __maybe_unused)
  782. {
  783. if (!sort__has_sym) {
  784. if (symbol_conf.use_callchain) {
  785. ui__error("Selected -g but \"sym\" not present in --sort/-s.");
  786. return -EINVAL;
  787. }
  788. } else if (callchain_param.mode != CHAIN_NONE) {
  789. if (callchain_register_param(&callchain_param) < 0) {
  790. ui__error("Can't register callchain params.\n");
  791. return -EINVAL;
  792. }
  793. }
  794. return 0;
  795. }
  796. static int __cmd_top(struct perf_top *top)
  797. {
  798. struct record_opts *opts = &top->record_opts;
  799. pthread_t thread;
  800. int ret;
  801. top->session = perf_session__new(NULL, false, NULL);
  802. if (top->session == NULL)
  803. return -1;
  804. machines__set_symbol_filter(&top->session->machines, symbol_filter);
  805. if (!objdump_path) {
  806. ret = perf_env__lookup_objdump(&top->session->header.env);
  807. if (ret)
  808. goto out_delete;
  809. }
  810. ret = perf_top__setup_sample_type(top);
  811. if (ret)
  812. goto out_delete;
  813. if (perf_session__register_idle_thread(top->session) == NULL)
  814. goto out_delete;
  815. machine__synthesize_threads(&top->session->machines.host, &opts->target,
  816. top->evlist->threads, false, opts->proc_map_timeout);
  817. if (sort__has_socket) {
  818. ret = perf_env__read_cpu_topology_map(&perf_env);
  819. if (ret < 0)
  820. goto out_err_cpu_topo;
  821. }
  822. ret = perf_top__start_counters(top);
  823. if (ret)
  824. goto out_delete;
  825. top->session->evlist = top->evlist;
  826. perf_session__set_id_hdr_size(top->session);
  827. /*
  828. * When perf is starting the traced process, all the events (apart from
  829. * group members) have enable_on_exec=1 set, so don't spoil it by
  830. * prematurely enabling them.
  831. *
  832. * XXX 'top' still doesn't start workloads like record, trace, but should,
  833. * so leave the check here.
  834. */
  835. if (!target__none(&opts->target))
  836. perf_evlist__enable(top->evlist);
  837. /* Wait for a minimal set of events before starting the snapshot */
  838. perf_evlist__poll(top->evlist, 100);
  839. perf_top__mmap_read(top);
  840. ret = -1;
  841. if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
  842. display_thread), top)) {
  843. ui__error("Could not create display thread.\n");
  844. goto out_delete;
  845. }
  846. if (top->realtime_prio) {
  847. struct sched_param param;
  848. param.sched_priority = top->realtime_prio;
  849. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  850. ui__error("Could not set realtime priority.\n");
  851. goto out_join;
  852. }
  853. }
  854. while (!done) {
  855. u64 hits = top->samples;
  856. perf_top__mmap_read(top);
  857. if (hits == top->samples)
  858. ret = perf_evlist__poll(top->evlist, 100);
  859. if (resize) {
  860. perf_top__resize(top);
  861. resize = 0;
  862. }
  863. }
  864. ret = 0;
  865. out_join:
  866. pthread_join(thread, NULL);
  867. out_delete:
  868. perf_session__delete(top->session);
  869. top->session = NULL;
  870. return ret;
  871. out_err_cpu_topo: {
  872. char errbuf[BUFSIZ];
  873. const char *err = strerror_r(-ret, errbuf, sizeof(errbuf));
  874. ui__error("Could not read the CPU topology map: %s\n", err);
  875. goto out_delete;
  876. }
  877. }
  878. static int
  879. callchain_opt(const struct option *opt, const char *arg, int unset)
  880. {
  881. symbol_conf.use_callchain = true;
  882. return record_callchain_opt(opt, arg, unset);
  883. }
  884. static int
  885. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  886. {
  887. struct record_opts *record = (struct record_opts *)opt->value;
  888. record->callgraph_set = true;
  889. callchain_param.enabled = !unset;
  890. callchain_param.record_mode = CALLCHAIN_FP;
  891. /*
  892. * --no-call-graph
  893. */
  894. if (unset) {
  895. symbol_conf.use_callchain = false;
  896. callchain_param.record_mode = CALLCHAIN_NONE;
  897. return 0;
  898. }
  899. return parse_callchain_top_opt(arg);
  900. }
  901. static int perf_top_config(const char *var, const char *value, void *cb)
  902. {
  903. if (!strcmp(var, "top.call-graph"))
  904. var = "call-graph.record-mode"; /* fall-through */
  905. if (!strcmp(var, "top.children")) {
  906. symbol_conf.cumulate_callchain = perf_config_bool(var, value);
  907. return 0;
  908. }
  909. return perf_default_config(var, value, cb);
  910. }
  911. static int
  912. parse_percent_limit(const struct option *opt, const char *arg,
  913. int unset __maybe_unused)
  914. {
  915. struct perf_top *top = opt->value;
  916. top->min_percent = strtof(arg, NULL);
  917. return 0;
  918. }
  919. const char top_callchain_help[] = CALLCHAIN_RECORD_HELP CALLCHAIN_REPORT_HELP
  920. "\n\t\t\t\tDefault: fp,graph,0.5,caller,function";
  921. int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
  922. {
  923. char errbuf[BUFSIZ];
  924. struct perf_top top = {
  925. .count_filter = 5,
  926. .delay_secs = 2,
  927. .record_opts = {
  928. .mmap_pages = UINT_MAX,
  929. .user_freq = UINT_MAX,
  930. .user_interval = ULLONG_MAX,
  931. .freq = 4000, /* 4 KHz */
  932. .target = {
  933. .uses_mmap = true,
  934. },
  935. .proc_map_timeout = 500,
  936. },
  937. .max_stack = PERF_MAX_STACK_DEPTH,
  938. .sym_pcnt_filter = 5,
  939. };
  940. struct record_opts *opts = &top.record_opts;
  941. struct target *target = &opts->target;
  942. const struct option options[] = {
  943. OPT_CALLBACK('e', "event", &top.evlist, "event",
  944. "event selector. use 'perf list' to list available events",
  945. parse_events_option),
  946. OPT_U64('c', "count", &opts->user_interval, "event period to sample"),
  947. OPT_STRING('p', "pid", &target->pid, "pid",
  948. "profile events on existing process id"),
  949. OPT_STRING('t', "tid", &target->tid, "tid",
  950. "profile events on existing thread id"),
  951. OPT_BOOLEAN('a', "all-cpus", &target->system_wide,
  952. "system-wide collection from all CPUs"),
  953. OPT_STRING('C', "cpu", &target->cpu_list, "cpu",
  954. "list of cpus to monitor"),
  955. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  956. "file", "vmlinux pathname"),
  957. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  958. "don't load vmlinux even if found"),
  959. OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
  960. "hide kernel symbols"),
  961. OPT_CALLBACK('m', "mmap-pages", &opts->mmap_pages, "pages",
  962. "number of mmap data pages",
  963. perf_evlist__parse_mmap_pages),
  964. OPT_INTEGER('r', "realtime", &top.realtime_prio,
  965. "collect data with this RT SCHED_FIFO priority"),
  966. OPT_INTEGER('d', "delay", &top.delay_secs,
  967. "number of seconds to delay between refreshes"),
  968. OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
  969. "dump the symbol table used for profiling"),
  970. OPT_INTEGER('f', "count-filter", &top.count_filter,
  971. "only display functions with more events than this"),
  972. OPT_BOOLEAN(0, "group", &opts->group,
  973. "put the counters into a counter group"),
  974. OPT_BOOLEAN('i', "no-inherit", &opts->no_inherit,
  975. "child tasks do not inherit counters"),
  976. OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
  977. "symbol to annotate"),
  978. OPT_BOOLEAN('z', "zero", &top.zero, "zero history across updates"),
  979. OPT_UINTEGER('F', "freq", &opts->user_freq, "profile at this frequency"),
  980. OPT_INTEGER('E', "entries", &top.print_entries,
  981. "display this many functions"),
  982. OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
  983. "hide user symbols"),
  984. OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
  985. OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
  986. OPT_INCR('v', "verbose", &verbose,
  987. "be more verbose (show counter open errors, etc)"),
  988. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  989. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  990. " Please refer the man page for the complete list."),
  991. OPT_STRING(0, "fields", &field_order, "key[,keys...]",
  992. "output field(s): overhead, period, sample plus all of sort keys"),
  993. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  994. "Show a column with the number of samples"),
  995. OPT_CALLBACK_NOOPT('g', NULL, &top.record_opts,
  996. NULL, "enables call-graph recording and display",
  997. &callchain_opt),
  998. OPT_CALLBACK(0, "call-graph", &top.record_opts,
  999. "record_mode[,record_size],print_type,threshold[,print_limit],order,sort_key[,branch]",
  1000. top_callchain_help, &parse_callchain_opt),
  1001. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  1002. "Accumulate callchains of children and show total overhead as well"),
  1003. OPT_INTEGER(0, "max-stack", &top.max_stack,
  1004. "Set the maximum stack depth when parsing the callchain. "
  1005. "Default: " __stringify(PERF_MAX_STACK_DEPTH)),
  1006. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  1007. "ignore callees of these functions in call graphs",
  1008. report_parse_ignore_callees_opt),
  1009. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  1010. "Show a column with the sum of periods"),
  1011. OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  1012. "only consider symbols in these dsos"),
  1013. OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1014. "only consider symbols in these comms"),
  1015. OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1016. "only consider these symbols"),
  1017. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  1018. "Interleave source code with assembly code (default)"),
  1019. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  1020. "Display raw encoding of assembly instructions (default)"),
  1021. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  1022. "Enable kernel symbol demangling"),
  1023. OPT_STRING(0, "objdump", &objdump_path, "path",
  1024. "objdump binary to use for disassembly and annotations"),
  1025. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  1026. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  1027. OPT_STRING('u', "uid", &target->uid_str, "user", "user to profile"),
  1028. OPT_CALLBACK(0, "percent-limit", &top, "percent",
  1029. "Don't show entries under that percent", parse_percent_limit),
  1030. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  1031. "How to display percentage of filtered entries", parse_filter_percentage),
  1032. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  1033. "width[,width...]",
  1034. "don't try to adjust column width, use these fixed values"),
  1035. OPT_UINTEGER(0, "proc-map-timeout", &opts->proc_map_timeout,
  1036. "per thread proc mmap processing timeout in ms"),
  1037. OPT_CALLBACK_NOOPT('b', "branch-any", &opts->branch_stack,
  1038. "branch any", "sample any taken branches",
  1039. parse_branch_stack),
  1040. OPT_CALLBACK('j', "branch-filter", &opts->branch_stack,
  1041. "branch filter mask", "branch stack filter modes",
  1042. parse_branch_stack),
  1043. OPT_END()
  1044. };
  1045. const char * const top_usage[] = {
  1046. "perf top [<options>]",
  1047. NULL
  1048. };
  1049. int status = hists__init();
  1050. if (status < 0)
  1051. return status;
  1052. top.evlist = perf_evlist__new();
  1053. if (top.evlist == NULL)
  1054. return -ENOMEM;
  1055. perf_config(perf_top_config, &top);
  1056. argc = parse_options(argc, argv, options, top_usage, 0);
  1057. if (argc)
  1058. usage_with_options(top_usage, options);
  1059. sort__mode = SORT_MODE__TOP;
  1060. /* display thread wants entries to be collapsed in a different tree */
  1061. sort__need_collapse = 1;
  1062. if (setup_sorting() < 0) {
  1063. if (sort_order)
  1064. parse_options_usage(top_usage, options, "s", 1);
  1065. if (field_order)
  1066. parse_options_usage(sort_order ? NULL : top_usage,
  1067. options, "fields", 0);
  1068. goto out_delete_evlist;
  1069. }
  1070. if (top.use_stdio)
  1071. use_browser = 0;
  1072. else if (top.use_tui)
  1073. use_browser = 1;
  1074. setup_browser(false);
  1075. status = target__validate(target);
  1076. if (status) {
  1077. target__strerror(target, status, errbuf, BUFSIZ);
  1078. ui__warning("%s\n", errbuf);
  1079. }
  1080. status = target__parse_uid(target);
  1081. if (status) {
  1082. int saved_errno = errno;
  1083. target__strerror(target, status, errbuf, BUFSIZ);
  1084. ui__error("%s\n", errbuf);
  1085. status = -saved_errno;
  1086. goto out_delete_evlist;
  1087. }
  1088. if (target__none(target))
  1089. target->system_wide = true;
  1090. if (perf_evlist__create_maps(top.evlist, target) < 0)
  1091. usage_with_options(top_usage, options);
  1092. if (!top.evlist->nr_entries &&
  1093. perf_evlist__add_default(top.evlist) < 0) {
  1094. ui__error("Not enough memory for event selector list\n");
  1095. goto out_delete_evlist;
  1096. }
  1097. symbol_conf.nr_events = top.evlist->nr_entries;
  1098. if (top.delay_secs < 1)
  1099. top.delay_secs = 1;
  1100. if (record_opts__config(opts)) {
  1101. status = -EINVAL;
  1102. goto out_delete_evlist;
  1103. }
  1104. top.sym_evsel = perf_evlist__first(top.evlist);
  1105. if (!symbol_conf.use_callchain) {
  1106. symbol_conf.cumulate_callchain = false;
  1107. perf_hpp__cancel_cumulate();
  1108. }
  1109. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  1110. callchain_param.order = ORDER_CALLER;
  1111. symbol_conf.priv_size = sizeof(struct annotation);
  1112. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1113. status = symbol__init(NULL);
  1114. if (status < 0)
  1115. goto out_delete_evlist;
  1116. sort__setup_elide(stdout);
  1117. get_term_dimensions(&top.winsize);
  1118. if (top.print_entries == 0) {
  1119. struct sigaction act = {
  1120. .sa_sigaction = perf_top__sig_winch,
  1121. .sa_flags = SA_SIGINFO,
  1122. };
  1123. perf_top__update_print_entries(&top);
  1124. sigaction(SIGWINCH, &act, NULL);
  1125. }
  1126. status = __cmd_top(&top);
  1127. out_delete_evlist:
  1128. perf_evlist__delete(top.evlist);
  1129. return status;
  1130. }