builtin-top.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  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 == DSO_BINARY_TYPE__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", perf_evsel__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", perf_evsel__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, perf_evsel__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", perf_evsel__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->target.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, 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, &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 (perf_target__has_cpu(&top->target))
  766. attr->sample_type |= PERF_SAMPLE_CPU;
  767. if (symbol_conf.use_callchain)
  768. attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
  769. attr->mmap = 1;
  770. attr->comm = 1;
  771. attr->inherit = top->inherit;
  772. fallback_missing_features:
  773. if (top->exclude_guest_missing)
  774. attr->exclude_guest = attr->exclude_host = 0;
  775. retry_sample_id:
  776. attr->sample_id_all = top->sample_id_all_missing ? 0 : 1;
  777. try_again:
  778. if (perf_evsel__open(counter, top->evlist->cpus,
  779. top->evlist->threads, top->group,
  780. group_fd) < 0) {
  781. int err = errno;
  782. if (err == EPERM || err == EACCES) {
  783. ui__error_paranoid();
  784. goto out_err;
  785. } else if (err == EINVAL) {
  786. if (!top->exclude_guest_missing &&
  787. (attr->exclude_guest || attr->exclude_host)) {
  788. pr_debug("Old kernel, cannot exclude "
  789. "guest or host samples.\n");
  790. top->exclude_guest_missing = true;
  791. goto fallback_missing_features;
  792. } else if (!top->sample_id_all_missing) {
  793. /*
  794. * Old kernel, no attr->sample_id_type_all field
  795. */
  796. top->sample_id_all_missing = true;
  797. goto retry_sample_id;
  798. }
  799. }
  800. /*
  801. * If it's cycles then fall back to hrtimer
  802. * based cpu-clock-tick sw counter, which
  803. * is always available even if no PMU support:
  804. */
  805. if (attr->type == PERF_TYPE_HARDWARE &&
  806. attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  807. if (verbose)
  808. ui__warning("Cycles event not supported,\n"
  809. "trying to fall back to cpu-clock-ticks\n");
  810. attr->type = PERF_TYPE_SOFTWARE;
  811. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  812. if (counter->name) {
  813. free(counter->name);
  814. counter->name = NULL;
  815. }
  816. goto try_again;
  817. }
  818. if (err == ENOENT) {
  819. ui__error("The %s event is not supported.\n",
  820. perf_evsel__name(counter));
  821. goto out_err;
  822. } else if (err == EMFILE) {
  823. ui__error("Too many events are opened.\n"
  824. "Try again after reducing the number of events\n");
  825. goto out_err;
  826. }
  827. ui__error("The sys_perf_event_open() syscall "
  828. "returned with %d (%s). /bin/dmesg "
  829. "may provide additional information.\n"
  830. "No CONFIG_PERF_EVENTS=y kernel support "
  831. "configured?\n", err, strerror(err));
  832. goto out_err;
  833. }
  834. }
  835. if (perf_evlist__mmap(evlist, top->mmap_pages, false) < 0) {
  836. ui__error("Failed to mmap with %d (%s)\n",
  837. errno, strerror(errno));
  838. goto out_err;
  839. }
  840. return;
  841. out_err:
  842. exit_browser(0);
  843. exit(0);
  844. }
  845. static int perf_top__setup_sample_type(struct perf_top *top)
  846. {
  847. if (!top->sort_has_symbols) {
  848. if (symbol_conf.use_callchain) {
  849. ui__error("Selected -g but \"sym\" not present in --sort/-s.");
  850. return -EINVAL;
  851. }
  852. } else if (!top->dont_use_callchains && callchain_param.mode != CHAIN_NONE) {
  853. if (callchain_register_param(&callchain_param) < 0) {
  854. ui__error("Can't register callchain params.\n");
  855. return -EINVAL;
  856. }
  857. }
  858. return 0;
  859. }
  860. static int __cmd_top(struct perf_top *top)
  861. {
  862. pthread_t thread;
  863. int ret;
  864. /*
  865. * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
  866. * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
  867. */
  868. top->session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
  869. if (top->session == NULL)
  870. return -ENOMEM;
  871. ret = perf_top__setup_sample_type(top);
  872. if (ret)
  873. goto out_delete;
  874. if (perf_target__has_task(&top->target))
  875. perf_event__synthesize_thread_map(&top->tool, top->evlist->threads,
  876. perf_event__process,
  877. &top->session->host_machine);
  878. else
  879. perf_event__synthesize_threads(&top->tool, perf_event__process,
  880. &top->session->host_machine);
  881. perf_top__start_counters(top);
  882. top->session->evlist = top->evlist;
  883. perf_session__update_sample_type(top->session);
  884. /* Wait for a minimal set of events before starting the snapshot */
  885. poll(top->evlist->pollfd, top->evlist->nr_fds, 100);
  886. perf_top__mmap_read(top);
  887. if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
  888. display_thread), top)) {
  889. ui__error("Could not create display thread.\n");
  890. exit(-1);
  891. }
  892. if (top->realtime_prio) {
  893. struct sched_param param;
  894. param.sched_priority = top->realtime_prio;
  895. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  896. ui__error("Could not set realtime priority.\n");
  897. exit(-1);
  898. }
  899. }
  900. while (1) {
  901. u64 hits = top->samples;
  902. perf_top__mmap_read(top);
  903. if (hits == top->samples)
  904. ret = poll(top->evlist->pollfd, top->evlist->nr_fds, 100);
  905. }
  906. out_delete:
  907. perf_session__delete(top->session);
  908. top->session = NULL;
  909. return 0;
  910. }
  911. static int
  912. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  913. {
  914. struct perf_top *top = (struct perf_top *)opt->value;
  915. char *tok, *tok2;
  916. char *endptr;
  917. /*
  918. * --no-call-graph
  919. */
  920. if (unset) {
  921. top->dont_use_callchains = true;
  922. return 0;
  923. }
  924. symbol_conf.use_callchain = true;
  925. if (!arg)
  926. return 0;
  927. tok = strtok((char *)arg, ",");
  928. if (!tok)
  929. return -1;
  930. /* get the output mode */
  931. if (!strncmp(tok, "graph", strlen(arg)))
  932. callchain_param.mode = CHAIN_GRAPH_ABS;
  933. else if (!strncmp(tok, "flat", strlen(arg)))
  934. callchain_param.mode = CHAIN_FLAT;
  935. else if (!strncmp(tok, "fractal", strlen(arg)))
  936. callchain_param.mode = CHAIN_GRAPH_REL;
  937. else if (!strncmp(tok, "none", strlen(arg))) {
  938. callchain_param.mode = CHAIN_NONE;
  939. symbol_conf.use_callchain = false;
  940. return 0;
  941. } else
  942. return -1;
  943. /* get the min percentage */
  944. tok = strtok(NULL, ",");
  945. if (!tok)
  946. goto setup;
  947. callchain_param.min_percent = strtod(tok, &endptr);
  948. if (tok == endptr)
  949. return -1;
  950. /* get the print limit */
  951. tok2 = strtok(NULL, ",");
  952. if (!tok2)
  953. goto setup;
  954. if (tok2[0] != 'c') {
  955. callchain_param.print_limit = strtod(tok2, &endptr);
  956. tok2 = strtok(NULL, ",");
  957. if (!tok2)
  958. goto setup;
  959. }
  960. /* get the call chain order */
  961. if (!strcmp(tok2, "caller"))
  962. callchain_param.order = ORDER_CALLER;
  963. else if (!strcmp(tok2, "callee"))
  964. callchain_param.order = ORDER_CALLEE;
  965. else
  966. return -1;
  967. setup:
  968. if (callchain_register_param(&callchain_param) < 0) {
  969. fprintf(stderr, "Can't register callchain params\n");
  970. return -1;
  971. }
  972. return 0;
  973. }
  974. static const char * const top_usage[] = {
  975. "perf top [<options>]",
  976. NULL
  977. };
  978. int cmd_top(int argc, const char **argv, const char *prefix __used)
  979. {
  980. struct perf_evsel *pos;
  981. int status;
  982. char errbuf[BUFSIZ];
  983. struct perf_top top = {
  984. .count_filter = 5,
  985. .delay_secs = 2,
  986. .freq = 4000, /* 4 KHz */
  987. .mmap_pages = 128,
  988. .sym_pcnt_filter = 5,
  989. .target = {
  990. .uses_mmap = true,
  991. },
  992. };
  993. char callchain_default_opt[] = "fractal,0.5,callee";
  994. const struct option options[] = {
  995. OPT_CALLBACK('e', "event", &top.evlist, "event",
  996. "event selector. use 'perf list' to list available events",
  997. parse_events_option),
  998. OPT_INTEGER('c', "count", &top.default_interval,
  999. "event period to sample"),
  1000. OPT_STRING('p', "pid", &top.target.pid, "pid",
  1001. "profile events on existing process id"),
  1002. OPT_STRING('t', "tid", &top.target.tid, "tid",
  1003. "profile events on existing thread id"),
  1004. OPT_BOOLEAN('a', "all-cpus", &top.target.system_wide,
  1005. "system-wide collection from all CPUs"),
  1006. OPT_STRING('C', "cpu", &top.target.cpu_list, "cpu",
  1007. "list of cpus to monitor"),
  1008. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1009. "file", "vmlinux pathname"),
  1010. OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
  1011. "hide kernel symbols"),
  1012. OPT_UINTEGER('m', "mmap-pages", &top.mmap_pages, "number of mmap data pages"),
  1013. OPT_INTEGER('r', "realtime", &top.realtime_prio,
  1014. "collect data with this RT SCHED_FIFO priority"),
  1015. OPT_INTEGER('d', "delay", &top.delay_secs,
  1016. "number of seconds to delay between refreshes"),
  1017. OPT_BOOLEAN('D', "dump-symtab", &top.dump_symtab,
  1018. "dump the symbol table used for profiling"),
  1019. OPT_INTEGER('f', "count-filter", &top.count_filter,
  1020. "only display functions with more events than this"),
  1021. OPT_BOOLEAN('g', "group", &top.group,
  1022. "put the counters into a counter group"),
  1023. OPT_BOOLEAN('i', "inherit", &top.inherit,
  1024. "child tasks inherit counters"),
  1025. OPT_STRING(0, "sym-annotate", &top.sym_filter, "symbol name",
  1026. "symbol to annotate"),
  1027. OPT_BOOLEAN('z', "zero", &top.zero,
  1028. "zero history across updates"),
  1029. OPT_INTEGER('F', "freq", &top.freq,
  1030. "profile at this frequency"),
  1031. OPT_INTEGER('E', "entries", &top.print_entries,
  1032. "display this many functions"),
  1033. OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
  1034. "hide user symbols"),
  1035. OPT_BOOLEAN(0, "tui", &top.use_tui, "Use the TUI interface"),
  1036. OPT_BOOLEAN(0, "stdio", &top.use_stdio, "Use the stdio interface"),
  1037. OPT_INCR('v', "verbose", &verbose,
  1038. "be more verbose (show counter open errors, etc)"),
  1039. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1040. "sort by key(s): pid, comm, dso, symbol, parent"),
  1041. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  1042. "Show a column with the number of samples"),
  1043. OPT_CALLBACK_DEFAULT('G', "call-graph", &top, "output_type,min_percent, call_order",
  1044. "Display callchains using output_type (graph, flat, fractal, or none), min percent threshold and callchain order. "
  1045. "Default: fractal,0.5,callee", &parse_callchain_opt,
  1046. callchain_default_opt),
  1047. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  1048. "Show a column with the sum of periods"),
  1049. OPT_STRING(0, "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  1050. "only consider symbols in these dsos"),
  1051. OPT_STRING(0, "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  1052. "only consider symbols in these comms"),
  1053. OPT_STRING(0, "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  1054. "only consider these symbols"),
  1055. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  1056. "Interleave source code with assembly code (default)"),
  1057. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  1058. "Display raw encoding of assembly instructions (default)"),
  1059. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  1060. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  1061. OPT_STRING('u', "uid", &top.target.uid_str, "user", "user to profile"),
  1062. OPT_END()
  1063. };
  1064. top.evlist = perf_evlist__new(NULL, NULL);
  1065. if (top.evlist == NULL)
  1066. return -ENOMEM;
  1067. symbol_conf.exclude_other = false;
  1068. argc = parse_options(argc, argv, options, top_usage, 0);
  1069. if (argc)
  1070. usage_with_options(top_usage, options);
  1071. if (sort_order == default_sort_order)
  1072. sort_order = "dso,symbol";
  1073. setup_sorting(top_usage, options);
  1074. if (top.use_stdio)
  1075. use_browser = 0;
  1076. else if (top.use_tui)
  1077. use_browser = 1;
  1078. setup_browser(false);
  1079. status = perf_target__validate(&top.target);
  1080. if (status) {
  1081. perf_target__strerror(&top.target, status, errbuf, BUFSIZ);
  1082. ui__warning("%s", errbuf);
  1083. }
  1084. status = perf_target__parse_uid(&top.target);
  1085. if (status) {
  1086. int saved_errno = errno;
  1087. perf_target__strerror(&top.target, status, errbuf, BUFSIZ);
  1088. ui__error("%s", errbuf);
  1089. status = -saved_errno;
  1090. goto out_delete_evlist;
  1091. }
  1092. if (perf_target__none(&top.target))
  1093. top.target.system_wide = true;
  1094. if (perf_evlist__create_maps(top.evlist, &top.target) < 0)
  1095. usage_with_options(top_usage, options);
  1096. if (!top.evlist->nr_entries &&
  1097. perf_evlist__add_default(top.evlist) < 0) {
  1098. ui__error("Not enough memory for event selector list\n");
  1099. return -ENOMEM;
  1100. }
  1101. symbol_conf.nr_events = top.evlist->nr_entries;
  1102. if (top.delay_secs < 1)
  1103. top.delay_secs = 1;
  1104. /*
  1105. * User specified count overrides default frequency.
  1106. */
  1107. if (top.default_interval)
  1108. top.freq = 0;
  1109. else if (top.freq) {
  1110. top.default_interval = top.freq;
  1111. } else {
  1112. ui__error("frequency and count are zero, aborting\n");
  1113. exit(EXIT_FAILURE);
  1114. }
  1115. list_for_each_entry(pos, &top.evlist->entries, node) {
  1116. /*
  1117. * Fill in the ones not specifically initialized via -c:
  1118. */
  1119. if (!pos->attr.sample_period)
  1120. pos->attr.sample_period = top.default_interval;
  1121. }
  1122. top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
  1123. symbol_conf.priv_size = sizeof(struct annotation);
  1124. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1125. if (symbol__init() < 0)
  1126. return -1;
  1127. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
  1128. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
  1129. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
  1130. /*
  1131. * Avoid annotation data structures overhead when symbols aren't on the
  1132. * sort list.
  1133. */
  1134. top.sort_has_symbols = sort_sym.list.next != NULL;
  1135. get_term_dimensions(&top.winsize);
  1136. if (top.print_entries == 0) {
  1137. struct sigaction act = {
  1138. .sa_sigaction = perf_top__sig_winch,
  1139. .sa_flags = SA_SIGINFO,
  1140. };
  1141. perf_top__update_print_entries(&top);
  1142. sigaction(SIGWINCH, &act, NULL);
  1143. }
  1144. status = __cmd_top(&top);
  1145. out_delete_evlist:
  1146. perf_evlist__delete(top.evlist);
  1147. return status;
  1148. }