builtin-top.c 35 KB

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