186470930SIngo Molnar /* 286470930SIngo Molnar * builtin-top.c 386470930SIngo Molnar * 486470930SIngo Molnar * Builtin top command: Display a continuously updated profile of 586470930SIngo Molnar * any workload, CPU or specific PID. 686470930SIngo Molnar * 786470930SIngo Molnar * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com> 886470930SIngo Molnar * 986470930SIngo Molnar * Improvements and fixes by: 1086470930SIngo Molnar * 1186470930SIngo Molnar * Arjan van de Ven <arjan@linux.intel.com> 1286470930SIngo Molnar * Yanmin Zhang <yanmin.zhang@intel.com> 1386470930SIngo Molnar * Wu Fengguang <fengguang.wu@intel.com> 1486470930SIngo Molnar * Mike Galbraith <efault@gmx.de> 1586470930SIngo Molnar * Paul Mackerras <paulus@samba.org> 1686470930SIngo Molnar * 1786470930SIngo Molnar * Released under the GPL v2. (and only v2, not any later version) 1886470930SIngo Molnar */ 1986470930SIngo Molnar #include "builtin.h" 2086470930SIngo Molnar 2186470930SIngo Molnar #include "perf.h" 2286470930SIngo Molnar 2386470930SIngo Molnar #include "util/symbol.h" 2486470930SIngo Molnar #include "util/color.h" 25439d473bSArnaldo Carvalho de Melo #include "util/thread.h" 2686470930SIngo Molnar #include "util/util.h" 2743cbcd8aSArnaldo Carvalho de Melo #include <linux/rbtree.h> 2886470930SIngo Molnar #include "util/parse-options.h" 2986470930SIngo Molnar #include "util/parse-events.h" 3086470930SIngo Molnar 318f28827aSFrederic Weisbecker #include "util/debug.h" 328f28827aSFrederic Weisbecker 3386470930SIngo Molnar #include <assert.h> 3486470930SIngo Molnar #include <fcntl.h> 3586470930SIngo Molnar 3686470930SIngo Molnar #include <stdio.h> 37923c42c1SMike Galbraith #include <termios.h> 38923c42c1SMike Galbraith #include <unistd.h> 3986470930SIngo Molnar 4086470930SIngo Molnar #include <errno.h> 4186470930SIngo Molnar #include <time.h> 4286470930SIngo Molnar #include <sched.h> 4386470930SIngo Molnar #include <pthread.h> 4486470930SIngo Molnar 4586470930SIngo Molnar #include <sys/syscall.h> 4686470930SIngo Molnar #include <sys/ioctl.h> 4786470930SIngo Molnar #include <sys/poll.h> 4886470930SIngo Molnar #include <sys/prctl.h> 4986470930SIngo Molnar #include <sys/wait.h> 5086470930SIngo Molnar #include <sys/uio.h> 5186470930SIngo Molnar #include <sys/mman.h> 5286470930SIngo Molnar 5386470930SIngo Molnar #include <linux/unistd.h> 5486470930SIngo Molnar #include <linux/types.h> 5586470930SIngo Molnar 5686470930SIngo Molnar static int fd[MAX_NR_CPUS][MAX_COUNTERS]; 5786470930SIngo Molnar 5886470930SIngo Molnar static int system_wide = 0; 5986470930SIngo Molnar 607e4ff9e3SMike Galbraith static int default_interval = 0; 6186470930SIngo Molnar 62923c42c1SMike Galbraith static int count_filter = 5; 633b6ed988SArnaldo Carvalho de Melo static int print_entries; 6486470930SIngo Molnar 6586470930SIngo Molnar static int target_pid = -1; 660fdc7e67SMike Galbraith static int inherit = 0; 6786470930SIngo Molnar static int profile_cpu = -1; 6886470930SIngo Molnar static int nr_cpus = 0; 6986470930SIngo Molnar static unsigned int realtime_prio = 0; 7086470930SIngo Molnar static int group = 0; 7186470930SIngo Molnar static unsigned int page_size; 7286470930SIngo Molnar static unsigned int mmap_pages = 16; 7342e59d7dSIngo Molnar static int freq = 1000; /* 1 KHz */ 7486470930SIngo Molnar 7586470930SIngo Molnar static int delay_secs = 2; 7642e59d7dSIngo Molnar static int zero = 0; 7742e59d7dSIngo Molnar static int dump_symtab = 0; 7886470930SIngo Molnar 798ffcda17SArnaldo Carvalho de Melo static bool hide_kernel_symbols = false; 808ffcda17SArnaldo Carvalho de Melo static bool hide_user_symbols = false; 8113cc5079SArnaldo Carvalho de Melo static struct winsize winsize; 8213cc5079SArnaldo Carvalho de Melo static const char *graph_line = 8313cc5079SArnaldo Carvalho de Melo "_____________________________________________________________________" 8413cc5079SArnaldo Carvalho de Melo "_____________________________________________________________________"; 8513cc5079SArnaldo Carvalho de Melo static const char *graph_dotted_line = 8613cc5079SArnaldo Carvalho de Melo "---------------------------------------------------------------------" 8713cc5079SArnaldo Carvalho de Melo "---------------------------------------------------------------------" 8813cc5079SArnaldo Carvalho de Melo "---------------------------------------------------------------------"; 898ffcda17SArnaldo Carvalho de Melo 9086470930SIngo Molnar /* 91923c42c1SMike Galbraith * Source 92923c42c1SMike Galbraith */ 93923c42c1SMike Galbraith 94923c42c1SMike Galbraith struct source_line { 95923c42c1SMike Galbraith u64 eip; 96923c42c1SMike Galbraith unsigned long count[MAX_COUNTERS]; 97923c42c1SMike Galbraith char *line; 98923c42c1SMike Galbraith struct source_line *next; 99923c42c1SMike Galbraith }; 100923c42c1SMike Galbraith 101923c42c1SMike Galbraith static char *sym_filter = NULL; 102923c42c1SMike Galbraith struct sym_entry *sym_filter_entry = NULL; 103923c42c1SMike Galbraith static int sym_pcnt_filter = 5; 104923c42c1SMike Galbraith static int sym_counter = 0; 10546ab9764SMike Galbraith static int display_weighted = -1; 106923c42c1SMike Galbraith 107923c42c1SMike Galbraith /* 10886470930SIngo Molnar * Symbols 10986470930SIngo Molnar */ 11086470930SIngo Molnar 11186470930SIngo Molnar struct sym_entry { 11286470930SIngo Molnar struct rb_node rb_node; 11386470930SIngo Molnar struct list_head node; 11486470930SIngo Molnar unsigned long count[MAX_COUNTERS]; 11586470930SIngo Molnar unsigned long snap_count; 11686470930SIngo Molnar double weight; 11786470930SIngo Molnar int skip; 11813cc5079SArnaldo Carvalho de Melo u16 name_len; 1198ffcda17SArnaldo Carvalho de Melo u8 origin; 120439d473bSArnaldo Carvalho de Melo struct map *map; 121923c42c1SMike Galbraith struct source_line *source; 122923c42c1SMike Galbraith struct source_line *lines; 123923c42c1SMike Galbraith struct source_line **lines_tail; 124923c42c1SMike Galbraith pthread_mutex_t source_lock; 12586470930SIngo Molnar }; 12686470930SIngo Molnar 127923c42c1SMike Galbraith /* 128923c42c1SMike Galbraith * Source functions 129923c42c1SMike Galbraith */ 130923c42c1SMike Galbraith 13113cc5079SArnaldo Carvalho de Melo static void get_term_dimensions(struct winsize *ws) 1323b6ed988SArnaldo Carvalho de Melo { 13313cc5079SArnaldo Carvalho de Melo char *s = getenv("LINES"); 1343b6ed988SArnaldo Carvalho de Melo 13513cc5079SArnaldo Carvalho de Melo if (s != NULL) { 13613cc5079SArnaldo Carvalho de Melo ws->ws_row = atoi(s); 13713cc5079SArnaldo Carvalho de Melo s = getenv("COLUMNS"); 13813cc5079SArnaldo Carvalho de Melo if (s != NULL) { 13913cc5079SArnaldo Carvalho de Melo ws->ws_col = atoi(s); 14013cc5079SArnaldo Carvalho de Melo if (ws->ws_row && ws->ws_col) 14113cc5079SArnaldo Carvalho de Melo return; 14213cc5079SArnaldo Carvalho de Melo } 14313cc5079SArnaldo Carvalho de Melo } 1443b6ed988SArnaldo Carvalho de Melo #ifdef TIOCGWINSZ 14513cc5079SArnaldo Carvalho de Melo if (ioctl(1, TIOCGWINSZ, ws) == 0 && 14613cc5079SArnaldo Carvalho de Melo ws->ws_row && ws->ws_col) 14713cc5079SArnaldo Carvalho de Melo return; 1483b6ed988SArnaldo Carvalho de Melo #endif 14913cc5079SArnaldo Carvalho de Melo ws->ws_row = 25; 15013cc5079SArnaldo Carvalho de Melo ws->ws_col = 80; 1513b6ed988SArnaldo Carvalho de Melo } 1523b6ed988SArnaldo Carvalho de Melo 15313cc5079SArnaldo Carvalho de Melo static void update_print_entries(struct winsize *ws) 1543b6ed988SArnaldo Carvalho de Melo { 15513cc5079SArnaldo Carvalho de Melo print_entries = ws->ws_row; 15613cc5079SArnaldo Carvalho de Melo 1573b6ed988SArnaldo Carvalho de Melo if (print_entries > 9) 1583b6ed988SArnaldo Carvalho de Melo print_entries -= 9; 1593b6ed988SArnaldo Carvalho de Melo } 1603b6ed988SArnaldo Carvalho de Melo 1613b6ed988SArnaldo Carvalho de Melo static void sig_winch_handler(int sig __used) 1623b6ed988SArnaldo Carvalho de Melo { 16313cc5079SArnaldo Carvalho de Melo get_term_dimensions(&winsize); 16413cc5079SArnaldo Carvalho de Melo update_print_entries(&winsize); 1653b6ed988SArnaldo Carvalho de Melo } 1663b6ed988SArnaldo Carvalho de Melo 167923c42c1SMike Galbraith static void parse_source(struct sym_entry *syme) 168923c42c1SMike Galbraith { 169923c42c1SMike Galbraith struct symbol *sym; 170439d473bSArnaldo Carvalho de Melo struct map *map; 171923c42c1SMike Galbraith FILE *file; 17283a0944fSIngo Molnar char command[PATH_MAX*2]; 173439d473bSArnaldo Carvalho de Melo const char *path; 174439d473bSArnaldo Carvalho de Melo u64 len; 175923c42c1SMike Galbraith 176923c42c1SMike Galbraith if (!syme) 177923c42c1SMike Galbraith return; 178923c42c1SMike Galbraith 179923c42c1SMike Galbraith if (syme->lines) { 180923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 181923c42c1SMike Galbraith goto out_assign; 182923c42c1SMike Galbraith } 183923c42c1SMike Galbraith 184923c42c1SMike Galbraith sym = (struct symbol *)(syme + 1); 185439d473bSArnaldo Carvalho de Melo map = syme->map; 186439d473bSArnaldo Carvalho de Melo path = map->dso->long_name; 187923c42c1SMike Galbraith 188923c42c1SMike Galbraith len = sym->end - sym->start; 189923c42c1SMike Galbraith 190439d473bSArnaldo Carvalho de Melo sprintf(command, 191439d473bSArnaldo Carvalho de Melo "objdump --start-address=0x%016Lx " 192439d473bSArnaldo Carvalho de Melo "--stop-address=0x%016Lx -dS %s", 193c88e4bf6SArnaldo Carvalho de Melo map->unmap_ip(map, sym->start), 194c88e4bf6SArnaldo Carvalho de Melo map->unmap_ip(map, sym->end), path); 195923c42c1SMike Galbraith 196923c42c1SMike Galbraith file = popen(command, "r"); 197923c42c1SMike Galbraith if (!file) 198923c42c1SMike Galbraith return; 199923c42c1SMike Galbraith 200923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 201923c42c1SMike Galbraith syme->lines_tail = &syme->lines; 202923c42c1SMike Galbraith while (!feof(file)) { 203923c42c1SMike Galbraith struct source_line *src; 204923c42c1SMike Galbraith size_t dummy = 0; 205923c42c1SMike Galbraith char *c; 206923c42c1SMike Galbraith 207923c42c1SMike Galbraith src = malloc(sizeof(struct source_line)); 208923c42c1SMike Galbraith assert(src != NULL); 209923c42c1SMike Galbraith memset(src, 0, sizeof(struct source_line)); 210923c42c1SMike Galbraith 211923c42c1SMike Galbraith if (getline(&src->line, &dummy, file) < 0) 212923c42c1SMike Galbraith break; 213923c42c1SMike Galbraith if (!src->line) 214923c42c1SMike Galbraith break; 215923c42c1SMike Galbraith 216923c42c1SMike Galbraith c = strchr(src->line, '\n'); 217923c42c1SMike Galbraith if (c) 218923c42c1SMike Galbraith *c = 0; 219923c42c1SMike Galbraith 220923c42c1SMike Galbraith src->next = NULL; 221923c42c1SMike Galbraith *syme->lines_tail = src; 222923c42c1SMike Galbraith syme->lines_tail = &src->next; 223923c42c1SMike Galbraith 224923c42c1SMike Galbraith if (strlen(src->line)>8 && src->line[8] == ':') { 225923c42c1SMike Galbraith src->eip = strtoull(src->line, NULL, 16); 226c88e4bf6SArnaldo Carvalho de Melo src->eip = map->unmap_ip(map, src->eip); 227923c42c1SMike Galbraith } 228923c42c1SMike Galbraith if (strlen(src->line)>8 && src->line[16] == ':') { 229923c42c1SMike Galbraith src->eip = strtoull(src->line, NULL, 16); 230c88e4bf6SArnaldo Carvalho de Melo src->eip = map->unmap_ip(map, src->eip); 231923c42c1SMike Galbraith } 232923c42c1SMike Galbraith } 233923c42c1SMike Galbraith pclose(file); 234923c42c1SMike Galbraith out_assign: 235923c42c1SMike Galbraith sym_filter_entry = syme; 236923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 237923c42c1SMike Galbraith } 238923c42c1SMike Galbraith 239923c42c1SMike Galbraith static void __zero_source_counters(struct sym_entry *syme) 240923c42c1SMike Galbraith { 241923c42c1SMike Galbraith int i; 242923c42c1SMike Galbraith struct source_line *line; 243923c42c1SMike Galbraith 244923c42c1SMike Galbraith line = syme->lines; 245923c42c1SMike Galbraith while (line) { 246923c42c1SMike Galbraith for (i = 0; i < nr_counters; i++) 247923c42c1SMike Galbraith line->count[i] = 0; 248923c42c1SMike Galbraith line = line->next; 249923c42c1SMike Galbraith } 250923c42c1SMike Galbraith } 251923c42c1SMike Galbraith 252923c42c1SMike Galbraith static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip) 253923c42c1SMike Galbraith { 254923c42c1SMike Galbraith struct source_line *line; 255923c42c1SMike Galbraith 256923c42c1SMike Galbraith if (syme != sym_filter_entry) 257923c42c1SMike Galbraith return; 258923c42c1SMike Galbraith 259923c42c1SMike Galbraith if (pthread_mutex_trylock(&syme->source_lock)) 260923c42c1SMike Galbraith return; 261923c42c1SMike Galbraith 262923c42c1SMike Galbraith if (!syme->source) 263923c42c1SMike Galbraith goto out_unlock; 264923c42c1SMike Galbraith 265923c42c1SMike Galbraith for (line = syme->lines; line; line = line->next) { 266923c42c1SMike Galbraith if (line->eip == ip) { 267923c42c1SMike Galbraith line->count[counter]++; 268923c42c1SMike Galbraith break; 269923c42c1SMike Galbraith } 270923c42c1SMike Galbraith if (line->eip > ip) 271923c42c1SMike Galbraith break; 272923c42c1SMike Galbraith } 273923c42c1SMike Galbraith out_unlock: 274923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 275923c42c1SMike Galbraith } 276923c42c1SMike Galbraith 277923c42c1SMike Galbraith static void lookup_sym_source(struct sym_entry *syme) 278923c42c1SMike Galbraith { 279923c42c1SMike Galbraith struct symbol *symbol = (struct symbol *)(syme + 1); 280923c42c1SMike Galbraith struct source_line *line; 281923c42c1SMike Galbraith char pattern[PATH_MAX]; 282923c42c1SMike Galbraith 283923c42c1SMike Galbraith sprintf(pattern, "<%s>:", symbol->name); 284923c42c1SMike Galbraith 285923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 286923c42c1SMike Galbraith for (line = syme->lines; line; line = line->next) { 287923c42c1SMike Galbraith if (strstr(line->line, pattern)) { 288923c42c1SMike Galbraith syme->source = line; 289923c42c1SMike Galbraith break; 290923c42c1SMike Galbraith } 291923c42c1SMike Galbraith } 292923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 293923c42c1SMike Galbraith } 294923c42c1SMike Galbraith 295923c42c1SMike Galbraith static void show_lines(struct source_line *queue, int count, int total) 296923c42c1SMike Galbraith { 297923c42c1SMike Galbraith int i; 298923c42c1SMike Galbraith struct source_line *line; 299923c42c1SMike Galbraith 300923c42c1SMike Galbraith line = queue; 301923c42c1SMike Galbraith for (i = 0; i < count; i++) { 302923c42c1SMike Galbraith float pcnt = 100.0*(float)line->count[sym_counter]/(float)total; 303923c42c1SMike Galbraith 304923c42c1SMike Galbraith printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line); 305923c42c1SMike Galbraith line = line->next; 306923c42c1SMike Galbraith } 307923c42c1SMike Galbraith } 308923c42c1SMike Galbraith 309923c42c1SMike Galbraith #define TRACE_COUNT 3 310923c42c1SMike Galbraith 311923c42c1SMike Galbraith static void show_details(struct sym_entry *syme) 312923c42c1SMike Galbraith { 313923c42c1SMike Galbraith struct symbol *symbol; 314923c42c1SMike Galbraith struct source_line *line; 315923c42c1SMike Galbraith struct source_line *line_queue = NULL; 316923c42c1SMike Galbraith int displayed = 0; 317923c42c1SMike Galbraith int line_queue_count = 0, total = 0, more = 0; 318923c42c1SMike Galbraith 319923c42c1SMike Galbraith if (!syme) 320923c42c1SMike Galbraith return; 321923c42c1SMike Galbraith 322923c42c1SMike Galbraith if (!syme->source) 323923c42c1SMike Galbraith lookup_sym_source(syme); 324923c42c1SMike Galbraith 325923c42c1SMike Galbraith if (!syme->source) 326923c42c1SMike Galbraith return; 327923c42c1SMike Galbraith 328923c42c1SMike Galbraith symbol = (struct symbol *)(syme + 1); 329923c42c1SMike Galbraith printf("Showing %s for %s\n", event_name(sym_counter), symbol->name); 330923c42c1SMike Galbraith printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); 331923c42c1SMike Galbraith 332923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 333923c42c1SMike Galbraith line = syme->source; 334923c42c1SMike Galbraith while (line) { 335923c42c1SMike Galbraith total += line->count[sym_counter]; 336923c42c1SMike Galbraith line = line->next; 337923c42c1SMike Galbraith } 338923c42c1SMike Galbraith 339923c42c1SMike Galbraith line = syme->source; 340923c42c1SMike Galbraith while (line) { 341923c42c1SMike Galbraith float pcnt = 0.0; 342923c42c1SMike Galbraith 343923c42c1SMike Galbraith if (!line_queue_count) 344923c42c1SMike Galbraith line_queue = line; 345923c42c1SMike Galbraith line_queue_count++; 346923c42c1SMike Galbraith 347923c42c1SMike Galbraith if (line->count[sym_counter]) 348923c42c1SMike Galbraith pcnt = 100.0 * line->count[sym_counter] / (float)total; 349923c42c1SMike Galbraith if (pcnt >= (float)sym_pcnt_filter) { 350923c42c1SMike Galbraith if (displayed <= print_entries) 351923c42c1SMike Galbraith show_lines(line_queue, line_queue_count, total); 352923c42c1SMike Galbraith else more++; 353923c42c1SMike Galbraith displayed += line_queue_count; 354923c42c1SMike Galbraith line_queue_count = 0; 355923c42c1SMike Galbraith line_queue = NULL; 356923c42c1SMike Galbraith } else if (line_queue_count > TRACE_COUNT) { 357923c42c1SMike Galbraith line_queue = line_queue->next; 358923c42c1SMike Galbraith line_queue_count--; 359923c42c1SMike Galbraith } 360923c42c1SMike Galbraith 361923c42c1SMike Galbraith line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8; 362923c42c1SMike Galbraith line = line->next; 363923c42c1SMike Galbraith } 364923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 365923c42c1SMike Galbraith if (more) 366923c42c1SMike Galbraith printf("%d lines not displayed, maybe increase display entries [e]\n", more); 367923c42c1SMike Galbraith } 36886470930SIngo Molnar 36986470930SIngo Molnar /* 3705b2bb75aSArnaldo Carvalho de Melo * Symbols will be added here in event__process_sample and will get out 37186470930SIngo Molnar * after decayed. 37286470930SIngo Molnar */ 37386470930SIngo Molnar static LIST_HEAD(active_symbols); 37486470930SIngo Molnar static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER; 37586470930SIngo Molnar 37686470930SIngo Molnar /* 37786470930SIngo Molnar * Ordering weight: count-1 * count-2 * ... / count-n 37886470930SIngo Molnar */ 37986470930SIngo Molnar static double sym_weight(const struct sym_entry *sym) 38086470930SIngo Molnar { 38186470930SIngo Molnar double weight = sym->snap_count; 38286470930SIngo Molnar int counter; 38386470930SIngo Molnar 38446ab9764SMike Galbraith if (!display_weighted) 38546ab9764SMike Galbraith return weight; 38646ab9764SMike Galbraith 38786470930SIngo Molnar for (counter = 1; counter < nr_counters-1; counter++) 38886470930SIngo Molnar weight *= sym->count[counter]; 38986470930SIngo Molnar 39086470930SIngo Molnar weight /= (sym->count[counter] + 1); 39186470930SIngo Molnar 39286470930SIngo Molnar return weight; 39386470930SIngo Molnar } 39486470930SIngo Molnar 39586470930SIngo Molnar static long samples; 39686470930SIngo Molnar static long userspace_samples; 39786470930SIngo Molnar static const char CONSOLE_CLEAR[] = "[H[2J"; 39886470930SIngo Molnar 39986470930SIngo Molnar static void __list_insert_active_sym(struct sym_entry *syme) 40086470930SIngo Molnar { 40186470930SIngo Molnar list_add(&syme->node, &active_symbols); 40286470930SIngo Molnar } 40386470930SIngo Molnar 40486470930SIngo Molnar static void list_remove_active_sym(struct sym_entry *syme) 40586470930SIngo Molnar { 40686470930SIngo Molnar pthread_mutex_lock(&active_symbols_lock); 40786470930SIngo Molnar list_del_init(&syme->node); 40886470930SIngo Molnar pthread_mutex_unlock(&active_symbols_lock); 40986470930SIngo Molnar } 41086470930SIngo Molnar 41186470930SIngo Molnar static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se) 41286470930SIngo Molnar { 41386470930SIngo Molnar struct rb_node **p = &tree->rb_node; 41486470930SIngo Molnar struct rb_node *parent = NULL; 41586470930SIngo Molnar struct sym_entry *iter; 41686470930SIngo Molnar 41786470930SIngo Molnar while (*p != NULL) { 41886470930SIngo Molnar parent = *p; 41986470930SIngo Molnar iter = rb_entry(parent, struct sym_entry, rb_node); 42086470930SIngo Molnar 42186470930SIngo Molnar if (se->weight > iter->weight) 42286470930SIngo Molnar p = &(*p)->rb_left; 42386470930SIngo Molnar else 42486470930SIngo Molnar p = &(*p)->rb_right; 42586470930SIngo Molnar } 42686470930SIngo Molnar 42786470930SIngo Molnar rb_link_node(&se->rb_node, parent, p); 42886470930SIngo Molnar rb_insert_color(&se->rb_node, tree); 42986470930SIngo Molnar } 43086470930SIngo Molnar 43186470930SIngo Molnar static void print_sym_table(void) 43286470930SIngo Molnar { 43386470930SIngo Molnar int printed = 0, j; 43446ab9764SMike Galbraith int counter, snap = !display_weighted ? sym_counter : 0; 43586470930SIngo Molnar float samples_per_sec = samples/delay_secs; 43686470930SIngo Molnar float ksamples_per_sec = (samples-userspace_samples)/delay_secs; 43786470930SIngo Molnar float sum_ksamples = 0.0; 43886470930SIngo Molnar struct sym_entry *syme, *n; 43986470930SIngo Molnar struct rb_root tmp = RB_ROOT; 44086470930SIngo Molnar struct rb_node *nd; 44113cc5079SArnaldo Carvalho de Melo int sym_width = 0, dso_width; 44213cc5079SArnaldo Carvalho de Melo const int win_width = winsize.ws_col - 1; 44386470930SIngo Molnar 44486470930SIngo Molnar samples = userspace_samples = 0; 44586470930SIngo Molnar 44686470930SIngo Molnar /* Sort the active symbols */ 44786470930SIngo Molnar pthread_mutex_lock(&active_symbols_lock); 44886470930SIngo Molnar syme = list_entry(active_symbols.next, struct sym_entry, node); 44986470930SIngo Molnar pthread_mutex_unlock(&active_symbols_lock); 45086470930SIngo Molnar 45186470930SIngo Molnar list_for_each_entry_safe_from(syme, n, &active_symbols, node) { 45246ab9764SMike Galbraith syme->snap_count = syme->count[snap]; 45386470930SIngo Molnar if (syme->snap_count != 0) { 45413cc5079SArnaldo Carvalho de Melo 4558ffcda17SArnaldo Carvalho de Melo if ((hide_user_symbols && 4568ffcda17SArnaldo Carvalho de Melo syme->origin == PERF_RECORD_MISC_USER) || 4578ffcda17SArnaldo Carvalho de Melo (hide_kernel_symbols && 4588ffcda17SArnaldo Carvalho de Melo syme->origin == PERF_RECORD_MISC_KERNEL)) { 4598ffcda17SArnaldo Carvalho de Melo list_remove_active_sym(syme); 4608ffcda17SArnaldo Carvalho de Melo continue; 4618ffcda17SArnaldo Carvalho de Melo } 46286470930SIngo Molnar syme->weight = sym_weight(syme); 46386470930SIngo Molnar rb_insert_active_sym(&tmp, syme); 46486470930SIngo Molnar sum_ksamples += syme->snap_count; 46586470930SIngo Molnar 46686470930SIngo Molnar for (j = 0; j < nr_counters; j++) 46786470930SIngo Molnar syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8; 46886470930SIngo Molnar } else 46986470930SIngo Molnar list_remove_active_sym(syme); 47086470930SIngo Molnar } 47186470930SIngo Molnar 47286470930SIngo Molnar puts(CONSOLE_CLEAR); 47386470930SIngo Molnar 47413cc5079SArnaldo Carvalho de Melo printf("%-*.*s\n", win_width, win_width, graph_dotted_line); 47586470930SIngo Molnar printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [", 47686470930SIngo Molnar samples_per_sec, 47786470930SIngo Molnar 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec))); 47886470930SIngo Molnar 47946ab9764SMike Galbraith if (nr_counters == 1 || !display_weighted) { 4809cffa8d5SPaul Mackerras printf("%Ld", (u64)attrs[0].sample_period); 48186470930SIngo Molnar if (freq) 48286470930SIngo Molnar printf("Hz "); 48386470930SIngo Molnar else 48486470930SIngo Molnar printf(" "); 48586470930SIngo Molnar } 48686470930SIngo Molnar 48746ab9764SMike Galbraith if (!display_weighted) 48846ab9764SMike Galbraith printf("%s", event_name(sym_counter)); 48946ab9764SMike Galbraith else for (counter = 0; counter < nr_counters; counter++) { 49086470930SIngo Molnar if (counter) 49186470930SIngo Molnar printf("/"); 49286470930SIngo Molnar 49386470930SIngo Molnar printf("%s", event_name(counter)); 49486470930SIngo Molnar } 49586470930SIngo Molnar 49686470930SIngo Molnar printf( "], "); 49786470930SIngo Molnar 49886470930SIngo Molnar if (target_pid != -1) 49986470930SIngo Molnar printf(" (target_pid: %d", target_pid); 50086470930SIngo Molnar else 50186470930SIngo Molnar printf(" (all"); 50286470930SIngo Molnar 50386470930SIngo Molnar if (profile_cpu != -1) 50486470930SIngo Molnar printf(", cpu: %d)\n", profile_cpu); 50586470930SIngo Molnar else { 50686470930SIngo Molnar if (target_pid != -1) 50786470930SIngo Molnar printf(")\n"); 50886470930SIngo Molnar else 50986470930SIngo Molnar printf(", %d CPUs)\n", nr_cpus); 51086470930SIngo Molnar } 51186470930SIngo Molnar 51213cc5079SArnaldo Carvalho de Melo printf("%-*.*s\n\n", win_width, win_width, graph_dotted_line); 51386470930SIngo Molnar 514923c42c1SMike Galbraith if (sym_filter_entry) { 515923c42c1SMike Galbraith show_details(sym_filter_entry); 516923c42c1SMike Galbraith return; 517923c42c1SMike Galbraith } 518923c42c1SMike Galbraith 51913cc5079SArnaldo Carvalho de Melo /* 52013cc5079SArnaldo Carvalho de Melo * Find the longest symbol name that will be displayed 52113cc5079SArnaldo Carvalho de Melo */ 52213cc5079SArnaldo Carvalho de Melo for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { 52313cc5079SArnaldo Carvalho de Melo syme = rb_entry(nd, struct sym_entry, rb_node); 52413cc5079SArnaldo Carvalho de Melo if (++printed > print_entries || 52513cc5079SArnaldo Carvalho de Melo (int)syme->snap_count < count_filter) 52613cc5079SArnaldo Carvalho de Melo continue; 52713cc5079SArnaldo Carvalho de Melo 52813cc5079SArnaldo Carvalho de Melo if (syme->name_len > sym_width) 52913cc5079SArnaldo Carvalho de Melo sym_width = syme->name_len; 53013cc5079SArnaldo Carvalho de Melo } 53113cc5079SArnaldo Carvalho de Melo 53213cc5079SArnaldo Carvalho de Melo printed = 0; 53313cc5079SArnaldo Carvalho de Melo 53486470930SIngo Molnar if (nr_counters == 1) 53586470930SIngo Molnar printf(" samples pcnt"); 53686470930SIngo Molnar else 53786470930SIngo Molnar printf(" weight samples pcnt"); 53886470930SIngo Molnar 53913cc5079SArnaldo Carvalho de Melo dso_width = winsize.ws_col - sym_width - 29; 54013cc5079SArnaldo Carvalho de Melo 5417ced156bSArnaldo Carvalho de Melo if (verbose) 5427ced156bSArnaldo Carvalho de Melo printf(" RIP "); 54313cc5079SArnaldo Carvalho de Melo printf(" %-*.*s DSO\n", sym_width, sym_width, "function"); 5447ced156bSArnaldo Carvalho de Melo printf(" %s _______ _____", 5457ced156bSArnaldo Carvalho de Melo nr_counters == 1 ? " " : "______"); 5467ced156bSArnaldo Carvalho de Melo if (verbose) 5477ced156bSArnaldo Carvalho de Melo printf(" ________________"); 54813cc5079SArnaldo Carvalho de Melo printf(" %-*.*s %-*.*s\n\n", sym_width, sym_width, graph_line, 54913cc5079SArnaldo Carvalho de Melo dso_width, dso_width, graph_line); 55086470930SIngo Molnar 55186470930SIngo Molnar for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { 55283a0944fSIngo Molnar struct symbol *sym; 55386470930SIngo Molnar double pcnt; 55486470930SIngo Molnar 55583a0944fSIngo Molnar syme = rb_entry(nd, struct sym_entry, rb_node); 55683a0944fSIngo Molnar sym = (struct symbol *)(syme + 1); 55783a0944fSIngo Molnar 558923c42c1SMike Galbraith if (++printed > print_entries || (int)syme->snap_count < count_filter) 55986470930SIngo Molnar continue; 56086470930SIngo Molnar 56186470930SIngo Molnar pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) / 56286470930SIngo Molnar sum_ksamples)); 56386470930SIngo Molnar 56446ab9764SMike Galbraith if (nr_counters == 1 || !display_weighted) 5655b2bb75aSArnaldo Carvalho de Melo printf("%20.2f ", syme->weight); 56686470930SIngo Molnar else 5675b2bb75aSArnaldo Carvalho de Melo printf("%9.1f %10ld ", syme->weight, syme->snap_count); 56886470930SIngo Molnar 5691e11fd82SFrederic Weisbecker percent_color_fprintf(stdout, "%4.1f%%", pcnt); 5707ced156bSArnaldo Carvalho de Melo if (verbose) 5715b2bb75aSArnaldo Carvalho de Melo printf(" %016llx", sym->start); 57213cc5079SArnaldo Carvalho de Melo printf(" %-*.*s", sym_width, sym_width, sym->name); 57313cc5079SArnaldo Carvalho de Melo printf(" %-*.*s", dso_width, dso_width, 57413cc5079SArnaldo Carvalho de Melo dso_width >= syme->map->dso->long_name_len ? 57513cc5079SArnaldo Carvalho de Melo syme->map->dso->long_name : 57613cc5079SArnaldo Carvalho de Melo syme->map->dso->short_name); 57742976487SMike Galbraith printf("\n"); 57886470930SIngo Molnar } 57986470930SIngo Molnar } 58086470930SIngo Molnar 581923c42c1SMike Galbraith static void prompt_integer(int *target, const char *msg) 582923c42c1SMike Galbraith { 583923c42c1SMike Galbraith char *buf = malloc(0), *p; 584923c42c1SMike Galbraith size_t dummy = 0; 585923c42c1SMike Galbraith int tmp; 586923c42c1SMike Galbraith 587923c42c1SMike Galbraith fprintf(stdout, "\n%s: ", msg); 588923c42c1SMike Galbraith if (getline(&buf, &dummy, stdin) < 0) 589923c42c1SMike Galbraith return; 590923c42c1SMike Galbraith 591923c42c1SMike Galbraith p = strchr(buf, '\n'); 592923c42c1SMike Galbraith if (p) 593923c42c1SMike Galbraith *p = 0; 594923c42c1SMike Galbraith 595923c42c1SMike Galbraith p = buf; 596923c42c1SMike Galbraith while(*p) { 597923c42c1SMike Galbraith if (!isdigit(*p)) 598923c42c1SMike Galbraith goto out_free; 599923c42c1SMike Galbraith p++; 600923c42c1SMike Galbraith } 601923c42c1SMike Galbraith tmp = strtoul(buf, NULL, 10); 602923c42c1SMike Galbraith *target = tmp; 603923c42c1SMike Galbraith out_free: 604923c42c1SMike Galbraith free(buf); 605923c42c1SMike Galbraith } 606923c42c1SMike Galbraith 607923c42c1SMike Galbraith static void prompt_percent(int *target, const char *msg) 608923c42c1SMike Galbraith { 609923c42c1SMike Galbraith int tmp = 0; 610923c42c1SMike Galbraith 611923c42c1SMike Galbraith prompt_integer(&tmp, msg); 612923c42c1SMike Galbraith if (tmp >= 0 && tmp <= 100) 613923c42c1SMike Galbraith *target = tmp; 614923c42c1SMike Galbraith } 615923c42c1SMike Galbraith 616923c42c1SMike Galbraith static void prompt_symbol(struct sym_entry **target, const char *msg) 617923c42c1SMike Galbraith { 618923c42c1SMike Galbraith char *buf = malloc(0), *p; 619923c42c1SMike Galbraith struct sym_entry *syme = *target, *n, *found = NULL; 620923c42c1SMike Galbraith size_t dummy = 0; 621923c42c1SMike Galbraith 622923c42c1SMike Galbraith /* zero counters of active symbol */ 623923c42c1SMike Galbraith if (syme) { 624923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 625923c42c1SMike Galbraith __zero_source_counters(syme); 626923c42c1SMike Galbraith *target = NULL; 627923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 628923c42c1SMike Galbraith } 629923c42c1SMike Galbraith 630923c42c1SMike Galbraith fprintf(stdout, "\n%s: ", msg); 631923c42c1SMike Galbraith if (getline(&buf, &dummy, stdin) < 0) 632923c42c1SMike Galbraith goto out_free; 633923c42c1SMike Galbraith 634923c42c1SMike Galbraith p = strchr(buf, '\n'); 635923c42c1SMike Galbraith if (p) 636923c42c1SMike Galbraith *p = 0; 637923c42c1SMike Galbraith 638923c42c1SMike Galbraith pthread_mutex_lock(&active_symbols_lock); 639923c42c1SMike Galbraith syme = list_entry(active_symbols.next, struct sym_entry, node); 640923c42c1SMike Galbraith pthread_mutex_unlock(&active_symbols_lock); 641923c42c1SMike Galbraith 642923c42c1SMike Galbraith list_for_each_entry_safe_from(syme, n, &active_symbols, node) { 643923c42c1SMike Galbraith struct symbol *sym = (struct symbol *)(syme + 1); 644923c42c1SMike Galbraith 645923c42c1SMike Galbraith if (!strcmp(buf, sym->name)) { 646923c42c1SMike Galbraith found = syme; 647923c42c1SMike Galbraith break; 648923c42c1SMike Galbraith } 649923c42c1SMike Galbraith } 650923c42c1SMike Galbraith 651923c42c1SMike Galbraith if (!found) { 652923c42c1SMike Galbraith fprintf(stderr, "Sorry, %s is not active.\n", sym_filter); 653923c42c1SMike Galbraith sleep(1); 654923c42c1SMike Galbraith return; 655923c42c1SMike Galbraith } else 656923c42c1SMike Galbraith parse_source(found); 657923c42c1SMike Galbraith 658923c42c1SMike Galbraith out_free: 659923c42c1SMike Galbraith free(buf); 660923c42c1SMike Galbraith } 661923c42c1SMike Galbraith 662091bd2e9SMike Galbraith static void print_mapped_keys(void) 663923c42c1SMike Galbraith { 664091bd2e9SMike Galbraith char *name = NULL; 665091bd2e9SMike Galbraith 666091bd2e9SMike Galbraith if (sym_filter_entry) { 667091bd2e9SMike Galbraith struct symbol *sym = (struct symbol *)(sym_filter_entry+1); 668091bd2e9SMike Galbraith name = sym->name; 669091bd2e9SMike Galbraith } 670091bd2e9SMike Galbraith 671091bd2e9SMike Galbraith fprintf(stdout, "\nMapped keys:\n"); 672091bd2e9SMike Galbraith fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs); 673091bd2e9SMike Galbraith fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries); 674091bd2e9SMike Galbraith 675091bd2e9SMike Galbraith if (nr_counters > 1) 67646ab9764SMike Galbraith fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter)); 677091bd2e9SMike Galbraith 678091bd2e9SMike Galbraith fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter); 679091bd2e9SMike Galbraith 68083a0944fSIngo Molnar if (vmlinux_name) { 681091bd2e9SMike Galbraith fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter); 682091bd2e9SMike Galbraith fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); 683091bd2e9SMike Galbraith fprintf(stdout, "\t[S] stop annotation.\n"); 684091bd2e9SMike Galbraith } 685091bd2e9SMike Galbraith 686091bd2e9SMike Galbraith if (nr_counters > 1) 68746ab9764SMike Galbraith fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0); 688091bd2e9SMike Galbraith 6898ffcda17SArnaldo Carvalho de Melo fprintf(stdout, 6908ffcda17SArnaldo Carvalho de Melo "\t[K] hide kernel_symbols symbols. \t(%s)\n", 6918ffcda17SArnaldo Carvalho de Melo hide_kernel_symbols ? "yes" : "no"); 6928ffcda17SArnaldo Carvalho de Melo fprintf(stdout, 6938ffcda17SArnaldo Carvalho de Melo "\t[U] hide user symbols. \t(%s)\n", 6948ffcda17SArnaldo Carvalho de Melo hide_user_symbols ? "yes" : "no"); 69546ab9764SMike Galbraith fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0); 696091bd2e9SMike Galbraith fprintf(stdout, "\t[qQ] quit.\n"); 697091bd2e9SMike Galbraith } 698091bd2e9SMike Galbraith 699091bd2e9SMike Galbraith static int key_mapped(int c) 700091bd2e9SMike Galbraith { 701091bd2e9SMike Galbraith switch (c) { 702091bd2e9SMike Galbraith case 'd': 703091bd2e9SMike Galbraith case 'e': 704091bd2e9SMike Galbraith case 'f': 705091bd2e9SMike Galbraith case 'z': 706091bd2e9SMike Galbraith case 'q': 707091bd2e9SMike Galbraith case 'Q': 7088ffcda17SArnaldo Carvalho de Melo case 'K': 7098ffcda17SArnaldo Carvalho de Melo case 'U': 710091bd2e9SMike Galbraith return 1; 711091bd2e9SMike Galbraith case 'E': 712091bd2e9SMike Galbraith case 'w': 713091bd2e9SMike Galbraith return nr_counters > 1 ? 1 : 0; 714091bd2e9SMike Galbraith case 'F': 715091bd2e9SMike Galbraith case 's': 716091bd2e9SMike Galbraith case 'S': 71783a0944fSIngo Molnar return vmlinux_name ? 1 : 0; 71883a0944fSIngo Molnar default: 71983a0944fSIngo Molnar break; 720091bd2e9SMike Galbraith } 721091bd2e9SMike Galbraith 722091bd2e9SMike Galbraith return 0; 723923c42c1SMike Galbraith } 724923c42c1SMike Galbraith 725923c42c1SMike Galbraith static void handle_keypress(int c) 726923c42c1SMike Galbraith { 727091bd2e9SMike Galbraith if (!key_mapped(c)) { 728091bd2e9SMike Galbraith struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; 729091bd2e9SMike Galbraith struct termios tc, save; 730091bd2e9SMike Galbraith 731091bd2e9SMike Galbraith print_mapped_keys(); 732091bd2e9SMike Galbraith fprintf(stdout, "\nEnter selection, or unmapped key to continue: "); 733091bd2e9SMike Galbraith fflush(stdout); 734091bd2e9SMike Galbraith 735091bd2e9SMike Galbraith tcgetattr(0, &save); 736091bd2e9SMike Galbraith tc = save; 737091bd2e9SMike Galbraith tc.c_lflag &= ~(ICANON | ECHO); 738091bd2e9SMike Galbraith tc.c_cc[VMIN] = 0; 739091bd2e9SMike Galbraith tc.c_cc[VTIME] = 0; 740091bd2e9SMike Galbraith tcsetattr(0, TCSANOW, &tc); 741091bd2e9SMike Galbraith 742091bd2e9SMike Galbraith poll(&stdin_poll, 1, -1); 743091bd2e9SMike Galbraith c = getc(stdin); 744091bd2e9SMike Galbraith 745091bd2e9SMike Galbraith tcsetattr(0, TCSAFLUSH, &save); 746091bd2e9SMike Galbraith if (!key_mapped(c)) 747091bd2e9SMike Galbraith return; 748091bd2e9SMike Galbraith } 749091bd2e9SMike Galbraith 750923c42c1SMike Galbraith switch (c) { 751923c42c1SMike Galbraith case 'd': 752923c42c1SMike Galbraith prompt_integer(&delay_secs, "Enter display delay"); 753dc79959aSTim Blechmann if (delay_secs < 1) 754dc79959aSTim Blechmann delay_secs = 1; 755923c42c1SMike Galbraith break; 756923c42c1SMike Galbraith case 'e': 757923c42c1SMike Galbraith prompt_integer(&print_entries, "Enter display entries (lines)"); 7583b6ed988SArnaldo Carvalho de Melo if (print_entries == 0) { 75913cc5079SArnaldo Carvalho de Melo sig_winch_handler(SIGWINCH); 7603b6ed988SArnaldo Carvalho de Melo signal(SIGWINCH, sig_winch_handler); 7613b6ed988SArnaldo Carvalho de Melo } else 7623b6ed988SArnaldo Carvalho de Melo signal(SIGWINCH, SIG_DFL); 763923c42c1SMike Galbraith break; 764923c42c1SMike Galbraith case 'E': 765923c42c1SMike Galbraith if (nr_counters > 1) { 766923c42c1SMike Galbraith int i; 767923c42c1SMike Galbraith 768923c42c1SMike Galbraith fprintf(stderr, "\nAvailable events:"); 769923c42c1SMike Galbraith for (i = 0; i < nr_counters; i++) 770923c42c1SMike Galbraith fprintf(stderr, "\n\t%d %s", i, event_name(i)); 771923c42c1SMike Galbraith 772923c42c1SMike Galbraith prompt_integer(&sym_counter, "Enter details event counter"); 773923c42c1SMike Galbraith 774923c42c1SMike Galbraith if (sym_counter >= nr_counters) { 775923c42c1SMike Galbraith fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0)); 776923c42c1SMike Galbraith sym_counter = 0; 777923c42c1SMike Galbraith sleep(1); 778923c42c1SMike Galbraith } 779923c42c1SMike Galbraith } else sym_counter = 0; 780923c42c1SMike Galbraith break; 781923c42c1SMike Galbraith case 'f': 782923c42c1SMike Galbraith prompt_integer(&count_filter, "Enter display event count filter"); 783923c42c1SMike Galbraith break; 784923c42c1SMike Galbraith case 'F': 785923c42c1SMike Galbraith prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)"); 786923c42c1SMike Galbraith break; 7878ffcda17SArnaldo Carvalho de Melo case 'K': 7888ffcda17SArnaldo Carvalho de Melo hide_kernel_symbols = !hide_kernel_symbols; 7898ffcda17SArnaldo Carvalho de Melo break; 790923c42c1SMike Galbraith case 'q': 791923c42c1SMike Galbraith case 'Q': 792923c42c1SMike Galbraith printf("exiting.\n"); 793923c42c1SMike Galbraith exit(0); 794923c42c1SMike Galbraith case 's': 795923c42c1SMike Galbraith prompt_symbol(&sym_filter_entry, "Enter details symbol"); 796923c42c1SMike Galbraith break; 797923c42c1SMike Galbraith case 'S': 798923c42c1SMike Galbraith if (!sym_filter_entry) 799923c42c1SMike Galbraith break; 800923c42c1SMike Galbraith else { 801923c42c1SMike Galbraith struct sym_entry *syme = sym_filter_entry; 802923c42c1SMike Galbraith 803923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 804923c42c1SMike Galbraith sym_filter_entry = NULL; 805923c42c1SMike Galbraith __zero_source_counters(syme); 806923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 807923c42c1SMike Galbraith } 808923c42c1SMike Galbraith break; 8098ffcda17SArnaldo Carvalho de Melo case 'U': 8108ffcda17SArnaldo Carvalho de Melo hide_user_symbols = !hide_user_symbols; 8118ffcda17SArnaldo Carvalho de Melo break; 81246ab9764SMike Galbraith case 'w': 81346ab9764SMike Galbraith display_weighted = ~display_weighted; 81446ab9764SMike Galbraith break; 815923c42c1SMike Galbraith case 'z': 816923c42c1SMike Galbraith zero = ~zero; 817923c42c1SMike Galbraith break; 81883a0944fSIngo Molnar default: 81983a0944fSIngo Molnar break; 820923c42c1SMike Galbraith } 821923c42c1SMike Galbraith } 822923c42c1SMike Galbraith 823f37a291cSIngo Molnar static void *display_thread(void *arg __used) 82486470930SIngo Molnar { 82586470930SIngo Molnar struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; 826923c42c1SMike Galbraith struct termios tc, save; 827923c42c1SMike Galbraith int delay_msecs, c; 82886470930SIngo Molnar 829923c42c1SMike Galbraith tcgetattr(0, &save); 830923c42c1SMike Galbraith tc = save; 831923c42c1SMike Galbraith tc.c_lflag &= ~(ICANON | ECHO); 832923c42c1SMike Galbraith tc.c_cc[VMIN] = 0; 833923c42c1SMike Galbraith tc.c_cc[VTIME] = 0; 834091bd2e9SMike Galbraith 835923c42c1SMike Galbraith repeat: 836923c42c1SMike Galbraith delay_msecs = delay_secs * 1000; 837923c42c1SMike Galbraith tcsetattr(0, TCSANOW, &tc); 838923c42c1SMike Galbraith /* trash return*/ 839923c42c1SMike Galbraith getc(stdin); 84086470930SIngo Molnar 84186470930SIngo Molnar do { 84286470930SIngo Molnar print_sym_table(); 84386470930SIngo Molnar } while (!poll(&stdin_poll, 1, delay_msecs) == 1); 84486470930SIngo Molnar 845923c42c1SMike Galbraith c = getc(stdin); 846923c42c1SMike Galbraith tcsetattr(0, TCSAFLUSH, &save); 847923c42c1SMike Galbraith 848923c42c1SMike Galbraith handle_keypress(c); 849923c42c1SMike Galbraith goto repeat; 85086470930SIngo Molnar 85186470930SIngo Molnar return NULL; 85286470930SIngo Molnar } 85386470930SIngo Molnar 8542ab52083SAnton Blanchard /* Tag samples to be skipped. */ 855f37a291cSIngo Molnar static const char *skip_symbols[] = { 8562ab52083SAnton Blanchard "default_idle", 8572ab52083SAnton Blanchard "cpu_idle", 8582ab52083SAnton Blanchard "enter_idle", 8592ab52083SAnton Blanchard "exit_idle", 8602ab52083SAnton Blanchard "mwait_idle", 86159b90056SArnaldo Carvalho de Melo "mwait_idle_with_hints", 8628357275bSArnaldo Carvalho de Melo "poll_idle", 8633a3393efSAnton Blanchard "ppc64_runlatch_off", 8643a3393efSAnton Blanchard "pseries_dedicated_idle_sleep", 8652ab52083SAnton Blanchard NULL 8662ab52083SAnton Blanchard }; 8672ab52083SAnton Blanchard 868439d473bSArnaldo Carvalho de Melo static int symbol_filter(struct map *map, struct symbol *sym) 86986470930SIngo Molnar { 87086470930SIngo Molnar struct sym_entry *syme; 87186470930SIngo Molnar const char *name = sym->name; 8722ab52083SAnton Blanchard int i; 87386470930SIngo Molnar 8743a3393efSAnton Blanchard /* 8753a3393efSAnton Blanchard * ppc64 uses function descriptors and appends a '.' to the 8763a3393efSAnton Blanchard * start of every instruction address. Remove it. 8773a3393efSAnton Blanchard */ 8783a3393efSAnton Blanchard if (name[0] == '.') 8793a3393efSAnton Blanchard name++; 8803a3393efSAnton Blanchard 88186470930SIngo Molnar if (!strcmp(name, "_text") || 88286470930SIngo Molnar !strcmp(name, "_etext") || 88386470930SIngo Molnar !strcmp(name, "_sinittext") || 88486470930SIngo Molnar !strncmp("init_module", name, 11) || 88586470930SIngo Molnar !strncmp("cleanup_module", name, 14) || 88686470930SIngo Molnar strstr(name, "_text_start") || 88786470930SIngo Molnar strstr(name, "_text_end")) 88886470930SIngo Molnar return 1; 88986470930SIngo Molnar 89000a192b3SArnaldo Carvalho de Melo syme = symbol__priv(sym); 891439d473bSArnaldo Carvalho de Melo syme->map = map; 892923c42c1SMike Galbraith pthread_mutex_init(&syme->source_lock, NULL); 893923c42c1SMike Galbraith if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) 894923c42c1SMike Galbraith sym_filter_entry = syme; 895923c42c1SMike Galbraith 8962ab52083SAnton Blanchard for (i = 0; skip_symbols[i]; i++) { 8972ab52083SAnton Blanchard if (!strcmp(skip_symbols[i], name)) { 89886470930SIngo Molnar syme->skip = 1; 8992ab52083SAnton Blanchard break; 9002ab52083SAnton Blanchard } 9012ab52083SAnton Blanchard } 90286470930SIngo Molnar 90313cc5079SArnaldo Carvalho de Melo if (!syme->skip) 90413cc5079SArnaldo Carvalho de Melo syme->name_len = strlen(sym->name); 90513cc5079SArnaldo Carvalho de Melo 90686470930SIngo Molnar return 0; 90786470930SIngo Molnar } 90886470930SIngo Molnar 90986470930SIngo Molnar static int parse_symbols(void) 91086470930SIngo Molnar { 91100a192b3SArnaldo Carvalho de Melo if (dsos__load_kernel(vmlinux_name, symbol_filter, 1) <= 0) 91286470930SIngo Molnar return -1; 91386470930SIngo Molnar 91486470930SIngo Molnar if (dump_symtab) 915439d473bSArnaldo Carvalho de Melo dsos__fprintf(stderr); 91686470930SIngo Molnar 91786470930SIngo Molnar return 0; 91886470930SIngo Molnar } 91986470930SIngo Molnar 9205b2bb75aSArnaldo Carvalho de Melo static void event__process_sample(const event_t *self, int counter) 92186470930SIngo Molnar { 9225b2bb75aSArnaldo Carvalho de Melo u64 ip = self->ip.ip; 923439d473bSArnaldo Carvalho de Melo struct map *map; 9245b2bb75aSArnaldo Carvalho de Melo struct sym_entry *syme; 9255b2bb75aSArnaldo Carvalho de Melo struct symbol *sym; 9268ffcda17SArnaldo Carvalho de Melo u8 origin = self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 92786470930SIngo Molnar 9288ffcda17SArnaldo Carvalho de Melo switch (origin) { 9295b2bb75aSArnaldo Carvalho de Melo case PERF_RECORD_MISC_USER: { 9308ffcda17SArnaldo Carvalho de Melo struct thread *thread; 9315b2bb75aSArnaldo Carvalho de Melo 9328ffcda17SArnaldo Carvalho de Melo if (hide_user_symbols) 9338ffcda17SArnaldo Carvalho de Melo return; 9348ffcda17SArnaldo Carvalho de Melo 9358ffcda17SArnaldo Carvalho de Melo thread = threads__findnew(self->ip.pid); 9365b2bb75aSArnaldo Carvalho de Melo if (thread == NULL) 9375b2bb75aSArnaldo Carvalho de Melo return; 9385b2bb75aSArnaldo Carvalho de Melo 9395b2bb75aSArnaldo Carvalho de Melo map = thread__find_map(thread, ip); 9405b2bb75aSArnaldo Carvalho de Melo if (map != NULL) { 9415b2bb75aSArnaldo Carvalho de Melo ip = map->map_ip(map, ip); 94266bd8424SArnaldo Carvalho de Melo sym = map__find_symbol(map, ip, symbol_filter); 9435b2bb75aSArnaldo Carvalho de Melo if (sym == NULL) 9445b2bb75aSArnaldo Carvalho de Melo return; 9455b2bb75aSArnaldo Carvalho de Melo userspace_samples++; 9465b2bb75aSArnaldo Carvalho de Melo break; 9475b2bb75aSArnaldo Carvalho de Melo } 9485b2bb75aSArnaldo Carvalho de Melo } 9495b2bb75aSArnaldo Carvalho de Melo /* 9505b2bb75aSArnaldo Carvalho de Melo * If this is outside of all known maps, 9515b2bb75aSArnaldo Carvalho de Melo * and is a negative address, try to look it 9525b2bb75aSArnaldo Carvalho de Melo * up in the kernel dso, as it might be a 9535b2bb75aSArnaldo Carvalho de Melo * vsyscall or vdso (which executes in user-mode). 9545b2bb75aSArnaldo Carvalho de Melo */ 9555b2bb75aSArnaldo Carvalho de Melo if ((long long)ip >= 0) 9565b2bb75aSArnaldo Carvalho de Melo return; 9575b2bb75aSArnaldo Carvalho de Melo /* Fall thru */ 9585b2bb75aSArnaldo Carvalho de Melo case PERF_RECORD_MISC_KERNEL: 9598ffcda17SArnaldo Carvalho de Melo if (hide_kernel_symbols) 9608ffcda17SArnaldo Carvalho de Melo return; 9618ffcda17SArnaldo Carvalho de Melo 9625b2bb75aSArnaldo Carvalho de Melo sym = kernel_maps__find_symbol(ip, &map); 9635b2bb75aSArnaldo Carvalho de Melo if (sym == NULL) 9645b2bb75aSArnaldo Carvalho de Melo return; 9655b2bb75aSArnaldo Carvalho de Melo break; 9665b2bb75aSArnaldo Carvalho de Melo default: 9675b2bb75aSArnaldo Carvalho de Melo return; 9685b2bb75aSArnaldo Carvalho de Melo } 9695b2bb75aSArnaldo Carvalho de Melo 97000a192b3SArnaldo Carvalho de Melo syme = symbol__priv(sym); 97186470930SIngo Molnar 97286470930SIngo Molnar if (!syme->skip) { 97386470930SIngo Molnar syme->count[counter]++; 9748ffcda17SArnaldo Carvalho de Melo syme->origin = origin; 975923c42c1SMike Galbraith record_precise_ip(syme, counter, ip); 97686470930SIngo Molnar pthread_mutex_lock(&active_symbols_lock); 97786470930SIngo Molnar if (list_empty(&syme->node) || !syme->node.next) 97886470930SIngo Molnar __list_insert_active_sym(syme); 97986470930SIngo Molnar pthread_mutex_unlock(&active_symbols_lock); 9805b2bb75aSArnaldo Carvalho de Melo ++samples; 98186470930SIngo Molnar return; 98286470930SIngo Molnar } 98386470930SIngo Molnar } 98486470930SIngo Molnar 9855b2bb75aSArnaldo Carvalho de Melo static void event__process_mmap(event_t *self) 98686470930SIngo Molnar { 9875b2bb75aSArnaldo Carvalho de Melo struct thread *thread = threads__findnew(self->mmap.pid); 98886470930SIngo Molnar 9895b2bb75aSArnaldo Carvalho de Melo if (thread != NULL) { 99000a192b3SArnaldo Carvalho de Melo struct map *map = map__new(&self->mmap, NULL, 0); 9915b2bb75aSArnaldo Carvalho de Melo if (map != NULL) 9925b2bb75aSArnaldo Carvalho de Melo thread__insert_map(thread, map); 9935b2bb75aSArnaldo Carvalho de Melo } 99486470930SIngo Molnar } 99586470930SIngo Molnar 9965b2bb75aSArnaldo Carvalho de Melo static void event__process_comm(event_t *self) 9975b2bb75aSArnaldo Carvalho de Melo { 9985b2bb75aSArnaldo Carvalho de Melo struct thread *thread = threads__findnew(self->comm.pid); 9995b2bb75aSArnaldo Carvalho de Melo 10005b2bb75aSArnaldo Carvalho de Melo if (thread != NULL) 10015b2bb75aSArnaldo Carvalho de Melo thread__set_comm(thread, self->comm.comm); 10025b2bb75aSArnaldo Carvalho de Melo } 10035b2bb75aSArnaldo Carvalho de Melo 10045b2bb75aSArnaldo Carvalho de Melo static int event__process(event_t *event) 10055b2bb75aSArnaldo Carvalho de Melo { 10065b2bb75aSArnaldo Carvalho de Melo switch (event->header.type) { 10075b2bb75aSArnaldo Carvalho de Melo case PERF_RECORD_COMM: 10085b2bb75aSArnaldo Carvalho de Melo event__process_comm(event); 10095b2bb75aSArnaldo Carvalho de Melo break; 10105b2bb75aSArnaldo Carvalho de Melo case PERF_RECORD_MMAP: 10115b2bb75aSArnaldo Carvalho de Melo event__process_mmap(event); 10125b2bb75aSArnaldo Carvalho de Melo break; 10135b2bb75aSArnaldo Carvalho de Melo default: 10145b2bb75aSArnaldo Carvalho de Melo break; 10155b2bb75aSArnaldo Carvalho de Melo } 10165b2bb75aSArnaldo Carvalho de Melo 10175b2bb75aSArnaldo Carvalho de Melo return 0; 101886470930SIngo Molnar } 101986470930SIngo Molnar 102086470930SIngo Molnar struct mmap_data { 102186470930SIngo Molnar int counter; 102286470930SIngo Molnar void *base; 1023f37a291cSIngo Molnar int mask; 102486470930SIngo Molnar unsigned int prev; 102586470930SIngo Molnar }; 102686470930SIngo Molnar 102786470930SIngo Molnar static unsigned int mmap_read_head(struct mmap_data *md) 102886470930SIngo Molnar { 1029cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 103086470930SIngo Molnar int head; 103186470930SIngo Molnar 103286470930SIngo Molnar head = pc->data_head; 103386470930SIngo Molnar rmb(); 103486470930SIngo Molnar 103586470930SIngo Molnar return head; 103686470930SIngo Molnar } 103786470930SIngo Molnar 10382f01190aSFrederic Weisbecker static void mmap_read_counter(struct mmap_data *md) 103986470930SIngo Molnar { 104086470930SIngo Molnar unsigned int head = mmap_read_head(md); 104186470930SIngo Molnar unsigned int old = md->prev; 104286470930SIngo Molnar unsigned char *data = md->base + page_size; 104386470930SIngo Molnar int diff; 104486470930SIngo Molnar 104586470930SIngo Molnar /* 104686470930SIngo Molnar * If we're further behind than half the buffer, there's a chance 104786470930SIngo Molnar * the writer will bite our tail and mess up the samples under us. 104886470930SIngo Molnar * 104986470930SIngo Molnar * If we somehow ended up ahead of the head, we got messed up. 105086470930SIngo Molnar * 105186470930SIngo Molnar * In either case, truncate and restart at head. 105286470930SIngo Molnar */ 105386470930SIngo Molnar diff = head - old; 105486470930SIngo Molnar if (diff > md->mask / 2 || diff < 0) { 1055f4f0b418SMike Galbraith fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 105686470930SIngo Molnar 105786470930SIngo Molnar /* 105886470930SIngo Molnar * head points to a known good entry, start there. 105986470930SIngo Molnar */ 106086470930SIngo Molnar old = head; 106186470930SIngo Molnar } 106286470930SIngo Molnar 106386470930SIngo Molnar for (; old != head;) { 106486470930SIngo Molnar event_t *event = (event_t *)&data[old & md->mask]; 106586470930SIngo Molnar 106686470930SIngo Molnar event_t event_copy; 106786470930SIngo Molnar 106886470930SIngo Molnar size_t size = event->header.size; 106986470930SIngo Molnar 107086470930SIngo Molnar /* 107186470930SIngo Molnar * Event straddles the mmap boundary -- header should always 107286470930SIngo Molnar * be inside due to u64 alignment of output. 107386470930SIngo Molnar */ 107486470930SIngo Molnar if ((old & md->mask) + size != ((old + size) & md->mask)) { 107586470930SIngo Molnar unsigned int offset = old; 107686470930SIngo Molnar unsigned int len = min(sizeof(*event), size), cpy; 107786470930SIngo Molnar void *dst = &event_copy; 107886470930SIngo Molnar 107986470930SIngo Molnar do { 108086470930SIngo Molnar cpy = min(md->mask + 1 - (offset & md->mask), len); 108186470930SIngo Molnar memcpy(dst, &data[offset & md->mask], cpy); 108286470930SIngo Molnar offset += cpy; 108386470930SIngo Molnar dst += cpy; 108486470930SIngo Molnar len -= cpy; 108586470930SIngo Molnar } while (len); 108686470930SIngo Molnar 108786470930SIngo Molnar event = &event_copy; 108886470930SIngo Molnar } 108986470930SIngo Molnar 10905b2bb75aSArnaldo Carvalho de Melo if (event->header.type == PERF_RECORD_SAMPLE) 10915b2bb75aSArnaldo Carvalho de Melo event__process_sample(event, md->counter); 10925b2bb75aSArnaldo Carvalho de Melo else 10935b2bb75aSArnaldo Carvalho de Melo event__process(event); 109486470930SIngo Molnar old += size; 109586470930SIngo Molnar } 109686470930SIngo Molnar 109786470930SIngo Molnar md->prev = old; 109886470930SIngo Molnar } 109986470930SIngo Molnar 110086470930SIngo Molnar static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; 110186470930SIngo Molnar static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; 110286470930SIngo Molnar 11032f01190aSFrederic Weisbecker static void mmap_read(void) 11042f01190aSFrederic Weisbecker { 11052f01190aSFrederic Weisbecker int i, counter; 11062f01190aSFrederic Weisbecker 11072f01190aSFrederic Weisbecker for (i = 0; i < nr_cpus; i++) { 11082f01190aSFrederic Weisbecker for (counter = 0; counter < nr_counters; counter++) 11092f01190aSFrederic Weisbecker mmap_read_counter(&mmap_array[i][counter]); 11102f01190aSFrederic Weisbecker } 11112f01190aSFrederic Weisbecker } 11122f01190aSFrederic Weisbecker 1113716c69feSIngo Molnar int nr_poll; 1114716c69feSIngo Molnar int group_fd; 1115716c69feSIngo Molnar 1116716c69feSIngo Molnar static void start_counter(int i, int counter) 111786470930SIngo Molnar { 1118cdd6c482SIngo Molnar struct perf_event_attr *attr; 11190fdc7e67SMike Galbraith int cpu; 112086470930SIngo Molnar 112186470930SIngo Molnar cpu = profile_cpu; 112286470930SIngo Molnar if (target_pid == -1 && profile_cpu == -1) 112386470930SIngo Molnar cpu = i; 112486470930SIngo Molnar 112586470930SIngo Molnar attr = attrs + counter; 112686470930SIngo Molnar 112786470930SIngo Molnar attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; 11287e4ff9e3SMike Galbraith 11297e4ff9e3SMike Galbraith if (freq) { 11307e4ff9e3SMike Galbraith attr->sample_type |= PERF_SAMPLE_PERIOD; 11317e4ff9e3SMike Galbraith attr->freq = 1; 11327e4ff9e3SMike Galbraith attr->sample_freq = freq; 11337e4ff9e3SMike Galbraith } 11347e4ff9e3SMike Galbraith 11350fdc7e67SMike Galbraith attr->inherit = (cpu < 0) && inherit; 11365b2bb75aSArnaldo Carvalho de Melo attr->mmap = 1; 113786470930SIngo Molnar 1138716c69feSIngo Molnar try_again: 1139cdd6c482SIngo Molnar fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0); 1140716c69feSIngo Molnar 114186470930SIngo Molnar if (fd[i][counter] < 0) { 114286470930SIngo Molnar int err = errno; 114386470930SIngo Molnar 1144c10edee2SPekka Enberg if (err == EPERM || err == EACCES) 1145716c69feSIngo Molnar die("No permission - are you root?\n"); 1146716c69feSIngo Molnar /* 1147716c69feSIngo Molnar * If it's cycles then fall back to hrtimer 1148716c69feSIngo Molnar * based cpu-clock-tick sw counter, which 1149716c69feSIngo Molnar * is always available even if no PMU support: 1150716c69feSIngo Molnar */ 1151716c69feSIngo Molnar if (attr->type == PERF_TYPE_HARDWARE 1152f4dbfa8fSPeter Zijlstra && attr->config == PERF_COUNT_HW_CPU_CYCLES) { 1153716c69feSIngo Molnar 11543da297a6SIngo Molnar if (verbose) 1155716c69feSIngo Molnar warning(" ... trying to fall back to cpu-clock-ticks\n"); 11563da297a6SIngo Molnar 1157716c69feSIngo Molnar attr->type = PERF_TYPE_SOFTWARE; 1158f4dbfa8fSPeter Zijlstra attr->config = PERF_COUNT_SW_CPU_CLOCK; 1159716c69feSIngo Molnar goto try_again; 1160716c69feSIngo Molnar } 116130c806a0SIngo Molnar printf("\n"); 116230c806a0SIngo Molnar error("perfcounter syscall returned with %d (%s)\n", 116330c806a0SIngo Molnar fd[i][counter], strerror(err)); 1164cdd6c482SIngo Molnar die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); 116586470930SIngo Molnar exit(-1); 116686470930SIngo Molnar } 116786470930SIngo Molnar assert(fd[i][counter] >= 0); 116886470930SIngo Molnar fcntl(fd[i][counter], F_SETFL, O_NONBLOCK); 116986470930SIngo Molnar 117086470930SIngo Molnar /* 117186470930SIngo Molnar * First counter acts as the group leader: 117286470930SIngo Molnar */ 117386470930SIngo Molnar if (group && group_fd == -1) 117486470930SIngo Molnar group_fd = fd[i][counter]; 117586470930SIngo Molnar 117686470930SIngo Molnar event_array[nr_poll].fd = fd[i][counter]; 117786470930SIngo Molnar event_array[nr_poll].events = POLLIN; 117886470930SIngo Molnar nr_poll++; 117986470930SIngo Molnar 118086470930SIngo Molnar mmap_array[i][counter].counter = counter; 118186470930SIngo Molnar mmap_array[i][counter].prev = 0; 118286470930SIngo Molnar mmap_array[i][counter].mask = mmap_pages*page_size - 1; 118386470930SIngo Molnar mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size, 118486470930SIngo Molnar PROT_READ, MAP_SHARED, fd[i][counter], 0); 118586470930SIngo Molnar if (mmap_array[i][counter].base == MAP_FAILED) 118686470930SIngo Molnar die("failed to mmap with %d (%s)\n", errno, strerror(errno)); 118786470930SIngo Molnar } 1188716c69feSIngo Molnar 1189716c69feSIngo Molnar static int __cmd_top(void) 1190716c69feSIngo Molnar { 1191716c69feSIngo Molnar pthread_t thread; 1192716c69feSIngo Molnar int i, counter; 1193716c69feSIngo Molnar int ret; 1194716c69feSIngo Molnar 11955b2bb75aSArnaldo Carvalho de Melo if (target_pid != -1) 11965b2bb75aSArnaldo Carvalho de Melo event__synthesize_thread(target_pid, event__process); 11975b2bb75aSArnaldo Carvalho de Melo else 11985b2bb75aSArnaldo Carvalho de Melo event__synthesize_threads(event__process); 11995b2bb75aSArnaldo Carvalho de Melo 1200716c69feSIngo Molnar for (i = 0; i < nr_cpus; i++) { 1201716c69feSIngo Molnar group_fd = -1; 1202716c69feSIngo Molnar for (counter = 0; counter < nr_counters; counter++) 1203716c69feSIngo Molnar start_counter(i, counter); 120486470930SIngo Molnar } 120586470930SIngo Molnar 12062f01190aSFrederic Weisbecker /* Wait for a minimal set of events before starting the snapshot */ 12072f01190aSFrederic Weisbecker poll(event_array, nr_poll, 100); 12082f01190aSFrederic Weisbecker 12092f01190aSFrederic Weisbecker mmap_read(); 12102f01190aSFrederic Weisbecker 121186470930SIngo Molnar if (pthread_create(&thread, NULL, display_thread, NULL)) { 121286470930SIngo Molnar printf("Could not create display thread.\n"); 121386470930SIngo Molnar exit(-1); 121486470930SIngo Molnar } 121586470930SIngo Molnar 121686470930SIngo Molnar if (realtime_prio) { 121786470930SIngo Molnar struct sched_param param; 121886470930SIngo Molnar 121986470930SIngo Molnar param.sched_priority = realtime_prio; 122086470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 122186470930SIngo Molnar printf("Could not set realtime priority.\n"); 122286470930SIngo Molnar exit(-1); 122386470930SIngo Molnar } 122486470930SIngo Molnar } 122586470930SIngo Molnar 122686470930SIngo Molnar while (1) { 122786470930SIngo Molnar int hits = samples; 122886470930SIngo Molnar 12292f01190aSFrederic Weisbecker mmap_read(); 123086470930SIngo Molnar 123186470930SIngo Molnar if (hits == samples) 123286470930SIngo Molnar ret = poll(event_array, nr_poll, 100); 123386470930SIngo Molnar } 123486470930SIngo Molnar 123586470930SIngo Molnar return 0; 123686470930SIngo Molnar } 123786470930SIngo Molnar 123886470930SIngo Molnar static const char * const top_usage[] = { 123986470930SIngo Molnar "perf top [<options>]", 124086470930SIngo Molnar NULL 124186470930SIngo Molnar }; 124286470930SIngo Molnar 124386470930SIngo Molnar static const struct option options[] = { 124486470930SIngo Molnar OPT_CALLBACK('e', "event", NULL, "event", 124586470930SIngo Molnar "event selector. use 'perf list' to list available events", 124686470930SIngo Molnar parse_events), 124786470930SIngo Molnar OPT_INTEGER('c', "count", &default_interval, 124886470930SIngo Molnar "event period to sample"), 124986470930SIngo Molnar OPT_INTEGER('p', "pid", &target_pid, 125086470930SIngo Molnar "profile events on existing pid"), 125186470930SIngo Molnar OPT_BOOLEAN('a', "all-cpus", &system_wide, 125286470930SIngo Molnar "system-wide collection from all CPUs"), 125386470930SIngo Molnar OPT_INTEGER('C', "CPU", &profile_cpu, 125486470930SIngo Molnar "CPU to profile on"), 125583a0944fSIngo Molnar OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), 12568ffcda17SArnaldo Carvalho de Melo OPT_BOOLEAN('K', "hide_kernel_symbols", &hide_kernel_symbols, 12578ffcda17SArnaldo Carvalho de Melo "hide kernel symbols"), 125886470930SIngo Molnar OPT_INTEGER('m', "mmap-pages", &mmap_pages, 125986470930SIngo Molnar "number of mmap data pages"), 126086470930SIngo Molnar OPT_INTEGER('r', "realtime", &realtime_prio, 126186470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 126286470930SIngo Molnar OPT_INTEGER('d', "delay", &delay_secs, 126386470930SIngo Molnar "number of seconds to delay between refreshes"), 126486470930SIngo Molnar OPT_BOOLEAN('D', "dump-symtab", &dump_symtab, 126586470930SIngo Molnar "dump the symbol table used for profiling"), 126686470930SIngo Molnar OPT_INTEGER('f', "count-filter", &count_filter, 126786470930SIngo Molnar "only display functions with more events than this"), 126886470930SIngo Molnar OPT_BOOLEAN('g', "group", &group, 126986470930SIngo Molnar "put the counters into a counter group"), 12700fdc7e67SMike Galbraith OPT_BOOLEAN('i', "inherit", &inherit, 12710fdc7e67SMike Galbraith "child tasks inherit counters"), 1272923c42c1SMike Galbraith OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name", 1273923c42c1SMike Galbraith "symbol to annotate - requires -k option"), 12741f208ea6SAnton Blanchard OPT_BOOLEAN('z', "zero", &zero, 127586470930SIngo Molnar "zero history across updates"), 127686470930SIngo Molnar OPT_INTEGER('F', "freq", &freq, 127786470930SIngo Molnar "profile at this frequency"), 127886470930SIngo Molnar OPT_INTEGER('E', "entries", &print_entries, 127986470930SIngo Molnar "display this many functions"), 12808ffcda17SArnaldo Carvalho de Melo OPT_BOOLEAN('U', "hide_user_symbols", &hide_user_symbols, 12818ffcda17SArnaldo Carvalho de Melo "hide user symbols"), 12823da297a6SIngo Molnar OPT_BOOLEAN('v', "verbose", &verbose, 12833da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 128486470930SIngo Molnar OPT_END() 128586470930SIngo Molnar }; 128686470930SIngo Molnar 1287f37a291cSIngo Molnar int cmd_top(int argc, const char **argv, const char *prefix __used) 128886470930SIngo Molnar { 128986470930SIngo Molnar int counter; 129086470930SIngo Molnar 129100a192b3SArnaldo Carvalho de Melo symbol__init(sizeof(struct sym_entry)); 129242976487SMike Galbraith 129386470930SIngo Molnar page_size = sysconf(_SC_PAGE_SIZE); 129486470930SIngo Molnar 129586470930SIngo Molnar argc = parse_options(argc, argv, options, top_usage, 0); 129686470930SIngo Molnar if (argc) 129786470930SIngo Molnar usage_with_options(top_usage, options); 129886470930SIngo Molnar 129986470930SIngo Molnar /* CPU and PID are mutually exclusive */ 130086470930SIngo Molnar if (target_pid != -1 && profile_cpu != -1) { 130186470930SIngo Molnar printf("WARNING: PID switch overriding CPU\n"); 130286470930SIngo Molnar sleep(1); 130386470930SIngo Molnar profile_cpu = -1; 130486470930SIngo Molnar } 130586470930SIngo Molnar 130686470930SIngo Molnar if (!nr_counters) 130786470930SIngo Molnar nr_counters = 1; 130886470930SIngo Molnar 130986470930SIngo Molnar if (delay_secs < 1) 131086470930SIngo Molnar delay_secs = 1; 131186470930SIngo Molnar 131286470930SIngo Molnar parse_symbols(); 1313923c42c1SMike Galbraith parse_source(sym_filter_entry); 131486470930SIngo Molnar 13157e4ff9e3SMike Galbraith 13167e4ff9e3SMike Galbraith /* 13177e4ff9e3SMike Galbraith * User specified count overrides default frequency. 13187e4ff9e3SMike Galbraith */ 13197e4ff9e3SMike Galbraith if (default_interval) 13207e4ff9e3SMike Galbraith freq = 0; 13217e4ff9e3SMike Galbraith else if (freq) { 13227e4ff9e3SMike Galbraith default_interval = freq; 13237e4ff9e3SMike Galbraith } else { 13247e4ff9e3SMike Galbraith fprintf(stderr, "frequency and count are zero, aborting\n"); 13257e4ff9e3SMike Galbraith exit(EXIT_FAILURE); 13267e4ff9e3SMike Galbraith } 13277e4ff9e3SMike Galbraith 132886470930SIngo Molnar /* 132986470930SIngo Molnar * Fill in the ones not specifically initialized via -c: 133086470930SIngo Molnar */ 133186470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) { 133286470930SIngo Molnar if (attrs[counter].sample_period) 133386470930SIngo Molnar continue; 133486470930SIngo Molnar 133586470930SIngo Molnar attrs[counter].sample_period = default_interval; 133686470930SIngo Molnar } 133786470930SIngo Molnar 133886470930SIngo Molnar nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); 133986470930SIngo Molnar assert(nr_cpus <= MAX_NR_CPUS); 134086470930SIngo Molnar assert(nr_cpus >= 0); 134186470930SIngo Molnar 134286470930SIngo Molnar if (target_pid != -1 || profile_cpu != -1) 134386470930SIngo Molnar nr_cpus = 1; 134486470930SIngo Molnar 134513cc5079SArnaldo Carvalho de Melo get_term_dimensions(&winsize); 13463b6ed988SArnaldo Carvalho de Melo if (print_entries == 0) { 134713cc5079SArnaldo Carvalho de Melo update_print_entries(&winsize); 13483b6ed988SArnaldo Carvalho de Melo signal(SIGWINCH, sig_winch_handler); 13493b6ed988SArnaldo Carvalho de Melo } 13503b6ed988SArnaldo Carvalho de Melo 135186470930SIngo Molnar return __cmd_top(); 135286470930SIngo Molnar } 1353