builtin-top.c 34 KB

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