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 6086470930SIngo Molnar static int default_interval = 100000; 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", 144439d473bSArnaldo Carvalho de Melo sym->start, sym->end, path); 145923c42c1SMike Galbraith 146923c42c1SMike Galbraith file = popen(command, "r"); 147923c42c1SMike Galbraith if (!file) 148923c42c1SMike Galbraith return; 149923c42c1SMike Galbraith 150923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 151923c42c1SMike Galbraith syme->lines_tail = &syme->lines; 152923c42c1SMike Galbraith while (!feof(file)) { 153923c42c1SMike Galbraith struct source_line *src; 154923c42c1SMike Galbraith size_t dummy = 0; 155923c42c1SMike Galbraith char *c; 156923c42c1SMike Galbraith 157923c42c1SMike Galbraith src = malloc(sizeof(struct source_line)); 158923c42c1SMike Galbraith assert(src != NULL); 159923c42c1SMike Galbraith memset(src, 0, sizeof(struct source_line)); 160923c42c1SMike Galbraith 161923c42c1SMike Galbraith if (getline(&src->line, &dummy, file) < 0) 162923c42c1SMike Galbraith break; 163923c42c1SMike Galbraith if (!src->line) 164923c42c1SMike Galbraith break; 165923c42c1SMike Galbraith 166923c42c1SMike Galbraith c = strchr(src->line, '\n'); 167923c42c1SMike Galbraith if (c) 168923c42c1SMike Galbraith *c = 0; 169923c42c1SMike Galbraith 170923c42c1SMike Galbraith src->next = NULL; 171923c42c1SMike Galbraith *syme->lines_tail = src; 172923c42c1SMike Galbraith syme->lines_tail = &src->next; 173923c42c1SMike Galbraith 174923c42c1SMike Galbraith if (strlen(src->line)>8 && src->line[8] == ':') { 175923c42c1SMike Galbraith src->eip = strtoull(src->line, NULL, 16); 176439d473bSArnaldo Carvalho de Melo src->eip += map->start; 177923c42c1SMike Galbraith } 178923c42c1SMike Galbraith if (strlen(src->line)>8 && src->line[16] == ':') { 179923c42c1SMike Galbraith src->eip = strtoull(src->line, NULL, 16); 180439d473bSArnaldo Carvalho de Melo src->eip += map->start; 181923c42c1SMike Galbraith } 182923c42c1SMike Galbraith } 183923c42c1SMike Galbraith pclose(file); 184923c42c1SMike Galbraith out_assign: 185923c42c1SMike Galbraith sym_filter_entry = syme; 186923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 187923c42c1SMike Galbraith } 188923c42c1SMike Galbraith 189923c42c1SMike Galbraith static void __zero_source_counters(struct sym_entry *syme) 190923c42c1SMike Galbraith { 191923c42c1SMike Galbraith int i; 192923c42c1SMike Galbraith struct source_line *line; 193923c42c1SMike Galbraith 194923c42c1SMike Galbraith line = syme->lines; 195923c42c1SMike Galbraith while (line) { 196923c42c1SMike Galbraith for (i = 0; i < nr_counters; i++) 197923c42c1SMike Galbraith line->count[i] = 0; 198923c42c1SMike Galbraith line = line->next; 199923c42c1SMike Galbraith } 200923c42c1SMike Galbraith } 201923c42c1SMike Galbraith 202923c42c1SMike Galbraith static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip) 203923c42c1SMike Galbraith { 204923c42c1SMike Galbraith struct source_line *line; 205923c42c1SMike Galbraith 206923c42c1SMike Galbraith if (syme != sym_filter_entry) 207923c42c1SMike Galbraith return; 208923c42c1SMike Galbraith 209923c42c1SMike Galbraith if (pthread_mutex_trylock(&syme->source_lock)) 210923c42c1SMike Galbraith return; 211923c42c1SMike Galbraith 212923c42c1SMike Galbraith if (!syme->source) 213923c42c1SMike Galbraith goto out_unlock; 214923c42c1SMike Galbraith 215923c42c1SMike Galbraith for (line = syme->lines; line; line = line->next) { 216923c42c1SMike Galbraith if (line->eip == ip) { 217923c42c1SMike Galbraith line->count[counter]++; 218923c42c1SMike Galbraith break; 219923c42c1SMike Galbraith } 220923c42c1SMike Galbraith if (line->eip > ip) 221923c42c1SMike Galbraith break; 222923c42c1SMike Galbraith } 223923c42c1SMike Galbraith out_unlock: 224923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 225923c42c1SMike Galbraith } 226923c42c1SMike Galbraith 227923c42c1SMike Galbraith static void lookup_sym_source(struct sym_entry *syme) 228923c42c1SMike Galbraith { 229923c42c1SMike Galbraith struct symbol *symbol = (struct symbol *)(syme + 1); 230923c42c1SMike Galbraith struct source_line *line; 231923c42c1SMike Galbraith char pattern[PATH_MAX]; 232923c42c1SMike Galbraith 233923c42c1SMike Galbraith sprintf(pattern, "<%s>:", symbol->name); 234923c42c1SMike Galbraith 235923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 236923c42c1SMike Galbraith for (line = syme->lines; line; line = line->next) { 237923c42c1SMike Galbraith if (strstr(line->line, pattern)) { 238923c42c1SMike Galbraith syme->source = line; 239923c42c1SMike Galbraith break; 240923c42c1SMike Galbraith } 241923c42c1SMike Galbraith } 242923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 243923c42c1SMike Galbraith } 244923c42c1SMike Galbraith 245923c42c1SMike Galbraith static void show_lines(struct source_line *queue, int count, int total) 246923c42c1SMike Galbraith { 247923c42c1SMike Galbraith int i; 248923c42c1SMike Galbraith struct source_line *line; 249923c42c1SMike Galbraith 250923c42c1SMike Galbraith line = queue; 251923c42c1SMike Galbraith for (i = 0; i < count; i++) { 252923c42c1SMike Galbraith float pcnt = 100.0*(float)line->count[sym_counter]/(float)total; 253923c42c1SMike Galbraith 254923c42c1SMike Galbraith printf("%8li %4.1f%%\t%s\n", line->count[sym_counter], pcnt, line->line); 255923c42c1SMike Galbraith line = line->next; 256923c42c1SMike Galbraith } 257923c42c1SMike Galbraith } 258923c42c1SMike Galbraith 259923c42c1SMike Galbraith #define TRACE_COUNT 3 260923c42c1SMike Galbraith 261923c42c1SMike Galbraith static void show_details(struct sym_entry *syme) 262923c42c1SMike Galbraith { 263923c42c1SMike Galbraith struct symbol *symbol; 264923c42c1SMike Galbraith struct source_line *line; 265923c42c1SMike Galbraith struct source_line *line_queue = NULL; 266923c42c1SMike Galbraith int displayed = 0; 267923c42c1SMike Galbraith int line_queue_count = 0, total = 0, more = 0; 268923c42c1SMike Galbraith 269923c42c1SMike Galbraith if (!syme) 270923c42c1SMike Galbraith return; 271923c42c1SMike Galbraith 272923c42c1SMike Galbraith if (!syme->source) 273923c42c1SMike Galbraith lookup_sym_source(syme); 274923c42c1SMike Galbraith 275923c42c1SMike Galbraith if (!syme->source) 276923c42c1SMike Galbraith return; 277923c42c1SMike Galbraith 278923c42c1SMike Galbraith symbol = (struct symbol *)(syme + 1); 279923c42c1SMike Galbraith printf("Showing %s for %s\n", event_name(sym_counter), symbol->name); 280923c42c1SMike Galbraith printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); 281923c42c1SMike Galbraith 282923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 283923c42c1SMike Galbraith line = syme->source; 284923c42c1SMike Galbraith while (line) { 285923c42c1SMike Galbraith total += line->count[sym_counter]; 286923c42c1SMike Galbraith line = line->next; 287923c42c1SMike Galbraith } 288923c42c1SMike Galbraith 289923c42c1SMike Galbraith line = syme->source; 290923c42c1SMike Galbraith while (line) { 291923c42c1SMike Galbraith float pcnt = 0.0; 292923c42c1SMike Galbraith 293923c42c1SMike Galbraith if (!line_queue_count) 294923c42c1SMike Galbraith line_queue = line; 295923c42c1SMike Galbraith line_queue_count++; 296923c42c1SMike Galbraith 297923c42c1SMike Galbraith if (line->count[sym_counter]) 298923c42c1SMike Galbraith pcnt = 100.0 * line->count[sym_counter] / (float)total; 299923c42c1SMike Galbraith if (pcnt >= (float)sym_pcnt_filter) { 300923c42c1SMike Galbraith if (displayed <= print_entries) 301923c42c1SMike Galbraith show_lines(line_queue, line_queue_count, total); 302923c42c1SMike Galbraith else more++; 303923c42c1SMike Galbraith displayed += line_queue_count; 304923c42c1SMike Galbraith line_queue_count = 0; 305923c42c1SMike Galbraith line_queue = NULL; 306923c42c1SMike Galbraith } else if (line_queue_count > TRACE_COUNT) { 307923c42c1SMike Galbraith line_queue = line_queue->next; 308923c42c1SMike Galbraith line_queue_count--; 309923c42c1SMike Galbraith } 310923c42c1SMike Galbraith 311923c42c1SMike Galbraith line->count[sym_counter] = zero ? 0 : line->count[sym_counter] * 7 / 8; 312923c42c1SMike Galbraith line = line->next; 313923c42c1SMike Galbraith } 314923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 315923c42c1SMike Galbraith if (more) 316923c42c1SMike Galbraith printf("%d lines not displayed, maybe increase display entries [e]\n", more); 317923c42c1SMike Galbraith } 31886470930SIngo Molnar 31986470930SIngo Molnar /* 32086470930SIngo Molnar * Symbols will be added here in record_ip and will get out 32186470930SIngo Molnar * after decayed. 32286470930SIngo Molnar */ 32386470930SIngo Molnar static LIST_HEAD(active_symbols); 32486470930SIngo Molnar static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER; 32586470930SIngo Molnar 32686470930SIngo Molnar /* 32786470930SIngo Molnar * Ordering weight: count-1 * count-2 * ... / count-n 32886470930SIngo Molnar */ 32986470930SIngo Molnar static double sym_weight(const struct sym_entry *sym) 33086470930SIngo Molnar { 33186470930SIngo Molnar double weight = sym->snap_count; 33286470930SIngo Molnar int counter; 33386470930SIngo Molnar 33446ab9764SMike Galbraith if (!display_weighted) 33546ab9764SMike Galbraith return weight; 33646ab9764SMike Galbraith 33786470930SIngo Molnar for (counter = 1; counter < nr_counters-1; counter++) 33886470930SIngo Molnar weight *= sym->count[counter]; 33986470930SIngo Molnar 34086470930SIngo Molnar weight /= (sym->count[counter] + 1); 34186470930SIngo Molnar 34286470930SIngo Molnar return weight; 34386470930SIngo Molnar } 34486470930SIngo Molnar 34586470930SIngo Molnar static long samples; 34686470930SIngo Molnar static long userspace_samples; 34786470930SIngo Molnar static const char CONSOLE_CLEAR[] = "[H[2J"; 34886470930SIngo Molnar 34986470930SIngo Molnar static void __list_insert_active_sym(struct sym_entry *syme) 35086470930SIngo Molnar { 35186470930SIngo Molnar list_add(&syme->node, &active_symbols); 35286470930SIngo Molnar } 35386470930SIngo Molnar 35486470930SIngo Molnar static void list_remove_active_sym(struct sym_entry *syme) 35586470930SIngo Molnar { 35686470930SIngo Molnar pthread_mutex_lock(&active_symbols_lock); 35786470930SIngo Molnar list_del_init(&syme->node); 35886470930SIngo Molnar pthread_mutex_unlock(&active_symbols_lock); 35986470930SIngo Molnar } 36086470930SIngo Molnar 36186470930SIngo Molnar static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se) 36286470930SIngo Molnar { 36386470930SIngo Molnar struct rb_node **p = &tree->rb_node; 36486470930SIngo Molnar struct rb_node *parent = NULL; 36586470930SIngo Molnar struct sym_entry *iter; 36686470930SIngo Molnar 36786470930SIngo Molnar while (*p != NULL) { 36886470930SIngo Molnar parent = *p; 36986470930SIngo Molnar iter = rb_entry(parent, struct sym_entry, rb_node); 37086470930SIngo Molnar 37186470930SIngo Molnar if (se->weight > iter->weight) 37286470930SIngo Molnar p = &(*p)->rb_left; 37386470930SIngo Molnar else 37486470930SIngo Molnar p = &(*p)->rb_right; 37586470930SIngo Molnar } 37686470930SIngo Molnar 37786470930SIngo Molnar rb_link_node(&se->rb_node, parent, p); 37886470930SIngo Molnar rb_insert_color(&se->rb_node, tree); 37986470930SIngo Molnar } 38086470930SIngo Molnar 38186470930SIngo Molnar static void print_sym_table(void) 38286470930SIngo Molnar { 38386470930SIngo Molnar int printed = 0, j; 38446ab9764SMike Galbraith int counter, snap = !display_weighted ? sym_counter : 0; 38586470930SIngo Molnar float samples_per_sec = samples/delay_secs; 38686470930SIngo Molnar float ksamples_per_sec = (samples-userspace_samples)/delay_secs; 38786470930SIngo Molnar float sum_ksamples = 0.0; 38886470930SIngo Molnar struct sym_entry *syme, *n; 38986470930SIngo Molnar struct rb_root tmp = RB_ROOT; 39086470930SIngo Molnar struct rb_node *nd; 39186470930SIngo Molnar 39286470930SIngo Molnar samples = userspace_samples = 0; 39386470930SIngo Molnar 39486470930SIngo Molnar /* Sort the active symbols */ 39586470930SIngo Molnar pthread_mutex_lock(&active_symbols_lock); 39686470930SIngo Molnar syme = list_entry(active_symbols.next, struct sym_entry, node); 39786470930SIngo Molnar pthread_mutex_unlock(&active_symbols_lock); 39886470930SIngo Molnar 39986470930SIngo Molnar list_for_each_entry_safe_from(syme, n, &active_symbols, node) { 40046ab9764SMike Galbraith syme->snap_count = syme->count[snap]; 40186470930SIngo Molnar if (syme->snap_count != 0) { 40286470930SIngo Molnar syme->weight = sym_weight(syme); 40386470930SIngo Molnar rb_insert_active_sym(&tmp, syme); 40486470930SIngo Molnar sum_ksamples += syme->snap_count; 40586470930SIngo Molnar 40686470930SIngo Molnar for (j = 0; j < nr_counters; j++) 40786470930SIngo Molnar syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8; 40886470930SIngo Molnar } else 40986470930SIngo Molnar list_remove_active_sym(syme); 41086470930SIngo Molnar } 41186470930SIngo Molnar 41286470930SIngo Molnar puts(CONSOLE_CLEAR); 41386470930SIngo Molnar 41486470930SIngo Molnar printf( 41586470930SIngo Molnar "------------------------------------------------------------------------------\n"); 41686470930SIngo Molnar printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [", 41786470930SIngo Molnar samples_per_sec, 41886470930SIngo Molnar 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec))); 41986470930SIngo Molnar 42046ab9764SMike Galbraith if (nr_counters == 1 || !display_weighted) { 4219cffa8d5SPaul Mackerras printf("%Ld", (u64)attrs[0].sample_period); 42286470930SIngo Molnar if (freq) 42386470930SIngo Molnar printf("Hz "); 42486470930SIngo Molnar else 42586470930SIngo Molnar printf(" "); 42686470930SIngo Molnar } 42786470930SIngo Molnar 42846ab9764SMike Galbraith if (!display_weighted) 42946ab9764SMike Galbraith printf("%s", event_name(sym_counter)); 43046ab9764SMike Galbraith else for (counter = 0; counter < nr_counters; counter++) { 43186470930SIngo Molnar if (counter) 43286470930SIngo Molnar printf("/"); 43386470930SIngo Molnar 43486470930SIngo Molnar printf("%s", event_name(counter)); 43586470930SIngo Molnar } 43686470930SIngo Molnar 43786470930SIngo Molnar printf( "], "); 43886470930SIngo Molnar 43986470930SIngo Molnar if (target_pid != -1) 44086470930SIngo Molnar printf(" (target_pid: %d", target_pid); 44186470930SIngo Molnar else 44286470930SIngo Molnar printf(" (all"); 44386470930SIngo Molnar 44486470930SIngo Molnar if (profile_cpu != -1) 44586470930SIngo Molnar printf(", cpu: %d)\n", profile_cpu); 44686470930SIngo Molnar else { 44786470930SIngo Molnar if (target_pid != -1) 44886470930SIngo Molnar printf(")\n"); 44986470930SIngo Molnar else 45086470930SIngo Molnar printf(", %d CPUs)\n", nr_cpus); 45186470930SIngo Molnar } 45286470930SIngo Molnar 45386470930SIngo Molnar printf("------------------------------------------------------------------------------\n\n"); 45486470930SIngo Molnar 455923c42c1SMike Galbraith if (sym_filter_entry) { 456923c42c1SMike Galbraith show_details(sym_filter_entry); 457923c42c1SMike Galbraith return; 458923c42c1SMike Galbraith } 459923c42c1SMike Galbraith 46086470930SIngo Molnar if (nr_counters == 1) 46186470930SIngo Molnar printf(" samples pcnt"); 46286470930SIngo Molnar else 46386470930SIngo Molnar printf(" weight samples pcnt"); 46486470930SIngo Molnar 4657ced156bSArnaldo Carvalho de Melo if (verbose) 4667ced156bSArnaldo Carvalho de Melo printf(" RIP "); 4677ced156bSArnaldo Carvalho de Melo printf(" kernel function\n"); 4687ced156bSArnaldo Carvalho de Melo printf(" %s _______ _____", 4697ced156bSArnaldo Carvalho de Melo nr_counters == 1 ? " " : "______"); 4707ced156bSArnaldo Carvalho de Melo if (verbose) 4717ced156bSArnaldo Carvalho de Melo printf(" ________________"); 4727ced156bSArnaldo Carvalho de Melo printf(" _______________\n\n"); 47386470930SIngo Molnar 47486470930SIngo Molnar for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) { 47583a0944fSIngo Molnar struct symbol *sym; 47686470930SIngo Molnar double pcnt; 47786470930SIngo Molnar 47883a0944fSIngo Molnar syme = rb_entry(nd, struct sym_entry, rb_node); 47983a0944fSIngo Molnar sym = (struct symbol *)(syme + 1); 48083a0944fSIngo Molnar 481923c42c1SMike Galbraith if (++printed > print_entries || (int)syme->snap_count < count_filter) 48286470930SIngo Molnar continue; 48386470930SIngo Molnar 48486470930SIngo Molnar pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) / 48586470930SIngo Molnar sum_ksamples)); 48686470930SIngo Molnar 48746ab9764SMike Galbraith if (nr_counters == 1 || !display_weighted) 48886470930SIngo Molnar printf("%20.2f - ", syme->weight); 48986470930SIngo Molnar else 49086470930SIngo Molnar printf("%9.1f %10ld - ", syme->weight, syme->snap_count); 49186470930SIngo Molnar 4921e11fd82SFrederic Weisbecker percent_color_fprintf(stdout, "%4.1f%%", pcnt); 4937ced156bSArnaldo Carvalho de Melo if (verbose) 4947ced156bSArnaldo Carvalho de Melo printf(" - %016llx", sym->start); 4957ced156bSArnaldo Carvalho de Melo printf(" : %s", sym->name); 496439d473bSArnaldo Carvalho de Melo if (syme->map->dso->name[0] == '[') 497439d473bSArnaldo Carvalho de Melo printf(" \t%s", syme->map->dso->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"); 666923c42c1SMike Galbraith break; 667923c42c1SMike Galbraith case 'e': 668923c42c1SMike Galbraith prompt_integer(&print_entries, "Enter display entries (lines)"); 669923c42c1SMike Galbraith break; 670923c42c1SMike Galbraith case 'E': 671923c42c1SMike Galbraith if (nr_counters > 1) { 672923c42c1SMike Galbraith int i; 673923c42c1SMike Galbraith 674923c42c1SMike Galbraith fprintf(stderr, "\nAvailable events:"); 675923c42c1SMike Galbraith for (i = 0; i < nr_counters; i++) 676923c42c1SMike Galbraith fprintf(stderr, "\n\t%d %s", i, event_name(i)); 677923c42c1SMike Galbraith 678923c42c1SMike Galbraith prompt_integer(&sym_counter, "Enter details event counter"); 679923c42c1SMike Galbraith 680923c42c1SMike Galbraith if (sym_counter >= nr_counters) { 681923c42c1SMike Galbraith fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0)); 682923c42c1SMike Galbraith sym_counter = 0; 683923c42c1SMike Galbraith sleep(1); 684923c42c1SMike Galbraith } 685923c42c1SMike Galbraith } else sym_counter = 0; 686923c42c1SMike Galbraith break; 687923c42c1SMike Galbraith case 'f': 688923c42c1SMike Galbraith prompt_integer(&count_filter, "Enter display event count filter"); 689923c42c1SMike Galbraith break; 690923c42c1SMike Galbraith case 'F': 691923c42c1SMike Galbraith prompt_percent(&sym_pcnt_filter, "Enter details display event filter (percent)"); 692923c42c1SMike Galbraith break; 693923c42c1SMike Galbraith case 'q': 694923c42c1SMike Galbraith case 'Q': 695923c42c1SMike Galbraith printf("exiting.\n"); 696923c42c1SMike Galbraith exit(0); 697923c42c1SMike Galbraith case 's': 698923c42c1SMike Galbraith prompt_symbol(&sym_filter_entry, "Enter details symbol"); 699923c42c1SMike Galbraith break; 700923c42c1SMike Galbraith case 'S': 701923c42c1SMike Galbraith if (!sym_filter_entry) 702923c42c1SMike Galbraith break; 703923c42c1SMike Galbraith else { 704923c42c1SMike Galbraith struct sym_entry *syme = sym_filter_entry; 705923c42c1SMike Galbraith 706923c42c1SMike Galbraith pthread_mutex_lock(&syme->source_lock); 707923c42c1SMike Galbraith sym_filter_entry = NULL; 708923c42c1SMike Galbraith __zero_source_counters(syme); 709923c42c1SMike Galbraith pthread_mutex_unlock(&syme->source_lock); 710923c42c1SMike Galbraith } 711923c42c1SMike Galbraith break; 71246ab9764SMike Galbraith case 'w': 71346ab9764SMike Galbraith display_weighted = ~display_weighted; 71446ab9764SMike Galbraith break; 715923c42c1SMike Galbraith case 'z': 716923c42c1SMike Galbraith zero = ~zero; 717923c42c1SMike Galbraith break; 71883a0944fSIngo Molnar default: 71983a0944fSIngo Molnar break; 720923c42c1SMike Galbraith } 721923c42c1SMike Galbraith } 722923c42c1SMike Galbraith 723f37a291cSIngo Molnar static void *display_thread(void *arg __used) 72486470930SIngo Molnar { 72586470930SIngo Molnar struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; 726923c42c1SMike Galbraith struct termios tc, save; 727923c42c1SMike Galbraith int delay_msecs, c; 72886470930SIngo Molnar 729923c42c1SMike Galbraith tcgetattr(0, &save); 730923c42c1SMike Galbraith tc = save; 731923c42c1SMike Galbraith tc.c_lflag &= ~(ICANON | ECHO); 732923c42c1SMike Galbraith tc.c_cc[VMIN] = 0; 733923c42c1SMike Galbraith tc.c_cc[VTIME] = 0; 734091bd2e9SMike Galbraith 735923c42c1SMike Galbraith repeat: 736923c42c1SMike Galbraith delay_msecs = delay_secs * 1000; 737923c42c1SMike Galbraith tcsetattr(0, TCSANOW, &tc); 738923c42c1SMike Galbraith /* trash return*/ 739923c42c1SMike Galbraith getc(stdin); 74086470930SIngo Molnar 74186470930SIngo Molnar do { 74286470930SIngo Molnar print_sym_table(); 74386470930SIngo Molnar } while (!poll(&stdin_poll, 1, delay_msecs) == 1); 74486470930SIngo Molnar 745923c42c1SMike Galbraith c = getc(stdin); 746923c42c1SMike Galbraith tcsetattr(0, TCSAFLUSH, &save); 747923c42c1SMike Galbraith 748923c42c1SMike Galbraith handle_keypress(c); 749923c42c1SMike Galbraith goto repeat; 75086470930SIngo Molnar 75186470930SIngo Molnar return NULL; 75286470930SIngo Molnar } 75386470930SIngo Molnar 7542ab52083SAnton Blanchard /* Tag samples to be skipped. */ 755f37a291cSIngo Molnar static const char *skip_symbols[] = { 7562ab52083SAnton Blanchard "default_idle", 7572ab52083SAnton Blanchard "cpu_idle", 7582ab52083SAnton Blanchard "enter_idle", 7592ab52083SAnton Blanchard "exit_idle", 7602ab52083SAnton Blanchard "mwait_idle", 76159b90056SArnaldo Carvalho de Melo "mwait_idle_with_hints", 7628357275bSArnaldo Carvalho de Melo "poll_idle", 7633a3393efSAnton Blanchard "ppc64_runlatch_off", 7643a3393efSAnton Blanchard "pseries_dedicated_idle_sleep", 7652ab52083SAnton Blanchard NULL 7662ab52083SAnton Blanchard }; 7672ab52083SAnton Blanchard 768439d473bSArnaldo Carvalho de Melo static int symbol_filter(struct map *map, struct symbol *sym) 76986470930SIngo Molnar { 77086470930SIngo Molnar struct sym_entry *syme; 77186470930SIngo Molnar const char *name = sym->name; 7722ab52083SAnton Blanchard int i; 77386470930SIngo Molnar 7743a3393efSAnton Blanchard /* 7753a3393efSAnton Blanchard * ppc64 uses function descriptors and appends a '.' to the 7763a3393efSAnton Blanchard * start of every instruction address. Remove it. 7773a3393efSAnton Blanchard */ 7783a3393efSAnton Blanchard if (name[0] == '.') 7793a3393efSAnton Blanchard name++; 7803a3393efSAnton Blanchard 78186470930SIngo Molnar if (!strcmp(name, "_text") || 78286470930SIngo Molnar !strcmp(name, "_etext") || 78386470930SIngo Molnar !strcmp(name, "_sinittext") || 78486470930SIngo Molnar !strncmp("init_module", name, 11) || 78586470930SIngo Molnar !strncmp("cleanup_module", name, 14) || 78686470930SIngo Molnar strstr(name, "_text_start") || 78786470930SIngo Molnar strstr(name, "_text_end")) 78886470930SIngo Molnar return 1; 78986470930SIngo Molnar 790439d473bSArnaldo Carvalho de Melo syme = dso__sym_priv(map->dso, sym); 791439d473bSArnaldo Carvalho de Melo syme->map = map; 792923c42c1SMike Galbraith pthread_mutex_init(&syme->source_lock, NULL); 793923c42c1SMike Galbraith if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter)) 794923c42c1SMike Galbraith sym_filter_entry = syme; 795923c42c1SMike Galbraith 7962ab52083SAnton Blanchard for (i = 0; skip_symbols[i]; i++) { 7972ab52083SAnton Blanchard if (!strcmp(skip_symbols[i], name)) { 79886470930SIngo Molnar syme->skip = 1; 7992ab52083SAnton Blanchard break; 8002ab52083SAnton Blanchard } 8012ab52083SAnton Blanchard } 80286470930SIngo Molnar 80386470930SIngo Molnar return 0; 80486470930SIngo Molnar } 80586470930SIngo Molnar 80686470930SIngo Molnar static int parse_symbols(void) 80786470930SIngo Molnar { 808439d473bSArnaldo Carvalho de Melo if (dsos__load_kernel(vmlinux_name, sizeof(struct sym_entry), 8095c206805SArnaldo Carvalho de Melo symbol_filter, verbose, 1) <= 0) 81086470930SIngo Molnar return -1; 81186470930SIngo Molnar 81286470930SIngo Molnar if (dump_symtab) 813439d473bSArnaldo Carvalho de Melo dsos__fprintf(stderr); 81486470930SIngo Molnar 81586470930SIngo Molnar return 0; 81686470930SIngo Molnar } 81786470930SIngo Molnar 81886470930SIngo Molnar /* 81986470930SIngo Molnar * Binary search in the histogram table and record the hit: 82086470930SIngo Molnar */ 8219cffa8d5SPaul Mackerras static void record_ip(u64 ip, int counter) 82286470930SIngo Molnar { 823439d473bSArnaldo Carvalho de Melo struct map *map; 824439d473bSArnaldo Carvalho de Melo struct symbol *sym = kernel_maps__find_symbol(ip, &map); 82586470930SIngo Molnar 82686470930SIngo Molnar if (sym != NULL) { 827439d473bSArnaldo Carvalho de Melo struct sym_entry *syme = dso__sym_priv(map->dso, sym); 82886470930SIngo Molnar 82986470930SIngo Molnar if (!syme->skip) { 83086470930SIngo Molnar syme->count[counter]++; 831923c42c1SMike Galbraith record_precise_ip(syme, counter, ip); 83286470930SIngo Molnar pthread_mutex_lock(&active_symbols_lock); 83386470930SIngo Molnar if (list_empty(&syme->node) || !syme->node.next) 83486470930SIngo Molnar __list_insert_active_sym(syme); 83586470930SIngo Molnar pthread_mutex_unlock(&active_symbols_lock); 83686470930SIngo Molnar return; 83786470930SIngo Molnar } 83886470930SIngo Molnar } 83986470930SIngo Molnar 84086470930SIngo Molnar samples--; 84186470930SIngo Molnar } 84286470930SIngo Molnar 843e6e18ec7SPeter Zijlstra static void process_event(u64 ip, int counter, int user) 84486470930SIngo Molnar { 84586470930SIngo Molnar samples++; 84686470930SIngo Molnar 847e6e18ec7SPeter Zijlstra if (user) { 84886470930SIngo Molnar userspace_samples++; 84986470930SIngo Molnar return; 85086470930SIngo Molnar } 85186470930SIngo Molnar 85286470930SIngo Molnar record_ip(ip, counter); 85386470930SIngo Molnar } 85486470930SIngo Molnar 85586470930SIngo Molnar struct mmap_data { 85686470930SIngo Molnar int counter; 85786470930SIngo Molnar void *base; 858f37a291cSIngo Molnar int mask; 85986470930SIngo Molnar unsigned int prev; 86086470930SIngo Molnar }; 86186470930SIngo Molnar 86286470930SIngo Molnar static unsigned int mmap_read_head(struct mmap_data *md) 86386470930SIngo Molnar { 864cdd6c482SIngo Molnar struct perf_event_mmap_page *pc = md->base; 86586470930SIngo Molnar int head; 86686470930SIngo Molnar 86786470930SIngo Molnar head = pc->data_head; 86886470930SIngo Molnar rmb(); 86986470930SIngo Molnar 87086470930SIngo Molnar return head; 87186470930SIngo Molnar } 87286470930SIngo Molnar 87386470930SIngo Molnar struct timeval last_read, this_read; 87486470930SIngo Molnar 8752f01190aSFrederic Weisbecker static void mmap_read_counter(struct mmap_data *md) 87686470930SIngo Molnar { 87786470930SIngo Molnar unsigned int head = mmap_read_head(md); 87886470930SIngo Molnar unsigned int old = md->prev; 87986470930SIngo Molnar unsigned char *data = md->base + page_size; 88086470930SIngo Molnar int diff; 88186470930SIngo Molnar 88286470930SIngo Molnar gettimeofday(&this_read, NULL); 88386470930SIngo Molnar 88486470930SIngo Molnar /* 88586470930SIngo Molnar * If we're further behind than half the buffer, there's a chance 88686470930SIngo Molnar * the writer will bite our tail and mess up the samples under us. 88786470930SIngo Molnar * 88886470930SIngo Molnar * If we somehow ended up ahead of the head, we got messed up. 88986470930SIngo Molnar * 89086470930SIngo Molnar * In either case, truncate and restart at head. 89186470930SIngo Molnar */ 89286470930SIngo Molnar diff = head - old; 89386470930SIngo Molnar if (diff > md->mask / 2 || diff < 0) { 89486470930SIngo Molnar struct timeval iv; 89586470930SIngo Molnar unsigned long msecs; 89686470930SIngo Molnar 89786470930SIngo Molnar timersub(&this_read, &last_read, &iv); 89886470930SIngo Molnar msecs = iv.tv_sec*1000 + iv.tv_usec/1000; 89986470930SIngo Molnar 90086470930SIngo Molnar fprintf(stderr, "WARNING: failed to keep up with mmap data." 90186470930SIngo Molnar " Last read %lu msecs ago.\n", msecs); 90286470930SIngo Molnar 90386470930SIngo Molnar /* 90486470930SIngo Molnar * head points to a known good entry, start there. 90586470930SIngo Molnar */ 90686470930SIngo Molnar old = head; 90786470930SIngo Molnar } 90886470930SIngo Molnar 90986470930SIngo Molnar last_read = this_read; 91086470930SIngo Molnar 91186470930SIngo Molnar for (; old != head;) { 91286470930SIngo Molnar event_t *event = (event_t *)&data[old & md->mask]; 91386470930SIngo Molnar 91486470930SIngo Molnar event_t event_copy; 91586470930SIngo Molnar 91686470930SIngo Molnar size_t size = event->header.size; 91786470930SIngo Molnar 91886470930SIngo Molnar /* 91986470930SIngo Molnar * Event straddles the mmap boundary -- header should always 92086470930SIngo Molnar * be inside due to u64 alignment of output. 92186470930SIngo Molnar */ 92286470930SIngo Molnar if ((old & md->mask) + size != ((old + size) & md->mask)) { 92386470930SIngo Molnar unsigned int offset = old; 92486470930SIngo Molnar unsigned int len = min(sizeof(*event), size), cpy; 92586470930SIngo Molnar void *dst = &event_copy; 92686470930SIngo Molnar 92786470930SIngo Molnar do { 92886470930SIngo Molnar cpy = min(md->mask + 1 - (offset & md->mask), len); 92986470930SIngo Molnar memcpy(dst, &data[offset & md->mask], cpy); 93086470930SIngo Molnar offset += cpy; 93186470930SIngo Molnar dst += cpy; 93286470930SIngo Molnar len -= cpy; 93386470930SIngo Molnar } while (len); 93486470930SIngo Molnar 93586470930SIngo Molnar event = &event_copy; 93686470930SIngo Molnar } 93786470930SIngo Molnar 93886470930SIngo Molnar old += size; 93986470930SIngo Molnar 940cdd6c482SIngo Molnar if (event->header.type == PERF_RECORD_SAMPLE) { 941e6e18ec7SPeter Zijlstra int user = 942cdd6c482SIngo Molnar (event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK) == PERF_RECORD_MISC_USER; 943e6e18ec7SPeter Zijlstra process_event(event->ip.ip, md->counter, user); 94486470930SIngo Molnar } 94586470930SIngo Molnar } 94686470930SIngo Molnar 94786470930SIngo Molnar md->prev = old; 94886470930SIngo Molnar } 94986470930SIngo Molnar 95086470930SIngo Molnar static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; 95186470930SIngo Molnar static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; 95286470930SIngo Molnar 9532f01190aSFrederic Weisbecker static void mmap_read(void) 9542f01190aSFrederic Weisbecker { 9552f01190aSFrederic Weisbecker int i, counter; 9562f01190aSFrederic Weisbecker 9572f01190aSFrederic Weisbecker for (i = 0; i < nr_cpus; i++) { 9582f01190aSFrederic Weisbecker for (counter = 0; counter < nr_counters; counter++) 9592f01190aSFrederic Weisbecker mmap_read_counter(&mmap_array[i][counter]); 9602f01190aSFrederic Weisbecker } 9612f01190aSFrederic Weisbecker } 9622f01190aSFrederic Weisbecker 963716c69feSIngo Molnar int nr_poll; 964716c69feSIngo Molnar int group_fd; 965716c69feSIngo Molnar 966716c69feSIngo Molnar static void start_counter(int i, int counter) 96786470930SIngo Molnar { 968cdd6c482SIngo Molnar struct perf_event_attr *attr; 9690fdc7e67SMike Galbraith int cpu; 97086470930SIngo Molnar 97186470930SIngo Molnar cpu = profile_cpu; 97286470930SIngo Molnar if (target_pid == -1 && profile_cpu == -1) 97386470930SIngo Molnar cpu = i; 97486470930SIngo Molnar 97586470930SIngo Molnar attr = attrs + counter; 97686470930SIngo Molnar 97786470930SIngo Molnar attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; 97886470930SIngo Molnar attr->freq = freq; 9790fdc7e67SMike Galbraith attr->inherit = (cpu < 0) && inherit; 98086470930SIngo Molnar 981716c69feSIngo Molnar try_again: 982cdd6c482SIngo Molnar fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0); 983716c69feSIngo Molnar 98486470930SIngo Molnar if (fd[i][counter] < 0) { 98586470930SIngo Molnar int err = errno; 98686470930SIngo Molnar 98786470930SIngo Molnar if (err == EPERM) 988716c69feSIngo Molnar die("No permission - are you root?\n"); 989716c69feSIngo Molnar /* 990716c69feSIngo Molnar * If it's cycles then fall back to hrtimer 991716c69feSIngo Molnar * based cpu-clock-tick sw counter, which 992716c69feSIngo Molnar * is always available even if no PMU support: 993716c69feSIngo Molnar */ 994716c69feSIngo Molnar if (attr->type == PERF_TYPE_HARDWARE 995f4dbfa8fSPeter Zijlstra && attr->config == PERF_COUNT_HW_CPU_CYCLES) { 996716c69feSIngo Molnar 9973da297a6SIngo Molnar if (verbose) 998716c69feSIngo Molnar warning(" ... trying to fall back to cpu-clock-ticks\n"); 9993da297a6SIngo Molnar 1000716c69feSIngo Molnar attr->type = PERF_TYPE_SOFTWARE; 1001f4dbfa8fSPeter Zijlstra attr->config = PERF_COUNT_SW_CPU_CLOCK; 1002716c69feSIngo Molnar goto try_again; 1003716c69feSIngo Molnar } 100430c806a0SIngo Molnar printf("\n"); 100530c806a0SIngo Molnar error("perfcounter syscall returned with %d (%s)\n", 100630c806a0SIngo Molnar fd[i][counter], strerror(err)); 1007cdd6c482SIngo Molnar die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); 100886470930SIngo Molnar exit(-1); 100986470930SIngo Molnar } 101086470930SIngo Molnar assert(fd[i][counter] >= 0); 101186470930SIngo Molnar fcntl(fd[i][counter], F_SETFL, O_NONBLOCK); 101286470930SIngo Molnar 101386470930SIngo Molnar /* 101486470930SIngo Molnar * First counter acts as the group leader: 101586470930SIngo Molnar */ 101686470930SIngo Molnar if (group && group_fd == -1) 101786470930SIngo Molnar group_fd = fd[i][counter]; 101886470930SIngo Molnar 101986470930SIngo Molnar event_array[nr_poll].fd = fd[i][counter]; 102086470930SIngo Molnar event_array[nr_poll].events = POLLIN; 102186470930SIngo Molnar nr_poll++; 102286470930SIngo Molnar 102386470930SIngo Molnar mmap_array[i][counter].counter = counter; 102486470930SIngo Molnar mmap_array[i][counter].prev = 0; 102586470930SIngo Molnar mmap_array[i][counter].mask = mmap_pages*page_size - 1; 102686470930SIngo Molnar mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size, 102786470930SIngo Molnar PROT_READ, MAP_SHARED, fd[i][counter], 0); 102886470930SIngo Molnar if (mmap_array[i][counter].base == MAP_FAILED) 102986470930SIngo Molnar die("failed to mmap with %d (%s)\n", errno, strerror(errno)); 103086470930SIngo Molnar } 1031716c69feSIngo Molnar 1032716c69feSIngo Molnar static int __cmd_top(void) 1033716c69feSIngo Molnar { 1034716c69feSIngo Molnar pthread_t thread; 1035716c69feSIngo Molnar int i, counter; 1036716c69feSIngo Molnar int ret; 1037716c69feSIngo Molnar 1038716c69feSIngo Molnar for (i = 0; i < nr_cpus; i++) { 1039716c69feSIngo Molnar group_fd = -1; 1040716c69feSIngo Molnar for (counter = 0; counter < nr_counters; counter++) 1041716c69feSIngo Molnar start_counter(i, counter); 104286470930SIngo Molnar } 104386470930SIngo Molnar 10442f01190aSFrederic Weisbecker /* Wait for a minimal set of events before starting the snapshot */ 10452f01190aSFrederic Weisbecker poll(event_array, nr_poll, 100); 10462f01190aSFrederic Weisbecker 10472f01190aSFrederic Weisbecker mmap_read(); 10482f01190aSFrederic Weisbecker 104986470930SIngo Molnar if (pthread_create(&thread, NULL, display_thread, NULL)) { 105086470930SIngo Molnar printf("Could not create display thread.\n"); 105186470930SIngo Molnar exit(-1); 105286470930SIngo Molnar } 105386470930SIngo Molnar 105486470930SIngo Molnar if (realtime_prio) { 105586470930SIngo Molnar struct sched_param param; 105686470930SIngo Molnar 105786470930SIngo Molnar param.sched_priority = realtime_prio; 105886470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 105986470930SIngo Molnar printf("Could not set realtime priority.\n"); 106086470930SIngo Molnar exit(-1); 106186470930SIngo Molnar } 106286470930SIngo Molnar } 106386470930SIngo Molnar 106486470930SIngo Molnar while (1) { 106586470930SIngo Molnar int hits = samples; 106686470930SIngo Molnar 10672f01190aSFrederic Weisbecker mmap_read(); 106886470930SIngo Molnar 106986470930SIngo Molnar if (hits == samples) 107086470930SIngo Molnar ret = poll(event_array, nr_poll, 100); 107186470930SIngo Molnar } 107286470930SIngo Molnar 107386470930SIngo Molnar return 0; 107486470930SIngo Molnar } 107586470930SIngo Molnar 107686470930SIngo Molnar static const char * const top_usage[] = { 107786470930SIngo Molnar "perf top [<options>]", 107886470930SIngo Molnar NULL 107986470930SIngo Molnar }; 108086470930SIngo Molnar 108186470930SIngo Molnar static const struct option options[] = { 108286470930SIngo Molnar OPT_CALLBACK('e', "event", NULL, "event", 108386470930SIngo Molnar "event selector. use 'perf list' to list available events", 108486470930SIngo Molnar parse_events), 108586470930SIngo Molnar OPT_INTEGER('c', "count", &default_interval, 108686470930SIngo Molnar "event period to sample"), 108786470930SIngo Molnar OPT_INTEGER('p', "pid", &target_pid, 108886470930SIngo Molnar "profile events on existing pid"), 108986470930SIngo Molnar OPT_BOOLEAN('a', "all-cpus", &system_wide, 109086470930SIngo Molnar "system-wide collection from all CPUs"), 109186470930SIngo Molnar OPT_INTEGER('C', "CPU", &profile_cpu, 109286470930SIngo Molnar "CPU to profile on"), 109383a0944fSIngo Molnar OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"), 109486470930SIngo Molnar OPT_INTEGER('m', "mmap-pages", &mmap_pages, 109586470930SIngo Molnar "number of mmap data pages"), 109686470930SIngo Molnar OPT_INTEGER('r', "realtime", &realtime_prio, 109786470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 109886470930SIngo Molnar OPT_INTEGER('d', "delay", &delay_secs, 109986470930SIngo Molnar "number of seconds to delay between refreshes"), 110086470930SIngo Molnar OPT_BOOLEAN('D', "dump-symtab", &dump_symtab, 110186470930SIngo Molnar "dump the symbol table used for profiling"), 110286470930SIngo Molnar OPT_INTEGER('f', "count-filter", &count_filter, 110386470930SIngo Molnar "only display functions with more events than this"), 110486470930SIngo Molnar OPT_BOOLEAN('g', "group", &group, 110586470930SIngo Molnar "put the counters into a counter group"), 11060fdc7e67SMike Galbraith OPT_BOOLEAN('i', "inherit", &inherit, 11070fdc7e67SMike Galbraith "child tasks inherit counters"), 1108923c42c1SMike Galbraith OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name", 1109923c42c1SMike Galbraith "symbol to annotate - requires -k option"), 11101f208ea6SAnton Blanchard OPT_BOOLEAN('z', "zero", &zero, 111186470930SIngo Molnar "zero history across updates"), 111286470930SIngo Molnar OPT_INTEGER('F', "freq", &freq, 111386470930SIngo Molnar "profile at this frequency"), 111486470930SIngo Molnar OPT_INTEGER('E', "entries", &print_entries, 111586470930SIngo Molnar "display this many functions"), 11163da297a6SIngo Molnar OPT_BOOLEAN('v', "verbose", &verbose, 11173da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 111886470930SIngo Molnar OPT_END() 111986470930SIngo Molnar }; 112086470930SIngo Molnar 1121f37a291cSIngo Molnar int cmd_top(int argc, const char **argv, const char *prefix __used) 112286470930SIngo Molnar { 112386470930SIngo Molnar int counter; 112486470930SIngo Molnar 112542976487SMike Galbraith symbol__init(); 112642976487SMike Galbraith 112786470930SIngo Molnar page_size = sysconf(_SC_PAGE_SIZE); 112886470930SIngo Molnar 112986470930SIngo Molnar argc = parse_options(argc, argv, options, top_usage, 0); 113086470930SIngo Molnar if (argc) 113186470930SIngo Molnar usage_with_options(top_usage, options); 113286470930SIngo Molnar 113386470930SIngo Molnar if (freq) { 113486470930SIngo Molnar default_interval = freq; 113586470930SIngo Molnar freq = 1; 113686470930SIngo Molnar } 113786470930SIngo Molnar 113886470930SIngo Molnar /* CPU and PID are mutually exclusive */ 113986470930SIngo Molnar if (target_pid != -1 && profile_cpu != -1) { 114086470930SIngo Molnar printf("WARNING: PID switch overriding CPU\n"); 114186470930SIngo Molnar sleep(1); 114286470930SIngo Molnar profile_cpu = -1; 114386470930SIngo Molnar } 114486470930SIngo Molnar 114586470930SIngo Molnar if (!nr_counters) 114686470930SIngo Molnar nr_counters = 1; 114786470930SIngo Molnar 114886470930SIngo Molnar if (delay_secs < 1) 114986470930SIngo Molnar delay_secs = 1; 115086470930SIngo Molnar 115186470930SIngo Molnar parse_symbols(); 1152923c42c1SMike Galbraith parse_source(sym_filter_entry); 115386470930SIngo Molnar 115486470930SIngo Molnar /* 115586470930SIngo Molnar * Fill in the ones not specifically initialized via -c: 115686470930SIngo Molnar */ 115786470930SIngo Molnar for (counter = 0; counter < nr_counters; counter++) { 115886470930SIngo Molnar if (attrs[counter].sample_period) 115986470930SIngo Molnar continue; 116086470930SIngo Molnar 116186470930SIngo Molnar attrs[counter].sample_period = default_interval; 116286470930SIngo Molnar } 116386470930SIngo Molnar 116486470930SIngo Molnar nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); 116586470930SIngo Molnar assert(nr_cpus <= MAX_NR_CPUS); 116686470930SIngo Molnar assert(nr_cpus >= 0); 116786470930SIngo Molnar 116886470930SIngo Molnar if (target_pid != -1 || profile_cpu != -1) 116986470930SIngo Molnar nr_cpus = 1; 117086470930SIngo Molnar 117186470930SIngo Molnar return __cmd_top(); 117286470930SIngo Molnar } 1173