builtin-top.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*
  2. * builtin-top.c
  3. *
  4. * Builtin top command: Display a continuously updated profile of
  5. * any workload, CPU or specific PID.
  6. *
  7. * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
  8. *
  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/color.h"
  22. #include "util/session.h"
  23. #include "util/symbol.h"
  24. #include "util/thread.h"
  25. #include "util/util.h"
  26. #include <linux/rbtree.h>
  27. #include "util/parse-options.h"
  28. #include "util/parse-events.h"
  29. #include "util/cpumap.h"
  30. #include "util/debug.h"
  31. #include <assert.h>
  32. #include <fcntl.h>
  33. #include <stdio.h>
  34. #include <termios.h>
  35. #include <unistd.h>
  36. #include <errno.h>
  37. #include <time.h>
  38. #include <sched.h>
  39. #include <pthread.h>
  40. #include <sys/syscall.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/poll.h>
  43. #include <sys/prctl.h>
  44. #include <sys/wait.h>
  45. #include <sys/uio.h>
  46. #include <sys/mman.h>
  47. #include <linux/unistd.h>
  48. #include <linux/types.h>
  49. static int fd[MAX_NR_CPUS][MAX_COUNTERS];
  50. static int system_wide = 0;
  51. static int default_interval = 0;
  52. static int count_filter = 5;
  53. static int print_entries;
  54. static int target_pid = -1;
  55. static int inherit = 0;
  56. static int profile_cpu = -1;
  57. static int nr_cpus = 0;
  58. static unsigned int realtime_prio = 0;
  59. static int group = 0;
  60. static unsigned int page_size;
  61. static unsigned int mmap_pages = 16;
  62. static int freq = 1000; /* 1 KHz */
  63. static int delay_secs = 2;
  64. static int zero = 0;
  65. static int dump_symtab = 0;
  66. static bool hide_kernel_symbols = false;
  67. static bool hide_user_symbols = false;
  68. static struct winsize winsize;
  69. /*
  70. * Source
  71. */
  72. struct source_line {
  73. u64 eip;
  74. unsigned long count[MAX_COUNTERS];
  75. char *line;
  76. struct source_line *next;
  77. };
  78. static char *sym_filter = NULL;
  79. struct sym_entry *sym_filter_entry = NULL;
  80. struct sym_entry *sym_filter_entry_sched = NULL;
  81. static int sym_pcnt_filter = 5;
  82. static int sym_counter = 0;
  83. static int display_weighted = -1;
  84. /*
  85. * Symbols
  86. */
  87. struct sym_entry_source {
  88. struct source_line *source;
  89. struct source_line *lines;
  90. struct source_line **lines_tail;
  91. pthread_mutex_t lock;
  92. };
  93. struct sym_entry {
  94. struct rb_node rb_node;
  95. struct list_head node;
  96. unsigned long snap_count;
  97. double weight;
  98. int skip;
  99. u16 name_len;
  100. u8 origin;
  101. struct map *map;
  102. struct sym_entry_source *src;
  103. unsigned long count[0];
  104. };
  105. /*
  106. * Source functions
  107. */
  108. static inline struct symbol *sym_entry__symbol(struct sym_entry *self)
  109. {
  110. return ((void *)self) + symbol_conf.priv_size;
  111. }
  112. static void get_term_dimensions(struct winsize *ws)
  113. {
  114. char *s = getenv("LINES");
  115. if (s != NULL) {
  116. ws->ws_row = atoi(s);
  117. s = getenv("COLUMNS");
  118. if (s != NULL) {
  119. ws->ws_col = atoi(s);
  120. if (ws->ws_row && ws->ws_col)
  121. return;
  122. }
  123. }
  124. #ifdef TIOCGWINSZ
  125. if (ioctl(1, TIOCGWINSZ, ws) == 0 &&
  126. ws->ws_row && ws->ws_col)
  127. return;
  128. #endif
  129. ws->ws_row = 25;
  130. ws->ws_col = 80;
  131. }
  132. static void update_print_entries(struct winsize *ws)
  133. {
  134. print_entries = ws->ws_row;
  135. if (print_entries > 9)
  136. print_entries -= 9;
  137. }
  138. static void sig_winch_handler(int sig __used)
  139. {
  140. get_term_dimensions(&winsize);
  141. update_print_entries(&winsize);
  142. }
  143. static void parse_source(struct sym_entry *syme)
  144. {
  145. struct symbol *sym;
  146. struct sym_entry_source *source;
  147. struct map *map;
  148. FILE *file;
  149. char command[PATH_MAX*2];
  150. const char *path;
  151. u64 len;
  152. if (!syme)
  153. return;
  154. if (syme->src == NULL) {
  155. syme->src = zalloc(sizeof(*source));
  156. if (syme->src == NULL)
  157. return;
  158. pthread_mutex_init(&syme->src->lock, NULL);
  159. }
  160. source = syme->src;
  161. if (source->lines) {
  162. pthread_mutex_lock(&source->lock);
  163. goto out_assign;
  164. }
  165. sym = sym_entry__symbol(syme);
  166. map = syme->map;
  167. path = map->dso->long_name;
  168. len = sym->end - sym->start;
  169. sprintf(command,
  170. "objdump --start-address=%#0*Lx --stop-address=%#0*Lx -dS %s",
  171. BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
  172. BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
  173. file = popen(command, "r");
  174. if (!file)
  175. return;
  176. pthread_mutex_lock(&source->lock);
  177. source->lines_tail = &source->lines;
  178. while (!feof(file)) {
  179. struct source_line *src;
  180. size_t dummy = 0;
  181. char *c, *sep;
  182. src = malloc(sizeof(struct source_line));
  183. assert(src != NULL);
  184. memset(src, 0, sizeof(struct source_line));
  185. if (getline(&src->line, &dummy, file) < 0)
  186. break;
  187. if (!src->line)
  188. break;
  189. c = strchr(src->line, '\n');
  190. if (c)
  191. *c = 0;
  192. src->next = NULL;
  193. *source->lines_tail = src;
  194. source->lines_tail = &src->next;
  195. src->eip = strtoull(src->line, &sep, 16);
  196. if (*sep == ':')
  197. src->eip = map__objdump_2ip(map, src->eip);
  198. else /* this line has no ip info (e.g. source line) */
  199. src->eip = 0;
  200. }
  201. pclose(file);
  202. out_assign:
  203. sym_filter_entry = syme;
  204. pthread_mutex_unlock(&source->lock);
  205. }
  206. static void __zero_source_counters(struct sym_entry *syme)
  207. {
  208. int i;
  209. struct source_line *line;
  210. line = syme->src->lines;
  211. while (line) {
  212. for (i = 0; i < nr_counters; i++)
  213. line->count[i] = 0;
  214. line = line->next;
  215. }
  216. }
  217. static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
  218. {
  219. struct source_line *line;
  220. if (syme != sym_filter_entry)
  221. return;
  222. if (pthread_mutex_trylock(&syme->src->lock))
  223. return;
  224. if (syme->src == NULL || syme->src->source == NULL)
  225. goto out_unlock;
  226. for (line = syme->src->lines; line; line = line->next) {
  227. /* skip lines without IP info */
  228. if (line->eip == 0)
  229. continue;
  230. if (line->eip == ip) {
  231. line->count[counter]++;
  232. break;
  233. }
  234. if (line->eip > ip)
  235. break;
  236. }
  237. out_unlock:
  238. pthread_mutex_unlock(&syme->src->lock);
  239. }
  240. #define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
  241. static void lookup_sym_source(struct sym_entry *syme)
  242. {
  243. struct symbol *symbol = sym_entry__symbol(syme);
  244. struct source_line *line;
  245. char pattern[PATTERN_LEN + 1];
  246. sprintf(pattern, "%0*Lx <", BITS_PER_LONG / 4,
  247. map__rip_2objdump(syme->map, symbol->start));
  248. pthread_mutex_lock(&syme->src->lock);
  249. for (line = syme->src->lines; line; line = line->next) {
  250. if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
  251. syme->src->source = line;
  252. break;
  253. }
  254. }
  255. pthread_mutex_unlock(&syme->src->lock);
  256. }
  257. static void show_lines(struct source_line *queue, int count, int total)
  258. {
  259. int i;
  260. struct source_line *line;
  261. line = queue;
  262. for (i = 0; i < count; i++) {
  263. float pcnt = 100.0*(float)line->count[sym_counter]/(float)total;
  264. printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line);
  265. line = line->next;
  266. }
  267. }
  268. #define TRACE_COUNT 3
  269. static void show_details(struct sym_entry *syme)
  270. {
  271. struct symbol *symbol;
  272. struct source_line *line;
  273. struct source_line *line_queue = NULL;
  274. int displayed = 0;
  275. int line_queue_count = 0, total = 0, more = 0;
  276. if (!syme)
  277. return;
  278. if (!syme->src->source)
  279. lookup_sym_source(syme);
  280. if (!syme->src->source)
  281. return;
  282. symbol = sym_entry__symbol(syme);
  283. printf("Showing %s for %s\n", event_name(sym_counter), symbol->name);
  284. printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
  285. pthread_mutex_lock(&syme->src->lock);
  286. line = syme->src->source;
  287. while (line) {
  288. total += line->count[sym_counter];
  289. line = line->next;
  290. }
  291. line = syme->src->source;
  292. while (line) {
  293. float pcnt = 0.0;
  294. if (!line_queue_count)
  295. line_queue = line;
  296. line_queue_count++;
  297. if (line->count[sym_counter])
  298. pcnt = 100.0 * line->count[sym_counter] / (float)total;
  299. if (pcnt >= (float)sym_pcnt_filter) {
  300. if (displayed <= print_entries)
  301. show_lines(line_queue, line_queue_count, total);
  302. else more++;
  303. displayed += line_queue_count;
  304. line_queue_count = 0;
  305. line_queue = NULL;
  306. } else if (line_queue_count > TRACE_COUNT) {
  307. line_queue = line_queue->next;
  308. line_queue_count--;
  309. }
  310. line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8;
  311. line = line->next;
  312. }
  313. pthread_mutex_unlock(&syme->src->lock);
  314. if (more)
  315. printf("%d lines not displayed, maybe increase display entries [e]\n", more);
  316. }
  317. /*
  318. * Symbols will be added here in event__process_sample and will get out
  319. * after decayed.
  320. */
  321. static LIST_HEAD(active_symbols);
  322. static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
  323. /*
  324. * Ordering weight: count-1 * count-2 * ... / count-n
  325. */
  326. static double sym_weight(const struct sym_entry *sym)
  327. {
  328. double weight = sym->snap_count;
  329. int counter;
  330. if (!display_weighted)
  331. return weight;
  332. for (counter = 1; counter < nr_counters-1; counter++)
  333. weight *= sym->count[counter];
  334. weight /= (sym->count[counter] + 1);
  335. return weight;
  336. }
  337. static long samples;
  338. static long userspace_samples;
  339. static const char CONSOLE_CLEAR[] = "";
  340. static void __list_insert_active_sym(struct sym_entry *syme)
  341. {
  342. list_add(&syme->node, &active_symbols);
  343. }
  344. static void list_remove_active_sym(struct sym_entry *syme)
  345. {
  346. pthread_mutex_lock(&active_symbols_lock);
  347. list_del_init(&syme->node);
  348. pthread_mutex_unlock(&active_symbols_lock);
  349. }
  350. static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
  351. {
  352. struct rb_node **p = &tree->rb_node;
  353. struct rb_node *parent = NULL;
  354. struct sym_entry *iter;
  355. while (*p != NULL) {
  356. parent = *p;
  357. iter = rb_entry(parent, struct sym_entry, rb_node);
  358. if (se->weight > iter->weight)
  359. p = &(*p)->rb_left;
  360. else
  361. p = &(*p)->rb_right;
  362. }
  363. rb_link_node(&se->rb_node, parent, p);
  364. rb_insert_color(&se->rb_node, tree);
  365. }
  366. static void print_sym_table(void)
  367. {
  368. int printed = 0, j;
  369. int counter, snap = !display_weighted ? sym_counter : 0;
  370. float samples_per_sec = samples/delay_secs;
  371. float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
  372. float sum_ksamples = 0.0;
  373. struct sym_entry *syme, *n;
  374. struct rb_root tmp = RB_ROOT;
  375. struct rb_node *nd;
  376. int sym_width = 0, dso_width = 0, max_dso_width;
  377. const int win_width = winsize.ws_col - 1;
  378. samples = userspace_samples = 0;
  379. /* Sort the active symbols */
  380. pthread_mutex_lock(&active_symbols_lock);
  381. syme = list_entry(active_symbols.next, struct sym_entry, node);
  382. pthread_mutex_unlock(&active_symbols_lock);
  383. list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
  384. syme->snap_count = syme->count[snap];
  385. if (syme->snap_count != 0) {
  386. if ((hide_user_symbols &&
  387. syme->origin == PERF_RECORD_MISC_USER) ||
  388. (hide_kernel_symbols &&
  389. syme->origin == PERF_RECORD_MISC_KERNEL)) {
  390. list_remove_active_sym(syme);
  391. continue;
  392. }
  393. syme->weight = sym_weight(syme);
  394. rb_insert_active_sym(&tmp, syme);
  395. sum_ksamples += syme->snap_count;
  396. for (j = 0; j < nr_counters; j++)
  397. syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
  398. } else
  399. list_remove_active_sym(syme);
  400. }
  401. puts(CONSOLE_CLEAR);
  402. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  403. printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
  404. samples_per_sec,
  405. 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
  406. if (nr_counters == 1 || !display_weighted) {
  407. printf("%Ld", (u64)attrs[0].sample_period);
  408. if (freq)
  409. printf("Hz ");
  410. else
  411. printf(" ");
  412. }
  413. if (!display_weighted)
  414. printf("%s", event_name(sym_counter));
  415. else for (counter = 0; counter < nr_counters; counter++) {
  416. if (counter)
  417. printf("/");
  418. printf("%s", event_name(counter));
  419. }
  420. printf( "], ");
  421. if (target_pid != -1)
  422. printf(" (target_pid: %d", target_pid);
  423. else
  424. printf(" (all");
  425. if (profile_cpu != -1)
  426. printf(", cpu: %d)\n", profile_cpu);
  427. else {
  428. if (target_pid != -1)
  429. printf(")\n");
  430. else
  431. printf(", %d CPUs)\n", nr_cpus);
  432. }
  433. printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
  434. if (sym_filter_entry) {
  435. show_details(sym_filter_entry);
  436. return;
  437. }
  438. /*
  439. * Find the longest symbol name that will be displayed
  440. */
  441. for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
  442. syme = rb_entry(nd, struct sym_entry, rb_node);
  443. if (++printed > print_entries ||
  444. (int)syme->snap_count < count_filter)
  445. continue;
  446. if (syme->map->dso->long_name_len > dso_width)
  447. dso_width = syme->map->dso->long_name_len;
  448. if (syme->name_len > sym_width)
  449. sym_width = syme->name_len;
  450. }
  451. printed = 0;
  452. max_dso_width = winsize.ws_col - sym_width - 29;
  453. if (dso_width > max_dso_width)
  454. dso_width = max_dso_width;
  455. putchar('\n');
  456. if (nr_counters == 1)
  457. printf(" samples pcnt");
  458. else
  459. printf(" weight samples pcnt");
  460. if (verbose)
  461. printf(" RIP ");
  462. printf(" %-*.*s DSO\n", sym_width, sym_width, "function");
  463. printf(" %s _______ _____",
  464. nr_counters == 1 ? " " : "______");
  465. if (verbose)
  466. printf(" ________________");
  467. printf(" %-*.*s", sym_width, sym_width, graph_line);
  468. printf(" %-*.*s", dso_width, dso_width, graph_line);
  469. puts("\n");
  470. for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
  471. struct symbol *sym;
  472. double pcnt;
  473. syme = rb_entry(nd, struct sym_entry, rb_node);
  474. sym = sym_entry__symbol(syme);
  475. if (++printed > print_entries || (int)syme->snap_count < count_filter)
  476. continue;
  477. pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
  478. sum_ksamples));
  479. if (nr_counters == 1 || !display_weighted)
  480. printf("%20.2f ", syme->weight);
  481. else
  482. printf("%9.1f %10ld ", syme->weight, syme->snap_count);
  483. percent_color_fprintf(stdout, "%4.1f%%", pcnt);
  484. if (verbose)
  485. printf(" %016llx", sym->start);
  486. printf(" %-*.*s", sym_width, sym_width, sym->name);
  487. printf(" %-*.*s\n", dso_width, dso_width,
  488. dso_width >= syme->map->dso->long_name_len ?
  489. syme->map->dso->long_name :
  490. syme->map->dso->short_name);
  491. }
  492. }
  493. static void prompt_integer(int *target, const char *msg)
  494. {
  495. char *buf = malloc(0), *p;
  496. size_t dummy = 0;
  497. int tmp;
  498. fprintf(stdout, "\n%s: ", msg);
  499. if (getline(&buf, &dummy, stdin) < 0)
  500. return;
  501. p = strchr(buf, '\n');
  502. if (p)
  503. *p = 0;
  504. p = buf;
  505. while(*p) {
  506. if (!isdigit(*p))
  507. goto out_free;
  508. p++;
  509. }
  510. tmp = strtoul(buf, NULL, 10);
  511. *target = tmp;
  512. out_free:
  513. free(buf);
  514. }
  515. static void prompt_percent(int *target, const char *msg)
  516. {
  517. int tmp = 0;
  518. prompt_integer(&tmp, msg);
  519. if (tmp >= 0 && tmp <= 100)
  520. *target = tmp;
  521. }
  522. static void prompt_symbol(struct sym_entry **target, const char *msg)
  523. {
  524. char *buf = malloc(0), *p;
  525. struct sym_entry *syme = *target, *n, *found = NULL;
  526. size_t dummy = 0;
  527. /* zero counters of active symbol */
  528. if (syme) {
  529. pthread_mutex_lock(&syme->src->lock);
  530. __zero_source_counters(syme);
  531. *target = NULL;
  532. pthread_mutex_unlock(&syme->src->lock);
  533. }
  534. fprintf(stdout, "\n%s: ", msg);
  535. if (getline(&buf, &dummy, stdin) < 0)
  536. goto out_free;
  537. p = strchr(buf, '\n');
  538. if (p)
  539. *p = 0;
  540. pthread_mutex_lock(&active_symbols_lock);
  541. syme = list_entry(active_symbols.next, struct sym_entry, node);
  542. pthread_mutex_unlock(&active_symbols_lock);
  543. list_for_each_entry_safe_from(syme, n, &active_symbols, node) {
  544. struct symbol *sym = sym_entry__symbol(syme);
  545. if (!strcmp(buf, sym->name)) {
  546. found = syme;
  547. break;
  548. }
  549. }
  550. if (!found) {
  551. fprintf(stderr, "Sorry, %s is not active.\n", buf);
  552. sleep(1);
  553. return;
  554. } else
  555. parse_source(found);
  556. out_free:
  557. free(buf);
  558. }
  559. static void print_mapped_keys(void)
  560. {
  561. char *name = NULL;
  562. if (sym_filter_entry) {
  563. struct symbol *sym = sym_entry__symbol(sym_filter_entry);
  564. name = sym->name;
  565. }
  566. fprintf(stdout, "\nMapped keys:\n");
  567. fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs);
  568. fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
  569. if (nr_counters > 1)
  570. fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter));
  571. fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
  572. fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter);
  573. fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL");
  574. fprintf(stdout, "\t[S] stop annotation.\n");
  575. if (nr_counters > 1)
  576. fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0);
  577. fprintf(stdout,
  578. "\t[K] hide kernel_symbols symbols. \t(%s)\n",
  579. hide_kernel_symbols ? "yes" : "no");
  580. fprintf(stdout,
  581. "\t[U] hide user symbols. \t(%s)\n",
  582. hide_user_symbols ? "yes" : "no");
  583. fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
  584. fprintf(stdout, "\t[qQ] quit.\n");
  585. }
  586. static int key_mapped(int c)
  587. {
  588. switch (c) {
  589. case 'd':
  590. case 'e':
  591. case 'f':
  592. case 'z':
  593. case 'q':
  594. case 'Q':
  595. case 'K':
  596. case 'U':
  597. case 'F':
  598. case 's':
  599. case 'S':
  600. return 1;
  601. case 'E':
  602. case 'w':
  603. return nr_counters > 1 ? 1 : 0;
  604. default:
  605. break;
  606. }
  607. return 0;
  608. }
  609. static void handle_keypress(int c)
  610. {
  611. if (!key_mapped(c)) {
  612. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  613. struct termios tc, save;
  614. print_mapped_keys();
  615. fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
  616. fflush(stdout);
  617. tcgetattr(0, &save);
  618. tc = save;
  619. tc.c_lflag &= ~(ICANON | ECHO);
  620. tc.c_cc[VMIN] = 0;
  621. tc.c_cc[VTIME] = 0;
  622. tcsetattr(0, TCSANOW, &tc);
  623. poll(&stdin_poll, 1, -1);
  624. c = getc(stdin);
  625. tcsetattr(0, TCSAFLUSH, &save);
  626. if (!key_mapped(c))
  627. return;
  628. }
  629. switch (c) {
  630. case 'd':
  631. prompt_integer(&delay_secs, "Enter display delay");
  632. if (delay_secs < 1)
  633. delay_secs = 1;
  634. break;
  635. case 'e':
  636. prompt_integer(&print_entries, "Enter display entries (lines)");
  637. if (print_entries == 0) {
  638. sig_winch_handler(SIGWINCH);
  639. signal(SIGWINCH, sig_winch_handler);
  640. } else
  641. signal(SIGWINCH, SIG_DFL);
  642. break;
  643. case 'E':
  644. if (nr_counters > 1) {
  645. int i;
  646. fprintf(stderr, "\nAvailable events:");
  647. for (i = 0; i < nr_counters; i++)
  648. fprintf(stderr, "\n\t%d %s", i, event_name(i));
  649. prompt_integer(&sym_counter, "Enter details event counter");
  650. if (sym_counter >= nr_counters) {
  651. fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0));
  652. sym_counter = 0;
  653. sleep(1);
  654. }
  655. } else sym_counter = 0;
  656. break;
  657. case 'f':
  658. prompt_integer(&count_filter, "Enter display event count filter");
  659. break;
  660. case 'F':
  661. prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)");
  662. break;
  663. case 'K':
  664. hide_kernel_symbols = !hide_kernel_symbols;
  665. break;
  666. case 'q':
  667. case 'Q':
  668. printf("exiting.\n");
  669. if (dump_symtab)
  670. dsos__fprintf(stderr);
  671. exit(0);
  672. case 's':
  673. prompt_symbol(&sym_filter_entry, "Enter details symbol");
  674. break;
  675. case 'S':
  676. if (!sym_filter_entry)
  677. break;
  678. else {
  679. struct sym_entry *syme = sym_filter_entry;
  680. pthread_mutex_lock(&syme->src->lock);
  681. sym_filter_entry = NULL;
  682. __zero_source_counters(syme);
  683. pthread_mutex_unlock(&syme->src->lock);
  684. }
  685. break;
  686. case 'U':
  687. hide_user_symbols = !hide_user_symbols;
  688. break;
  689. case 'w':
  690. display_weighted = ~display_weighted;
  691. break;
  692. case 'z':
  693. zero = ~zero;
  694. break;
  695. default:
  696. break;
  697. }
  698. }
  699. static void *display_thread(void *arg __used)
  700. {
  701. struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
  702. struct termios tc, save;
  703. int delay_msecs, c;
  704. tcgetattr(0, &save);
  705. tc = save;
  706. tc.c_lflag &= ~(ICANON | ECHO);
  707. tc.c_cc[VMIN] = 0;
  708. tc.c_cc[VTIME] = 0;
  709. repeat:
  710. delay_msecs = delay_secs * 1000;
  711. tcsetattr(0, TCSANOW, &tc);
  712. /* trash return*/
  713. getc(stdin);
  714. do {
  715. print_sym_table();
  716. } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
  717. c = getc(stdin);
  718. tcsetattr(0, TCSAFLUSH, &save);
  719. handle_keypress(c);
  720. goto repeat;
  721. return NULL;
  722. }
  723. /* Tag samples to be skipped. */
  724. static const char *skip_symbols[] = {
  725. "default_idle",
  726. "cpu_idle",
  727. "enter_idle",
  728. "exit_idle",
  729. "mwait_idle",
  730. "mwait_idle_with_hints",
  731. "poll_idle",
  732. "ppc64_runlatch_off",
  733. "pseries_dedicated_idle_sleep",
  734. NULL
  735. };
  736. static int symbol_filter(struct map *map, struct symbol *sym)
  737. {
  738. struct sym_entry *syme;
  739. const char *name = sym->name;
  740. int i;
  741. /*
  742. * ppc64 uses function descriptors and appends a '.' to the
  743. * start of every instruction address. Remove it.
  744. */
  745. if (name[0] == '.')
  746. name++;
  747. if (!strcmp(name, "_text") ||
  748. !strcmp(name, "_etext") ||
  749. !strcmp(name, "_sinittext") ||
  750. !strncmp("init_module", name, 11) ||
  751. !strncmp("cleanup_module", name, 14) ||
  752. strstr(name, "_text_start") ||
  753. strstr(name, "_text_end"))
  754. return 1;
  755. syme = symbol__priv(sym);
  756. syme->map = map;
  757. syme->src = NULL;
  758. if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) {
  759. /* schedule initial sym_filter_entry setup */
  760. sym_filter_entry_sched = syme;
  761. sym_filter = NULL;
  762. }
  763. for (i = 0; skip_symbols[i]; i++) {
  764. if (!strcmp(skip_symbols[i], name)) {
  765. syme->skip = 1;
  766. break;
  767. }
  768. }
  769. if (!syme->skip)
  770. syme->name_len = strlen(sym->name);
  771. return 0;
  772. }
  773. static void event__process_sample(const event_t *self,
  774. struct perf_session *session, int counter)
  775. {
  776. u64 ip = self->ip.ip;
  777. struct sym_entry *syme;
  778. struct addr_location al;
  779. u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
  780. ++samples;
  781. switch (origin) {
  782. case PERF_RECORD_MISC_USER:
  783. ++userspace_samples;
  784. if (hide_user_symbols)
  785. return;
  786. break;
  787. case PERF_RECORD_MISC_KERNEL:
  788. if (hide_kernel_symbols)
  789. return;
  790. break;
  791. default:
  792. return;
  793. }
  794. if (event__preprocess_sample(self, session, &al, symbol_filter) < 0 ||
  795. al.filtered)
  796. return;
  797. if (al.sym == NULL) {
  798. /*
  799. * As we do lazy loading of symtabs we only will know if the
  800. * specified vmlinux file is invalid when we actually have a
  801. * hit in kernel space and then try to load it. So if we get
  802. * here and there are _no_ symbols in the DSO backing the
  803. * kernel map, bail out.
  804. *
  805. * We may never get here, for instance, if we use -K/
  806. * --hide-kernel-symbols, even if the user specifies an
  807. * invalid --vmlinux ;-)
  808. */
  809. if (al.map == session->vmlinux_maps[MAP__FUNCTION] &&
  810. RB_EMPTY_ROOT(&al.map->dso->symbols[MAP__FUNCTION])) {
  811. pr_err("The %s file can't be used\n",
  812. symbol_conf.vmlinux_name);
  813. exit(1);
  814. }
  815. return;
  816. }
  817. /* let's see, whether we need to install initial sym_filter_entry */
  818. if (sym_filter_entry_sched) {
  819. sym_filter_entry = sym_filter_entry_sched;
  820. sym_filter_entry_sched = NULL;
  821. parse_source(sym_filter_entry);
  822. }
  823. syme = symbol__priv(al.sym);
  824. if (!syme->skip) {
  825. syme->count[counter]++;
  826. syme->origin = origin;
  827. record_precise_ip(syme, counter, ip);
  828. pthread_mutex_lock(&active_symbols_lock);
  829. if (list_empty(&syme->node) || !syme->node.next)
  830. __list_insert_active_sym(syme);
  831. pthread_mutex_unlock(&active_symbols_lock);
  832. }
  833. }
  834. static int event__process(event_t *event, struct perf_session *session)
  835. {
  836. switch (event->header.type) {
  837. case PERF_RECORD_COMM:
  838. event__process_comm(event, session);
  839. break;
  840. case PERF_RECORD_MMAP:
  841. event__process_mmap(event, session);
  842. break;
  843. case PERF_RECORD_FORK:
  844. case PERF_RECORD_EXIT:
  845. event__process_task(event, session);
  846. break;
  847. default:
  848. break;
  849. }
  850. return 0;
  851. }
  852. struct mmap_data {
  853. int counter;
  854. void *base;
  855. int mask;
  856. unsigned int prev;
  857. };
  858. static unsigned int mmap_read_head(struct mmap_data *md)
  859. {
  860. struct perf_event_mmap_page *pc = md->base;
  861. int head;
  862. head = pc->data_head;
  863. rmb();
  864. return head;
  865. }
  866. static void perf_session__mmap_read_counter(struct perf_session *self,
  867. struct mmap_data *md)
  868. {
  869. unsigned int head = mmap_read_head(md);
  870. unsigned int old = md->prev;
  871. unsigned char *data = md->base + page_size;
  872. int diff;
  873. /*
  874. * If we're further behind than half the buffer, there's a chance
  875. * the writer will bite our tail and mess up the samples under us.
  876. *
  877. * If we somehow ended up ahead of the head, we got messed up.
  878. *
  879. * In either case, truncate and restart at head.
  880. */
  881. diff = head - old;
  882. if (diff > md->mask / 2 || diff < 0) {
  883. fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
  884. /*
  885. * head points to a known good entry, start there.
  886. */
  887. old = head;
  888. }
  889. for (; old != head;) {
  890. event_t *event = (event_t *)&data[old & md->mask];
  891. event_t event_copy;
  892. size_t size = event->header.size;
  893. /*
  894. * Event straddles the mmap boundary -- header should always
  895. * be inside due to u64 alignment of output.
  896. */
  897. if ((old & md->mask) + size != ((old + size) & md->mask)) {
  898. unsigned int offset = old;
  899. unsigned int len = min(sizeof(*event), size), cpy;
  900. void *dst = &event_copy;
  901. do {
  902. cpy = min(md->mask + 1 - (offset & md->mask), len);
  903. memcpy(dst, &data[offset & md->mask], cpy);
  904. offset += cpy;
  905. dst += cpy;
  906. len -= cpy;
  907. } while (len);
  908. event = &event_copy;
  909. }
  910. if (event->header.type == PERF_RECORD_SAMPLE)
  911. event__process_sample(event, self, md->counter);
  912. else
  913. event__process(event, self);
  914. old += size;
  915. }
  916. md->prev = old;
  917. }
  918. static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
  919. static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
  920. static void perf_session__mmap_read(struct perf_session *self)
  921. {
  922. int i, counter;
  923. for (i = 0; i < nr_cpus; i++) {
  924. for (counter = 0; counter < nr_counters; counter++)
  925. perf_session__mmap_read_counter(self, &mmap_array[i][counter]);
  926. }
  927. }
  928. int nr_poll;
  929. int group_fd;
  930. static void start_counter(int i, int counter)
  931. {
  932. struct perf_event_attr *attr;
  933. int cpu;
  934. cpu = profile_cpu;
  935. if (target_pid == -1 && profile_cpu == -1)
  936. cpu = cpumap[i];
  937. attr = attrs + counter;
  938. attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
  939. if (freq) {
  940. attr->sample_type |= PERF_SAMPLE_PERIOD;
  941. attr->freq = 1;
  942. attr->sample_freq = freq;
  943. }
  944. attr->inherit = (cpu < 0) && inherit;
  945. attr->mmap = 1;
  946. try_again:
  947. fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
  948. if (fd[i][counter] < 0) {
  949. int err = errno;
  950. if (err == EPERM || err == EACCES)
  951. die("No permission - are you root?\n");
  952. /*
  953. * If it's cycles then fall back to hrtimer
  954. * based cpu-clock-tick sw counter, which
  955. * is always available even if no PMU support:
  956. */
  957. if (attr->type == PERF_TYPE_HARDWARE
  958. && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
  959. if (verbose)
  960. warning(" ... trying to fall back to cpu-clock-ticks\n");
  961. attr->type = PERF_TYPE_SOFTWARE;
  962. attr->config = PERF_COUNT_SW_CPU_CLOCK;
  963. goto try_again;
  964. }
  965. printf("\n");
  966. error("perfcounter syscall returned with %d (%s)\n",
  967. fd[i][counter], strerror(err));
  968. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  969. exit(-1);
  970. }
  971. assert(fd[i][counter] >= 0);
  972. fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
  973. /*
  974. * First counter acts as the group leader:
  975. */
  976. if (group && group_fd == -1)
  977. group_fd = fd[i][counter];
  978. event_array[nr_poll].fd = fd[i][counter];
  979. event_array[nr_poll].events = POLLIN;
  980. nr_poll++;
  981. mmap_array[i][counter].counter = counter;
  982. mmap_array[i][counter].prev = 0;
  983. mmap_array[i][counter].mask = mmap_pages*page_size - 1;
  984. mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
  985. PROT_READ, MAP_SHARED, fd[i][counter], 0);
  986. if (mmap_array[i][counter].base == MAP_FAILED)
  987. die("failed to mmap with %d (%s)\n", errno, strerror(errno));
  988. }
  989. static int __cmd_top(void)
  990. {
  991. pthread_t thread;
  992. int i, counter;
  993. int ret;
  994. /*
  995. * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
  996. * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
  997. */
  998. struct perf_session *session = perf_session__new(NULL, O_WRONLY, false);
  999. if (session == NULL)
  1000. return -ENOMEM;
  1001. if (target_pid != -1)
  1002. event__synthesize_thread(target_pid, event__process, session);
  1003. else
  1004. event__synthesize_threads(event__process, session);
  1005. for (i = 0; i < nr_cpus; i++) {
  1006. group_fd = -1;
  1007. for (counter = 0; counter < nr_counters; counter++)
  1008. start_counter(i, counter);
  1009. }
  1010. /* Wait for a minimal set of events before starting the snapshot */
  1011. poll(event_array, nr_poll, 100);
  1012. perf_session__mmap_read(session);
  1013. if (pthread_create(&thread, NULL, display_thread, NULL)) {
  1014. printf("Could not create display thread.\n");
  1015. exit(-1);
  1016. }
  1017. if (realtime_prio) {
  1018. struct sched_param param;
  1019. param.sched_priority = realtime_prio;
  1020. if (sched_setscheduler(0, SCHED_FIFO, &param)) {
  1021. printf("Could not set realtime priority.\n");
  1022. exit(-1);
  1023. }
  1024. }
  1025. while (1) {
  1026. int hits = samples;
  1027. perf_session__mmap_read(session);
  1028. if (hits == samples)
  1029. ret = poll(event_array, nr_poll, 100);
  1030. }
  1031. return 0;
  1032. }
  1033. static const char * const top_usage[] = {
  1034. "perf top [<options>]",
  1035. NULL
  1036. };
  1037. static const struct option options[] = {
  1038. OPT_CALLBACK('e', "event", NULL, "event",
  1039. "event selector. use 'perf list' to list available events",
  1040. parse_events),
  1041. OPT_INTEGER('c', "count", &default_interval,
  1042. "event period to sample"),
  1043. OPT_INTEGER('p', "pid", &target_pid,
  1044. "profile events on existing pid"),
  1045. OPT_BOOLEAN('a', "all-cpus", &system_wide,
  1046. "system-wide collection from all CPUs"),
  1047. OPT_INTEGER('C', "CPU", &profile_cpu,
  1048. "CPU to profile on"),
  1049. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  1050. "file", "vmlinux pathname"),
  1051. OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols,
  1052. "hide kernel symbols"),
  1053. OPT_INTEGER('m', "mmap-pages", &mmap_pages,
  1054. "number of mmap data pages"),
  1055. OPT_INTEGER('r', "realtime", &realtime_prio,
  1056. "collect data with this RT SCHED_FIFO priority"),
  1057. OPT_INTEGER('d', "delay", &delay_secs,
  1058. "number of seconds to delay between refreshes"),
  1059. OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
  1060. "dump the symbol table used for profiling"),
  1061. OPT_INTEGER('f', "count-filter", &count_filter,
  1062. "only display functions with more events than this"),
  1063. OPT_BOOLEAN('g', "group", &group,
  1064. "put the counters into a counter group"),
  1065. OPT_BOOLEAN('i', "inherit", &inherit,
  1066. "child tasks inherit counters"),
  1067. OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
  1068. "symbol to annotate"),
  1069. OPT_BOOLEAN('z', "zero", &zero,
  1070. "zero history across updates"),
  1071. OPT_INTEGER('F', "freq", &freq,
  1072. "profile at this frequency"),
  1073. OPT_INTEGER('E', "entries", &print_entries,
  1074. "display this many functions"),
  1075. OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols,
  1076. "hide user symbols"),
  1077. OPT_BOOLEAN('v', "verbose", &verbose,
  1078. "be more verbose (show counter open errors, etc)"),
  1079. OPT_END()
  1080. };
  1081. int cmd_top(int argc, const char **argv, const char *prefix __used)
  1082. {
  1083. int counter;
  1084. page_size = sysconf(_SC_PAGE_SIZE);
  1085. argc = parse_options(argc, argv, options, top_usage, 0);
  1086. if (argc)
  1087. usage_with_options(top_usage, options);
  1088. /* CPU and PID are mutually exclusive */
  1089. if (target_pid != -1 && profile_cpu != -1) {
  1090. printf("WARNING: PID switch overriding CPU\n");
  1091. sleep(1);
  1092. profile_cpu = -1;
  1093. }
  1094. if (!nr_counters)
  1095. nr_counters = 1;
  1096. symbol_conf.priv_size = (sizeof(struct sym_entry) +
  1097. (nr_counters + 1) * sizeof(unsigned long));
  1098. symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
  1099. if (symbol__init() < 0)
  1100. return -1;
  1101. if (delay_secs < 1)
  1102. delay_secs = 1;
  1103. /*
  1104. * User specified count overrides default frequency.
  1105. */
  1106. if (default_interval)
  1107. freq = 0;
  1108. else if (freq) {
  1109. default_interval = freq;
  1110. } else {
  1111. fprintf(stderr, "frequency and count are zero, aborting\n");
  1112. exit(EXIT_FAILURE);
  1113. }
  1114. /*
  1115. * Fill in the ones not specifically initialized via -c:
  1116. */
  1117. for (counter = 0; counter < nr_counters; counter++) {
  1118. if (attrs[counter].sample_period)
  1119. continue;
  1120. attrs[counter].sample_period = default_interval;
  1121. }
  1122. if (target_pid != -1 || profile_cpu != -1)
  1123. nr_cpus = 1;
  1124. else
  1125. nr_cpus = read_cpu_map();
  1126. get_term_dimensions(&winsize);
  1127. if (print_entries == 0) {
  1128. update_print_entries(&winsize);
  1129. signal(SIGWINCH, sig_winch_handler);
  1130. }
  1131. return __cmd_top();
  1132. }