builtin-top.c 35 KB

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