builtin-top.c 35 KB

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