builtin-top.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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/session.h"
  28. #include "util/symbol.h"
  29. #include "util/thread.h"
  30. #include "util/thread_map.h"
  31. #include "util/top.h"
  32. #include "util/util.h"
  33. #include <linux/rbtree.h>
  34. #include "util/parse-options.h"
  35. #include "util/parse-events.h"
  36. #include "util/cpumap.h"
  37. #include "util/xyarray.h"
  38. #include "util/sort.h"
  39. #include "util/debug.h"
  40. #include <assert.h>
  41. #include <elf.h>
  42. #include <fcntl.h>
  43. #include <stdio.h>
  44. #include <termios.h>
  45. #include <unistd.h>
  46. #include <inttypes.h>
  47. #include <errno.h>
  48. #include <time.h>
  49. #include <sched.h>
  50. #include <sys/syscall.h>
  51. #include <sys/ioctl.h>
  52. #include <sys/poll.h>
  53. #include <sys/prctl.h>
  54. #include <sys/wait.h>
  55. #include <sys/uio.h>
  56. #include <sys/utsname.h>
  57. #include <sys/mman.h>
  58. #include <linux/unistd.h>
  59. #include <linux/types.h>
  60. void get_term_dimensions(struct winsize *ws)
  61. {
  62. char *s = getenv("LINES");
  63. if (s != NULL) {
  64. ws->ws_row = atoi(s);
  65. s = getenv("COLUMNS");
  66. if (s != NULL) {
  67. ws->ws_col = atoi(s);
  68. if (ws->ws_row && ws->ws_col)
  69. return;
  70. }
  71. }
  72. #ifdef TIOCGWINSZ
  73. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  74. ws->ws_row && ws->ws_col)
  75. return;
  76. #endif
  77. ws->ws_row = 25;
  78. ws->ws_col = 80;
  79. }
  80. static void perf_top__update_print_entries(struct perf_top *top)
  81. {
  82. if (top->print_entries > 9)
  83. top->print_entries -= 9;
  84. }
  85. static void perf_top__sig_winch(int sig __used, siginfo_t *info __used, void *arg)
  86. {
  87. struct perf_top *top = arg;
  88. get_term_dimensions(&top->winsize);
  89. if (!top->print_entries
  90. || (top->print_entries+4) > top->winsize.ws_row) {
  91. top->print_entries = top->winsize.ws_row;
  92. } else {
  93. top->print_entries += 4;
  94. top->winsize.ws_row = top->print_entries;
  95. }
  96. perf_top__update_print_entries(top);
  97. }
  98. static int perf_top__parse_source(struct perf_top *top, struct hist_entry *he)
  99. {
  100. struct symbol *sym;
  101. struct annotation *notes;
  102. struct map *map;
  103. int err = -1;
  104. if (!he || !he->ms.sym)
  105. return -1;
  106. sym = he->ms.sym;
  107. map = he->ms.map;
  108. /*
  109. * We can't annotate with just /proc/kallsyms
  110. */
  111. if (map->dso->symtab_type == SYMTAB__KALLSYMS) {
  112. pr_err("Can't annotate %s: No vmlinux file was found in the "
  113. "path\n", sym->name);
  114. sleep(1);
  115. return -1;
  116. }
  117. notes = symbol__annotation(sym);
  118. if (notes->src != NULL) {
  119. pthread_mutex_lock(&notes->lock);
  120. goto out_assign;
  121. }
  122. pthread_mutex_lock(&notes->lock);
  123. if (symbol__alloc_hist(sym) < 0) {
  124. pthread_mutex_unlock(&notes->lock);
  125. pr_err("Not enough memory for annotating '%s' symbol!\n",
  126. sym->name);
  127. sleep(1);
  128. return err;
  129. }
  130. err = symbol__annotate(sym, map, 0);
  131. if (err == 0) {
  132. out_assign:
  133. top->sym_filter_entry = he;
  134. }
  135. pthread_mutex_unlock(&notes->lock);
  136. return err;
  137. }
  138. static void __zero_source_counters(struct hist_entry *he)
  139. {
  140. struct symbol *sym = he->ms.sym;
  141. symbol__annotate_zero_histograms(sym);
  142. }
  143. static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
  144. {
  145. struct utsname uts;
  146. int err = uname(&uts);
  147. ui__warning("Out of bounds address found:\n\n"
  148. "Addr: %" PRIx64 "\n"
  149. "DSO: %s %c\n"
  150. "Map: %" PRIx64 "-%" PRIx64 "\n"
  151. "Symbol: %" PRIx64 "-%" PRIx64 " %c %s\n"
  152. "Arch: %s\n"
  153. "Kernel: %s\n"
  154. "Tools: %s\n\n"
  155. "Not all samples will be on the annotation output.\n\n"
  156. "Please report to linux-kernel@vger.kernel.org\n",
  157. ip, map->dso->long_name, dso__symtab_origin(map->dso),
  158. map->start, map->end, sym->start, sym->end,
  159. sym->binding == STB_GLOBAL ? 'g' :
  160. sym->binding == STB_LOCAL ? 'l' : 'w', sym->name,
  161. err ? "[unknown]" : uts.machine,
  162. err ? "[unknown]" : uts.release, perf_version_string);
  163. if (use_browser <= 0)
  164. sleep(5);
  165. map->erange_warned = true;
  166. }
  167. static void perf_top__record_precise_ip(struct perf_top *top,
  168. struct hist_entry *he,
  169. int counter, u64 ip)
  170. {
  171. struct annotation *notes;
  172. struct symbol *sym;
  173. int err;
  174. if (he == NULL || he->ms.sym == NULL ||
  175. ((top->sym_filter_entry == NULL ||
  176. top->sym_filter_entry->ms.sym != he->ms.sym) && use_browser != 1))
  177. return;
  178. sym = he->ms.sym;
  179. notes = symbol__annotation(sym);
  180. if (pthread_mutex_trylock(&notes->lock))
  181. return;
  182. if (notes->src == NULL && symbol__alloc_hist(sym) < 0) {
  183. pthread_mutex_unlock(&notes->lock);
  184. pr_err("Not enough memory for annotating '%s' symbol!\n",
  185. sym->name);
  186. sleep(1);
  187. return;
  188. }
  189. ip = he->ms.map->map_ip(he->ms.map, ip);
  190. err = symbol__inc_addr_samples(sym, he->ms.map, counter, ip);
  191. pthread_mutex_unlock(&notes->lock);
  192. if (err == -ERANGE && !he->ms.map->erange_warned)
  193. ui__warn_map_erange(he->ms.map, sym, ip);
  194. }
  195. static void perf_top__show_details(struct perf_top *top)
  196. {
  197. struct hist_entry *he = top->sym_filter_entry;
  198. struct annotation *notes;
  199. struct symbol *symbol;
  200. int more;
  201. if (!he)
  202. return;
  203. symbol = he->ms.sym;
  204. notes = symbol__annotation(symbol);
  205. pthread_mutex_lock(&notes->lock);
  206. if (notes->src == NULL)
  207. goto out_unlock;
  208. printf("Showing %s for %s\n", event_name(top->sym_evsel), symbol->name);
  209. printf(" Events Pcnt (>=%d%%)\n", top->sym_pcnt_filter);
  210. more = symbol__annotate_printf(symbol, he->ms.map, top->sym_evsel->idx,
  211. 0, top->sym_pcnt_filter, top->print_entries, 4);
  212. if (top->zero)
  213. symbol__annotate_zero_histogram(symbol, top->sym_evsel->idx);
  214. else
  215. symbol__annotate_decay_histogram(symbol, top->sym_evsel->idx);
  216. if (more != 0)
  217. printf("%d lines not displayed, maybe increase display entries [e]\n", more);
  218. out_unlock:
  219. pthread_mutex_unlock(&notes->lock);
  220. }
  221. static const char CONSOLE_CLEAR[] = "";
  222. static struct hist_entry *perf_evsel__add_hist_entry(struct perf_evsel *evsel,
  223. struct addr_location *al,
  224. struct perf_sample *sample)
  225. {
  226. struct hist_entry *he;
  227. he = __hists__add_entry(&evsel->hists, al, NULL, sample->period);
  228. if (he == NULL)
  229. return NULL;
  230. hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
  231. return he;
  232. }
  233. static void perf_top__print_sym_table(struct perf_top *top)
  234. {
  235. char bf[160];
  236. int printed = 0;
  237. const int win_width = top->winsize.ws_col - 1;
  238. puts(CONSOLE_CLEAR);
  239. perf_top__header_snprintf(top, bf, sizeof(bf));
  240. printf("%s\n", bf);
  241. perf_top__reset_sample_counters(top);
  242. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  243. if (top->sym_evsel->hists.stats.nr_lost_warned !=
  244. top->sym_evsel->hists.stats.nr_events[PERF_RECORD_LOST]) {
  245. top->sym_evsel->hists.stats.nr_lost_warned =
  246. top->sym_evsel->hists.stats.nr_events[PERF_RECORD_LOST];
  247. color_fprintf(stdout, PERF_COLOR_RED,
  248. "WARNING: LOST %d chunks, Check IO/CPU overload",
  249. top->sym_evsel->hists.stats.nr_lost_warned);
  250. ++printed;
  251. }
  252. if (top->sym_filter_entry) {
  253. perf_top__show_details(top);
  254. return;
  255. }
  256. hists__collapse_resort_threaded(&top->sym_evsel->hists);
  257. hists__output_resort_threaded(&top->sym_evsel->hists);
  258. hists__decay_entries_threaded(&top->sym_evsel->hists,
  259. top->hide_user_symbols,
  260. top->hide_kernel_symbols);
  261. hists__output_recalc_col_len(&top->sym_evsel->hists,
  262. top->winsize.ws_row - 3);
  263. putchar('\n');
  264. hists__fprintf(&top->sym_evsel->hists, NULL, false, false,
  265. top->winsize.ws_row - 4 - printed, win_width, stdout);
  266. }
  267. static void prompt_integer(int *target, const char *msg)
  268. {
  269. char *buf = malloc(0), *p;
  270. size_t dummy = 0;
  271. int tmp;
  272. fprintf(stdout, "\n%s: ", msg);
  273. if (getline(&buf, &dummy, stdin) < 0)
  274. return;
  275. p = strchr(buf, '\n');
  276. if (p)
  277. *p = 0;
  278. p = buf;
  279. while(*p) {
  280. if (!isdigit(*p))
  281. goto out_free;
  282. p++;
  283. }
  284. tmp = strtoul(buf, NULL, 10);
  285. *target = tmp;
  286. out_free:
  287. free(buf);
  288. }
  289. static void prompt_percent(int *target, const char *msg)
  290. {
  291. int tmp = 0;
  292. prompt_integer(&tmp, msg);
  293. if (tmp >= 0 && tmp <= 100)
  294. *target = tmp;
  295. }
  296. static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
  297. {
  298. char *buf = malloc(0), *p;
  299. struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
  300. struct rb_node *next;
  301. size_t dummy = 0;
  302. /* zero counters of active symbol */
  303. if (syme) {
  304. __zero_source_counters(syme);
  305. top->sym_filter_entry = NULL;
  306. }
  307. fprintf(stdout, "\n%s: ", msg);
  308. if (getline(&buf, &dummy, stdin) < 0)
  309. goto out_free;
  310. p = strchr(buf, '\n');
  311. if (p)
  312. *p = 0;
  313. next = rb_first(&top->sym_evsel->hists.entries);
  314. while (next) {
  315. n = rb_entry(next, struct hist_entry, rb_node);
  316. if (n->ms.sym && !strcmp(buf, n->ms.sym->name)) {
  317. found = n;
  318. break;
  319. }
  320. next = rb_next(&n->rb_node);
  321. }
  322. if (!found) {
  323. fprintf(stderr, "Sorry, %s is not active.\n", buf);
  324. sleep(1);
  325. } else
  326. perf_top__parse_source(top, found);
  327. out_free:
  328. free(buf);
  329. }
  330. static void perf_top__print_mapped_keys(struct perf_top *top)
  331. {
  332. char *name = NULL;
  333. if (top->sym_filter_entry) {
  334. struct symbol *sym = top->sym_filter_entry->ms.sym;
  335. name = sym->name;
  336. }
  337. fprintf(stdout, "\nMapped keys:\n");
  338. fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top->delay_secs);
  339. fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top->print_entries);
  340. if (top->evlist->nr_entries > 1)
  341. fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(top->sym_evsel));
  342. fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top->count_filter);
  343. fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", top->sym_pcnt_filter);
  344. fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
  345. fprintf(stdout, "\t[S] stop annotation.\n");
  346. fprintf(stdout,
  347. "\t[K] hide kernel_symbols symbols. \t(%s)\n",
  348. top->hide_kernel_symbols ? "yes" : "no");
  349. fprintf(stdout,
  350. "\t[U] hide user symbols. \t(%s)\n",
  351. top->hide_user_symbols ? "yes" : "no");
  352. fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top->zero ? 1 : 0);
  353. fprintf(stdout, "\t[qQ] quit.\n");
  354. }
  355. static int perf_top__key_mapped(struct perf_top *top, int c)
  356. {
  357. switch (c) {
  358. case 'd':
  359. case 'e':
  360. case 'f':
  361. case 'z':
  362. case 'q':
  363. case 'Q':
  364. case 'K':
  365. case 'U':
  366. case 'F':
  367. case 's':
  368. case 'S':
  369. return 1;
  370. case 'E':
  371. return top->evlist->nr_entries > 1 ? 1 : 0;
  372. default:
  373. break;
  374. }
  375. return 0;
  376. }
  377. static void perf_top__handle_keypress(struct perf_top *top, int c)
  378. {
  379. if (!perf_top__key_mapped(top, c)) {
  380. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  381. struct termios tc, save;
  382. perf_top__print_mapped_keys(top);
  383. fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
  384. fflush(stdout);
  385. tcgetattr(0, &save);
  386. tc = save;
  387. tc.c_lflag &= ~(ICANON | ECHO);
  388. tc.c_cc[VMIN] = 0;
  389. tc.c_cc[VTIME] = 0;
  390. tcsetattr(0, TCSANOW, &tc);
  391. poll(&stdin_poll, 1, -1);
  392. c = getc(stdin);
  393. tcsetattr(0, TCSAFLUSH, &save);
  394. if (!perf_top__key_mapped(top, c))
  395. return;
  396. }
  397. switch (c) {
  398. case 'd':
  399. prompt_integer(&top->delay_secs, "Enter display delay");
  400. if (top->delay_secs < 1)
  401. top->delay_secs = 1;
  402. break;
  403. case 'e':
  404. prompt_integer(&top->print_entries, "Enter display entries (lines)");
  405. if (top->print_entries == 0) {
  406. struct sigaction act = {
  407. .sa_sigaction = perf_top__sig_winch,
  408. .sa_flags = SA_SIGINFO,
  409. };
  410. perf_top__sig_winch(SIGWINCH, NULL, top);
  411. sigaction(SIGWINCH, &act, NULL);
  412. } else {
  413. perf_top__sig_winch(SIGWINCH, NULL, top);
  414. signal(SIGWINCH, SIG_DFL);
  415. }
  416. break;
  417. case 'E':
  418. if (top->evlist->nr_entries > 1) {
  419. /* Select 0 as the default event: */
  420. int counter = 0;
  421. fprintf(stderr, "\nAvailable events:");
  422. list_for_each_entry(top->sym_evsel, &top->evlist->entries, node)
  423. fprintf(stderr, "\n\t%d %s", top->sym_evsel->idx, event_name(top->sym_evsel));
  424. prompt_integer(&counter, "Enter details event counter");
  425. if (counter >= top->evlist->nr_entries) {
  426. top->sym_evsel = list_entry(top->evlist->entries.next, struct perf_evsel, node);
  427. fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(top->sym_evsel));
  428. sleep(1);
  429. break;
  430. }
  431. list_for_each_entry(top->sym_evsel, &top->evlist->entries, node)
  432. if (top->sym_evsel->idx == counter)
  433. break;
  434. } else
  435. top->sym_evsel = list_entry(top->evlist->entries.next, struct perf_evsel, node);
  436. break;
  437. case 'f':
  438. prompt_integer(&top->count_filter, "Enter display event count filter");
  439. break;
  440. case 'F':
  441. prompt_percent(&top->sym_pcnt_filter,
  442. "Enter details display event filter (percent)");
  443. break;
  444. case 'K':
  445. top->hide_kernel_symbols = !top->hide_kernel_symbols;
  446. break;
  447. case 'q':
  448. case 'Q':
  449. printf("exiting.\n");
  450. if (top->dump_symtab)
  451. perf_session__fprintf_dsos(top->session, stderr);
  452. exit(0);
  453. case 's':
  454. perf_top__prompt_symbol(top, "Enter details symbol");
  455. break;
  456. case 'S':
  457. if (!top->sym_filter_entry)
  458. break;
  459. else {
  460. struct hist_entry *syme = top->sym_filter_entry;
  461. top->sym_filter_entry = NULL;
  462. __zero_source_counters(syme);
  463. }
  464. break;
  465. case 'U':
  466. top->hide_user_symbols = !top->hide_user_symbols;
  467. break;
  468. case 'z':
  469. top->zero = !top->zero;
  470. break;
  471. default:
  472. break;
  473. }
  474. }
  475. static void perf_top__sort_new_samples(void *arg)
  476. {
  477. struct perf_top *t = arg;
  478. perf_top__reset_sample_counters(t);
  479. if (t->evlist->selected != NULL)
  480. t->sym_evsel = t->evlist->selected;
  481. hists__collapse_resort_threaded(&t->sym_evsel->hists);
  482. hists__output_resort_threaded(&t->sym_evsel->hists);
  483. hists__decay_entries_threaded(&t->sym_evsel->hists,
  484. t->hide_user_symbols,
  485. t->hide_kernel_symbols);
  486. }
  487. static void *display_thread_tui(void *arg)
  488. {
  489. struct perf_evsel *pos;
  490. struct perf_top *top = arg;
  491. const char *help = "For a higher level overview, try: perf top --sort comm,dso";
  492. perf_top__sort_new_samples(top);
  493. /*
  494. * Initialize the uid_filter_str, in the future the TUI will allow
  495. * Zooming in/out UIDs. For now juse use whatever the user passed
  496. * via --uid.
  497. */
  498. list_for_each_entry(pos, &top->evlist->entries, node)
  499. pos->hists.uid_filter_str = top->uid_str;
  500. perf_evlist__tui_browse_hists(top->evlist, help,
  501. perf_top__sort_new_samples,
  502. top, top->delay_secs);
  503. exit_browser(0);
  504. exit(0);
  505. return NULL;
  506. }
  507. static void *display_thread(void *arg)
  508. {
  509. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  510. struct termios tc, save;
  511. struct perf_top *top = arg;
  512. int delay_msecs, c;
  513. tcgetattr(0, &save);
  514. tc = save;
  515. tc.c_lflag &= ~(ICANON | ECHO);
  516. tc.c_cc[VMIN] = 0;
  517. tc.c_cc[VTIME] = 0;
  518. pthread__unblock_sigwinch();
  519. repeat:
  520. delay_msecs = top->delay_secs * 1000;
  521. tcsetattr(0, TCSANOW, &tc);
  522. /* trash return*/
  523. getc(stdin);
  524. while (1) {
  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. goto process_hotkey;
  539. }
  540. }
  541. process_hotkey:
  542. c = getc(stdin);
  543. tcsetattr(0, TCSAFLUSH, &save);
  544. perf_top__handle_keypress(top, c);
  545. goto repeat;
  546. return NULL;
  547. }
  548. /* Tag samples to be skipped. */
  549. static const char *skip_symbols[] = {
  550. "intel_idle",
  551. "default_idle",
  552. "native_safe_halt",
  553. "cpu_idle",
  554. "enter_idle",
  555. "exit_idle",
  556. "mwait_idle",
  557. "mwait_idle_with_hints",
  558. "poll_idle",
  559. "ppc64_runlatch_off",
  560. "pseries_dedicated_idle_sleep",
  561. NULL
  562. };
  563. static int symbol_filter(struct map *map __used, struct symbol *sym)
  564. {
  565. const char *name = sym->name;
  566. int i;
  567. /*
  568. * ppc64 uses function descriptors and appends a '.' to the
  569. * start of every instruction address. Remove it.
  570. */
  571. if (name[0] == '.')
  572. name++;
  573. if (!strcmp(name, "_text") ||
  574. !strcmp(name, "_etext") ||
  575. !strcmp(name, "_sinittext") ||
  576. !strncmp("init_module", name, 11) ||
  577. !strncmp("cleanup_module", name, 14) ||
  578. strstr(name, "_text_start") ||
  579. strstr(name, "_text_end"))
  580. return 1;
  581. for (i = 0; skip_symbols[i]; i++) {
  582. if (!strcmp(skip_symbols[i], name)) {
  583. sym->ignore = true;
  584. break;
  585. }
  586. }
  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 symbol *parent = NULL;
  597. u64 ip = event->ip.ip;
  598. struct addr_location al;
  599. int err;
  600. if (!machine && perf_guest) {
  601. pr_err("Can't find guest [%d]'s kernel information\n",
  602. event->ip.pid);
  603. return;
  604. }
  605. if (!machine) {
  606. pr_err("%u unprocessable samples recorded.",
  607. top->session->hists.stats.nr_unprocessable_samples++);
  608. return;
  609. }
  610. if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
  611. top->exact_samples++;
  612. if (perf_event__preprocess_sample(event, machine, &al, sample,
  613. symbol_filter) < 0 ||
  614. al.filtered)
  615. return;
  616. if (!top->kptr_restrict_warned &&
  617. symbol_conf.kptr_restrict &&
  618. al.cpumode == PERF_RECORD_MISC_KERNEL) {
  619. ui__warning(
  620. "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  621. "Check /proc/sys/kernel/kptr_restrict.\n\n"
  622. "Kernel%s samples will not be resolved.\n",
  623. !RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION]) ?
  624. " modules" : "");
  625. if (use_browser <= 0)
  626. sleep(5);
  627. top->kptr_restrict_warned = true;
  628. }
  629. if (al.sym == NULL) {
  630. const char *msg = "Kernel samples will not be resolved.\n";
  631. /*
  632. * As we do lazy loading of symtabs we only will know if the
  633. * specified vmlinux file is invalid when we actually have a
  634. * hit in kernel space and then try to load it. So if we get
  635. * here and there are _no_ symbols in the DSO backing the
  636. * kernel map, bail out.
  637. *
  638. * We may never get here, for instance, if we use -K/
  639. * --hide-kernel-symbols, even if the user specifies an
  640. * invalid --vmlinux ;-)
  641. */
  642. if (!top->kptr_restrict_warned && !top->vmlinux_warned &&
  643. al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
  644. RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
  645. if (symbol_conf.vmlinux_name) {
  646. ui__warning("The %s file can't be used.\n%s",
  647. symbol_conf.vmlinux_name, msg);
  648. } else {
  649. ui__warning("A vmlinux file was not found.\n%s",
  650. msg);
  651. }
  652. if (use_browser <= 0)
  653. sleep(5);
  654. top->vmlinux_warned = true;
  655. }
  656. }
  657. if (al.sym == NULL || !al.sym->ignore) {
  658. struct hist_entry *he;
  659. if ((sort__has_parent || symbol_conf.use_callchain) &&
  660. sample->callchain) {
  661. err = machine__resolve_callchain(machine, evsel, al.thread,
  662. sample->callchain, &parent);
  663. if (err)
  664. return;
  665. }
  666. he = perf_evsel__add_hist_entry(evsel, &al, sample);
  667. if (he == NULL) {
  668. pr_err("Problem incrementing symbol period, skipping event\n");
  669. return;
  670. }
  671. if (symbol_conf.use_callchain) {
  672. err = callchain_append(he->callchain, &evsel->hists.callchain_cursor,
  673. sample->period);
  674. if (err)
  675. return;
  676. }
  677. if (top->sort_has_symbols)
  678. perf_top__record_precise_ip(top, he, evsel->idx, ip);
  679. }
  680. return;
  681. }
  682. static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
  683. {
  684. struct perf_sample sample;
  685. struct perf_evsel *evsel;
  686. struct perf_session *session = top->session;
  687. union perf_event *event;
  688. struct machine *machine;
  689. u8 origin;
  690. int ret;
  691. while ((event = perf_evlist__mmap_read(top->evlist, idx)) != NULL) {
  692. ret = perf_session__parse_sample(session, event, &sample);
  693. if (ret) {
  694. pr_err("Can't parse sample, err = %d\n", ret);
  695. continue;
  696. }
  697. evsel = perf_evlist__id2evsel(session->evlist, sample.id);
  698. assert(evsel != NULL);
  699. origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  700. if (event->header.type == PERF_RECORD_SAMPLE)
  701. ++top->samples;
  702. switch (origin) {
  703. case PERF_RECORD_MISC_USER:
  704. ++top->us_samples;
  705. if (top->hide_user_symbols)
  706. continue;
  707. machine = perf_session__find_host_machine(session);
  708. break;
  709. case PERF_RECORD_MISC_KERNEL:
  710. ++top->kernel_samples;
  711. if (top->hide_kernel_symbols)
  712. continue;
  713. machine = perf_session__find_host_machine(session);
  714. break;
  715. case PERF_RECORD_MISC_GUEST_KERNEL:
  716. ++top->guest_kernel_samples;
  717. machine = perf_session__find_machine(session, event->ip.pid);
  718. break;
  719. case PERF_RECORD_MISC_GUEST_USER:
  720. ++top->guest_us_samples;
  721. /*
  722. * TODO: we don't process guest user from host side
  723. * except simple counting.
  724. */
  725. /* Fall thru */
  726. default:
  727. continue;
  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, event->header.type);
  734. perf_event__process(&top->tool, event, &sample, machine);
  735. } else
  736. ++session->hists.stats.nr_unknown_events;
  737. }
  738. }
  739. static void perf_top__mmap_read(struct perf_top *top)
  740. {
  741. int i;
  742. for (i = 0; i < top->evlist->nr_mmaps; i++)
  743. perf_top__mmap_read_idx(top, i);
  744. }
  745. static void perf_top__start_counters(struct perf_top *top)
  746. {
  747. struct perf_evsel *counter, *first;
  748. struct perf_evlist *evlist = top->evlist;
  749. first = list_entry(evlist->entries.next, struct perf_evsel, node);
  750. list_for_each_entry(counter, &evlist->entries, node) {
  751. struct perf_event_attr *attr = &counter->attr;
  752. struct xyarray *group_fd = NULL;
  753. if (top->group && counter != first)
  754. group_fd = first->fd;
  755. attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  756. if (top->freq) {
  757. attr->sample_type |= PERF_SAMPLE_PERIOD;
  758. attr->freq = 1;
  759. attr->sample_freq = top->freq;
  760. }
  761. if (evlist->nr_entries > 1) {
  762. attr->sample_type |= PERF_SAMPLE_ID;
  763. attr->read_format |= PERF_FORMAT_ID;
  764. }
  765. if (symbol_conf.use_callchain)
  766. attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
  767. attr->mmap = 1;
  768. attr->comm = 1;
  769. attr->inherit = top->inherit;
  770. fallback_missing_features:
  771. if (top->exclude_guest_missing)
  772. attr->exclude_guest = attr->exclude_host = 0;
  773. retry_sample_id:
  774. attr->sample_id_all = top->sample_id_all_missing ? 0 : 1;
  775. try_again:
  776. if (perf_evsel__open(counter, top->evlist->cpus,
  777. top->evlist->threads, top->group,
  778. group_fd) < 0) {
  779. int err = errno;
  780. if (err == EPERM || err == EACCES) {
  781. ui__error_paranoid();
  782. goto out_err;
  783. } else if (err == EINVAL) {
  784. if (!top->exclude_guest_missing &&
  785. (attr->exclude_guest || attr->exclude_host)) {
  786. pr_debug("Old kernel, cannot exclude "
  787. "guest or host samples.\n");
  788. top->exclude_guest_missing = true;
  789. goto fallback_missing_features;
  790. } else if (!top->sample_id_all_missing) {
  791. /*
  792. * Old kernel, no attr->sample_id_type_all field
  793. */
  794. top->sample_id_all_missing = true;
  795. goto retry_sample_id;
  796. }
  797. }
  798. /*
  799. * If it's cycles then fall back to hrtimer
  800. * based cpu-clock-tick sw counter, which
  801. * is always available even if no PMU support:
  802. */
  803. if (attr->type == PERF_TYPE_HARDWARE &&
  804. attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  805. if (verbose)
  806. ui__warning("Cycles event not supported,\n"
  807. "trying to fall back to cpu-clock-ticks\n");
  808. attr->type = PERF_TYPE_SOFTWARE;
  809. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  810. goto try_again;
  811. }
  812. if (err == ENOENT) {
  813. ui__warning("The %s event is not supported.\n",
  814. event_name(counter));
  815. goto out_err;
  816. } else if (err == EMFILE) {
  817. ui__warning("Too many events are opened.\n"
  818. "Try again after reducing the number of events\n");
  819. goto out_err;
  820. }
  821. ui__warning("The sys_perf_event_open() syscall "
  822. "returned with %d (%s). /bin/dmesg "
  823. "may provide additional information.\n"
  824. "No CONFIG_PERF_EVENTS=y kernel support "
  825. "configured?\n", err, strerror(err));
  826. goto out_err;
  827. }
  828. }
  829. if (perf_evlist__mmap(evlist, top->mmap_pages, false) < 0) {
  830. ui__warning("Failed to mmap with %d (%s)\n",
  831. errno, strerror(errno));
  832. goto out_err;
  833. }
  834. return;
  835. out_err:
  836. exit_browser(0);
  837. exit(0);
  838. }
  839. static int perf_top__setup_sample_type(struct perf_top *top)
  840. {
  841. if (!top->sort_has_symbols) {
  842. if (symbol_conf.use_callchain) {
  843. ui__warning("Selected -g but \"sym\" not present in --sort/-s.");
  844. return -EINVAL;
  845. }
  846. } else if (!top->dont_use_callchains && callchain_param.mode != CHAIN_NONE) {
  847. if (callchain_register_param(&callchain_param) < 0) {
  848. ui__warning("Can't register callchain params.\n");
  849. return -EINVAL;
  850. }
  851. }
  852. return 0;
  853. }
  854. static int __cmd_top(struct perf_top *top)
  855. {
  856. pthread_t thread;
  857. int ret;
  858. /*
  859. * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
  860. * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
  861. */
  862. top->session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
  863. if (top->session == NULL)
  864. return -ENOMEM;
  865. ret = perf_top__setup_sample_type(top);
  866. if (ret)
  867. goto out_delete;
  868. if (top->target_tid || top->uid != UINT_MAX)
  869. perf_event__synthesize_thread_map(&top->tool, top->evlist->threads,
  870. perf_event__process,
  871. &top->session->host_machine);
  872. else
  873. perf_event__synthesize_threads(&top->tool, perf_event__process,
  874. &top->session->host_machine);
  875. perf_top__start_counters(top);
  876. top->session->evlist = top->evlist;
  877. perf_session__update_sample_type(top->session);
  878. /* Wait for a minimal set of events before starting the snapshot */
  879. poll(top->evlist->pollfd, top->evlist->nr_fds, 100);
  880. perf_top__mmap_read(top);
  881. if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
  882. display_thread), top)) {
  883. printf("Could not create display thread.\n");
  884. exit(-1);
  885. }
  886. if (top->realtime_prio) {
  887. struct sched_param param;
  888. param.sched_priority = top->realtime_prio;
  889. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  890. printf("Could not set realtime priority.\n");
  891. exit(-1);
  892. }
  893. }
  894. while (1) {
  895. u64 hits = top->samples;
  896. perf_top__mmap_read(top);
  897. if (hits == top->samples)
  898. ret = poll(top->evlist->pollfd, top->evlist->nr_fds, 100);
  899. }
  900. out_delete:
  901. perf_session__delete(top->session);
  902. top->session = NULL;
  903. return 0;
  904. }
  905. static int
  906. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  907. {
  908. struct perf_top *top = (struct perf_top *)opt->value;
  909. char *tok, *tok2;
  910. char *endptr;
  911. /*
  912. * --no-call-graph
  913. */
  914. if (unset) {
  915. top->dont_use_callchains = true;
  916. return 0;
  917. }
  918. symbol_conf.use_callchain = true;
  919. if (!arg)
  920. return 0;
  921. tok = strtok((char *)arg, ",");
  922. if (!tok)
  923. return -1;
  924. /* get the output mode */
  925. if (!strncmp(tok, "graph", strlen(arg)))
  926. callchain_param.mode = CHAIN_GRAPH_ABS;
  927. else if (!strncmp(tok, "flat", strlen(arg)))
  928. callchain_param.mode = CHAIN_FLAT;
  929. else if (!strncmp(tok, "fractal", strlen(arg)))
  930. callchain_param.mode = CHAIN_GRAPH_REL;
  931. else if (!strncmp(tok, "none", strlen(arg))) {
  932. callchain_param.mode = CHAIN_NONE;
  933. symbol_conf.use_callchain = false;
  934. return 0;
  935. } else
  936. return -1;
  937. /* get the min percentage */
  938. tok = strtok(NULL, ",");
  939. if (!tok)
  940. goto setup;
  941. callchain_param.min_percent = strtod(tok, &endptr);
  942. if (tok == endptr)
  943. return -1;
  944. /* get the print limit */
  945. tok2 = strtok(NULL, ",");
  946. if (!tok2)
  947. goto setup;
  948. if (tok2[0] != 'c') {
  949. callchain_param.print_limit = strtod(tok2, &endptr);
  950. tok2 = strtok(NULL, ",");
  951. if (!tok2)
  952. goto setup;
  953. }
  954. /* get the call chain order */
  955. if (!strcmp(tok2, "caller"))
  956. callchain_param.order = ORDER_CALLER;
  957. else if (!strcmp(tok2, "callee"))
  958. callchain_param.order = ORDER_CALLEE;
  959. else
  960. return -1;
  961. setup:
  962. if (callchain_register_param(&callchain_param) < 0) {
  963. fprintf(stderr, "Can't register callchain params\n");
  964. return -1;
  965. }
  966. return 0;
  967. }
  968. static const char * const top_usage[] = {
  969. "perf top [<options>]",
  970. NULL
  971. };
  972. int cmd_top(int argc, const char **argv, const char *prefix __used)
  973. {
  974. struct perf_evsel *pos;
  975. int status = -ENOMEM;
  976. struct perf_top top = {
  977. .count_filter = 5,
  978. .delay_secs = 2,
  979. .uid = UINT_MAX,
  980. .freq = 1000, /* 1 KHz */
  981. .mmap_pages = 128,
  982. .sym_pcnt_filter = 5,
  983. };
  984. char callchain_default_opt[] = "fractal,0.5,callee";
  985. const struct option options[] = {
  986. OPT_CALLBACK('e', "event", &top.evlist, "event",
  987. "event selector. use 'perf list' to list available events",
  988. parse_events_option),
  989. OPT_INTEGER('c', "count", &top.default_interval,
  990. "event period to sample"),
  991. OPT_STRING('p', "pid", &top.target_pid, "pid",
  992. "profile events on existing process id"),
  993. OPT_STRING('t', "tid", &top.target_tid, "tid",
  994. "profile events on existing thread id"),
  995. OPT_BOOLEAN('a', "all-cpus", &top.system_wide,
  996. "system-wide collection from all CPUs"),
  997. OPT_STRING('C', "cpu", &top.cpu_list, "cpu",
  998. "list of cpus to monitor"),
  999. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1000. "file", "vmlinux pathname"),
  1001. OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
  1002. "hide kernel symbols"),
  1003. OPT_UINTEGER('m', "mmap-pages", &top.mmap_pages, "number of mmap data pages"),
  1004. OPT_INTEGER('r', "realtime", &top.realtime_prio,
  1005. "collect data with this RT SCHED_FIFO priority"),
  1006. OPT_INTEGER('d', "delay", &top.delay_secs,
  1007. "number of seconds to delay between refreshes"),
  1008. OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
  1009. "dump the symbol table used for profiling"),
  1010. OPT_INTEGER('f', "count-filter", &top.count_filter,
  1011. "only display functions with more events than this"),
  1012. OPT_BOOLEAN('g', "group", &top.group,
  1013. "put the counters into a counter group"),
  1014. OPT_BOOLEAN('i', "inherit", &top.inherit,
  1015. "child tasks inherit counters"),
  1016. OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
  1017. "symbol to annotate"),
  1018. OPT_BOOLEAN('z', "zero", &top.zero,
  1019. "zero history across updates"),
  1020. OPT_INTEGER('F', "freq", &top.freq,
  1021. "profile at this frequency"),
  1022. OPT_INTEGER('E', "entries", &top.print_entries,
  1023. "display this many functions"),
  1024. OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
  1025. "hide user symbols"),
  1026. OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
  1027. OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
  1028. OPT_INCR('v', "verbose", &verbose,
  1029. "be more verbose (show counter open errors, etc)"),
  1030. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1031. "sort by key(s): pid, comm, dso, symbol, parent"),
  1032. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  1033. "Show a column with the number of samples"),
  1034. OPT_CALLBACK_DEFAULT('G', "call-graph", &top, "output_type,min_percent, call_order",
  1035. "Display callchains using output_type (graph, flat, fractal, or none), min percent threshold and callchain order. "
  1036. "Default: fractal,0.5,callee", &parse_callchain_opt,
  1037. callchain_default_opt),
  1038. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  1039. "Show a column with the sum of periods"),
  1040. OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  1041. "only consider symbols in these dsos"),
  1042. OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1043. "only consider symbols in these comms"),
  1044. OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1045. "only consider these symbols"),
  1046. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  1047. "Interleave source code with assembly code (default)"),
  1048. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  1049. "Display raw encoding of assembly instructions (default)"),
  1050. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  1051. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  1052. OPT_STRING('u', "uid", &top.uid_str, "user", "user to profile"),
  1053. OPT_END()
  1054. };
  1055. top.evlist = perf_evlist__new(NULL, NULL);
  1056. if (top.evlist == NULL)
  1057. return -ENOMEM;
  1058. symbol_conf.exclude_other = false;
  1059. argc = parse_options(argc, argv, options, top_usage, 0);
  1060. if (argc)
  1061. usage_with_options(top_usage, options);
  1062. if (sort_order == default_sort_order)
  1063. sort_order = "dso,symbol";
  1064. setup_sorting(top_usage, options);
  1065. if (top.use_stdio)
  1066. use_browser = 0;
  1067. else if (top.use_tui)
  1068. use_browser = 1;
  1069. setup_browser(false);
  1070. top.uid = parse_target_uid(top.uid_str, top.target_tid, top.target_pid);
  1071. if (top.uid_str != NULL && top.uid == UINT_MAX - 1)
  1072. goto out_delete_evlist;
  1073. /* CPU and PID are mutually exclusive */
  1074. if (top.target_tid && top.cpu_list) {
  1075. printf("WARNING: PID switch overriding CPU\n");
  1076. sleep(1);
  1077. top.cpu_list = NULL;
  1078. }
  1079. if (top.target_pid)
  1080. top.target_tid = top.target_pid;
  1081. if (perf_evlist__create_maps(top.evlist, top.target_pid,
  1082. top.target_tid, top.uid, top.cpu_list) < 0)
  1083. usage_with_options(top_usage, options);
  1084. if (!top.evlist->nr_entries &&
  1085. perf_evlist__add_default(top.evlist) < 0) {
  1086. pr_err("Not enough memory for event selector list\n");
  1087. return -ENOMEM;
  1088. }
  1089. symbol_conf.nr_events = top.evlist->nr_entries;
  1090. if (top.delay_secs < 1)
  1091. top.delay_secs = 1;
  1092. /*
  1093. * User specified count overrides default frequency.
  1094. */
  1095. if (top.default_interval)
  1096. top.freq = 0;
  1097. else if (top.freq) {
  1098. top.default_interval = top.freq;
  1099. } else {
  1100. fprintf(stderr, "frequency and count are zero, aborting\n");
  1101. exit(EXIT_FAILURE);
  1102. }
  1103. list_for_each_entry(pos, &top.evlist->entries, node) {
  1104. /*
  1105. * Fill in the ones not specifically initialized via -c:
  1106. */
  1107. if (!pos->attr.sample_period)
  1108. pos->attr.sample_period = top.default_interval;
  1109. }
  1110. top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
  1111. symbol_conf.priv_size = sizeof(struct annotation);
  1112. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1113. if (symbol__init() < 0)
  1114. return -1;
  1115. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
  1116. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
  1117. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
  1118. /*
  1119. * Avoid annotation data structures overhead when symbols aren't on the
  1120. * sort list.
  1121. */
  1122. top.sort_has_symbols = sort_sym.list.next != NULL;
  1123. get_term_dimensions(&top.winsize);
  1124. if (top.print_entries == 0) {
  1125. struct sigaction act = {
  1126. .sa_sigaction = perf_top__sig_winch,
  1127. .sa_flags = SA_SIGINFO,
  1128. };
  1129. perf_top__update_print_entries(&top);
  1130. sigaction(SIGWINCH, &act, NULL);
  1131. }
  1132. status = __cmd_top(&top);
  1133. out_delete_evlist:
  1134. perf_evlist__delete(top.evlist);
  1135. return status;
  1136. }