builtin-top.c 35 KB

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