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