builtin-top.c 35 KB

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