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; 6386470930SIngo Molnar static int print_entries = 15; 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 7986470930SIngo Molnar /* 80923c42c1SMike Galbraith * Source 81923c42c1SMike Galbraith */ 82923c42c1SMike Galbraith 83923c42c1SMike Galbraith struct source_line { 84923c42c1SMike Galbraith u64 eip; 85923c42c1SMike Galbraith unsigned long count[MAX_COUNTERS]; 86923c42c1SMike Galbraith char *line; 87923c42c1SMike Galbraith struct source_line *next; 88923c42c1SMike Galbraith }; 89923c42c1SMike Galbraith 90923c42c1SMike Galbraith static char *sym_filter = NULL; 91923c42c1SMike Galbraith struct sym_entry *sym_filter_entry = NULL; 92923c42c1SMike Galbraith static int sym_pcnt_filter = 5; 93923c42c1SMike Galbraith static int sym_counter = 0; 9446ab9764SMike Galbraith static int display_weighted = -1; 95923c42c1SMike Galbraith 96923c42c1SMike Galbraith /* 9786470930SIngo Molnar * Symbols 9886470930SIngo Molnar */ 9986470930SIngo Molnar 10086470930SIngo Molnar struct sym_entry { 10186470930SIngo Molnar struct rb_node rb_node; 10286470930SIngo Molnar struct list_head node; 10386470930SIngo Molnar unsigned long count[MAX_COUNTERS]; 10486470930SIngo Molnar unsigned long snap_count; 10586470930SIngo Molnar double weight; 10686470930SIngo Molnar int skip; 107439d473bSArnaldo Carvalho de Melo struct map *map; 108923c42c1SMike Galbraith struct source_line *source; 109923c42c1SMike Galbraith struct source_line *lines; 110923c42c1SMike Galbraith struct source_line **lines_tail; 111923c42c1SMike Galbraith pthread_mutex_t source_lock; 11286470930SIngo Molnar }; 11386470930SIngo Molnar 114923c42c1SMike Galbraith /* 115923c42c1SMike Galbraith * Source functions 116923c42c1SMike Galbraith */ 117923c42c1SMike Galbraith 118923c42c1SMike Galbraith static void parse_source(struct sym_entry *syme) 119923c42c1SMike Galbraith { 120923c42c1SMike Galbraith struct symbol *sym; 121439d473bSArnaldo Carvalho de Melo struct map *map; 122923c42c1SMike Galbraith FILE *file; 12383a0944fSIngo Molnar char command[PATH_MAX*2]; 124439d473bSArnaldo Carvalho de Melo const char *path; 125439d473bSArnaldo Carvalho de Melo u64 len; 126923c42c1SMike Galbraith 127923c42c1SMike Galbraith if (!syme) 128923c42c1SMike Galbraith return; 129923c42c1SMike Galbraith 130923c42c1SMike Galbraith if (syme->lines) { 131923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 132923c42c1SMike Galbraith goto out_assign; 133923c42c1SMike Galbraith } 134923c42c1SMike Galbraith 135923c42c1SMike Galbraith sym = (struct symbol *)(syme + 1); 136439d473bSArnaldo Carvalho de Melo map = syme->map; 137439d473bSArnaldo Carvalho de Melo path = map->dso->long_name; 138923c42c1SMike Galbraith 139923c42c1SMike Galbraith len = sym->end - sym->start; 140923c42c1SMike Galbraith 141439d473bSArnaldo Carvalho de Melo sprintf(command, 142439d473bSArnaldo Carvalho de Melo "objdump --start-address=0x%016Lx " 143439d473bSArnaldo Carvalho de Melo "--stop-address=0x%016Lx -dS %s", 144c88e4bf6SArnaldo Carvalho de Melo map->unmap_ip(map, sym->start), 145c88e4bf6SArnaldo Carvalho de Melo map->unmap_ip(map, sym->end), path); 146923c42c1SMike Galbraith 147923c42c1SMike Galbraith file = popen(command, "r"); 148923c42c1SMike Galbraith if (!file) 149923c42c1SMike Galbraith return; 150923c42c1SMike Galbraith 151923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 152923c42c1SMike Galbraith syme->lines_tail = &syme->lines; 153923c42c1SMike Galbraith while (!feof(file)) { 154923c42c1SMike Galbraith struct source_line *src; 155923c42c1SMike Galbraith size_t dummy = 0; 156923c42c1SMike Galbraith char *c; 157923c42c1SMike Galbraith 158923c42c1SMike Galbraith src = malloc(sizeof(struct source_line)); 159923c42c1SMike Galbraith assert(src != NULL); 160923c42c1SMike Galbraith memset(src, 0, sizeof(struct source_line)); 161923c42c1SMike Galbraith 162923c42c1SMike Galbraith if (getline(&src->line, &dummy, file) < 0) 163923c42c1SMike Galbraith break; 164923c42c1SMike Galbraith if (!src->line) 165923c42c1SMike Galbraith break; 166923c42c1SMike Galbraith 167923c42c1SMike Galbraith c = strchr(src->line, '\n'); 168923c42c1SMike Galbraith if (c) 169923c42c1SMike Galbraith *c = 0; 170923c42c1SMike Galbraith 171923c42c1SMike Galbraith src->next = NULL; 172923c42c1SMike Galbraith *syme->lines_tail = src; 173923c42c1SMike Galbraith syme->lines_tail = &src->next; 174923c42c1SMike Galbraith 175923c42c1SMike Galbraith if (strlen(src->line)>8 && src->line[8] == ':') { 176923c42c1SMike Galbraith src->eip = strtoull(src->line, NULL, 16); 177c88e4bf6SArnaldo Carvalho de Melo src->eip = map->unmap_ip(map, src->eip); 178923c42c1SMike Galbraith } 179923c42c1SMike Galbraith if (strlen(src->line)>8 && src->line[16] == ':') { 180923c42c1SMike Galbraith src->eip = strtoull(src->line, NULL, 16); 181c88e4bf6SArnaldo Carvalho de Melo src->eip = map->unmap_ip(map, src->eip); 182923c42c1SMike Galbraith } 183923c42c1SMike Galbraith } 184923c42c1SMike Galbraith pclose(file); 185923c42c1SMike Galbraith out_assign: 186923c42c1SMike Galbraith sym_filter_entry = syme; 187923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 188923c42c1SMike Galbraith } 189923c42c1SMike Galbraith 190923c42c1SMike Galbraith static void __zero_source_counters(struct sym_entry *syme) 191923c42c1SMike Galbraith { 192923c42c1SMike Galbraith int i; 193923c42c1SMike Galbraith struct source_line *line; 194923c42c1SMike Galbraith 195923c42c1SMike Galbraith line = syme->lines; 196923c42c1SMike Galbraith while (line) { 197923c42c1SMike Galbraith for (i = 0; i < nr_counters; i++) 198923c42c1SMike Galbraith line->count[i] = 0; 199923c42c1SMike Galbraith line = line->next; 200923c42c1SMike Galbraith } 201923c42c1SMike Galbraith } 202923c42c1SMike Galbraith 203923c42c1SMike Galbraith static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip) 204923c42c1SMike Galbraith { 205923c42c1SMike Galbraith struct source_line *line; 206923c42c1SMike Galbraith 207923c42c1SMike Galbraith if (syme != sym_filter_entry) 208923c42c1SMike Galbraith return; 209923c42c1SMike Galbraith 210923c42c1SMike Galbraith if (pthread_mutex_trylock(&syme->source_lock)) 211923c42c1SMike Galbraith return; 212923c42c1SMike Galbraith 213923c42c1SMike Galbraith if (!syme->source) 214923c42c1SMike Galbraith goto out_unlock; 215923c42c1SMike Galbraith 216923c42c1SMike Galbraith for (line = syme->lines; line; line = line->next) { 217923c42c1SMike Galbraith if (line->eip == ip) { 218923c42c1SMike Galbraith line->count[counter]++; 219923c42c1SMike Galbraith break; 220923c42c1SMike Galbraith } 221923c42c1SMike Galbraith if (line->eip > ip) 222923c42c1SMike Galbraith break; 223923c42c1SMike Galbraith } 224923c42c1SMike Galbraith out_unlock: 225923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 226923c42c1SMike Galbraith } 227923c42c1SMike Galbraith 228923c42c1SMike Galbraith static void lookup_sym_source(struct sym_entry *syme) 229923c42c1SMike Galbraith { 230923c42c1SMike Galbraith struct symbol *symbol = (struct symbol *)(syme + 1); 231923c42c1SMike Galbraith struct source_line *line; 232923c42c1SMike Galbraith char pattern[PATH_MAX]; 233923c42c1SMike Galbraith 234923c42c1SMike Galbraith sprintf(pattern, "<%s>:", symbol->name); 235923c42c1SMike Galbraith 236923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 237923c42c1SMike Galbraith for (line = syme->lines; line; line = line->next) { 238923c42c1SMike Galbraith if (strstr(line->line, pattern)) { 239923c42c1SMike Galbraith syme->source = line; 240923c42c1SMike Galbraith break; 241923c42c1SMike Galbraith } 242923c42c1SMike Galbraith } 243923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 244923c42c1SMike Galbraith } 245923c42c1SMike Galbraith 246923c42c1SMike Galbraith static void show_lines(struct source_line *queue, int count, int total) 247923c42c1SMike Galbraith { 248923c42c1SMike Galbraith int i; 249923c42c1SMike Galbraith struct source_line *line; 250923c42c1SMike Galbraith 251923c42c1SMike Galbraith line = queue; 252923c42c1SMike Galbraith for (i = 0; i < count; i++) { 253923c42c1SMike Galbraith float pcnt = 100.0*(float)line->count[sym_counter]/(float)total; 254923c42c1SMike Galbraith 255923c42c1SMike Galbraith printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line); 256923c42c1SMike Galbraith line = line->next; 257923c42c1SMike Galbraith } 258923c42c1SMike Galbraith } 259923c42c1SMike Galbraith 260923c42c1SMike Galbraith #define TRACE_COUNT 3 261923c42c1SMike Galbraith 262923c42c1SMike Galbraith static void show_details(struct sym_entry *syme) 263923c42c1SMike Galbraith { 264923c42c1SMike Galbraith struct symbol *symbol; 265923c42c1SMike Galbraith struct source_line *line; 266923c42c1SMike Galbraith struct source_line *line_queue = NULL; 267923c42c1SMike Galbraith int displayed = 0; 268923c42c1SMike Galbraith int line_queue_count = 0, total = 0, more = 0; 269923c42c1SMike Galbraith 270923c42c1SMike Galbraith if (!syme) 271923c42c1SMike Galbraith return; 272923c42c1SMike Galbraith 273923c42c1SMike Galbraith if (!syme->source) 274923c42c1SMike Galbraith lookup_sym_source(syme); 275923c42c1SMike Galbraith 276923c42c1SMike Galbraith if (!syme->source) 277923c42c1SMike Galbraith return; 278923c42c1SMike Galbraith 279923c42c1SMike Galbraith symbol = (struct symbol *)(syme + 1); 280923c42c1SMike Galbraith printf("Showing %s for %s\n", event_name(sym_counter), symbol->name); 281923c42c1SMike Galbraith printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); 282923c42c1SMike Galbraith 283923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 284923c42c1SMike Galbraith line = syme->source; 285923c42c1SMike Galbraith while (line) { 286923c42c1SMike Galbraith total += line->count[sym_counter]; 287923c42c1SMike Galbraith line = line->next; 288923c42c1SMike Galbraith } 289923c42c1SMike Galbraith 290923c42c1SMike Galbraith line = syme->source; 291923c42c1SMike Galbraith while (line) { 292923c42c1SMike Galbraith float pcnt = 0.0; 293923c42c1SMike Galbraith 294923c42c1SMike Galbraith if (!line_queue_count) 295923c42c1SMike Galbraith line_queue = line; 296923c42c1SMike Galbraith line_queue_count++; 297923c42c1SMike Galbraith 298923c42c1SMike Galbraith if (line->count[sym_counter]) 299923c42c1SMike Galbraith pcnt = 100.0 * line->count[sym_counter] / (float)total; 300923c42c1SMike Galbraith if (pcnt >= (float)sym_pcnt_filter) { 301923c42c1SMike Galbraith if (displayed <= print_entries) 302923c42c1SMike Galbraith show_lines(line_queue, line_queue_count, total); 303923c42c1SMike Galbraith else more++; 304923c42c1SMike Galbraith displayed += line_queue_count; 305923c42c1SMike Galbraith line_queue_count = 0; 306923c42c1SMike Galbraith line_queue = NULL; 307923c42c1SMike Galbraith } else if (line_queue_count > TRACE_COUNT) { 308923c42c1SMike Galbraith line_queue = line_queue->next; 309923c42c1SMike Galbraith line_queue_count--; 310923c42c1SMike Galbraith } 311923c42c1SMike Galbraith 312923c42c1SMike Galbraith line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8; 313923c42c1SMike Galbraith line = line->next; 314923c42c1SMike Galbraith } 315923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 316923c42c1SMike Galbraith if (more) 317923c42c1SMike Galbraith printf("%d lines not displayed, maybe increase display entries [e]\n", more); 318923c42c1SMike Galbraith } 31986470930SIngo Molnar 32086470930SIngo Molnar /* 3215b2bb75aSArnaldo Carvalho de Melo * Symbols will be added here in event__process_sample and will get out 32286470930SIngo Molnar * after decayed. 32386470930SIngo Molnar */ 32486470930SIngo Molnar static LIST_HEAD(active_symbols); 32586470930SIngo Molnar static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER; 32686470930SIngo Molnar 32786470930SIngo Molnar /* 32886470930SIngo Molnar * Ordering weight: count-1 * count-2 * ... / count-n 32986470930SIngo Molnar */ 33086470930SIngo Molnar static double sym_weight(const struct sym_entry *sym) 33186470930SIngo Molnar { 33286470930SIngo Molnar double weight = sym->snap_count; 33386470930SIngo Molnar int counter; 33486470930SIngo Molnar 33546ab9764SMike Galbraith if (!display_weighted) 33646ab9764SMike Galbraith return weight; 33746ab9764SMike Galbraith 33886470930SIngo Molnar for (counter = 1; counter < nr_counters-1; counter++) 33986470930SIngo Molnar weight *= sym->count[counter]; 34086470930SIngo Molnar 34186470930SIngo Molnar weight /= (sym->count[counter] + 1); 34286470930SIngo Molnar 34386470930SIngo Molnar return weight; 34486470930SIngo Molnar } 34586470930SIngo Molnar 34686470930SIngo Molnar static long samples; 34786470930SIngo Molnar static long userspace_samples; 34886470930SIngo Molnar static const char CONSOLE_CLEAR[] = "[H[2J"; 34986470930SIngo Molnar 35086470930SIngo Molnar static void __list_insert_active_sym(struct sym_entry *syme) 35186470930SIngo Molnar { 35286470930SIngo Molnar list_add(&syme->node, &active_symbols); 35386470930SIngo Molnar } 35486470930SIngo Molnar 35586470930SIngo Molnar static void list_remove_active_sym(struct sym_entry *syme) 35686470930SIngo Molnar { 35786470930SIngo Molnar pthread_mutex_lock(&active_symbols_lock); 35886470930SIngo Molnar list_del_init(&syme->node); 35986470930SIngo Molnar pthread_mutex_unlock(&active_symbols_lock); 36086470930SIngo Molnar } 36186470930SIngo Molnar 36286470930SIngo Molnar static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se) 36386470930SIngo Molnar { 36486470930SIngo Molnar struct rb_node **p = &tree->rb_node; 36586470930SIngo Molnar struct rb_node *parent = NULL; 36686470930SIngo Molnar struct sym_entry *iter; 36786470930SIngo Molnar 36886470930SIngo Molnar while (*p != NULL) { 36986470930SIngo Molnar parent = *p; 37086470930SIngo Molnar iter = rb_entry(parent, struct sym_entry, rb_node); 37186470930SIngo Molnar 37286470930SIngo Molnar if (se->weight > iter->weight) 37386470930SIngo Molnar p = &(*p)->rb_left; 37486470930SIngo Molnar else 37586470930SIngo Molnar p = &(*p)->rb_right; 37686470930SIngo Molnar } 37786470930SIngo Molnar 37886470930SIngo Molnar rb_link_node(&se->rb_node, parent, p); 37986470930SIngo Molnar rb_insert_color(&se->rb_node, tree); 38086470930SIngo Molnar } 38186470930SIngo Molnar 38286470930SIngo Molnar static void print_sym_table(void) 38386470930SIngo Molnar { 38486470930SIngo Molnar int printed = 0, j; 38546ab9764SMike Galbraith int counter, snap = !display_weighted ? sym_counter : 0; 38686470930SIngo Molnar float samples_per_sec = samples/delay_secs; 38786470930SIngo Molnar float ksamples_per_sec = (samples-userspace_samples)/delay_secs; 38886470930SIngo Molnar float sum_ksamples = 0.0; 38986470930SIngo Molnar struct sym_entry *syme, *n; 39086470930SIngo Molnar struct rb_root tmp = RB_ROOT; 39186470930SIngo Molnar struct rb_node *nd; 39286470930SIngo Molnar 39386470930SIngo Molnar samples = userspace_samples = 0; 39486470930SIngo Molnar 39586470930SIngo Molnar /* Sort the active symbols */ 39686470930SIngo Molnar pthread_mutex_lock(&active_symbols_lock); 39786470930SIngo Molnar syme = list_entry(active_symbols.next, struct sym_entry, node); 39886470930SIngo Molnar pthread_mutex_unlock(&active_symbols_lock); 39986470930SIngo Molnar 40086470930SIngo Molnar list_for_each_entry_safe_from(syme, n, &active_symbols, node) { 40146ab9764SMike Galbraith syme->snap_count = syme->count[snap]; 40286470930SIngo Molnar if (syme->snap_count != 0) { 40386470930SIngo Molnar syme->weight = sym_weight(syme); 40486470930SIngo Molnar rb_insert_active_sym(&tmp, syme); 40586470930SIngo Molnar sum_ksamples += syme->snap_count; 40686470930SIngo Molnar 40786470930SIngo Molnar for (j = 0; j < nr_counters; j++) 40886470930SIngo Molnar syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8; 40986470930SIngo Molnar } else 41086470930SIngo Molnar list_remove_active_sym(syme); 41186470930SIngo Molnar } 41286470930SIngo Molnar 41386470930SIngo Molnar puts(CONSOLE_CLEAR); 41486470930SIngo Molnar 41586470930SIngo Molnar printf( 41686470930SIngo Molnar "------------------------------------------------------------------------------\n"); 41786470930SIngo Molnar printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [", 41886470930SIngo Molnar samples_per_sec, 41986470930SIngo Molnar 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec))); 42086470930SIngo Molnar 42146ab9764SMike Galbraith if (nr_counters == 1 || !display_weighted) { 4229cffa8d5SPaul Mackerras printf("%Ld", (u64)attrs[0].sample_period); 42386470930SIngo Molnar if (freq) 42486470930SIngo Molnar printf("Hz "); 42586470930SIngo Molnar else 42686470930SIngo Molnar printf(" "); 42786470930SIngo Molnar } 42886470930SIngo Molnar 42946ab9764SMike Galbraith if (!display_weighted) 43046ab9764SMike Galbraith printf("%s", event_name(sym_counter)); 43146ab9764SMike Galbraith else for (counter = 0; counter < nr_counters; counter++) { 43286470930SIngo Molnar if (counter) 43386470930SIngo Molnar printf("/"); 43486470930SIngo Molnar 43586470930SIngo Molnar printf("%s", event_name(counter)); 43686470930SIngo Molnar } 43786470930SIngo Molnar 43886470930SIngo Molnar printf( "], "); 43986470930SIngo Molnar 44086470930SIngo Molnar if (target_pid != -1) 44186470930SIngo Molnar printf(" (target_pid: %d", target_pid); 44286470930SIngo Molnar else 44386470930SIngo Molnar printf(" (all"); 44486470930SIngo Molnar 44586470930SIngo Molnar if (profile_cpu != -1) 44686470930SIngo Molnar printf(", cpu: %d)\n", profile_cpu); 44786470930SIngo Molnar else { 44886470930SIngo Molnar if (target_pid != -1) 44986470930SIngo Molnar printf(")\n"); 45086470930SIngo Molnar else 45186470930SIngo Molnar printf(", %d CPUs)\n", nr_cpus); 45286470930SIngo Molnar } 45386470930SIngo Molnar 45486470930SIngo Molnar printf("------------------------------------------------------------------------------\n\n"); 45586470930SIngo Molnar 456923c42c1SMike Galbraith if (sym_filter_entry) { 457923c42c1SMike Galbraith show_details(sym_filter_entry); 458923c42c1SMike Galbraith return; 459923c42c1SMike Galbraith } 460923c42c1SMike Galbraith 46186470930SIngo Molnar if (nr_counters == 1) 46286470930SIngo Molnar printf(" samples pcnt"); 46386470930SIngo Molnar else 46486470930SIngo Molnar printf(" weight samples pcnt"); 46586470930SIngo Molnar 4667ced156bSArnaldo Carvalho de Melo if (verbose) 4677ced156bSArnaldo Carvalho de Melo printf(" RIP "); 4685b2bb75aSArnaldo Carvalho de Melo printf(" function DSO\n"); 4697ced156bSArnaldo Carvalho de Melo printf(" %s _______ _____", 4707ced156bSArnaldo Carvalho de Melo nr_counters == 1 ? " " : "______"); 4717ced156bSArnaldo Carvalho de Melo if (verbose) 4727ced156bSArnaldo Carvalho de Melo printf(" ________________"); 4735b2bb75aSArnaldo Carvalho de Melo printf(" ________________________________ ________________\n\n"); 47486470930SIngo Molnar 47586470930SIngo Molnar for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { 47683a0944fSIngo Molnar struct symbol *sym; 47786470930SIngo Molnar double pcnt; 47886470930SIngo Molnar 47983a0944fSIngo Molnar syme = rb_entry(nd, struct sym_entry, rb_node); 48083a0944fSIngo Molnar sym = (struct symbol *)(syme + 1); 48183a0944fSIngo Molnar 482923c42c1SMike Galbraith if (++printed > print_entries || (int)syme->snap_count < count_filter) 48386470930SIngo Molnar continue; 48486470930SIngo Molnar 48586470930SIngo Molnar pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) / 48686470930SIngo Molnar sum_ksamples)); 48786470930SIngo Molnar 48846ab9764SMike Galbraith if (nr_counters == 1 || !display_weighted) 4895b2bb75aSArnaldo Carvalho de Melo printf("%20.2f ", syme->weight); 49086470930SIngo Molnar else 4915b2bb75aSArnaldo Carvalho de Melo printf("%9.1f %10ld ", syme->weight, syme->snap_count); 49286470930SIngo Molnar 4931e11fd82SFrederic Weisbecker percent_color_fprintf(stdout, "%4.1f%%", pcnt); 4947ced156bSArnaldo Carvalho de Melo if (verbose) 4955b2bb75aSArnaldo Carvalho de Melo printf(" %016llx", sym->start); 4965b2bb75aSArnaldo Carvalho de Melo printf(" %-32s", sym->name); 4975b2bb75aSArnaldo Carvalho de Melo printf(" %s", syme->map->dso->short_name); 49842976487SMike Galbraith printf("\n"); 49986470930SIngo Molnar } 50086470930SIngo Molnar } 50186470930SIngo Molnar 502923c42c1SMike Galbraith static void prompt_integer(int *target, const char *msg) 503923c42c1SMike Galbraith { 504923c42c1SMike Galbraith char *buf = malloc(0), *p; 505923c42c1SMike Galbraith size_t dummy = 0; 506923c42c1SMike Galbraith int tmp; 507923c42c1SMike Galbraith 508923c42c1SMike Galbraith fprintf(stdout, "\n%s: ", msg); 509923c42c1SMike Galbraith if (getline(&buf, &dummy, stdin) < 0) 510923c42c1SMike Galbraith return; 511923c42c1SMike Galbraith 512923c42c1SMike Galbraith p = strchr(buf, '\n'); 513923c42c1SMike Galbraith if (p) 514923c42c1SMike Galbraith *p = 0; 515923c42c1SMike Galbraith 516923c42c1SMike Galbraith p = buf; 517923c42c1SMike Galbraith while(*p) { 518923c42c1SMike Galbraith if (!isdigit(*p)) 519923c42c1SMike Galbraith goto out_free; 520923c42c1SMike Galbraith p++; 521923c42c1SMike Galbraith } 522923c42c1SMike Galbraith tmp = strtoul(buf, NULL, 10); 523923c42c1SMike Galbraith *target = tmp; 524923c42c1SMike Galbraith out_free: 525923c42c1SMike Galbraith free(buf); 526923c42c1SMike Galbraith } 527923c42c1SMike Galbraith 528923c42c1SMike Galbraith static void prompt_percent(int *target, const char *msg) 529923c42c1SMike Galbraith { 530923c42c1SMike Galbraith int tmp = 0; 531923c42c1SMike Galbraith 532923c42c1SMike Galbraith prompt_integer(&tmp, msg); 533923c42c1SMike Galbraith if (tmp >= 0 && tmp <= 100) 534923c42c1SMike Galbraith *target = tmp; 535923c42c1SMike Galbraith } 536923c42c1SMike Galbraith 537923c42c1SMike Galbraith static void prompt_symbol(struct sym_entry **target, const char *msg) 538923c42c1SMike Galbraith { 539923c42c1SMike Galbraith char *buf = malloc(0), *p; 540923c42c1SMike Galbraith struct sym_entry *syme = *target, *n, *found = NULL; 541923c42c1SMike Galbraith size_t dummy = 0; 542923c42c1SMike Galbraith 543923c42c1SMike Galbraith /* zero counters of active symbol */ 544923c42c1SMike Galbraith if (syme) { 545923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 546923c42c1SMike Galbraith __zero_source_counters(syme); 547923c42c1SMike Galbraith *target = NULL; 548923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 549923c42c1SMike Galbraith } 550923c42c1SMike Galbraith 551923c42c1SMike Galbraith fprintf(stdout, "\n%s: ", msg); 552923c42c1SMike Galbraith if (getline(&buf, &dummy, stdin) < 0) 553923c42c1SMike Galbraith goto out_free; 554923c42c1SMike Galbraith 555923c42c1SMike Galbraith p = strchr(buf, '\n'); 556923c42c1SMike Galbraith if (p) 557923c42c1SMike Galbraith *p = 0; 558923c42c1SMike Galbraith 559923c42c1SMike Galbraith pthread_mutex_lock(&active_symbols_lock); 560923c42c1SMike Galbraith syme = list_entry(active_symbols.next, struct sym_entry, node); 561923c42c1SMike Galbraith pthread_mutex_unlock(&active_symbols_lock); 562923c42c1SMike Galbraith 563923c42c1SMike Galbraith list_for_each_entry_safe_from(syme, n, &active_symbols, node) { 564923c42c1SMike Galbraith struct symbol *sym = (struct symbol *)(syme + 1); 565923c42c1SMike Galbraith 566923c42c1SMike Galbraith if (!strcmp(buf, sym->name)) { 567923c42c1SMike Galbraith found = syme; 568923c42c1SMike Galbraith break; 569923c42c1SMike Galbraith } 570923c42c1SMike Galbraith } 571923c42c1SMike Galbraith 572923c42c1SMike Galbraith if (!found) { 573923c42c1SMike Galbraith fprintf(stderr, "Sorry, %s is not active.\n", sym_filter); 574923c42c1SMike Galbraith sleep(1); 575923c42c1SMike Galbraith return; 576923c42c1SMike Galbraith } else 577923c42c1SMike Galbraith parse_source(found); 578923c42c1SMike Galbraith 579923c42c1SMike Galbraith out_free: 580923c42c1SMike Galbraith free(buf); 581923c42c1SMike Galbraith } 582923c42c1SMike Galbraith 583091bd2e9SMike Galbraith static void print_mapped_keys(void) 584923c42c1SMike Galbraith { 585091bd2e9SMike Galbraith char *name = NULL; 586091bd2e9SMike Galbraith 587091bd2e9SMike Galbraith if (sym_filter_entry) { 588091bd2e9SMike Galbraith struct symbol *sym = (struct symbol *)(sym_filter_entry+1); 589091bd2e9SMike Galbraith name = sym->name; 590091bd2e9SMike Galbraith } 591091bd2e9SMike Galbraith 592091bd2e9SMike Galbraith fprintf(stdout, "\nMapped keys:\n"); 593091bd2e9SMike Galbraith fprintf(stdout, "\t[d] display refresh delay. \t(%d)\n", delay_secs); 594091bd2e9SMike Galbraith fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries); 595091bd2e9SMike Galbraith 596091bd2e9SMike Galbraith if (nr_counters > 1) 59746ab9764SMike Galbraith fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter)); 598091bd2e9SMike Galbraith 599091bd2e9SMike Galbraith fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter); 600091bd2e9SMike Galbraith 60183a0944fSIngo Molnar if (vmlinux_name) { 602091bd2e9SMike Galbraith fprintf(stdout, "\t[F] annotate display filter (percent). \t(%d%%)\n", sym_pcnt_filter); 603091bd2e9SMike Galbraith fprintf(stdout, "\t[s] annotate symbol. \t(%s)\n", name?: "NULL"); 604091bd2e9SMike Galbraith fprintf(stdout, "\t[S] stop annotation.\n"); 605091bd2e9SMike Galbraith } 606091bd2e9SMike Galbraith 607091bd2e9SMike Galbraith if (nr_counters > 1) 60846ab9764SMike Galbraith fprintf(stdout, "\t[w] toggle display weighted/count[E]r. \t(%d)\n", display_weighted ? 1 : 0); 609091bd2e9SMike Galbraith 61046ab9764SMike Galbraith fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0); 611091bd2e9SMike Galbraith fprintf(stdout, "\t[qQ] quit.\n"); 612091bd2e9SMike Galbraith } 613091bd2e9SMike Galbraith 614091bd2e9SMike Galbraith static int key_mapped(int c) 615091bd2e9SMike Galbraith { 616091bd2e9SMike Galbraith switch (c) { 617091bd2e9SMike Galbraith case 'd': 618091bd2e9SMike Galbraith case 'e': 619091bd2e9SMike Galbraith case 'f': 620091bd2e9SMike Galbraith case 'z': 621091bd2e9SMike Galbraith case 'q': 622091bd2e9SMike Galbraith case 'Q': 623091bd2e9SMike Galbraith return 1; 624091bd2e9SMike Galbraith case 'E': 625091bd2e9SMike Galbraith case 'w': 626091bd2e9SMike Galbraith return nr_counters > 1 ? 1 : 0; 627091bd2e9SMike Galbraith case 'F': 628091bd2e9SMike Galbraith case 's': 629091bd2e9SMike Galbraith case 'S': 63083a0944fSIngo Molnar return vmlinux_name ? 1 : 0; 63183a0944fSIngo Molnar default: 63283a0944fSIngo Molnar break; 633091bd2e9SMike Galbraith } 634091bd2e9SMike Galbraith 635091bd2e9SMike Galbraith return 0; 636923c42c1SMike Galbraith } 637923c42c1SMike Galbraith 638923c42c1SMike Galbraith static void handle_keypress(int c) 639923c42c1SMike Galbraith { 640091bd2e9SMike Galbraith if (!key_mapped(c)) { 641091bd2e9SMike Galbraith struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; 642091bd2e9SMike Galbraith struct termios tc, save; 643091bd2e9SMike Galbraith 644091bd2e9SMike Galbraith print_mapped_keys(); 645091bd2e9SMike Galbraith fprintf(stdout, "\nEnter selection, or unmapped key to continue: "); 646091bd2e9SMike Galbraith fflush(stdout); 647091bd2e9SMike Galbraith 648091bd2e9SMike Galbraith tcgetattr(0, &save); 649091bd2e9SMike Galbraith tc = save; 650091bd2e9SMike Galbraith tc.c_lflag &= ~(ICANON | ECHO); 651091bd2e9SMike Galbraith tc.c_cc[VMIN] = 0; 652091bd2e9SMike Galbraith tc.c_cc[VTIME] = 0; 653091bd2e9SMike Galbraith tcsetattr(0, TCSANOW, &tc); 654091bd2e9SMike Galbraith 655091bd2e9SMike Galbraith poll(&stdin_poll, 1, -1); 656091bd2e9SMike Galbraith c = getc(stdin); 657091bd2e9SMike Galbraith 658091bd2e9SMike Galbraith tcsetattr(0, TCSAFLUSH, &save); 659091bd2e9SMike Galbraith if (!key_mapped(c)) 660091bd2e9SMike Galbraith return; 661091bd2e9SMike Galbraith } 662091bd2e9SMike Galbraith 663923c42c1SMike Galbraith switch (c) { 664923c42c1SMike Galbraith case 'd': 665923c42c1SMike Galbraith prompt_integer(&delay_secs, "Enter display delay"); 666dc79959aSTim Blechmann if (delay_secs < 1) 667dc79959aSTim Blechmann delay_secs = 1; 668923c42c1SMike Galbraith break; 669923c42c1SMike Galbraith case 'e': 670923c42c1SMike Galbraith prompt_integer(&print_entries, "Enter display entries (lines)"); 671923c42c1SMike Galbraith break; 672923c42c1SMike Galbraith case 'E': 673923c42c1SMike Galbraith if (nr_counters > 1) { 674923c42c1SMike Galbraith int i; 675923c42c1SMike Galbraith 676923c42c1SMike Galbraith fprintf(stderr, "\nAvailable events:"); 677923c42c1SMike Galbraith for (i = 0; i < nr_counters; i++) 678923c42c1SMike Galbraith fprintf(stderr, "\n\t%d %s", i, event_name(i)); 679923c42c1SMike Galbraith 680923c42c1SMike Galbraith prompt_integer(&sym_counter, "Enter details event counter"); 681923c42c1SMike Galbraith 682923c42c1SMike Galbraith if (sym_counter >= nr_counters) { 683923c42c1SMike Galbraith fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0)); 684923c42c1SMike Galbraith sym_counter = 0; 685923c42c1SMike Galbraith sleep(1); 686923c42c1SMike Galbraith } 687923c42c1SMike Galbraith } else sym_counter = 0; 688923c42c1SMike Galbraith break; 689923c42c1SMike Galbraith case 'f': 690923c42c1SMike Galbraith prompt_integer(&count_filter, "Enter display event count filter"); 691923c42c1SMike Galbraith break; 692923c42c1SMike Galbraith case 'F': 693923c42c1SMike Galbraith prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)"); 694923c42c1SMike Galbraith break; 695923c42c1SMike Galbraith case 'q': 696923c42c1SMike Galbraith case 'Q': 697923c42c1SMike Galbraith printf("exiting.\n"); 698923c42c1SMike Galbraith exit(0); 699923c42c1SMike Galbraith case 's': 700923c42c1SMike Galbraith prompt_symbol(&sym_filter_entry, "Enter details symbol"); 701923c42c1SMike Galbraith break; 702923c42c1SMike Galbraith case 'S': 703923c42c1SMike Galbraith if (!sym_filter_entry) 704923c42c1SMike Galbraith break; 705923c42c1SMike Galbraith else { 706923c42c1SMike Galbraith struct sym_entry *syme = sym_filter_entry; 707923c42c1SMike Galbraith 708923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 709923c42c1SMike Galbraith sym_filter_entry = NULL; 710923c42c1SMike Galbraith __zero_source_counters(syme); 711923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 712923c42c1SMike Galbraith } 713923c42c1SMike Galbraith break; 71446ab9764SMike Galbraith case 'w': 71546ab9764SMike Galbraith display_weighted = ~display_weighted; 71646ab9764SMike Galbraith break; 717923c42c1SMike Galbraith case 'z': 718923c42c1SMike Galbraith zero = ~zero; 719923c42c1SMike Galbraith break; 72083a0944fSIngo Molnar default: 72183a0944fSIngo Molnar break; 722923c42c1SMike Galbraith } 723923c42c1SMike Galbraith } 724923c42c1SMike Galbraith 725f37a291cSIngo Molnar static void *display_thread(void *arg __used) 72686470930SIngo Molnar { 72786470930SIngo Molnar struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; 728923c42c1SMike Galbraith struct termios tc, save; 729923c42c1SMike Galbraith int delay_msecs, c; 73086470930SIngo Molnar 731923c42c1SMike Galbraith tcgetattr(0, &save); 732923c42c1SMike Galbraith tc = save; 733923c42c1SMike Galbraith tc.c_lflag &= ~(ICANON | ECHO); 734923c42c1SMike Galbraith tc.c_cc[VMIN] = 0; 735923c42c1SMike Galbraith tc.c_cc[VTIME] = 0; 736091bd2e9SMike Galbraith 737923c42c1SMike Galbraith repeat: 738923c42c1SMike Galbraith delay_msecs = delay_secs * 1000; 739923c42c1SMike Galbraith tcsetattr(0, TCSANOW, &tc); 740923c42c1SMike Galbraith /* trash return*/ 741923c42c1SMike Galbraith getc(stdin); 74286470930SIngo Molnar 74386470930SIngo Molnar do { 74486470930SIngo Molnar print_sym_table(); 74586470930SIngo Molnar } while (!poll(&stdin_poll, 1, delay_msecs) == 1); 74686470930SIngo Molnar 747923c42c1SMike Galbraith c = getc(stdin); 748923c42c1SMike Galbraith tcsetattr(0, TCSAFLUSH, &save); 749923c42c1SMike Galbraith 750923c42c1SMike Galbraith handle_keypress(c); 751923c42c1SMike Galbraith goto repeat; 75286470930SIngo Molnar 75386470930SIngo Molnar return NULL; 75486470930SIngo Molnar } 75586470930SIngo Molnar 7562ab52083SAnton Blanchard /* Tag samples to be skipped. */ 757f37a291cSIngo Molnar static const char *skip_symbols[] = { 7582ab52083SAnton Blanchard "default_idle", 7592ab52083SAnton Blanchard "cpu_idle", 7602ab52083SAnton Blanchard "enter_idle", 7612ab52083SAnton Blanchard "exit_idle", 7622ab52083SAnton Blanchard "mwait_idle", 76359b90056SArnaldo Carvalho de Melo "mwait_idle_with_hints", 7648357275bSArnaldo Carvalho de Melo "poll_idle", 7653a3393efSAnton Blanchard "ppc64_runlatch_off", 7663a3393efSAnton Blanchard "pseries_dedicated_idle_sleep", 7672ab52083SAnton Blanchard NULL 7682ab52083SAnton Blanchard }; 7692ab52083SAnton Blanchard 770439d473bSArnaldo Carvalho de Melo static int symbol_filter(struct map *map, struct symbol *sym) 77186470930SIngo Molnar { 77286470930SIngo Molnar struct sym_entry *syme; 77386470930SIngo Molnar const char *name = sym->name; 7742ab52083SAnton Blanchard int i; 77586470930SIngo Molnar 7763a3393efSAnton Blanchard /* 7773a3393efSAnton Blanchard * ppc64 uses function descriptors and appends a '.' to the 7783a3393efSAnton Blanchard * start of every instruction address. Remove it. 7793a3393efSAnton Blanchard */ 7803a3393efSAnton Blanchard if (name[0] == '.') 7813a3393efSAnton Blanchard name++; 7823a3393efSAnton Blanchard 78386470930SIngo Molnar if (!strcmp(name, "_text") || 78486470930SIngo Molnar !strcmp(name, "_etext") || 78586470930SIngo Molnar !strcmp(name, "_sinittext") || 78686470930SIngo Molnar !strncmp("init_module", name, 11) || 78786470930SIngo Molnar !strncmp("cleanup_module", name, 14) || 78886470930SIngo Molnar strstr(name, "_text_start") || 78986470930SIngo Molnar strstr(name, "_text_end")) 79086470930SIngo Molnar return 1; 79186470930SIngo Molnar 792439d473bSArnaldo Carvalho de Melo syme = dso__sym_priv(map->dso, sym); 793439d473bSArnaldo Carvalho de Melo syme->map = map; 794923c42c1SMike Galbraith pthread_mutex_init(&syme->source_lock, NULL); 795923c42c1SMike Galbraith if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) 796923c42c1SMike Galbraith sym_filter_entry = syme; 797923c42c1SMike Galbraith 7982ab52083SAnton Blanchard for (i = 0; skip_symbols[i]; i++) { 7992ab52083SAnton Blanchard if (!strcmp(skip_symbols[i], name)) { 80086470930SIngo Molnar syme->skip = 1; 8012ab52083SAnton Blanchard break; 8022ab52083SAnton Blanchard } 8032ab52083SAnton Blanchard } 80486470930SIngo Molnar 80586470930SIngo Molnar return 0; 80686470930SIngo Molnar } 80786470930SIngo Molnar 80886470930SIngo Molnar static int parse_symbols(void) 80986470930SIngo Molnar { 810439d473bSArnaldo Carvalho de Melo if (dsos__load_kernel(vmlinux_name, sizeof(struct sym_entry), 8116beba7adSArnaldo Carvalho de Melo symbol_filter, 1) <= 0) 81286470930SIngo Molnar return -1; 81386470930SIngo Molnar 81486470930SIngo Molnar if (dump_symtab) 815439d473bSArnaldo Carvalho de Melo dsos__fprintf(stderr); 81686470930SIngo Molnar 81786470930SIngo Molnar return 0; 81886470930SIngo Molnar } 81986470930SIngo Molnar 8205b2bb75aSArnaldo Carvalho de Melo static void event__process_sample(const event_t *self, int counter) 82186470930SIngo Molnar { 8225b2bb75aSArnaldo Carvalho de Melo u64 ip = self->ip.ip; 823439d473bSArnaldo Carvalho de Melo struct map *map; 8245b2bb75aSArnaldo Carvalho de Melo struct sym_entry *syme; 8255b2bb75aSArnaldo Carvalho de Melo struct symbol *sym; 82686470930SIngo Molnar 8275b2bb75aSArnaldo Carvalho de Melo switch (self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK) { 8285b2bb75aSArnaldo Carvalho de Melo case PERF_RECORD_MISC_USER: { 8295b2bb75aSArnaldo Carvalho de Melo struct thread *thread = threads__findnew(self->ip.pid); 8305b2bb75aSArnaldo Carvalho de Melo 8315b2bb75aSArnaldo Carvalho de Melo if (thread == NULL) 8325b2bb75aSArnaldo Carvalho de Melo return; 8335b2bb75aSArnaldo Carvalho de Melo 8345b2bb75aSArnaldo Carvalho de Melo map = thread__find_map(thread, ip); 8355b2bb75aSArnaldo Carvalho de Melo if (map != NULL) { 8365b2bb75aSArnaldo Carvalho de Melo ip = map->map_ip(map, ip); 83766bd8424SArnaldo Carvalho de Melo sym = map__find_symbol(map, ip, symbol_filter); 8385b2bb75aSArnaldo Carvalho de Melo if (sym == NULL) 8395b2bb75aSArnaldo Carvalho de Melo return; 8405b2bb75aSArnaldo Carvalho de Melo userspace_samples++; 8415b2bb75aSArnaldo Carvalho de Melo break; 8425b2bb75aSArnaldo Carvalho de Melo } 8435b2bb75aSArnaldo Carvalho de Melo } 8445b2bb75aSArnaldo Carvalho de Melo /* 8455b2bb75aSArnaldo Carvalho de Melo * If this is outside of all known maps, 8465b2bb75aSArnaldo Carvalho de Melo * and is a negative address, try to look it 8475b2bb75aSArnaldo Carvalho de Melo * up in the kernel dso, as it might be a 8485b2bb75aSArnaldo Carvalho de Melo * vsyscall or vdso (which executes in user-mode). 8495b2bb75aSArnaldo Carvalho de Melo */ 8505b2bb75aSArnaldo Carvalho de Melo if ((long long)ip >= 0) 8515b2bb75aSArnaldo Carvalho de Melo return; 8525b2bb75aSArnaldo Carvalho de Melo /* Fall thru */ 8535b2bb75aSArnaldo Carvalho de Melo case PERF_RECORD_MISC_KERNEL: 8545b2bb75aSArnaldo Carvalho de Melo sym = kernel_maps__find_symbol(ip, &map); 8555b2bb75aSArnaldo Carvalho de Melo if (sym == NULL) 8565b2bb75aSArnaldo Carvalho de Melo return; 8575b2bb75aSArnaldo Carvalho de Melo break; 8585b2bb75aSArnaldo Carvalho de Melo default: 8595b2bb75aSArnaldo Carvalho de Melo return; 8605b2bb75aSArnaldo Carvalho de Melo } 8615b2bb75aSArnaldo Carvalho de Melo 8625b2bb75aSArnaldo Carvalho de Melo syme = dso__sym_priv(map->dso, sym); 86386470930SIngo Molnar 86486470930SIngo Molnar if (!syme->skip) { 86586470930SIngo Molnar syme->count[counter]++; 866923c42c1SMike Galbraith record_precise_ip(syme, counter, ip); 86786470930SIngo Molnar pthread_mutex_lock(&active_symbols_lock); 86886470930SIngo Molnar if (list_empty(&syme->node) || !syme->node.next) 86986470930SIngo Molnar __list_insert_active_sym(syme); 87086470930SIngo Molnar pthread_mutex_unlock(&active_symbols_lock); 8715b2bb75aSArnaldo Carvalho de Melo ++samples; 87286470930SIngo Molnar return; 87386470930SIngo Molnar } 87486470930SIngo Molnar } 87586470930SIngo Molnar 8765b2bb75aSArnaldo Carvalho de Melo static void event__process_mmap(event_t *self) 87786470930SIngo Molnar { 8785b2bb75aSArnaldo Carvalho de Melo struct thread *thread = threads__findnew(self->mmap.pid); 87986470930SIngo Molnar 8805b2bb75aSArnaldo Carvalho de Melo if (thread != NULL) { 8815b2bb75aSArnaldo Carvalho de Melo struct map *map = map__new(&self->mmap, NULL, 0, 88266bd8424SArnaldo Carvalho de Melo sizeof(struct sym_entry)); 8835b2bb75aSArnaldo Carvalho de Melo if (map != NULL) 8845b2bb75aSArnaldo Carvalho de Melo thread__insert_map(thread, map); 8855b2bb75aSArnaldo Carvalho de Melo } 88686470930SIngo Molnar } 88786470930SIngo Molnar 8885b2bb75aSArnaldo Carvalho de Melo static void event__process_comm(event_t *self) 8895b2bb75aSArnaldo Carvalho de Melo { 8905b2bb75aSArnaldo Carvalho de Melo struct thread *thread = threads__findnew(self->comm.pid); 8915b2bb75aSArnaldo Carvalho de Melo 8925b2bb75aSArnaldo Carvalho de Melo if (thread != NULL) 8935b2bb75aSArnaldo Carvalho de Melo thread__set_comm(thread, self->comm.comm); 8945b2bb75aSArnaldo Carvalho de Melo } 8955b2bb75aSArnaldo Carvalho de Melo 8965b2bb75aSArnaldo Carvalho de Melo static int event__process(event_t *event) 8975b2bb75aSArnaldo Carvalho de Melo { 8985b2bb75aSArnaldo Carvalho de Melo switch (event->header.type) { 8995b2bb75aSArnaldo Carvalho de Melo case PERF_RECORD_COMM: 9005b2bb75aSArnaldo Carvalho de Melo event__process_comm(event); 9015b2bb75aSArnaldo Carvalho de Melo break; 9025b2bb75aSArnaldo Carvalho de Melo case PERF_RECORD_MMAP: 9035b2bb75aSArnaldo Carvalho de Melo event__process_mmap(event); 9045b2bb75aSArnaldo Carvalho de Melo break; 9055b2bb75aSArnaldo Carvalho de Melo default: 9065b2bb75aSArnaldo Carvalho de Melo break; 9075b2bb75aSArnaldo Carvalho de Melo } 9085b2bb75aSArnaldo Carvalho de Melo 9095b2bb75aSArnaldo Carvalho de Melo return 0; 91086470930SIngo Molnar } 91186470930SIngo Molnar 91286470930SIngo Molnar struct mmap_data { 91386470930SIngo Molnar int counter; 91486470930SIngo Molnar void *base; 915f37a291cSIngo Molnar int mask; 91686470930SIngo Molnar unsigned int prev; 91786470930SIngo Molnar }; 91886470930SIngo Molnar 91986470930SIngo Molnar static unsigned int mmap_read_head(struct mmap_data *md) 92086470930SIngo Molnar { 921cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 92286470930SIngo Molnar int head; 92386470930SIngo Molnar 92486470930SIngo Molnar head = pc->data_head; 92586470930SIngo Molnar rmb(); 92686470930SIngo Molnar 92786470930SIngo Molnar return head; 92886470930SIngo Molnar } 92986470930SIngo Molnar 9302f01190aSFrederic Weisbecker static void mmap_read_counter(struct mmap_data *md) 93186470930SIngo Molnar { 93286470930SIngo Molnar unsigned int head = mmap_read_head(md); 93386470930SIngo Molnar unsigned int old = md->prev; 93486470930SIngo Molnar unsigned char *data = md->base + page_size; 93586470930SIngo Molnar int diff; 93686470930SIngo Molnar 93786470930SIngo Molnar /* 93886470930SIngo Molnar * If we're further behind than half the buffer, there's a chance 93986470930SIngo Molnar * the writer will bite our tail and mess up the samples under us. 94086470930SIngo Molnar * 94186470930SIngo Molnar * If we somehow ended up ahead of the head, we got messed up. 94286470930SIngo Molnar * 94386470930SIngo Molnar * In either case, truncate and restart at head. 94486470930SIngo Molnar */ 94586470930SIngo Molnar diff = head - old; 94686470930SIngo Molnar if (diff > md->mask / 2 || diff < 0) { 947f4f0b418SMike Galbraith fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); 94886470930SIngo Molnar 94986470930SIngo Molnar /* 95086470930SIngo Molnar * head points to a known good entry, start there. 95186470930SIngo Molnar */ 95286470930SIngo Molnar old = head; 95386470930SIngo Molnar } 95486470930SIngo Molnar 95586470930SIngo Molnar for (; old != head;) { 95686470930SIngo Molnar event_t *event = (event_t *)&data[old & md->mask]; 95786470930SIngo Molnar 95886470930SIngo Molnar event_t event_copy; 95986470930SIngo Molnar 96086470930SIngo Molnar size_t size = event->header.size; 96186470930SIngo Molnar 96286470930SIngo Molnar /* 96386470930SIngo Molnar * Event straddles the mmap boundary -- header should always 96486470930SIngo Molnar * be inside due to u64 alignment of output. 96586470930SIngo Molnar */ 96686470930SIngo Molnar if ((old & md->mask) + size != ((old + size) & md->mask)) { 96786470930SIngo Molnar unsigned int offset = old; 96886470930SIngo Molnar unsigned int len = min(sizeof(*event), size), cpy; 96986470930SIngo Molnar void *dst = &event_copy; 97086470930SIngo Molnar 97186470930SIngo Molnar do { 97286470930SIngo Molnar cpy = min(md->mask + 1 - (offset & md->mask), len); 97386470930SIngo Molnar memcpy(dst, &data[offset & md->mask], cpy); 97486470930SIngo Molnar offset += cpy; 97586470930SIngo Molnar dst += cpy; 97686470930SIngo Molnar len -= cpy; 97786470930SIngo Molnar } while (len); 97886470930SIngo Molnar 97986470930SIngo Molnar event = &event_copy; 98086470930SIngo Molnar } 98186470930SIngo Molnar 9825b2bb75aSArnaldo Carvalho de Melo if (event->header.type == PERF_RECORD_SAMPLE) 9835b2bb75aSArnaldo Carvalho de Melo event__process_sample(event, md->counter); 9845b2bb75aSArnaldo Carvalho de Melo else 9855b2bb75aSArnaldo Carvalho de Melo event__process(event); 98686470930SIngo Molnar old += size; 98786470930SIngo Molnar } 98886470930SIngo Molnar 98986470930SIngo Molnar md->prev = old; 99086470930SIngo Molnar } 99186470930SIngo Molnar 99286470930SIngo Molnar static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; 99386470930SIngo Molnar static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; 99486470930SIngo Molnar 9952f01190aSFrederic Weisbecker static void mmap_read(void) 9962f01190aSFrederic Weisbecker { 9972f01190aSFrederic Weisbecker int i, counter; 9982f01190aSFrederic Weisbecker 9992f01190aSFrederic Weisbecker for (i = 0; i < nr_cpus; i++) { 10002f01190aSFrederic Weisbecker for (counter = 0; counter < nr_counters; counter++) 10012f01190aSFrederic Weisbecker mmap_read_counter(&mmap_array[i][counter]); 10022f01190aSFrederic Weisbecker } 10032f01190aSFrederic Weisbecker } 10042f01190aSFrederic Weisbecker 1005716c69feSIngo Molnar int nr_poll; 1006716c69feSIngo Molnar int group_fd; 1007716c69feSIngo Molnar 1008716c69feSIngo Molnar static void start_counter(int i, int counter) 100986470930SIngo Molnar { 1010cdd6c482SIngo Molnar struct perf_event_attr *attr; 10110fdc7e67SMike Galbraith int cpu; 101286470930SIngo Molnar 101386470930SIngo Molnar cpu = profile_cpu; 101486470930SIngo Molnar if (target_pid == -1 && profile_cpu == -1) 101586470930SIngo Molnar cpu = i; 101686470930SIngo Molnar 101786470930SIngo Molnar attr = attrs + counter; 101886470930SIngo Molnar 101986470930SIngo Molnar attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; 10207e4ff9e3SMike Galbraith 10217e4ff9e3SMike Galbraith if (freq) { 10227e4ff9e3SMike Galbraith attr->sample_type |= PERF_SAMPLE_PERIOD; 10237e4ff9e3SMike Galbraith attr->freq = 1; 10247e4ff9e3SMike Galbraith attr->sample_freq = freq; 10257e4ff9e3SMike Galbraith } 10267e4ff9e3SMike Galbraith 10270fdc7e67SMike Galbraith attr->inherit = (cpu < 0) && inherit; 10285b2bb75aSArnaldo Carvalho de Melo attr->mmap = 1; 102986470930SIngo Molnar 1030716c69feSIngo Molnar try_again: 1031cdd6c482SIngo Molnar fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0); 1032716c69feSIngo Molnar 103386470930SIngo Molnar if (fd[i][counter] < 0) { 103486470930SIngo Molnar int err = errno; 103586470930SIngo Molnar 103686470930SIngo Molnar if (err == EPERM) 1037716c69feSIngo Molnar die("No permission - are you root?\n"); 1038716c69feSIngo Molnar /* 1039716c69feSIngo Molnar * If it's cycles then fall back to hrtimer 1040716c69feSIngo Molnar * based cpu-clock-tick sw counter, which 1041716c69feSIngo Molnar * is always available even if no PMU support: 1042716c69feSIngo Molnar */ 1043716c69feSIngo Molnar if (attr->type == PERF_TYPE_HARDWARE 1044f4dbfa8fSPeter Zijlstra && attr->config == PERF_COUNT_HW_CPU_CYCLES) { 1045716c69feSIngo Molnar 10463da297a6SIngo Molnar if (verbose) 1047716c69feSIngo Molnar warning(" ... trying to fall back to cpu-clock-ticks\n"); 10483da297a6SIngo Molnar 1049716c69feSIngo Molnar attr->type = PERF_TYPE_SOFTWARE; 1050f4dbfa8fSPeter Zijlstra attr->config = PERF_COUNT_SW_CPU_CLOCK; 1051716c69feSIngo Molnar goto try_again; 1052716c69feSIngo Molnar } 105330c806a0SIngo Molnar printf("\n"); 105430c806a0SIngo Molnar error("perfcounter syscall returned with %d (%s)\n", 105530c806a0SIngo Molnar fd[i][counter], strerror(err)); 1056cdd6c482SIngo Molnar die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); 105786470930SIngo Molnar exit(-1); 105886470930SIngo Molnar } 105986470930SIngo Molnar assert(fd[i][counter] >= 0); 106086470930SIngo Molnar fcntl(fd[i][counter], F_SETFL, O_NONBLOCK); 106186470930SIngo Molnar 106286470930SIngo Molnar /* 106386470930SIngo Molnar * First counter acts as the group leader: 106486470930SIngo Molnar */ 106586470930SIngo Molnar if (group && group_fd == -1) 106686470930SIngo Molnar group_fd = fd[i][counter]; 106786470930SIngo Molnar 106886470930SIngo Molnar event_array[nr_poll].fd = fd[i][counter]; 106986470930SIngo Molnar event_array[nr_poll].events = POLLIN; 107086470930SIngo Molnar nr_poll++; 107186470930SIngo Molnar 107286470930SIngo Molnar mmap_array[i][counter].counter = counter; 107386470930SIngo Molnar mmap_array[i][counter].prev = 0; 107486470930SIngo Molnar mmap_array[i][counter].mask = mmap_pages*page_size - 1; 107586470930SIngo Molnar mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size, 107686470930SIngo Molnar PROT_READ, MAP_SHARED, fd[i][counter], 0); 107786470930SIngo Molnar if (mmap_array[i][counter].base == MAP_FAILED) 107886470930SIngo Molnar die("failed to mmap with %d (%s)\n", errno, strerror(errno)); 107986470930SIngo Molnar } 1080716c69feSIngo Molnar 1081716c69feSIngo Molnar static int __cmd_top(void) 1082716c69feSIngo Molnar { 1083716c69feSIngo Molnar pthread_t thread; 1084716c69feSIngo Molnar int i, counter; 1085716c69feSIngo Molnar int ret; 1086716c69feSIngo Molnar 10875b2bb75aSArnaldo Carvalho de Melo if (target_pid != -1) 10885b2bb75aSArnaldo Carvalho de Melo event__synthesize_thread(target_pid, event__process); 10895b2bb75aSArnaldo Carvalho de Melo else 10905b2bb75aSArnaldo Carvalho de Melo event__synthesize_threads(event__process); 10915b2bb75aSArnaldo Carvalho de Melo 1092716c69feSIngo Molnar for (i = 0; i < nr_cpus; i++) { 1093716c69feSIngo Molnar group_fd = -1; 1094716c69feSIngo Molnar for (counter = 0; counter < nr_counters; counter++) 1095716c69feSIngo Molnar start_counter(i, counter); 109686470930SIngo Molnar } 109786470930SIngo Molnar 10982f01190aSFrederic Weisbecker /* Wait for a minimal set of events before starting the snapshot */ 10992f01190aSFrederic Weisbecker poll(event_array, nr_poll, 100); 11002f01190aSFrederic Weisbecker 11012f01190aSFrederic Weisbecker mmap_read(); 11022f01190aSFrederic Weisbecker 110386470930SIngo Molnar if (pthread_create(&thread, NULL, display_thread, NULL)) { 110486470930SIngo Molnar printf("Could not create display thread.\n"); 110586470930SIngo Molnar exit(-1); 110686470930SIngo Molnar } 110786470930SIngo Molnar 110886470930SIngo Molnar if (realtime_prio) { 110986470930SIngo Molnar struct sched_param param; 111086470930SIngo Molnar 111186470930SIngo Molnar param.sched_priority = realtime_prio; 111286470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 111386470930SIngo Molnar printf("Could not set realtime priority.\n"); 111486470930SIngo Molnar exit(-1); 111586470930SIngo Molnar } 111686470930SIngo Molnar } 111786470930SIngo Molnar 111886470930SIngo Molnar while (1) { 111986470930SIngo Molnar int hits = samples; 112086470930SIngo Molnar 11212f01190aSFrederic Weisbecker mmap_read(); 112286470930SIngo Molnar 112386470930SIngo Molnar if (hits == samples) 112486470930SIngo Molnar ret = poll(event_array, nr_poll, 100); 112586470930SIngo Molnar } 112686470930SIngo Molnar 112786470930SIngo Molnar return 0; 112886470930SIngo Molnar } 112986470930SIngo Molnar 113086470930SIngo Molnar static const char * const top_usage[] = { 113186470930SIngo Molnar "perf top [<options>]", 113286470930SIngo Molnar NULL 113386470930SIngo Molnar }; 113486470930SIngo Molnar 113586470930SIngo Molnar static const struct option options[] = { 113686470930SIngo Molnar OPT_CALLBACK('e', "event", NULL, "event", 113786470930SIngo Molnar "event selector. use 'perf list' to list available events", 113886470930SIngo Molnar parse_events), 113986470930SIngo Molnar OPT_INTEGER('c', "count", &default_interval, 114086470930SIngo Molnar "event period to sample"), 114186470930SIngo Molnar OPT_INTEGER('p', "pid", &target_pid, 114286470930SIngo Molnar "profile events on existing pid"), 114386470930SIngo Molnar OPT_BOOLEAN('a', "all-cpus", &system_wide, 114486470930SIngo Molnar "system-wide collection from all CPUs"), 114586470930SIngo Molnar OPT_INTEGER('C', "CPU", &profile_cpu, 114686470930SIngo Molnar "CPU to profile on"), 114783a0944fSIngo Molnar OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), 114886470930SIngo Molnar OPT_INTEGER('m', "mmap-pages", &mmap_pages, 114986470930SIngo Molnar "number of mmap data pages"), 115086470930SIngo Molnar OPT_INTEGER('r', "realtime", &realtime_prio, 115186470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 115286470930SIngo Molnar OPT_INTEGER('d', "delay", &delay_secs, 115386470930SIngo Molnar "number of seconds to delay between refreshes"), 115486470930SIngo Molnar OPT_BOOLEAN('D', "dump-symtab", &dump_symtab, 115586470930SIngo Molnar "dump the symbol table used for profiling"), 115686470930SIngo Molnar OPT_INTEGER('f', "count-filter", &count_filter, 115786470930SIngo Molnar "only display functions with more events than this"), 115886470930SIngo Molnar OPT_BOOLEAN('g', "group", &group, 115986470930SIngo Molnar "put the counters into a counter group"), 11600fdc7e67SMike Galbraith OPT_BOOLEAN('i', "inherit", &inherit, 11610fdc7e67SMike Galbraith "child tasks inherit counters"), 1162923c42c1SMike Galbraith OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name", 1163923c42c1SMike Galbraith "symbol to annotate - requires -k option"), 11641f208ea6SAnton Blanchard OPT_BOOLEAN('z', "zero", &zero, 116586470930SIngo Molnar "zero history across updates"), 116686470930SIngo Molnar OPT_INTEGER('F', "freq", &freq, 116786470930SIngo Molnar "profile at this frequency"), 116886470930SIngo Molnar OPT_INTEGER('E', "entries", &print_entries, 116986470930SIngo Molnar "display this many functions"), 11703da297a6SIngo Molnar OPT_BOOLEAN('v', "verbose", &verbose, 11713da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 117286470930SIngo Molnar OPT_END() 117386470930SIngo Molnar }; 117486470930SIngo Molnar 1175f37a291cSIngo Molnar int cmd_top(int argc, const char **argv, const char *prefix __used) 117686470930SIngo Molnar { 117786470930SIngo Molnar int counter; 117886470930SIngo Molnar 117942976487SMike Galbraith symbol__init(); 118042976487SMike Galbraith 118186470930SIngo Molnar page_size = sysconf(_SC_PAGE_SIZE); 118286470930SIngo Molnar 118386470930SIngo Molnar argc = parse_options(argc, argv, options, top_usage, 0); 118486470930SIngo Molnar if (argc) 118586470930SIngo Molnar usage_with_options(top_usage, options); 118686470930SIngo Molnar 118786470930SIngo Molnar /* CPU and PID are mutually exclusive */ 118886470930SIngo Molnar if (target_pid != -1 && profile_cpu != -1) { 118986470930SIngo Molnar printf("WARNING: PID switch overriding CPU\n"); 119086470930SIngo Molnar sleep(1); 119186470930SIngo Molnar profile_cpu = -1; 119286470930SIngo Molnar } 119386470930SIngo Molnar 119486470930SIngo Molnar if (!nr_counters) 119586470930SIngo Molnar nr_counters = 1; 119686470930SIngo Molnar 119786470930SIngo Molnar if (delay_secs < 1) 119886470930SIngo Molnar delay_secs = 1; 119986470930SIngo Molnar 120086470930SIngo Molnar parse_symbols(); 1201923c42c1SMike Galbraith parse_source(sym_filter_entry); 120286470930SIngo Molnar 12037e4ff9e3SMike Galbraith 12047e4ff9e3SMike Galbraith /* 12057e4ff9e3SMike Galbraith * User specified count overrides default frequency. 12067e4ff9e3SMike Galbraith */ 12077e4ff9e3SMike Galbraith if (default_interval) 12087e4ff9e3SMike Galbraith freq = 0; 12097e4ff9e3SMike Galbraith else if (freq) { 12107e4ff9e3SMike Galbraith default_interval = freq; 12117e4ff9e3SMike Galbraith } else { 12127e4ff9e3SMike Galbraith fprintf(stderr, "frequency and count are zero, aborting\n"); 12137e4ff9e3SMike Galbraith exit(EXIT_FAILURE); 12147e4ff9e3SMike Galbraith } 12157e4ff9e3SMike Galbraith 121686470930SIngo Molnar /* 121786470930SIngo Molnar * Fill in the ones not specifically initialized via -c: 121886470930SIngo Molnar */ 121986470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) { 122086470930SIngo Molnar if (attrs[counter].sample_period) 122186470930SIngo Molnar continue; 122286470930SIngo Molnar 122386470930SIngo Molnar attrs[counter].sample_period = default_interval; 122486470930SIngo Molnar } 122586470930SIngo Molnar 122686470930SIngo Molnar nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); 122786470930SIngo Molnar assert(nr_cpus <= MAX_NR_CPUS); 122886470930SIngo Molnar assert(nr_cpus >= 0); 122986470930SIngo Molnar 123086470930SIngo Molnar if (target_pid != -1 || profile_cpu != -1) 123186470930SIngo Molnar nr_cpus = 1; 123286470930SIngo Molnar 123386470930SIngo Molnar return __cmd_top(); 123486470930SIngo Molnar } 1235