Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * kerneltop.c: show top kernel functions - performance counters showcase |
| 3 | |
| 4 | Build with: |
| 5 | |
| 6 | cc -O6 -Wall `pkg-config --cflags --libs glib-2.0` -o kerneltop kerneltop.c |
| 7 | |
| 8 | Sample output: |
| 9 | |
| 10 | ------------------------------------------------------------------------------ |
| 11 | KernelTop: 2669 irqs/sec [NMI, cache-misses/cache-refs], (all, cpu: 2) |
| 12 | ------------------------------------------------------------------------------ |
| 13 | |
| 14 | weight RIP kernel function |
| 15 | ______ ________________ _______________ |
| 16 | |
| 17 | 35.20 - ffffffff804ce74b : skb_copy_and_csum_dev |
| 18 | 33.00 - ffffffff804cb740 : sock_alloc_send_skb |
| 19 | 31.26 - ffffffff804ce808 : skb_push |
| 20 | 22.43 - ffffffff80510004 : tcp_established_options |
| 21 | 19.00 - ffffffff8027d250 : find_get_page |
| 22 | 15.76 - ffffffff804e4fc9 : eth_type_trans |
| 23 | 15.20 - ffffffff804d8baa : dst_release |
| 24 | 14.86 - ffffffff804cf5d8 : skb_release_head_state |
| 25 | 14.00 - ffffffff802217d5 : read_hpet |
| 26 | 12.00 - ffffffff804ffb7f : __ip_local_out |
| 27 | 11.97 - ffffffff804fc0c8 : ip_local_deliver_finish |
| 28 | 8.54 - ffffffff805001a3 : ip_queue_xmit |
| 29 | |
| 30 | Started by Ingo Molnar <mingo@redhat.com> |
| 31 | |
| 32 | Improvements and fixes by: |
| 33 | |
| 34 | Arjan van de Ven <arjan@linux.intel.com> |
| 35 | Yanmin Zhang <yanmin.zhang@intel.com> |
| 36 | Mike Galbraith <efault@gmx.de> |
| 37 | |
| 38 | Released under the GPL v2. (and only v2, not any later version) |
| 39 | |
| 40 | */ |
| 41 | #define _GNU_SOURCE |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/time.h> |
| 45 | #include <unistd.h> |
| 46 | #include <stdint.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <string.h> |
| 49 | #include <getopt.h> |
| 50 | #include <assert.h> |
| 51 | #include <fcntl.h> |
| 52 | #include <stdio.h> |
| 53 | #include <errno.h> |
| 54 | #include <ctype.h> |
| 55 | #include <time.h> |
| 56 | |
| 57 | #include <glib.h> |
| 58 | |
| 59 | #include <sys/syscall.h> |
| 60 | #include <sys/ioctl.h> |
| 61 | #include <sys/poll.h> |
| 62 | #include <sys/prctl.h> |
| 63 | #include <sys/wait.h> |
| 64 | #include <sys/uio.h> |
| 65 | |
| 66 | #include <linux/unistd.h> |
| 67 | |
Wu Fengguang | cea92ce | 2009-03-20 10:08:02 +0800 | [diff] [blame] | 68 | #include "perfcounters.h" |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 69 | |
| 70 | const char *event_types [] = { |
| 71 | "CPU cycles", |
| 72 | "instructions", |
| 73 | "cache-refs", |
| 74 | "cache-misses", |
| 75 | "branches", |
| 76 | "branch-misses", |
| 77 | "bus cycles" |
| 78 | }; |
| 79 | |
| 80 | const unsigned int default_count[] = { |
| 81 | 1000000, |
| 82 | 1000000, |
| 83 | 10000, |
| 84 | 10000, |
| 85 | 1000000, |
| 86 | 10000, |
| 87 | }; |
| 88 | |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 89 | static __u64 count_filter = 100; |
| 90 | |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 91 | static int event_count[MAX_COUNTERS]; |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 92 | |
| 93 | static int tid = -1; |
| 94 | static int profile_cpu = -1; |
| 95 | static int nr_cpus = 0; |
| 96 | static int nmi = 1; |
| 97 | static int group = 0; |
| 98 | |
| 99 | static char *vmlinux; |
| 100 | |
| 101 | static char *sym_filter; |
| 102 | static unsigned long filter_start; |
| 103 | static unsigned long filter_end; |
| 104 | |
| 105 | static int delay_secs = 2; |
| 106 | static int zero; |
| 107 | static int dump_symtab; |
| 108 | |
| 109 | struct source_line { |
| 110 | uint64_t EIP; |
| 111 | unsigned long count; |
| 112 | char *line; |
| 113 | }; |
| 114 | |
| 115 | static GList *lines; |
| 116 | |
| 117 | static void display_help(void) |
| 118 | { |
| 119 | printf( |
| 120 | "Usage: kerneltop [<options>]\n\n" |
| 121 | "KernelTop Options (up to %d event types can be specified at once):\n\n", |
| 122 | MAX_COUNTERS); |
| 123 | printf( |
Wu Fengguang | 95bb3be | 2009-03-20 10:08:04 +0800 | [diff] [blame^] | 124 | " -e EID --event=EID # event type ID [default: 0]\n" |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 125 | " 0: CPU cycles\n" |
| 126 | " 1: instructions\n" |
| 127 | " 2: cache accesses\n" |
| 128 | " 3: cache misses\n" |
| 129 | " 4: branch instructions\n" |
| 130 | " 5: branch prediction misses\n" |
| 131 | " 6: bus cycles\n\n" |
| 132 | " rNNN: raw PMU events (eventsel+umask)\n\n" |
| 133 | " -c CNT --count=CNT # event period to sample\n\n" |
| 134 | " -C CPU --cpu=CPU # CPU (-1 for all) [default: -1]\n" |
| 135 | " -p PID --pid=PID # PID of sampled task (-1 for all) [default: -1]\n\n" |
| 136 | " -d delay --delay=<seconds> # sampling/display delay [default: 2]\n" |
| 137 | " -f CNT --filter=CNT # min-event-count filter [default: 100]\n\n" |
| 138 | " -s symbol --symbol=<symbol> # function to be showed annotated one-shot\n" |
| 139 | " -x path --vmlinux=<path> # the vmlinux binary, required for -s use:\n" |
| 140 | " -z --zero # zero counts after display\n" |
| 141 | " -D --dump_symtab # dump symbol table to stderr on startup\n" |
| 142 | "\n"); |
| 143 | |
| 144 | exit(0); |
| 145 | } |
| 146 | |
| 147 | static void process_options(int argc, char *argv[]) |
| 148 | { |
| 149 | int error = 0, counter; |
| 150 | |
| 151 | for (;;) { |
| 152 | int option_index = 0; |
| 153 | /** Options for getopt */ |
| 154 | static struct option long_options[] = { |
| 155 | {"count", required_argument, NULL, 'c'}, |
| 156 | {"cpu", required_argument, NULL, 'C'}, |
| 157 | {"delay", required_argument, NULL, 'd'}, |
| 158 | {"dump_symtab", no_argument, NULL, 'D'}, |
Wu Fengguang | 95bb3be | 2009-03-20 10:08:04 +0800 | [diff] [blame^] | 159 | {"event", required_argument, NULL, 'e'}, |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 160 | {"filter", required_argument, NULL, 'f'}, |
| 161 | {"group", required_argument, NULL, 'g'}, |
| 162 | {"help", no_argument, NULL, 'h'}, |
| 163 | {"nmi", required_argument, NULL, 'n'}, |
| 164 | {"pid", required_argument, NULL, 'p'}, |
| 165 | {"vmlinux", required_argument, NULL, 'x'}, |
| 166 | {"symbol", required_argument, NULL, 's'}, |
| 167 | {"zero", no_argument, NULL, 'z'}, |
| 168 | {NULL, 0, NULL, 0 } |
| 169 | }; |
| 170 | int c = getopt_long(argc, argv, "c:C:d:De:f:g:hn:p:s:x:z", |
| 171 | long_options, &option_index); |
| 172 | if (c == -1) |
| 173 | break; |
| 174 | |
| 175 | switch (c) { |
| 176 | case 'c': |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 177 | event_count[nr_counters] = atoi(optarg); break; |
| 178 | case 'C': |
| 179 | /* CPU and PID are mutually exclusive */ |
| 180 | if (tid != -1) { |
| 181 | printf("WARNING: CPU switch overriding PID\n"); |
| 182 | sleep(1); |
| 183 | tid = -1; |
| 184 | } |
| 185 | profile_cpu = atoi(optarg); break; |
| 186 | case 'd': delay_secs = atoi(optarg); break; |
| 187 | case 'D': dump_symtab = 1; break; |
| 188 | |
Wu Fengguang | 95bb3be | 2009-03-20 10:08:04 +0800 | [diff] [blame^] | 189 | case 'e': error = parse_events(optarg); break; |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 190 | |
| 191 | case 'f': count_filter = atoi(optarg); break; |
| 192 | case 'g': group = atoi(optarg); break; |
| 193 | case 'h': display_help(); break; |
| 194 | case 'n': nmi = atoi(optarg); break; |
| 195 | case 'p': |
| 196 | /* CPU and PID are mutually exclusive */ |
| 197 | if (profile_cpu != -1) { |
| 198 | printf("WARNING: PID switch overriding CPU\n"); |
| 199 | sleep(1); |
| 200 | profile_cpu = -1; |
| 201 | } |
| 202 | tid = atoi(optarg); break; |
| 203 | case 's': sym_filter = strdup(optarg); break; |
| 204 | case 'x': vmlinux = strdup(optarg); break; |
| 205 | case 'z': zero = 1; break; |
| 206 | default: error = 1; break; |
| 207 | } |
| 208 | } |
| 209 | if (error) |
| 210 | display_help(); |
| 211 | |
Wu Fengguang | 95bb3be | 2009-03-20 10:08:04 +0800 | [diff] [blame^] | 212 | if (!nr_counters) { |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 213 | nr_counters = 1; |
Wu Fengguang | 95bb3be | 2009-03-20 10:08:04 +0800 | [diff] [blame^] | 214 | event_id[0] = 0; |
| 215 | } |
Ingo Molnar | e0143ba | 2009-03-23 21:29:59 +0100 | [diff] [blame] | 216 | |
| 217 | for (counter = 0; counter < nr_counters; counter++) { |
| 218 | if (event_count[counter]) |
| 219 | continue; |
| 220 | |
| 221 | if (event_id[counter] < PERF_HW_EVENTS_MAX) |
| 222 | event_count[counter] = default_count[event_id[counter]]; |
| 223 | else |
| 224 | event_count[counter] = 100000; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | static uint64_t min_ip; |
| 229 | static uint64_t max_ip = -1ll; |
| 230 | |
| 231 | struct sym_entry { |
| 232 | unsigned long long addr; |
| 233 | char *sym; |
| 234 | unsigned long count[MAX_COUNTERS]; |
| 235 | int skip; |
| 236 | GList *source; |
| 237 | }; |
| 238 | |
| 239 | #define MAX_SYMS 100000 |
| 240 | |
| 241 | static int sym_table_count; |
| 242 | |
| 243 | struct sym_entry *sym_filter_entry; |
| 244 | |
| 245 | static struct sym_entry sym_table[MAX_SYMS]; |
| 246 | |
| 247 | static void show_details(struct sym_entry *sym); |
| 248 | |
| 249 | /* |
| 250 | * Ordering weight: count-1 * count-1 * ... / count-n |
| 251 | */ |
| 252 | static double sym_weight(const struct sym_entry *sym) |
| 253 | { |
| 254 | double weight; |
| 255 | int counter; |
| 256 | |
| 257 | weight = sym->count[0]; |
| 258 | |
| 259 | for (counter = 1; counter < nr_counters-1; counter++) |
| 260 | weight *= sym->count[counter]; |
| 261 | |
| 262 | weight /= (sym->count[counter] + 1); |
| 263 | |
| 264 | return weight; |
| 265 | } |
| 266 | |
| 267 | static int compare(const void *__sym1, const void *__sym2) |
| 268 | { |
| 269 | const struct sym_entry *sym1 = __sym1, *sym2 = __sym2; |
| 270 | |
| 271 | return sym_weight(sym1) < sym_weight(sym2); |
| 272 | } |
| 273 | |
| 274 | static time_t last_refresh; |
| 275 | static long events; |
| 276 | static long userspace_events; |
| 277 | static const char CONSOLE_CLEAR[] = "[H[2J"; |
| 278 | |
| 279 | static struct sym_entry tmp[MAX_SYMS]; |
| 280 | |
| 281 | static void print_sym_table(void) |
| 282 | { |
| 283 | int i, printed; |
| 284 | int counter; |
| 285 | float events_per_sec = events/delay_secs; |
| 286 | float kevents_per_sec = (events-userspace_events)/delay_secs; |
| 287 | |
| 288 | memcpy(tmp, sym_table, sizeof(sym_table[0])*sym_table_count); |
| 289 | qsort(tmp, sym_table_count, sizeof(tmp[0]), compare); |
| 290 | |
| 291 | write(1, CONSOLE_CLEAR, strlen(CONSOLE_CLEAR)); |
| 292 | |
| 293 | printf( |
| 294 | "------------------------------------------------------------------------------\n"); |
| 295 | printf( " KernelTop:%8.0f irqs/sec kernel:%3.1f%% [%s, ", |
| 296 | events_per_sec, |
| 297 | 100.0 - (100.0*((events_per_sec-kevents_per_sec)/events_per_sec)), |
| 298 | nmi ? "NMI" : "IRQ"); |
| 299 | |
| 300 | if (nr_counters == 1) |
| 301 | printf("%d ", event_count[0]); |
| 302 | |
| 303 | for (counter = 0; counter < nr_counters; counter++) { |
| 304 | if (counter) |
| 305 | printf("/"); |
| 306 | |
| 307 | if (event_id[counter] < PERF_HW_EVENTS_MAX) |
| 308 | printf( "%s", event_types[event_id[counter]]); |
| 309 | else |
| 310 | printf( "raw:%04lx", event_id[counter]); |
| 311 | } |
| 312 | |
| 313 | printf( "], "); |
| 314 | |
| 315 | if (tid != -1) |
| 316 | printf(" (tid: %d", tid); |
| 317 | else |
| 318 | printf(" (all"); |
| 319 | |
| 320 | if (profile_cpu != -1) |
| 321 | printf(", cpu: %d)\n", profile_cpu); |
| 322 | else { |
| 323 | if (tid != -1) |
| 324 | printf(")\n"); |
| 325 | else |
| 326 | printf(", %d CPUs)\n", nr_cpus); |
| 327 | } |
| 328 | |
| 329 | printf("------------------------------------------------------------------------------\n\n"); |
| 330 | |
| 331 | if (nr_counters == 1) |
| 332 | printf(" events"); |
| 333 | else |
| 334 | printf(" weight events"); |
| 335 | |
| 336 | printf(" RIP kernel function\n" |
| 337 | " ______ ______ ________________ _______________\n\n" |
| 338 | ); |
| 339 | |
| 340 | printed = 0; |
| 341 | for (i = 0; i < sym_table_count; i++) { |
| 342 | int count; |
| 343 | |
| 344 | if (nr_counters == 1) { |
| 345 | if (printed <= 18 && |
| 346 | tmp[i].count[0] >= count_filter) { |
| 347 | printf("%19.2f - %016llx : %s\n", |
| 348 | sym_weight(tmp + i), tmp[i].addr, tmp[i].sym); |
| 349 | printed++; |
| 350 | } |
| 351 | } else { |
| 352 | if (printed <= 18 && |
| 353 | tmp[i].count[0] >= count_filter) { |
| 354 | printf("%8.1f %10ld - %016llx : %s\n", |
| 355 | sym_weight(tmp + i), |
| 356 | tmp[i].count[0], |
| 357 | tmp[i].addr, tmp[i].sym); |
| 358 | printed++; |
| 359 | } |
| 360 | } |
| 361 | /* |
| 362 | * Add decay to the counts: |
| 363 | */ |
| 364 | for (count = 0; count < nr_counters; count++) |
| 365 | sym_table[i].count[count] = zero ? 0 : sym_table[i].count[count] * 7 / 8; |
| 366 | } |
| 367 | |
| 368 | if (sym_filter_entry) |
| 369 | show_details(sym_filter_entry); |
| 370 | |
| 371 | last_refresh = time(NULL); |
| 372 | |
| 373 | { |
| 374 | struct pollfd stdin_poll = { .fd = 0, .events = POLLIN }; |
| 375 | |
| 376 | if (poll(&stdin_poll, 1, 0) == 1) { |
| 377 | printf("key pressed - exiting.\n"); |
| 378 | exit(0); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | static int read_symbol(FILE *in, struct sym_entry *s) |
| 384 | { |
| 385 | static int filter_match = 0; |
| 386 | char *sym, stype; |
| 387 | char str[500]; |
| 388 | int rc, pos; |
| 389 | |
| 390 | rc = fscanf(in, "%llx %c %499s", &s->addr, &stype, str); |
| 391 | if (rc == EOF) |
| 392 | return -1; |
| 393 | |
| 394 | assert(rc == 3); |
| 395 | |
| 396 | /* skip until end of line: */ |
| 397 | pos = strlen(str); |
| 398 | do { |
| 399 | rc = fgetc(in); |
| 400 | if (rc == '\n' || rc == EOF || pos >= 499) |
| 401 | break; |
| 402 | str[pos] = rc; |
| 403 | pos++; |
| 404 | } while (1); |
| 405 | str[pos] = 0; |
| 406 | |
| 407 | sym = str; |
| 408 | |
| 409 | /* Filter out known duplicates and non-text symbols. */ |
| 410 | if (!strcmp(sym, "_text")) |
| 411 | return 1; |
| 412 | if (!min_ip && !strcmp(sym, "_stext")) |
| 413 | return 1; |
| 414 | if (!strcmp(sym, "_etext") || !strcmp(sym, "_sinittext")) |
| 415 | return 1; |
| 416 | if (stype != 'T' && stype != 't') |
| 417 | return 1; |
| 418 | if (!strncmp("init_module", sym, 11) || !strncmp("cleanup_module", sym, 14)) |
| 419 | return 1; |
| 420 | if (strstr(sym, "_text_start") || strstr(sym, "_text_end")) |
| 421 | return 1; |
| 422 | |
| 423 | s->sym = malloc(strlen(str)); |
| 424 | assert(s->sym); |
| 425 | |
| 426 | strcpy((char *)s->sym, str); |
| 427 | s->skip = 0; |
| 428 | |
| 429 | /* Tag events to be skipped. */ |
| 430 | if (!strcmp("default_idle", s->sym) || !strcmp("cpu_idle", s->sym)) |
| 431 | s->skip = 1; |
| 432 | if (!strcmp("enter_idle", s->sym) || !strcmp("exit_idle", s->sym)) |
| 433 | s->skip = 1; |
| 434 | |
| 435 | if (filter_match == 1) { |
| 436 | filter_end = s->addr; |
| 437 | filter_match = -1; |
| 438 | if (filter_end - filter_start > 10000) { |
| 439 | printf("hm, too large filter symbol <%s> - skipping.\n", |
| 440 | sym_filter); |
| 441 | printf("symbol filter start: %016lx\n", filter_start); |
| 442 | printf(" end: %016lx\n", filter_end); |
| 443 | filter_end = filter_start = 0; |
| 444 | sym_filter = NULL; |
| 445 | sleep(1); |
| 446 | } |
| 447 | } |
| 448 | if (filter_match == 0 && sym_filter && !strcmp(s->sym, sym_filter)) { |
| 449 | filter_match = 1; |
| 450 | filter_start = s->addr; |
| 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | int compare_addr(const void *__sym1, const void *__sym2) |
| 457 | { |
| 458 | const struct sym_entry *sym1 = __sym1, *sym2 = __sym2; |
| 459 | |
| 460 | return sym1->addr > sym2->addr; |
| 461 | } |
| 462 | |
| 463 | static void sort_symbol_table(void) |
| 464 | { |
| 465 | int i, dups; |
| 466 | |
| 467 | do { |
| 468 | qsort(sym_table, sym_table_count, sizeof(sym_table[0]), compare_addr); |
| 469 | for (i = 0, dups = 0; i < sym_table_count; i++) { |
| 470 | if (sym_table[i].addr == sym_table[i+1].addr) { |
| 471 | sym_table[i+1].addr = -1ll; |
| 472 | dups++; |
| 473 | } |
| 474 | } |
| 475 | sym_table_count -= dups; |
| 476 | } while(dups); |
| 477 | } |
| 478 | |
| 479 | static void parse_symbols(void) |
| 480 | { |
| 481 | struct sym_entry *last; |
| 482 | |
| 483 | FILE *kallsyms = fopen("/proc/kallsyms", "r"); |
| 484 | |
| 485 | if (!kallsyms) { |
| 486 | printf("Could not open /proc/kallsyms - no CONFIG_KALLSYMS_ALL=y?\n"); |
| 487 | exit(-1); |
| 488 | } |
| 489 | |
| 490 | while (!feof(kallsyms)) { |
| 491 | if (read_symbol(kallsyms, &sym_table[sym_table_count]) == 0) { |
| 492 | sym_table_count++; |
| 493 | assert(sym_table_count <= MAX_SYMS); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | sort_symbol_table(); |
| 498 | min_ip = sym_table[0].addr; |
| 499 | max_ip = sym_table[sym_table_count-1].addr; |
| 500 | last = sym_table + sym_table_count++; |
| 501 | |
| 502 | last->addr = -1ll; |
| 503 | last->sym = "<end>"; |
| 504 | |
| 505 | if (filter_end) { |
| 506 | int count; |
| 507 | for (count=0; count < sym_table_count; count ++) { |
| 508 | if (!strcmp(sym_table[count].sym, sym_filter)) { |
| 509 | sym_filter_entry = &sym_table[count]; |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | if (dump_symtab) { |
| 515 | int i; |
| 516 | |
| 517 | for (i = 0; i < sym_table_count; i++) |
| 518 | fprintf(stderr, "%llx %s\n", |
| 519 | sym_table[i].addr, sym_table[i].sym); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
| 524 | static void parse_vmlinux(char *filename) |
| 525 | { |
| 526 | FILE *file; |
| 527 | char command[PATH_MAX*2]; |
| 528 | if (!filename) |
| 529 | return; |
| 530 | |
| 531 | sprintf(command, "objdump --start-address=0x%016lx --stop-address=0x%016lx -dS %s", filter_start, filter_end, filename); |
| 532 | |
| 533 | file = popen(command, "r"); |
| 534 | if (!file) |
| 535 | return; |
| 536 | |
| 537 | while (!feof(file)) { |
| 538 | struct source_line *src; |
| 539 | size_t dummy = 0; |
| 540 | char *c; |
| 541 | |
| 542 | src = malloc(sizeof(struct source_line)); |
| 543 | assert(src != NULL); |
| 544 | memset(src, 0, sizeof(struct source_line)); |
| 545 | |
| 546 | if (getline(&src->line, &dummy, file) < 0) |
| 547 | break; |
| 548 | if (!src->line) |
| 549 | break; |
| 550 | |
| 551 | c = strchr(src->line, '\n'); |
| 552 | if (c) |
| 553 | *c = 0; |
| 554 | |
| 555 | lines = g_list_prepend(lines, src); |
| 556 | |
| 557 | if (strlen(src->line)>8 && src->line[8] == ':') |
| 558 | src->EIP = strtoull(src->line, NULL, 16); |
| 559 | if (strlen(src->line)>8 && src->line[16] == ':') |
| 560 | src->EIP = strtoull(src->line, NULL, 16); |
| 561 | } |
| 562 | pclose(file); |
| 563 | lines = g_list_reverse(lines); |
| 564 | } |
| 565 | |
| 566 | static void record_precise_ip(uint64_t ip) |
| 567 | { |
| 568 | struct source_line *line; |
| 569 | GList *item; |
| 570 | |
| 571 | item = g_list_first(lines); |
| 572 | while (item) { |
| 573 | line = item->data; |
| 574 | if (line->EIP == ip) |
| 575 | line->count++; |
| 576 | if (line->EIP > ip) |
| 577 | break; |
| 578 | item = g_list_next(item); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | static void lookup_sym_in_vmlinux(struct sym_entry *sym) |
| 583 | { |
| 584 | struct source_line *line; |
| 585 | GList *item; |
| 586 | char pattern[PATH_MAX]; |
| 587 | sprintf(pattern, "<%s>:", sym->sym); |
| 588 | |
| 589 | item = g_list_first(lines); |
| 590 | while (item) { |
| 591 | line = item->data; |
| 592 | if (strstr(line->line, pattern)) { |
| 593 | sym->source = item; |
| 594 | break; |
| 595 | } |
| 596 | item = g_list_next(item); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | void show_lines(GList *item_queue, int item_queue_count) |
| 601 | { |
| 602 | int i; |
| 603 | struct source_line *line; |
| 604 | |
| 605 | for (i = 0; i < item_queue_count; i++) { |
| 606 | line = item_queue->data; |
| 607 | printf("%8li\t%s\n", line->count, line->line); |
| 608 | item_queue = g_list_next(item_queue); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | #define TRACE_COUNT 3 |
| 613 | |
| 614 | static void show_details(struct sym_entry *sym) |
| 615 | { |
| 616 | struct source_line *line; |
| 617 | GList *item; |
| 618 | int displayed = 0; |
| 619 | GList *item_queue = NULL; |
| 620 | int item_queue_count = 0; |
| 621 | |
| 622 | if (!sym->source) |
| 623 | lookup_sym_in_vmlinux(sym); |
| 624 | if (!sym->source) |
| 625 | return; |
| 626 | |
| 627 | printf("Showing details for %s\n", sym->sym); |
| 628 | |
| 629 | item = sym->source; |
| 630 | while (item) { |
| 631 | line = item->data; |
| 632 | if (displayed && strstr(line->line, ">:")) |
| 633 | break; |
| 634 | |
| 635 | if (!item_queue_count) |
| 636 | item_queue = item; |
| 637 | item_queue_count ++; |
| 638 | |
| 639 | if (line->count >= count_filter) { |
| 640 | show_lines(item_queue, item_queue_count); |
| 641 | item_queue_count = 0; |
| 642 | item_queue = NULL; |
| 643 | } else if (item_queue_count > TRACE_COUNT) { |
| 644 | item_queue = g_list_next(item_queue); |
| 645 | item_queue_count --; |
| 646 | } |
| 647 | |
| 648 | line->count = 0; |
| 649 | displayed++; |
| 650 | if (displayed > 300) |
| 651 | break; |
| 652 | item = g_list_next(item); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Binary search in the histogram table and record the hit: |
| 658 | */ |
| 659 | static void record_ip(uint64_t ip, int counter) |
| 660 | { |
| 661 | int left_idx, middle_idx, right_idx, idx; |
| 662 | unsigned long left, middle, right; |
| 663 | |
| 664 | record_precise_ip(ip); |
| 665 | |
| 666 | left_idx = 0; |
| 667 | right_idx = sym_table_count-1; |
| 668 | assert(ip <= max_ip && ip >= min_ip); |
| 669 | |
| 670 | while (left_idx + 1 < right_idx) { |
| 671 | middle_idx = (left_idx + right_idx) / 2; |
| 672 | |
| 673 | left = sym_table[ left_idx].addr; |
| 674 | middle = sym_table[middle_idx].addr; |
| 675 | right = sym_table[ right_idx].addr; |
| 676 | |
| 677 | if (!(left <= middle && middle <= right)) { |
| 678 | printf("%016lx...\n%016lx...\n%016lx\n", left, middle, right); |
| 679 | printf("%d %d %d\n", left_idx, middle_idx, right_idx); |
| 680 | } |
| 681 | assert(left <= middle && middle <= right); |
| 682 | if (!(left <= ip && ip <= right)) { |
| 683 | printf(" left: %016lx\n", left); |
| 684 | printf(" ip: %016lx\n", ip); |
| 685 | printf("right: %016lx\n", right); |
| 686 | } |
| 687 | assert(left <= ip && ip <= right); |
| 688 | /* |
| 689 | * [ left .... target .... middle .... right ] |
| 690 | * => right := middle |
| 691 | */ |
| 692 | if (ip < middle) { |
| 693 | right_idx = middle_idx; |
| 694 | continue; |
| 695 | } |
| 696 | /* |
| 697 | * [ left .... middle ... target ... right ] |
| 698 | * => left := middle |
| 699 | */ |
| 700 | left_idx = middle_idx; |
| 701 | } |
| 702 | |
| 703 | idx = left_idx; |
| 704 | |
| 705 | if (!sym_table[idx].skip) |
| 706 | sym_table[idx].count[counter]++; |
| 707 | else events--; |
| 708 | } |
| 709 | |
| 710 | static void process_event(uint64_t ip, int counter) |
| 711 | { |
| 712 | events++; |
| 713 | |
| 714 | if (ip < min_ip || ip > max_ip) { |
| 715 | userspace_events++; |
| 716 | return; |
| 717 | } |
| 718 | |
| 719 | record_ip(ip, counter); |
| 720 | } |
| 721 | |
| 722 | int main(int argc, char *argv[]) |
| 723 | { |
| 724 | struct pollfd event_array[MAX_NR_CPUS][MAX_COUNTERS]; |
| 725 | struct perf_counter_hw_event hw_event; |
| 726 | int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
| 727 | int i, counter, group_fd; |
| 728 | unsigned int cpu; |
| 729 | uint64_t ip; |
| 730 | ssize_t res; |
| 731 | int ret; |
| 732 | |
| 733 | process_options(argc, argv); |
| 734 | |
| 735 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 736 | if (tid != -1 || profile_cpu != -1) |
| 737 | nr_cpus = 1; |
| 738 | |
| 739 | assert(nr_cpus <= MAX_NR_CPUS); |
| 740 | |
| 741 | for (i = 0; i < nr_cpus; i++) { |
| 742 | group_fd = -1; |
| 743 | for (counter = 0; counter < nr_counters; counter++) { |
| 744 | |
| 745 | cpu = profile_cpu; |
| 746 | if (tid == -1 && profile_cpu == -1) |
| 747 | cpu = i; |
| 748 | |
| 749 | memset(&hw_event, 0, sizeof(hw_event)); |
| 750 | hw_event.type = event_id[counter]; |
| 751 | hw_event.raw = event_raw[counter]; |
| 752 | hw_event.irq_period = event_count[counter]; |
| 753 | hw_event.record_type = PERF_RECORD_IRQ; |
| 754 | hw_event.nmi = nmi; |
| 755 | |
| 756 | fd[i][counter] = sys_perf_counter_open(&hw_event, tid, cpu, group_fd, 0); |
| 757 | fcntl(fd[i][counter], F_SETFL, O_NONBLOCK); |
| 758 | if (fd[i][counter] < 0) { |
| 759 | printf("kerneltop error: syscall returned with %d (%s)\n", |
| 760 | fd[i][counter], strerror(-fd[i][counter])); |
| 761 | if (fd[i][counter] == -1) |
| 762 | printf("Are you root?\n"); |
| 763 | exit(-1); |
| 764 | } |
| 765 | assert(fd[i][counter] >= 0); |
| 766 | |
| 767 | /* |
| 768 | * First counter acts as the group leader: |
| 769 | */ |
| 770 | if (group && group_fd == -1) |
| 771 | group_fd = fd[i][counter]; |
| 772 | |
| 773 | event_array[i][counter].fd = fd[i][counter]; |
| 774 | event_array[i][counter].events = POLLIN; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | parse_symbols(); |
| 779 | if (vmlinux && sym_filter_entry) |
| 780 | parse_vmlinux(vmlinux); |
| 781 | |
| 782 | printf("KernelTop refresh period: %d seconds\n", delay_secs); |
| 783 | last_refresh = time(NULL); |
| 784 | |
| 785 | while (1) { |
| 786 | int hits = events; |
| 787 | |
| 788 | for (i = 0; i < nr_cpus; i++) { |
| 789 | for (counter = 0; counter < nr_counters; counter++) { |
| 790 | res = read(fd[i][counter], (char *) &ip, sizeof(ip)); |
| 791 | if (res > 0) { |
| 792 | assert(res == sizeof(ip)); |
| 793 | |
| 794 | process_event(ip, counter); |
| 795 | } |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | if (time(NULL) >= last_refresh + delay_secs) { |
| 800 | print_sym_table(); |
| 801 | events = userspace_events = 0; |
| 802 | } |
| 803 | |
| 804 | if (hits == events) |
| 805 | ret = poll(event_array[0], nr_cpus, 1000); |
| 806 | hits = events; |
| 807 | } |
| 808 | |
| 809 | return 0; |
| 810 | } |