builtin-top.c 35 KB

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