builtin-top.c 35 KB

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