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