builtin-top.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  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. perf_top__sort_new_samples(top);
  496. /*
  497. * Initialize the uid_filter_str, in the future the TUI will allow
  498. * Zooming in/out UIDs. For now juse use whatever the user passed
  499. * via --uid.
  500. */
  501. list_for_each_entry(pos, &top->evlist->entries, node)
  502. pos->hists.uid_filter_str = top->target.uid_str;
  503. perf_evlist__tui_browse_hists(top->evlist, help,
  504. perf_top__sort_new_samples,
  505. top, top->delay_secs);
  506. exit_browser(0);
  507. exit(0);
  508. return NULL;
  509. }
  510. static void *display_thread(void *arg)
  511. {
  512. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  513. struct termios tc, save;
  514. struct perf_top *top = arg;
  515. int delay_msecs, c;
  516. tcgetattr(0, &save);
  517. tc = save;
  518. tc.c_lflag &= ~(ICANON | ECHO);
  519. tc.c_cc[VMIN] = 0;
  520. tc.c_cc[VTIME] = 0;
  521. pthread__unblock_sigwinch();
  522. repeat:
  523. delay_msecs = top->delay_secs * 1000;
  524. tcsetattr(0, TCSANOW, &tc);
  525. /* trash return*/
  526. getc(stdin);
  527. while (1) {
  528. perf_top__print_sym_table(top);
  529. /*
  530. * Either timeout expired or we got an EINTR due to SIGWINCH,
  531. * refresh screen in both cases.
  532. */
  533. switch (poll(&stdin_poll, 1, delay_msecs)) {
  534. case 0:
  535. continue;
  536. case -1:
  537. if (errno == EINTR)
  538. continue;
  539. /* Fall trhu */
  540. default:
  541. goto process_hotkey;
  542. }
  543. }
  544. process_hotkey:
  545. c = getc(stdin);
  546. tcsetattr(0, TCSAFLUSH, &save);
  547. perf_top__handle_keypress(top, c);
  548. goto repeat;
  549. return NULL;
  550. }
  551. /* Tag samples to be skipped. */
  552. static const char *skip_symbols[] = {
  553. "intel_idle",
  554. "default_idle",
  555. "native_safe_halt",
  556. "cpu_idle",
  557. "enter_idle",
  558. "exit_idle",
  559. "mwait_idle",
  560. "mwait_idle_with_hints",
  561. "poll_idle",
  562. "ppc64_runlatch_off",
  563. "pseries_dedicated_idle_sleep",
  564. NULL
  565. };
  566. static int symbol_filter(struct map *map __maybe_unused, struct symbol *sym)
  567. {
  568. const char *name = sym->name;
  569. int i;
  570. /*
  571. * ppc64 uses function descriptors and appends a '.' to the
  572. * start of every instruction address. Remove it.
  573. */
  574. if (name[0] == '.')
  575. name++;
  576. if (!strcmp(name, "_text") ||
  577. !strcmp(name, "_etext") ||
  578. !strcmp(name, "_sinittext") ||
  579. !strncmp("init_module", name, 11) ||
  580. !strncmp("cleanup_module", name, 14) ||
  581. strstr(name, "_text_start") ||
  582. strstr(name, "_text_end"))
  583. return 1;
  584. for (i = 0; skip_symbols[i]; i++) {
  585. if (!strcmp(skip_symbols[i], name)) {
  586. sym->ignore = true;
  587. break;
  588. }
  589. }
  590. return 0;
  591. }
  592. static void perf_event__process_sample(struct perf_tool *tool,
  593. const union perf_event *event,
  594. struct perf_evsel *evsel,
  595. struct perf_sample *sample,
  596. struct machine *machine)
  597. {
  598. struct perf_top *top = container_of(tool, struct perf_top, tool);
  599. struct symbol *parent = NULL;
  600. u64 ip = event->ip.ip;
  601. struct addr_location al;
  602. int err;
  603. if (!machine && perf_guest) {
  604. static struct intlist *seen;
  605. if (!seen)
  606. seen = intlist__new();
  607. if (!intlist__has_entry(seen, event->ip.pid)) {
  608. pr_err("Can't find guest [%d]'s kernel information\n",
  609. event->ip.pid);
  610. intlist__add(seen, event->ip.pid);
  611. }
  612. return;
  613. }
  614. if (!machine) {
  615. pr_err("%u unprocessable samples recorded.",
  616. top->session->hists.stats.nr_unprocessable_samples++);
  617. return;
  618. }
  619. if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
  620. top->exact_samples++;
  621. if (perf_event__preprocess_sample(event, machine, &al, sample,
  622. symbol_filter) < 0 ||
  623. al.filtered)
  624. return;
  625. if (!top->kptr_restrict_warned &&
  626. symbol_conf.kptr_restrict &&
  627. al.cpumode == PERF_RECORD_MISC_KERNEL) {
  628. ui__warning(
  629. "Kernel address maps (/proc/{kallsyms,modules}) are restricted.\n\n"
  630. "Check /proc/sys/kernel/kptr_restrict.\n\n"
  631. "Kernel%s samples will not be resolved.\n",
  632. !RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION]) ?
  633. " modules" : "");
  634. if (use_browser <= 0)
  635. sleep(5);
  636. top->kptr_restrict_warned = true;
  637. }
  638. if (al.sym == NULL) {
  639. const char *msg = "Kernel samples will not be resolved.\n";
  640. /*
  641. * As we do lazy loading of symtabs we only will know if the
  642. * specified vmlinux file is invalid when we actually have a
  643. * hit in kernel space and then try to load it. So if we get
  644. * here and there are _no_ symbols in the DSO backing the
  645. * kernel map, bail out.
  646. *
  647. * We may never get here, for instance, if we use -K/
  648. * --hide-kernel-symbols, even if the user specifies an
  649. * invalid --vmlinux ;-)
  650. */
  651. if (!top->kptr_restrict_warned && !top->vmlinux_warned &&
  652. al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
  653. RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
  654. if (symbol_conf.vmlinux_name) {
  655. ui__warning("The %s file can't be used.\n%s",
  656. symbol_conf.vmlinux_name, msg);
  657. } else {
  658. ui__warning("A vmlinux file was not found.\n%s",
  659. msg);
  660. }
  661. if (use_browser <= 0)
  662. sleep(5);
  663. top->vmlinux_warned = true;
  664. }
  665. }
  666. if (al.sym == NULL || !al.sym->ignore) {
  667. struct hist_entry *he;
  668. if ((sort__has_parent || symbol_conf.use_callchain) &&
  669. sample->callchain) {
  670. err = machine__resolve_callchain(machine, evsel,
  671. al.thread, sample,
  672. &parent);
  673. if (err)
  674. return;
  675. }
  676. he = perf_evsel__add_hist_entry(evsel, &al, sample);
  677. if (he == NULL) {
  678. pr_err("Problem incrementing symbol period, skipping event\n");
  679. return;
  680. }
  681. if (symbol_conf.use_callchain) {
  682. err = callchain_append(he->callchain, &callchain_cursor,
  683. sample->period);
  684. if (err)
  685. return;
  686. }
  687. if (top->sort_has_symbols)
  688. perf_top__record_precise_ip(top, he, evsel->idx, ip);
  689. }
  690. return;
  691. }
  692. static void perf_top__mmap_read_idx(struct perf_top *top, int idx)
  693. {
  694. struct perf_sample sample;
  695. struct perf_evsel *evsel;
  696. struct perf_session *session = top->session;
  697. union perf_event *event;
  698. struct machine *machine;
  699. u8 origin;
  700. int ret;
  701. while ((event = perf_evlist__mmap_read(top->evlist, idx)) != NULL) {
  702. ret = perf_evlist__parse_sample(top->evlist, event, &sample);
  703. if (ret) {
  704. pr_err("Can't parse sample, err = %d\n", ret);
  705. continue;
  706. }
  707. evsel = perf_evlist__id2evsel(session->evlist, sample.id);
  708. assert(evsel != NULL);
  709. origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  710. if (event->header.type == PERF_RECORD_SAMPLE)
  711. ++top->samples;
  712. switch (origin) {
  713. case PERF_RECORD_MISC_USER:
  714. ++top->us_samples;
  715. if (top->hide_user_symbols)
  716. continue;
  717. machine = perf_session__find_host_machine(session);
  718. break;
  719. case PERF_RECORD_MISC_KERNEL:
  720. ++top->kernel_samples;
  721. if (top->hide_kernel_symbols)
  722. continue;
  723. machine = perf_session__find_host_machine(session);
  724. break;
  725. case PERF_RECORD_MISC_GUEST_KERNEL:
  726. ++top->guest_kernel_samples;
  727. machine = perf_session__find_machine(session, event->ip.pid);
  728. break;
  729. case PERF_RECORD_MISC_GUEST_USER:
  730. ++top->guest_us_samples;
  731. /*
  732. * TODO: we don't process guest user from host side
  733. * except simple counting.
  734. */
  735. /* Fall thru */
  736. default:
  737. continue;
  738. }
  739. if (event->header.type == PERF_RECORD_SAMPLE) {
  740. perf_event__process_sample(&top->tool, event, evsel,
  741. &sample, machine);
  742. } else if (event->header.type < PERF_RECORD_MAX) {
  743. hists__inc_nr_events(&evsel->hists, event->header.type);
  744. machine__process_event(machine, event);
  745. } else
  746. ++session->hists.stats.nr_unknown_events;
  747. }
  748. }
  749. static void perf_top__mmap_read(struct perf_top *top)
  750. {
  751. int i;
  752. for (i = 0; i < top->evlist->nr_mmaps; i++)
  753. perf_top__mmap_read_idx(top, i);
  754. }
  755. static void perf_top__start_counters(struct perf_top *top)
  756. {
  757. struct perf_evsel *counter;
  758. struct perf_evlist *evlist = top->evlist;
  759. if (top->group)
  760. perf_evlist__set_leader(evlist);
  761. list_for_each_entry(counter, &evlist->entries, node) {
  762. struct perf_event_attr *attr = &counter->attr;
  763. attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  764. if (top->freq) {
  765. attr->sample_type |= PERF_SAMPLE_PERIOD;
  766. attr->freq = 1;
  767. attr->sample_freq = top->freq;
  768. }
  769. if (evlist->nr_entries > 1) {
  770. attr->sample_type |= PERF_SAMPLE_ID;
  771. attr->read_format |= PERF_FORMAT_ID;
  772. }
  773. if (perf_target__has_cpu(&top->target))
  774. attr->sample_type |= PERF_SAMPLE_CPU;
  775. if (symbol_conf.use_callchain)
  776. attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
  777. attr->mmap = 1;
  778. attr->comm = 1;
  779. attr->inherit = top->inherit;
  780. fallback_missing_features:
  781. if (top->exclude_guest_missing)
  782. attr->exclude_guest = attr->exclude_host = 0;
  783. retry_sample_id:
  784. attr->sample_id_all = top->sample_id_all_missing ? 0 : 1;
  785. try_again:
  786. if (perf_evsel__open(counter, top->evlist->cpus,
  787. top->evlist->threads) < 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. } else if ((err == EOPNOTSUPP) && (attr->precise_ip)) {
  835. ui__error("\'precise\' request may not be supported. "
  836. "Try removing 'p' modifier\n");
  837. goto out_err;
  838. }
  839. ui__error("The sys_perf_event_open() syscall "
  840. "returned with %d (%s). /bin/dmesg "
  841. "may provide additional information.\n"
  842. "No CONFIG_PERF_EVENTS=y kernel support "
  843. "configured?\n", err, strerror(err));
  844. goto out_err;
  845. }
  846. }
  847. if (perf_evlist__mmap(evlist, top->mmap_pages, false) < 0) {
  848. ui__error("Failed to mmap with %d (%s)\n",
  849. errno, strerror(errno));
  850. goto out_err;
  851. }
  852. return;
  853. out_err:
  854. exit_browser(0);
  855. exit(0);
  856. }
  857. static int perf_top__setup_sample_type(struct perf_top *top)
  858. {
  859. if (!top->sort_has_symbols) {
  860. if (symbol_conf.use_callchain) {
  861. ui__error("Selected -g but \"sym\" not present in --sort/-s.");
  862. return -EINVAL;
  863. }
  864. } else if (!top->dont_use_callchains && callchain_param.mode != CHAIN_NONE) {
  865. if (callchain_register_param(&callchain_param) < 0) {
  866. ui__error("Can't register callchain params.\n");
  867. return -EINVAL;
  868. }
  869. }
  870. return 0;
  871. }
  872. static int __cmd_top(struct perf_top *top)
  873. {
  874. pthread_t thread;
  875. int ret;
  876. /*
  877. * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
  878. * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
  879. */
  880. top->session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
  881. if (top->session == NULL)
  882. return -ENOMEM;
  883. ret = perf_top__setup_sample_type(top);
  884. if (ret)
  885. goto out_delete;
  886. if (perf_target__has_task(&top->target))
  887. perf_event__synthesize_thread_map(&top->tool, top->evlist->threads,
  888. perf_event__process,
  889. &top->session->host_machine);
  890. else
  891. perf_event__synthesize_threads(&top->tool, perf_event__process,
  892. &top->session->host_machine);
  893. perf_top__start_counters(top);
  894. top->session->evlist = top->evlist;
  895. perf_session__set_id_hdr_size(top->session);
  896. /* Wait for a minimal set of events before starting the snapshot */
  897. poll(top->evlist->pollfd, top->evlist->nr_fds, 100);
  898. perf_top__mmap_read(top);
  899. if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
  900. display_thread), top)) {
  901. ui__error("Could not create display thread.\n");
  902. exit(-1);
  903. }
  904. if (top->realtime_prio) {
  905. struct sched_param param;
  906. param.sched_priority = top->realtime_prio;
  907. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  908. ui__error("Could not set realtime priority.\n");
  909. exit(-1);
  910. }
  911. }
  912. while (1) {
  913. u64 hits = top->samples;
  914. perf_top__mmap_read(top);
  915. if (hits == top->samples)
  916. ret = poll(top->evlist->pollfd, top->evlist->nr_fds, 100);
  917. }
  918. out_delete:
  919. perf_session__delete(top->session);
  920. top->session = NULL;
  921. return 0;
  922. }
  923. static int
  924. parse_callchain_opt(const struct option *opt, const char *arg, int unset)
  925. {
  926. struct perf_top *top = (struct perf_top *)opt->value;
  927. char *tok, *tok2;
  928. char *endptr;
  929. /*
  930. * --no-call-graph
  931. */
  932. if (unset) {
  933. top->dont_use_callchains = true;
  934. return 0;
  935. }
  936. symbol_conf.use_callchain = true;
  937. if (!arg)
  938. return 0;
  939. tok = strtok((char *)arg, ",");
  940. if (!tok)
  941. return -1;
  942. /* get the output mode */
  943. if (!strncmp(tok, "graph", strlen(arg)))
  944. callchain_param.mode = CHAIN_GRAPH_ABS;
  945. else if (!strncmp(tok, "flat", strlen(arg)))
  946. callchain_param.mode = CHAIN_FLAT;
  947. else if (!strncmp(tok, "fractal", strlen(arg)))
  948. callchain_param.mode = CHAIN_GRAPH_REL;
  949. else if (!strncmp(tok, "none", strlen(arg))) {
  950. callchain_param.mode = CHAIN_NONE;
  951. symbol_conf.use_callchain = false;
  952. return 0;
  953. } else
  954. return -1;
  955. /* get the min percentage */
  956. tok = strtok(NULL, ",");
  957. if (!tok)
  958. goto setup;
  959. callchain_param.min_percent = strtod(tok, &endptr);
  960. if (tok == endptr)
  961. return -1;
  962. /* get the print limit */
  963. tok2 = strtok(NULL, ",");
  964. if (!tok2)
  965. goto setup;
  966. if (tok2[0] != 'c') {
  967. callchain_param.print_limit = strtod(tok2, &endptr);
  968. tok2 = strtok(NULL, ",");
  969. if (!tok2)
  970. goto setup;
  971. }
  972. /* get the call chain order */
  973. if (!strcmp(tok2, "caller"))
  974. callchain_param.order = ORDER_CALLER;
  975. else if (!strcmp(tok2, "callee"))
  976. callchain_param.order = ORDER_CALLEE;
  977. else
  978. return -1;
  979. setup:
  980. if (callchain_register_param(&callchain_param) < 0) {
  981. fprintf(stderr, "Can't register callchain params\n");
  982. return -1;
  983. }
  984. return 0;
  985. }
  986. int cmd_top(int argc, const char **argv, const char *prefix __maybe_unused)
  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. const char * const top_usage[] = {
  1073. "perf top [<options>]",
  1074. NULL
  1075. };
  1076. top.evlist = perf_evlist__new(NULL, NULL);
  1077. if (top.evlist == NULL)
  1078. return -ENOMEM;
  1079. symbol_conf.exclude_other = false;
  1080. argc = parse_options(argc, argv, options, top_usage, 0);
  1081. if (argc)
  1082. usage_with_options(top_usage, options);
  1083. if (sort_order == default_sort_order)
  1084. sort_order = "dso,symbol";
  1085. setup_sorting(top_usage, options);
  1086. if (top.use_stdio)
  1087. use_browser = 0;
  1088. else if (top.use_tui)
  1089. use_browser = 1;
  1090. setup_browser(false);
  1091. status = perf_target__validate(&top.target);
  1092. if (status) {
  1093. perf_target__strerror(&top.target, status, errbuf, BUFSIZ);
  1094. ui__warning("%s", errbuf);
  1095. }
  1096. status = perf_target__parse_uid(&top.target);
  1097. if (status) {
  1098. int saved_errno = errno;
  1099. perf_target__strerror(&top.target, status, errbuf, BUFSIZ);
  1100. ui__error("%s", errbuf);
  1101. status = -saved_errno;
  1102. goto out_delete_evlist;
  1103. }
  1104. if (perf_target__none(&top.target))
  1105. top.target.system_wide = true;
  1106. if (perf_evlist__create_maps(top.evlist, &top.target) < 0)
  1107. usage_with_options(top_usage, options);
  1108. if (!top.evlist->nr_entries &&
  1109. perf_evlist__add_default(top.evlist) < 0) {
  1110. ui__error("Not enough memory for event selector list\n");
  1111. return -ENOMEM;
  1112. }
  1113. symbol_conf.nr_events = top.evlist->nr_entries;
  1114. if (top.delay_secs < 1)
  1115. top.delay_secs = 1;
  1116. /*
  1117. * User specified count overrides default frequency.
  1118. */
  1119. if (top.default_interval)
  1120. top.freq = 0;
  1121. else if (top.freq) {
  1122. top.default_interval = top.freq;
  1123. } else {
  1124. ui__error("frequency and count are zero, aborting\n");
  1125. exit(EXIT_FAILURE);
  1126. }
  1127. list_for_each_entry(pos, &top.evlist->entries, node) {
  1128. /*
  1129. * Fill in the ones not specifically initialized via -c:
  1130. */
  1131. if (!pos->attr.sample_period)
  1132. pos->attr.sample_period = top.default_interval;
  1133. }
  1134. top.sym_evsel = perf_evlist__first(top.evlist);
  1135. symbol_conf.priv_size = sizeof(struct annotation);
  1136. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1137. if (symbol__init() < 0)
  1138. return -1;
  1139. sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
  1140. sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
  1141. sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
  1142. /*
  1143. * Avoid annotation data structures overhead when symbols aren't on the
  1144. * sort list.
  1145. */
  1146. top.sort_has_symbols = sort_sym.list.next != NULL;
  1147. get_term_dimensions(&top.winsize);
  1148. if (top.print_entries == 0) {
  1149. struct sigaction act = {
  1150. .sa_sigaction = perf_top__sig_winch,
  1151. .sa_flags = SA_SIGINFO,
  1152. };
  1153. perf_top__update_print_entries(&top);
  1154. sigaction(SIGWINCH, &act, NULL);
  1155. }
  1156. status = __cmd_top(&top);
  1157. out_delete_evlist:
  1158. perf_evlist__delete(top.evlist);
  1159. return status;
  1160. }