builtin-top.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. *
  9. * Improvements and fixes by:
  10. *
  11. * Arjan van de Ven <arjan@linux.intel.com>
  12. * Yanmin Zhang <yanmin.zhang@intel.com>
  13. * Wu Fengguang <fengguang.wu@intel.com>
  14. * Mike Galbraith <efault@gmx.de>
  15. * Paul Mackerras <paulus@samba.org>
  16. *
  17. * Released under the GPL v2. (and only v2, not any later version)
  18. */
  19. #include "builtin.h"
  20. #include "perf.h"
  21. #include "util/annotate.h"
  22. #include "util/cache.h"
  23. #include "util/color.h"
  24. #include "util/evlist.h"
  25. #include "util/evsel.h"
  26. #include "util/session.h"
  27. #include "util/symbol.h"
  28. #include "util/thread.h"
  29. #include "util/thread_map.h"
  30. #include "util/top.h"
  31. #include "util/util.h"
  32. #include <linux/rbtree.h>
  33. #include "util/parse-options.h"
  34. #include "util/parse-events.h"
  35. #include "util/cpumap.h"
  36. #include "util/xyarray.h"
  37. #include "util/debug.h"
  38. #include <assert.h>
  39. #include <fcntl.h>
  40. #include <stdio.h>
  41. #include <termios.h>
  42. #include <unistd.h>
  43. #include <inttypes.h>
  44. #include <errno.h>
  45. #include <time.h>
  46. #include <sched.h>
  47. #include <sys/syscall.h>
  48. #include <sys/ioctl.h>
  49. #include <sys/poll.h>
  50. #include <sys/prctl.h>
  51. #include <sys/wait.h>
  52. #include <sys/uio.h>
  53. #include <sys/mman.h>
  54. #include <linux/unistd.h>
  55. #include <linux/types.h>
  56. #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
  57. static struct perf_top top = {
  58. .count_filter = 5,
  59. .delay_secs = 2,
  60. .display_weighted = -1,
  61. .target_pid = -1,
  62. .target_tid = -1,
  63. .active_symbols = LIST_HEAD_INIT(top.active_symbols),
  64. .active_symbols_lock = PTHREAD_MUTEX_INITIALIZER,
  65. .active_symbols_cond = PTHREAD_COND_INITIALIZER,
  66. .freq = 1000, /* 1 KHz */
  67. };
  68. static bool system_wide = false;
  69. static bool use_tui, use_stdio;
  70. static int default_interval = 0;
  71. static bool inherit = false;
  72. static int realtime_prio = 0;
  73. static bool group = false;
  74. static unsigned int page_size;
  75. static unsigned int mmap_pages = 128;
  76. static bool dump_symtab = false;
  77. static struct winsize winsize;
  78. static const char *sym_filter = NULL;
  79. struct sym_entry *sym_filter_entry_sched = NULL;
  80. static int sym_pcnt_filter = 5;
  81. /*
  82. * Source functions
  83. */
  84. void get_term_dimensions(struct winsize *ws)
  85. {
  86. char *s = getenv("LINES");
  87. if (s != NULL) {
  88. ws->ws_row = atoi(s);
  89. s = getenv("COLUMNS");
  90. if (s != NULL) {
  91. ws->ws_col = atoi(s);
  92. if (ws->ws_row && ws->ws_col)
  93. return;
  94. }
  95. }
  96. #ifdef TIOCGWINSZ
  97. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  98. ws->ws_row && ws->ws_col)
  99. return;
  100. #endif
  101. ws->ws_row = 25;
  102. ws->ws_col = 80;
  103. }
  104. static void update_print_entries(struct winsize *ws)
  105. {
  106. top.print_entries = ws->ws_row;
  107. if (top.print_entries > 9)
  108. top.print_entries -= 9;
  109. }
  110. static void sig_winch_handler(int sig __used)
  111. {
  112. get_term_dimensions(&winsize);
  113. update_print_entries(&winsize);
  114. }
  115. static int parse_source(struct sym_entry *syme)
  116. {
  117. struct symbol *sym;
  118. struct annotation *notes;
  119. struct map *map;
  120. int err = -1;
  121. if (!syme)
  122. return -1;
  123. sym = sym_entry__symbol(syme);
  124. map = syme->map;
  125. /*
  126. * We can't annotate with just /proc/kallsyms
  127. */
  128. if (map->dso->symtab_type == SYMTAB__KALLSYMS) {
  129. pr_err("Can't annotate %s: No vmlinux file was found in the "
  130. "path\n", sym->name);
  131. sleep(1);
  132. return -1;
  133. }
  134. notes = symbol__annotation(sym);
  135. if (notes->src != NULL) {
  136. pthread_mutex_lock(&notes->lock);
  137. goto out_assign;
  138. }
  139. pthread_mutex_lock(&notes->lock);
  140. if (symbol__alloc_hist(sym, top.evlist->nr_entries) < 0) {
  141. pthread_mutex_unlock(&notes->lock);
  142. pr_err("Not enough memory for annotating '%s' symbol!\n",
  143. sym->name);
  144. sleep(1);
  145. return err;
  146. }
  147. err = symbol__annotate(sym, syme->map, 0);
  148. if (err == 0) {
  149. out_assign:
  150. top.sym_filter_entry = syme;
  151. }
  152. pthread_mutex_unlock(&notes->lock);
  153. return err;
  154. }
  155. static void __zero_source_counters(struct sym_entry *syme)
  156. {
  157. struct symbol *sym = sym_entry__symbol(syme);
  158. symbol__annotate_zero_histograms(sym);
  159. }
  160. static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
  161. {
  162. struct annotation *notes;
  163. struct symbol *sym;
  164. if (syme != top.sym_filter_entry)
  165. return;
  166. sym = sym_entry__symbol(syme);
  167. notes = symbol__annotation(sym);
  168. if (pthread_mutex_trylock(&notes->lock))
  169. return;
  170. ip = syme->map->map_ip(syme->map, ip);
  171. symbol__inc_addr_samples(sym, syme->map, counter, ip);
  172. pthread_mutex_unlock(&notes->lock);
  173. }
  174. static void show_details(struct sym_entry *syme)
  175. {
  176. struct annotation *notes;
  177. struct symbol *symbol;
  178. int more;
  179. if (!syme)
  180. return;
  181. symbol = sym_entry__symbol(syme);
  182. notes = symbol__annotation(symbol);
  183. pthread_mutex_lock(&notes->lock);
  184. if (notes->src == NULL)
  185. goto out_unlock;
  186. printf("Showing %s for %s\n", event_name(top.sym_evsel), symbol->name);
  187. printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
  188. more = symbol__annotate_printf(symbol, syme->map, top.sym_evsel->idx,
  189. 0, sym_pcnt_filter, top.print_entries, 4);
  190. if (top.zero)
  191. symbol__annotate_zero_histogram(symbol, top.sym_evsel->idx);
  192. else
  193. symbol__annotate_decay_histogram(symbol, top.sym_evsel->idx);
  194. if (more != 0)
  195. printf("%d lines not displayed, maybe increase display entries [e]\n", more);
  196. out_unlock:
  197. pthread_mutex_unlock(&notes->lock);
  198. }
  199. static const char CONSOLE_CLEAR[] = "";
  200. static void __list_insert_active_sym(struct sym_entry *syme)
  201. {
  202. list_add(&syme->node, &top.active_symbols);
  203. }
  204. static void print_sym_table(struct perf_session *session)
  205. {
  206. char bf[160];
  207. int printed = 0;
  208. struct rb_node *nd;
  209. struct sym_entry *syme;
  210. struct rb_root tmp = RB_ROOT;
  211. const int win_width = winsize.ws_col - 1;
  212. int sym_width, dso_width, dso_short_width;
  213. float sum_ksamples = perf_top__decay_samples(&top, &tmp);
  214. puts(CONSOLE_CLEAR);
  215. perf_top__header_snprintf(&top, bf, sizeof(bf));
  216. printf("%s\n", bf);
  217. perf_top__reset_sample_counters(&top);
  218. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  219. if (session->hists.stats.total_lost != 0) {
  220. color_fprintf(stdout, PERF_COLOR_RED, "WARNING:");
  221. printf(" LOST %" PRIu64 " events, Check IO/CPU overload\n",
  222. session->hists.stats.total_lost);
  223. }
  224. if (top.sym_filter_entry) {
  225. show_details(top.sym_filter_entry);
  226. return;
  227. }
  228. perf_top__find_widths(&top, &tmp, &dso_width, &dso_short_width,
  229. &sym_width);
  230. if (sym_width + dso_width > winsize.ws_col - 29) {
  231. dso_width = dso_short_width;
  232. if (sym_width + dso_width > winsize.ws_col - 29)
  233. sym_width = winsize.ws_col - dso_width - 29;
  234. }
  235. putchar('\n');
  236. if (top.evlist->nr_entries == 1)
  237. printf(" samples pcnt");
  238. else
  239. printf(" weight samples pcnt");
  240. if (verbose)
  241. printf(" RIP ");
  242. printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
  243. printf(" %s _______ _____",
  244. top.evlist->nr_entries == 1 ? " " : "______");
  245. if (verbose)
  246. printf(" ________________");
  247. printf(" %-*.*s", sym_width, sym_width, graph_line);
  248. printf(" %-*.*s", dso_width, dso_width, graph_line);
  249. puts("\n");
  250. for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
  251. struct symbol *sym;
  252. double pcnt;
  253. syme = rb_entry(nd, struct sym_entry, rb_node);
  254. sym = sym_entry__symbol(syme);
  255. if (++printed > top.print_entries ||
  256. (int)syme->snap_count < top.count_filter)
  257. continue;
  258. pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
  259. sum_ksamples));
  260. if (top.evlist->nr_entries == 1 || !top.display_weighted)
  261. printf("%20.2f ", syme->weight);
  262. else
  263. printf("%9.1f %10ld ", syme->weight, syme->snap_count);
  264. percent_color_fprintf(stdout, "%4.1f%%", pcnt);
  265. if (verbose)
  266. printf(" %016" PRIx64, sym->start);
  267. printf(" %-*.*s", sym_width, sym_width, sym->name);
  268. printf(" %-*.*s\n", dso_width, dso_width,
  269. dso_width >= syme->map->dso->long_name_len ?
  270. syme->map->dso->long_name :
  271. syme->map->dso->short_name);
  272. }
  273. }
  274. static void prompt_integer(int *target, const char *msg)
  275. {
  276. char *buf = malloc(0), *p;
  277. size_t dummy = 0;
  278. int tmp;
  279. fprintf(stdout, "\n%s: ", msg);
  280. if (getline(&buf, &dummy, stdin) < 0)
  281. return;
  282. p = strchr(buf, '\n');
  283. if (p)
  284. *p = 0;
  285. p = buf;
  286. while(*p) {
  287. if (!isdigit(*p))
  288. goto out_free;
  289. p++;
  290. }
  291. tmp = strtoul(buf, NULL, 10);
  292. *target = tmp;
  293. out_free:
  294. free(buf);
  295. }
  296. static void prompt_percent(int *target, const char *msg)
  297. {
  298. int tmp = 0;
  299. prompt_integer(&tmp, msg);
  300. if (tmp >= 0 && tmp <= 100)
  301. *target = tmp;
  302. }
  303. static void prompt_symbol(struct sym_entry **target, const char *msg)
  304. {
  305. char *buf = malloc(0), *p;
  306. struct sym_entry *syme = *target, *n, *found = NULL;
  307. size_t dummy = 0;
  308. /* zero counters of active symbol */
  309. if (syme) {
  310. __zero_source_counters(syme);
  311. *target = NULL;
  312. }
  313. fprintf(stdout, "\n%s: ", msg);
  314. if (getline(&buf, &dummy, stdin) < 0)
  315. goto out_free;
  316. p = strchr(buf, '\n');
  317. if (p)
  318. *p = 0;
  319. pthread_mutex_lock(&top.active_symbols_lock);
  320. syme = list_entry(top.active_symbols.next, struct sym_entry, node);
  321. pthread_mutex_unlock(&top.active_symbols_lock);
  322. list_for_each_entry_safe_from(syme, n, &top.active_symbols, node) {
  323. struct symbol *sym = sym_entry__symbol(syme);
  324. if (!strcmp(buf, sym->name)) {
  325. found = syme;
  326. break;
  327. }
  328. }
  329. if (!found) {
  330. fprintf(stderr, "Sorry, %s is not active.\n", buf);
  331. sleep(1);
  332. return;
  333. } else
  334. parse_source(found);
  335. out_free:
  336. free(buf);
  337. }
  338. static void print_mapped_keys(void)
  339. {
  340. char *name = NULL;
  341. if (top.sym_filter_entry) {
  342. struct symbol *sym = sym_entry__symbol(top.sym_filter_entry);
  343. name = sym->name;
  344. }
  345. fprintf(stdout, "\nMapped keys:\n");
  346. fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", top.delay_secs);
  347. fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", top.print_entries);
  348. if (top.evlist->nr_entries > 1)
  349. fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(top.sym_evsel));
  350. fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", top.count_filter);
  351. fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
  352. fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
  353. fprintf(stdout, "\t[S] stop annotation.\n");
  354. if (top.evlist->nr_entries > 1)
  355. fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", top.display_weighted ? 1 : 0);
  356. fprintf(stdout,
  357. "\t[K] hide kernel_symbols symbols. \t(%s)\n",
  358. top.hide_kernel_symbols ? "yes" : "no");
  359. fprintf(stdout,
  360. "\t[U] hide user symbols. \t(%s)\n",
  361. top.hide_user_symbols ? "yes" : "no");
  362. fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", top.zero ? 1 : 0);
  363. fprintf(stdout, "\t[qQ] quit.\n");
  364. }
  365. static int key_mapped(int c)
  366. {
  367. switch (c) {
  368. case 'd':
  369. case 'e':
  370. case 'f':
  371. case 'z':
  372. case 'q':
  373. case 'Q':
  374. case 'K':
  375. case 'U':
  376. case 'F':
  377. case 's':
  378. case 'S':
  379. return 1;
  380. case 'E':
  381. case 'w':
  382. return top.evlist->nr_entries > 1 ? 1 : 0;
  383. default:
  384. break;
  385. }
  386. return 0;
  387. }
  388. static void handle_keypress(struct perf_session *session, int c)
  389. {
  390. if (!key_mapped(c)) {
  391. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  392. struct termios tc, save;
  393. print_mapped_keys();
  394. fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
  395. fflush(stdout);
  396. tcgetattr(0, &save);
  397. tc = save;
  398. tc.c_lflag &= ~(ICANON | ECHO);
  399. tc.c_cc[VMIN] = 0;
  400. tc.c_cc[VTIME] = 0;
  401. tcsetattr(0, TCSANOW, &tc);
  402. poll(&stdin_poll, 1, -1);
  403. c = getc(stdin);
  404. tcsetattr(0, TCSAFLUSH, &save);
  405. if (!key_mapped(c))
  406. return;
  407. }
  408. switch (c) {
  409. case 'd':
  410. prompt_integer(&top.delay_secs, "Enter display delay");
  411. if (top.delay_secs < 1)
  412. top.delay_secs = 1;
  413. break;
  414. case 'e':
  415. prompt_integer(&top.print_entries, "Enter display entries (lines)");
  416. if (top.print_entries == 0) {
  417. sig_winch_handler(SIGWINCH);
  418. signal(SIGWINCH, sig_winch_handler);
  419. } else
  420. signal(SIGWINCH, SIG_DFL);
  421. break;
  422. case 'E':
  423. if (top.evlist->nr_entries > 1) {
  424. /* Select 0 as the default event: */
  425. int counter = 0;
  426. fprintf(stderr, "\nAvailable events:");
  427. list_for_each_entry(top.sym_evsel, &top.evlist->entries, node)
  428. fprintf(stderr, "\n\t%d %s", top.sym_evsel->idx, event_name(top.sym_evsel));
  429. prompt_integer(&counter, "Enter details event counter");
  430. if (counter >= top.evlist->nr_entries) {
  431. top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
  432. fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(top.sym_evsel));
  433. sleep(1);
  434. break;
  435. }
  436. list_for_each_entry(top.sym_evsel, &top.evlist->entries, node)
  437. if (top.sym_evsel->idx == counter)
  438. break;
  439. } else
  440. top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
  441. break;
  442. case 'f':
  443. prompt_integer(&top.count_filter, "Enter display event count filter");
  444. break;
  445. case 'F':
  446. prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
  447. break;
  448. case 'K':
  449. top.hide_kernel_symbols = !top.hide_kernel_symbols;
  450. break;
  451. case 'q':
  452. case 'Q':
  453. printf("exiting.\n");
  454. if (dump_symtab)
  455. perf_session__fprintf_dsos(session, stderr);
  456. exit(0);
  457. case 's':
  458. prompt_symbol(&top.sym_filter_entry, "Enter details symbol");
  459. break;
  460. case 'S':
  461. if (!top.sym_filter_entry)
  462. break;
  463. else {
  464. struct sym_entry *syme = top.sym_filter_entry;
  465. top.sym_filter_entry = NULL;
  466. __zero_source_counters(syme);
  467. }
  468. break;
  469. case 'U':
  470. top.hide_user_symbols = !top.hide_user_symbols;
  471. break;
  472. case 'w':
  473. top.display_weighted = ~top.display_weighted;
  474. break;
  475. case 'z':
  476. top.zero = !top.zero;
  477. break;
  478. default:
  479. break;
  480. }
  481. }
  482. static void *display_thread_tui(void *arg __used)
  483. {
  484. int err = 0;
  485. pthread_mutex_lock(&top.active_symbols_lock);
  486. while (list_empty(&top.active_symbols)) {
  487. err = pthread_cond_wait(&top.active_symbols_cond,
  488. &top.active_symbols_lock);
  489. if (err)
  490. break;
  491. }
  492. pthread_mutex_unlock(&top.active_symbols_lock);
  493. if (!err)
  494. perf_top__tui_browser(&top);
  495. exit_browser(0);
  496. exit(0);
  497. return NULL;
  498. }
  499. static void *display_thread(void *arg __used)
  500. {
  501. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  502. struct termios tc, save;
  503. int delay_msecs, c;
  504. struct perf_session *session = (struct perf_session *) arg;
  505. tcgetattr(0, &save);
  506. tc = save;
  507. tc.c_lflag &= ~(ICANON | ECHO);
  508. tc.c_cc[VMIN] = 0;
  509. tc.c_cc[VTIME] = 0;
  510. repeat:
  511. delay_msecs = top.delay_secs * 1000;
  512. tcsetattr(0, TCSANOW, &tc);
  513. /* trash return*/
  514. getc(stdin);
  515. do {
  516. print_sym_table(session);
  517. } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
  518. c = getc(stdin);
  519. tcsetattr(0, TCSAFLUSH, &save);
  520. handle_keypress(session, c);
  521. goto repeat;
  522. return NULL;
  523. }
  524. /* Tag samples to be skipped. */
  525. static const char *skip_symbols[] = {
  526. "default_idle",
  527. "native_safe_halt",
  528. "cpu_idle",
  529. "enter_idle",
  530. "exit_idle",
  531. "mwait_idle",
  532. "mwait_idle_with_hints",
  533. "poll_idle",
  534. "ppc64_runlatch_off",
  535. "pseries_dedicated_idle_sleep",
  536. NULL
  537. };
  538. static int symbol_filter(struct map *map, struct symbol *sym)
  539. {
  540. struct sym_entry *syme;
  541. const char *name = sym->name;
  542. int i;
  543. /*
  544. * ppc64 uses function descriptors and appends a '.' to the
  545. * start of every instruction address. Remove it.
  546. */
  547. if (name[0] == '.')
  548. name++;
  549. if (!strcmp(name, "_text") ||
  550. !strcmp(name, "_etext") ||
  551. !strcmp(name, "_sinittext") ||
  552. !strncmp("init_module", name, 11) ||
  553. !strncmp("cleanup_module", name, 14) ||
  554. strstr(name, "_text_start") ||
  555. strstr(name, "_text_end"))
  556. return 1;
  557. syme = symbol__priv(sym);
  558. syme->map = map;
  559. symbol__annotate_init(map, sym);
  560. if (!top.sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
  561. /* schedule initial sym_filter_entry setup */
  562. sym_filter_entry_sched = syme;
  563. sym_filter = NULL;
  564. }
  565. for (i = 0; skip_symbols[i]; i++) {
  566. if (!strcmp(skip_symbols[i], name)) {
  567. sym->ignore = true;
  568. break;
  569. }
  570. }
  571. return 0;
  572. }
  573. static void perf_event__process_sample(const union perf_event *event,
  574. struct perf_sample *sample,
  575. struct perf_session *session)
  576. {
  577. u64 ip = event->ip.ip;
  578. struct sym_entry *syme;
  579. struct addr_location al;
  580. struct machine *machine;
  581. u8 origin = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  582. ++top.samples;
  583. switch (origin) {
  584. case PERF_RECORD_MISC_USER:
  585. ++top.us_samples;
  586. if (top.hide_user_symbols)
  587. return;
  588. machine = perf_session__find_host_machine(session);
  589. break;
  590. case PERF_RECORD_MISC_KERNEL:
  591. ++top.kernel_samples;
  592. if (top.hide_kernel_symbols)
  593. return;
  594. machine = perf_session__find_host_machine(session);
  595. break;
  596. case PERF_RECORD_MISC_GUEST_KERNEL:
  597. ++top.guest_kernel_samples;
  598. machine = perf_session__find_machine(session, event->ip.pid);
  599. break;
  600. case PERF_RECORD_MISC_GUEST_USER:
  601. ++top.guest_us_samples;
  602. /*
  603. * TODO: we don't process guest user from host side
  604. * except simple counting.
  605. */
  606. return;
  607. default:
  608. return;
  609. }
  610. if (!machine && perf_guest) {
  611. pr_err("Can't find guest [%d]'s kernel information\n",
  612. event->ip.pid);
  613. return;
  614. }
  615. if (event->header.misc & PERF_RECORD_MISC_EXACT_IP)
  616. top.exact_samples++;
  617. if (perf_event__preprocess_sample(event, session, &al, sample,
  618. symbol_filter) < 0 ||
  619. al.filtered)
  620. return;
  621. if (al.sym == NULL) {
  622. /*
  623. * As we do lazy loading of symtabs we only will know if the
  624. * specified vmlinux file is invalid when we actually have a
  625. * hit in kernel space and then try to load it. So if we get
  626. * here and there are _no_ symbols in the DSO backing the
  627. * kernel map, bail out.
  628. *
  629. * We may never get here, for instance, if we use -K/
  630. * --hide-kernel-symbols, even if the user specifies an
  631. * invalid --vmlinux ;-)
  632. */
  633. if (al.map == machine->vmlinux_maps[MAP__FUNCTION] &&
  634. RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
  635. ui__warning("The %s file can't be used\n",
  636. symbol_conf.vmlinux_name);
  637. exit_browser(0);
  638. exit(1);
  639. }
  640. return;
  641. }
  642. /* let's see, whether we need to install initial sym_filter_entry */
  643. if (sym_filter_entry_sched) {
  644. top.sym_filter_entry = sym_filter_entry_sched;
  645. sym_filter_entry_sched = NULL;
  646. if (parse_source(top.sym_filter_entry) < 0) {
  647. struct symbol *sym = sym_entry__symbol(top.sym_filter_entry);
  648. pr_err("Can't annotate %s", sym->name);
  649. if (top.sym_filter_entry->map->dso->symtab_type == SYMTAB__KALLSYMS) {
  650. pr_err(": No vmlinux file was found in the path:\n");
  651. machine__fprintf_vmlinux_path(machine, stderr);
  652. } else
  653. pr_err(".\n");
  654. exit(1);
  655. }
  656. }
  657. syme = symbol__priv(al.sym);
  658. if (!al.sym->ignore) {
  659. struct perf_evsel *evsel;
  660. evsel = perf_evlist__id2evsel(top.evlist, sample->id);
  661. assert(evsel != NULL);
  662. syme->count[evsel->idx]++;
  663. record_precise_ip(syme, evsel->idx, ip);
  664. pthread_mutex_lock(&top.active_symbols_lock);
  665. if (list_empty(&syme->node) || !syme->node.next) {
  666. static bool first = true;
  667. __list_insert_active_sym(syme);
  668. if (first) {
  669. pthread_cond_broadcast(&top.active_symbols_cond);
  670. first = false;
  671. }
  672. }
  673. pthread_mutex_unlock(&top.active_symbols_lock);
  674. }
  675. }
  676. static void perf_session__mmap_read_idx(struct perf_session *self, int idx)
  677. {
  678. struct perf_sample sample;
  679. union perf_event *event;
  680. int ret;
  681. while ((event = perf_evlist__mmap_read(top.evlist, idx)) != NULL) {
  682. ret = perf_session__parse_sample(self, event, &sample);
  683. if (ret) {
  684. pr_err("Can't parse sample, err = %d\n", ret);
  685. continue;
  686. }
  687. if (event->header.type == PERF_RECORD_SAMPLE)
  688. perf_event__process_sample(event, &sample, self);
  689. else
  690. perf_event__process(event, &sample, self);
  691. }
  692. }
  693. static void perf_session__mmap_read(struct perf_session *self)
  694. {
  695. int i;
  696. for (i = 0; i < top.evlist->nr_mmaps; i++)
  697. perf_session__mmap_read_idx(self, i);
  698. }
  699. static void start_counters(struct perf_evlist *evlist)
  700. {
  701. struct perf_evsel *counter;
  702. list_for_each_entry(counter, &evlist->entries, node) {
  703. struct perf_event_attr *attr = &counter->attr;
  704. attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  705. if (top.freq) {
  706. attr->sample_type |= PERF_SAMPLE_PERIOD;
  707. attr->freq = 1;
  708. attr->sample_freq = top.freq;
  709. }
  710. if (evlist->nr_entries > 1) {
  711. attr->sample_type |= PERF_SAMPLE_ID;
  712. attr->read_format |= PERF_FORMAT_ID;
  713. }
  714. attr->mmap = 1;
  715. attr->inherit = inherit;
  716. try_again:
  717. if (perf_evsel__open(counter, top.evlist->cpus,
  718. top.evlist->threads, group) < 0) {
  719. int err = errno;
  720. if (err == EPERM || err == EACCES) {
  721. ui__warning_paranoid();
  722. goto out_err;
  723. }
  724. /*
  725. * If it's cycles then fall back to hrtimer
  726. * based cpu-clock-tick sw counter, which
  727. * is always available even if no PMU support:
  728. */
  729. if (attr->type == PERF_TYPE_HARDWARE &&
  730. attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  731. if (verbose)
  732. ui__warning("Cycles event not supported,\n"
  733. "trying to fall back to cpu-clock-ticks\n");
  734. attr->type = PERF_TYPE_SOFTWARE;
  735. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  736. goto try_again;
  737. }
  738. if (err == ENOENT) {
  739. ui__warning("The %s event is not supported.\n",
  740. event_name(counter));
  741. goto out_err;
  742. }
  743. ui__warning("The sys_perf_event_open() syscall "
  744. "returned with %d (%s). /bin/dmesg "
  745. "may provide additional information.\n"
  746. "No CONFIG_PERF_EVENTS=y kernel support "
  747. "configured?\n", err, strerror(err));
  748. goto out_err;
  749. }
  750. }
  751. if (perf_evlist__mmap(evlist, mmap_pages, false) < 0) {
  752. ui__warning("Failed to mmap with %d (%s)\n",
  753. errno, strerror(errno));
  754. goto out_err;
  755. }
  756. return;
  757. out_err:
  758. exit_browser(0);
  759. exit(0);
  760. }
  761. static int __cmd_top(void)
  762. {
  763. pthread_t thread;
  764. int ret __used;
  765. /*
  766. * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
  767. * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
  768. */
  769. struct perf_session *session = perf_session__new(NULL, O_WRONLY, false, false, NULL);
  770. if (session == NULL)
  771. return -ENOMEM;
  772. if (top.target_tid != -1)
  773. perf_event__synthesize_thread_map(top.evlist->threads,
  774. perf_event__process, session);
  775. else
  776. perf_event__synthesize_threads(perf_event__process, session);
  777. start_counters(top.evlist);
  778. session->evlist = top.evlist;
  779. perf_session__update_sample_type(session);
  780. /* Wait for a minimal set of events before starting the snapshot */
  781. poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
  782. perf_session__mmap_read(session);
  783. if (pthread_create(&thread, NULL, (use_browser > 0 ? display_thread_tui :
  784. display_thread), session)) {
  785. printf("Could not create display thread.\n");
  786. exit(-1);
  787. }
  788. if (realtime_prio) {
  789. struct sched_param param;
  790. param.sched_priority = realtime_prio;
  791. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  792. printf("Could not set realtime priority.\n");
  793. exit(-1);
  794. }
  795. }
  796. while (1) {
  797. u64 hits = top.samples;
  798. perf_session__mmap_read(session);
  799. if (hits == top.samples)
  800. ret = poll(top.evlist->pollfd, top.evlist->nr_fds, 100);
  801. }
  802. return 0;
  803. }
  804. static const char * const top_usage[] = {
  805. "perf top [<options>]",
  806. NULL
  807. };
  808. static const struct option options[] = {
  809. OPT_CALLBACK('e', "event", &top.evlist, "event",
  810. "event selector. use 'perf list' to list available events",
  811. parse_events),
  812. OPT_INTEGER('c', "count", &default_interval,
  813. "event period to sample"),
  814. OPT_INTEGER('p', "pid", &top.target_pid,
  815. "profile events on existing process id"),
  816. OPT_INTEGER('t', "tid", &top.target_tid,
  817. "profile events on existing thread id"),
  818. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  819. "system-wide collection from all CPUs"),
  820. OPT_STRING('C', "cpu", &top.cpu_list, "cpu",
  821. "list of cpus to monitor"),
  822. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  823. "file", "vmlinux pathname"),
  824. OPT_BOOLEAN('K', "hide_kernel_symbols", &top.hide_kernel_symbols,
  825. "hide kernel symbols"),
  826. OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"),
  827. OPT_INTEGER('r', "realtime", &realtime_prio,
  828. "collect data with this RT SCHED_FIFO priority"),
  829. OPT_INTEGER('d', "delay", &top.delay_secs,
  830. "number of seconds to delay between refreshes"),
  831. OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
  832. "dump the symbol table used for profiling"),
  833. OPT_INTEGER('f', "count-filter", &top.count_filter,
  834. "only display functions with more events than this"),
  835. OPT_BOOLEAN('g', "group", &group,
  836. "put the counters into a counter group"),
  837. OPT_BOOLEAN('i', "inherit", &inherit,
  838. "child tasks inherit counters"),
  839. OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
  840. "symbol to annotate"),
  841. OPT_BOOLEAN('z', "zero", &top.zero,
  842. "zero history across updates"),
  843. OPT_INTEGER('F', "freq", &top.freq,
  844. "profile at this frequency"),
  845. OPT_INTEGER('E', "entries", &top.print_entries,
  846. "display this many functions"),
  847. OPT_BOOLEAN('U', "hide_user_symbols", &top.hide_user_symbols,
  848. "hide user symbols"),
  849. OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
  850. OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
  851. OPT_INCR('v', "verbose", &verbose,
  852. "be more verbose (show counter open errors, etc)"),
  853. OPT_END()
  854. };
  855. int cmd_top(int argc, const char **argv, const char *prefix __used)
  856. {
  857. struct perf_evsel *pos;
  858. int status = -ENOMEM;
  859. top.evlist = perf_evlist__new(NULL, NULL);
  860. if (top.evlist == NULL)
  861. return -ENOMEM;
  862. page_size = sysconf(_SC_PAGE_SIZE);
  863. argc = parse_options(argc, argv, options, top_usage, 0);
  864. if (argc)
  865. usage_with_options(top_usage, options);
  866. /*
  867. * XXX For now start disabled, only using TUI if explicitely asked for.
  868. * Change that when handle_keys equivalent gets written, live annotation
  869. * done, etc.
  870. */
  871. use_browser = 0;
  872. if (use_stdio)
  873. use_browser = 0;
  874. else if (use_tui)
  875. use_browser = 1;
  876. setup_browser(false);
  877. /* CPU and PID are mutually exclusive */
  878. if (top.target_tid > 0 && top.cpu_list) {
  879. printf("WARNING: PID switch overriding CPU\n");
  880. sleep(1);
  881. top.cpu_list = NULL;
  882. }
  883. if (top.target_pid != -1)
  884. top.target_tid = top.target_pid;
  885. if (perf_evlist__create_maps(top.evlist, top.target_pid,
  886. top.target_tid, top.cpu_list) < 0)
  887. usage_with_options(top_usage, options);
  888. if (!top.evlist->nr_entries &&
  889. perf_evlist__add_default(top.evlist) < 0) {
  890. pr_err("Not enough memory for event selector list\n");
  891. return -ENOMEM;
  892. }
  893. if (top.delay_secs < 1)
  894. top.delay_secs = 1;
  895. /*
  896. * User specified count overrides default frequency.
  897. */
  898. if (default_interval)
  899. top.freq = 0;
  900. else if (top.freq) {
  901. default_interval = top.freq;
  902. } else {
  903. fprintf(stderr, "frequency and count are zero, aborting\n");
  904. exit(EXIT_FAILURE);
  905. }
  906. list_for_each_entry(pos, &top.evlist->entries, node) {
  907. if (perf_evsel__alloc_fd(pos, top.evlist->cpus->nr,
  908. top.evlist->threads->nr) < 0)
  909. goto out_free_fd;
  910. /*
  911. * Fill in the ones not specifically initialized via -c:
  912. */
  913. if (pos->attr.sample_period)
  914. continue;
  915. pos->attr.sample_period = default_interval;
  916. }
  917. if (perf_evlist__alloc_pollfd(top.evlist) < 0 ||
  918. perf_evlist__alloc_mmap(top.evlist) < 0)
  919. goto out_free_fd;
  920. top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
  921. symbol_conf.priv_size = (sizeof(struct sym_entry) + sizeof(struct annotation) +
  922. (top.evlist->nr_entries + 1) * sizeof(unsigned long));
  923. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  924. if (symbol__init() < 0)
  925. return -1;
  926. get_term_dimensions(&winsize);
  927. if (top.print_entries == 0) {
  928. update_print_entries(&winsize);
  929. signal(SIGWINCH, sig_winch_handler);
  930. }
  931. status = __cmd_top();
  932. out_free_fd:
  933. perf_evlist__delete(top.evlist);
  934. return status;
  935. }