blob: 2aea913f7eb7955be07cb0122ec4094c994f480f [file] [log] [blame]
Ingo Molnar07800602009-04-20 15:00:56 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * 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)
Ingo Molnar07800602009-04-20 15:00:56 +020018 */
Ingo Molnarbf9e1872009-06-02 23:37:05 +020019#include "builtin.h"
Ingo Molnar07800602009-04-20 15:00:56 +020020
Peter Zijlstra1a482f32009-05-23 18:28:58 +020021#include "perf.h"
Ingo Molnarbf9e1872009-06-02 23:37:05 +020022
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -030023#include "util/symbol.h"
Ingo Molnar8fc03212009-06-04 15:19:47 +020024#include "util/color.h"
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -030025#include "util/thread.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020026#include "util/util.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030027#include <linux/rbtree.h>
Ingo Molnarb456bae2009-05-26 09:17:18 +020028#include "util/parse-options.h"
29#include "util/parse-events.h"
Ingo Molnar07800602009-04-20 15:00:56 +020030
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020031#include "util/debug.h"
32
Ingo Molnar07800602009-04-20 15:00:56 +020033#include <assert.h>
34#include <fcntl.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020035
Ingo Molnar07800602009-04-20 15:00:56 +020036#include <stdio.h>
Mike Galbraith923c42c2009-07-22 20:36:03 +020037#include <termios.h>
38#include <unistd.h>
Ingo Molnar0e9b20b2009-05-26 09:17:18 +020039
Ingo Molnar07800602009-04-20 15:00:56 +020040#include <errno.h>
Ingo Molnar07800602009-04-20 15:00:56 +020041#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
Ingo Molnara21ca2c2009-06-06 09:58:57 +020056static int fd[MAX_NR_CPUS][MAX_COUNTERS];
57
Ingo Molnar42e59d72009-10-06 15:14:21 +020058static int system_wide = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020059
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +020060static int default_interval = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020061
Ingo Molnar42e59d72009-10-06 15:14:21 +020062static int count_filter = 5;
63static int print_entries = 15;
Ingo Molnar07800602009-04-20 15:00:56 +020064
Ingo Molnar42e59d72009-10-06 15:14:21 +020065static int target_pid = -1;
66static int inherit = 0;
67static int profile_cpu = -1;
68static int nr_cpus = 0;
69static unsigned int realtime_prio = 0;
70static int group = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020071static unsigned int page_size;
Ingo Molnar42e59d72009-10-06 15:14:21 +020072static unsigned int mmap_pages = 16;
73static int freq = 1000; /* 1 KHz */
Ingo Molnar07800602009-04-20 15:00:56 +020074
Ingo Molnar42e59d72009-10-06 15:14:21 +020075static int delay_secs = 2;
76static int zero = 0;
77static int dump_symtab = 0;
Ingo Molnar07800602009-04-20 15:00:56 +020078
Ingo Molnar07800602009-04-20 15:00:56 +020079/*
Mike Galbraith923c42c2009-07-22 20:36:03 +020080 * Source
81 */
82
83struct source_line {
84 u64 eip;
85 unsigned long count[MAX_COUNTERS];
86 char *line;
87 struct source_line *next;
88};
89
Ingo Molnar42e59d72009-10-06 15:14:21 +020090static char *sym_filter = NULL;
91struct sym_entry *sym_filter_entry = NULL;
92static int sym_pcnt_filter = 5;
93static int sym_counter = 0;
94static int display_weighted = -1;
Mike Galbraith923c42c2009-07-22 20:36:03 +020095
96/*
Ingo Molnar07800602009-04-20 15:00:56 +020097 * Symbols
98 */
99
Ingo Molnar07800602009-04-20 15:00:56 +0200100struct sym_entry {
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300101 struct rb_node rb_node;
102 struct list_head node;
Ingo Molnar07800602009-04-20 15:00:56 +0200103 unsigned long count[MAX_COUNTERS];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300104 unsigned long snap_count;
105 double weight;
Ingo Molnar07800602009-04-20 15:00:56 +0200106 int skip;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300107 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200108 struct source_line *source;
109 struct source_line *lines;
110 struct source_line **lines_tail;
111 pthread_mutex_t source_lock;
Ingo Molnar07800602009-04-20 15:00:56 +0200112};
113
Mike Galbraith923c42c2009-07-22 20:36:03 +0200114/*
115 * Source functions
116 */
117
118static void parse_source(struct sym_entry *syme)
119{
120 struct symbol *sym;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300121 struct map *map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200122 FILE *file;
Ingo Molnar83a09442009-08-15 12:26:57 +0200123 char command[PATH_MAX*2];
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300124 const char *path;
125 u64 len;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200126
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);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300136 map = syme->map;
137 path = map->dso->long_name;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200138
Mike Galbraith923c42c2009-07-22 20:36:03 +0200139 len = sym->end - sym->start;
140
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300141 sprintf(command,
142 "objdump --start-address=0x%016Lx "
143 "--stop-address=0x%016Lx -dS %s",
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200144 map->unmap_ip(map, sym->start),
145 map->unmap_ip(map, sym->end), path);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200146
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);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200177 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200178 }
179 if (strlen(src->line)>8 && src->line[16] == ':') {
180 src->eip = strtoull(src->line, NULL, 16);
Arnaldo Carvalho de Meloc88e4bf2009-10-20 15:54:55 -0200181 src->eip = map->unmap_ip(map, src->eip);
Mike Galbraith923c42c2009-07-22 20:36:03 +0200182 }
183 }
184 pclose(file);
185out_assign:
186 sym_filter_entry = syme;
187 pthread_mutex_unlock(&syme->source_lock);
188}
189
190static 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
203static 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 }
224out_unlock:
225 pthread_mutex_unlock(&syme->source_lock);
226}
227
228static 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];
Mike Galbraith923c42c2009-07-22 20:36:03 +0200233
234 sprintf(pattern, "<%s>:", symbol->name);
235
Mike Galbraith923c42c2009-07-22 20:36:03 +0200236 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
246static 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
262static 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}
Ingo Molnar07800602009-04-20 15:00:56 +0200319
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300320/*
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200321 * Symbols will be added here in event__process_sample and will get out
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300322 * after decayed.
323 */
324static LIST_HEAD(active_symbols);
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300325static pthread_mutex_t active_symbols_lock = PTHREAD_MUTEX_INITIALIZER;
Ingo Molnar07800602009-04-20 15:00:56 +0200326
Ingo Molnar07800602009-04-20 15:00:56 +0200327/*
328 * Ordering weight: count-1 * count-2 * ... / count-n
329 */
330static double sym_weight(const struct sym_entry *sym)
331{
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300332 double weight = sym->snap_count;
Ingo Molnar07800602009-04-20 15:00:56 +0200333 int counter;
334
Mike Galbraith46ab9762009-07-24 10:09:50 +0200335 if (!display_weighted)
336 return weight;
337
Ingo Molnar07800602009-04-20 15:00:56 +0200338 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
Ingo Molnar2debbc82009-06-05 14:29:10 +0200346static long samples;
347static long userspace_samples;
Ingo Molnar07800602009-04-20 15:00:56 +0200348static const char CONSOLE_CLEAR[] = "";
349
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300350static void __list_insert_active_sym(struct sym_entry *syme)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300351{
352 list_add(&syme->node, &active_symbols);
353}
354
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300355static 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
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300362static 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
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300372 if (se->weight > iter->weight)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300373 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}
Ingo Molnar07800602009-04-20 15:00:56 +0200381
382static void print_sym_table(void)
383{
Ingo Molnar233f0b92009-06-03 21:48:40 +0200384 int printed = 0, j;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200385 int counter, snap = !display_weighted ? sym_counter : 0;
Ingo Molnar2debbc82009-06-05 14:29:10 +0200386 float samples_per_sec = samples/delay_secs;
387 float ksamples_per_sec = (samples-userspace_samples)/delay_secs;
388 float sum_ksamples = 0.0;
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300389 struct sym_entry *syme, *n;
390 struct rb_root tmp = RB_ROOT;
391 struct rb_node *nd;
Ingo Molnar07800602009-04-20 15:00:56 +0200392
Ingo Molnar2debbc82009-06-05 14:29:10 +0200393 samples = userspace_samples = 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200394
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300395 /* Sort the active symbols */
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300396 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) {
Mike Galbraith46ab9762009-07-24 10:09:50 +0200401 syme->snap_count = syme->count[snap];
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300402 if (syme->snap_count != 0) {
403 syme->weight = sym_weight(syme);
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300404 rb_insert_active_sym(&tmp, syme);
Ingo Molnar2debbc82009-06-05 14:29:10 +0200405 sum_ksamples += syme->snap_count;
Mike Galbraithd94b9432009-05-25 09:57:56 +0200406
407 for (j = 0; j < nr_counters; j++)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300408 syme->count[j] = zero ? 0 : syme->count[j] * 7 / 8;
409 } else
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300410 list_remove_active_sym(syme);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200411 }
412
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200413 puts(CONSOLE_CLEAR);
Ingo Molnar07800602009-04-20 15:00:56 +0200414
415 printf(
416"------------------------------------------------------------------------------\n");
Ingo Molnarf2521b62009-06-03 19:17:25 +0200417 printf( " PerfTop:%8.0f irqs/sec kernel:%4.1f%% [",
Ingo Molnar2debbc82009-06-05 14:29:10 +0200418 samples_per_sec,
419 100.0 - (100.0*((samples_per_sec-ksamples_per_sec)/samples_per_sec)));
Ingo Molnar07800602009-04-20 15:00:56 +0200420
Mike Galbraith46ab9762009-07-24 10:09:50 +0200421 if (nr_counters == 1 || !display_weighted) {
Paul Mackerras9cffa8d2009-06-19 22:21:42 +1000422 printf("%Ld", (u64)attrs[0].sample_period);
Ingo Molnarcf1f4572009-06-05 13:27:02 +0200423 if (freq)
424 printf("Hz ");
425 else
426 printf(" ");
427 }
Ingo Molnar07800602009-04-20 15:00:56 +0200428
Mike Galbraith46ab9762009-07-24 10:09:50 +0200429 if (!display_weighted)
430 printf("%s", event_name(sym_counter));
431 else for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnar07800602009-04-20 15:00:56 +0200432 if (counter)
433 printf("/");
434
435 printf("%s", event_name(counter));
436 }
437
438 printf( "], ");
439
Ingo Molnarb456bae2009-05-26 09:17:18 +0200440 if (target_pid != -1)
441 printf(" (target_pid: %d", target_pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200442 else
443 printf(" (all");
444
445 if (profile_cpu != -1)
446 printf(", cpu: %d)\n", profile_cpu);
447 else {
Ingo Molnarb456bae2009-05-26 09:17:18 +0200448 if (target_pid != -1)
Ingo Molnar07800602009-04-20 15:00:56 +0200449 printf(")\n");
450 else
451 printf(", %d CPUs)\n", nr_cpus);
452 }
453
454 printf("------------------------------------------------------------------------------\n\n");
455
Mike Galbraith923c42c2009-07-22 20:36:03 +0200456 if (sym_filter_entry) {
457 show_details(sym_filter_entry);
458 return;
459 }
460
Ingo Molnar07800602009-04-20 15:00:56 +0200461 if (nr_counters == 1)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200462 printf(" samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200463 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200464 printf(" weight samples pcnt");
Ingo Molnar07800602009-04-20 15:00:56 +0200465
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300466 if (verbose)
467 printf(" RIP ");
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200468 printf(" function DSO\n");
469 printf(" %s _______ _____",
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300470 nr_counters == 1 ? " " : "______");
471 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200472 printf(" ________________");
473 printf(" ________________________________ ________________\n\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200474
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300475 for (nd = rb_first(&tmp); nd; nd = rb_next(nd)) {
Ingo Molnar83a09442009-08-15 12:26:57 +0200476 struct symbol *sym;
Ingo Molnar8fc03212009-06-04 15:19:47 +0200477 double pcnt;
Ingo Molnar07800602009-04-20 15:00:56 +0200478
Ingo Molnar83a09442009-08-15 12:26:57 +0200479 syme = rb_entry(nd, struct sym_entry, rb_node);
480 sym = (struct symbol *)(syme + 1);
481
Mike Galbraith923c42c2009-07-22 20:36:03 +0200482 if (++printed > print_entries || (int)syme->snap_count < count_filter)
Arnaldo Carvalho de Meloc44613a2009-05-29 17:03:07 -0300483 continue;
Ingo Molnar07800602009-04-20 15:00:56 +0200484
Ingo Molnar2debbc82009-06-05 14:29:10 +0200485 pcnt = 100.0 - (100.0 * ((sum_ksamples - syme->snap_count) /
486 sum_ksamples));
Mike Galbraithd94b9432009-05-25 09:57:56 +0200487
Mike Galbraith46ab9762009-07-24 10:09:50 +0200488 if (nr_counters == 1 || !display_weighted)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200489 printf("%20.2f ", syme->weight);
Mike Galbraithd94b9432009-05-25 09:57:56 +0200490 else
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200491 printf("%9.1f %10ld ", syme->weight, syme->snap_count);
Ingo Molnar8fc03212009-06-04 15:19:47 +0200492
Frederic Weisbecker1e11fd82009-07-02 20:14:34 +0200493 percent_color_fprintf(stdout, "%4.1f%%", pcnt);
Arnaldo Carvalho de Melo7ced1562009-08-26 11:51:26 -0300494 if (verbose)
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200495 printf(" %016llx", sym->start);
496 printf(" %-32s", sym->name);
497 printf(" %s", syme->map->dso->short_name);
Mike Galbraith42976482009-07-02 08:09:46 +0200498 printf("\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200499 }
Ingo Molnar07800602009-04-20 15:00:56 +0200500}
501
Mike Galbraith923c42c2009-07-22 20:36:03 +0200502static 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;
524out_free:
525 free(buf);
526}
527
528static 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
537static 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
579out_free:
580 free(buf);
581}
582
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200583static void print_mapped_keys(void)
Mike Galbraith923c42c2009-07-22 20:36:03 +0200584{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200585 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
Ingo Molnar83a09442009-08-15 12:26:57 +0200601 if (vmlinux_name) {
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200602 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
Mike Galbraith46ab9762009-07-24 10:09:50 +0200610 fprintf(stdout, "\t[z] toggle sample zeroing. \t(%d)\n", zero ? 1 : 0);
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200611 fprintf(stdout, "\t[qQ] quit.\n");
612}
613
614static 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':
Ingo Molnar83a09442009-08-15 12:26:57 +0200630 return vmlinux_name ? 1 : 0;
631 default:
632 break;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200633 }
634
635 return 0;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200636}
637
638static void handle_keypress(int c)
639{
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200640 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
Mike Galbraith923c42c2009-07-22 20:36:03 +0200663 switch (c) {
664 case 'd':
665 prompt_integer(&delay_secs, "Enter display delay");
Tim Blechmanndc799592009-10-17 18:08:29 +0200666 if (delay_secs < 1)
667 delay_secs = 1;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200668 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;
Mike Galbraith46ab9762009-07-24 10:09:50 +0200714 case 'w':
715 display_weighted = ~display_weighted;
716 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200717 case 'z':
718 zero = ~zero;
719 break;
Ingo Molnar83a09442009-08-15 12:26:57 +0200720 default:
721 break;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200722 }
723}
724
Ingo Molnarf37a2912009-07-01 12:37:06 +0200725static void *display_thread(void *arg __used)
Ingo Molnar07800602009-04-20 15:00:56 +0200726{
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200727 struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
Mike Galbraith923c42c2009-07-22 20:36:03 +0200728 struct termios tc, save;
729 int delay_msecs, c;
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200730
Mike Galbraith923c42c2009-07-22 20:36:03 +0200731 tcgetattr(0, &save);
732 tc = save;
733 tc.c_lflag &= ~(ICANON | ECHO);
734 tc.c_cc[VMIN] = 0;
735 tc.c_cc[VTIME] = 0;
Mike Galbraith091bd2e2009-08-04 10:21:23 +0200736
Mike Galbraith923c42c2009-07-22 20:36:03 +0200737repeat:
738 delay_msecs = delay_secs * 1000;
739 tcsetattr(0, TCSANOW, &tc);
740 /* trash return*/
741 getc(stdin);
Ingo Molnar07800602009-04-20 15:00:56 +0200742
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200743 do {
Ingo Molnar07800602009-04-20 15:00:56 +0200744 print_sym_table();
Frederic Weisbecker0f5486b2009-06-04 20:48:04 +0200745 } while (!poll(&stdin_poll, 1, delay_msecs) == 1);
746
Mike Galbraith923c42c2009-07-22 20:36:03 +0200747 c = getc(stdin);
748 tcsetattr(0, TCSAFLUSH, &save);
749
750 handle_keypress(c);
751 goto repeat;
Ingo Molnar07800602009-04-20 15:00:56 +0200752
753 return NULL;
754}
755
Anton Blanchard2ab52082009-07-01 09:00:46 +1000756/* Tag samples to be skipped. */
Ingo Molnarf37a2912009-07-01 12:37:06 +0200757static const char *skip_symbols[] = {
Anton Blanchard2ab52082009-07-01 09:00:46 +1000758 "default_idle",
759 "cpu_idle",
760 "enter_idle",
761 "exit_idle",
762 "mwait_idle",
Arnaldo Carvalho de Melo59b90052009-07-26 19:06:19 -0300763 "mwait_idle_with_hints",
Arnaldo Carvalho de Melo83572752009-09-25 15:02:39 -0700764 "poll_idle",
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000765 "ppc64_runlatch_off",
766 "pseries_dedicated_idle_sleep",
Anton Blanchard2ab52082009-07-01 09:00:46 +1000767 NULL
768};
769
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300770static int symbol_filter(struct map *map, struct symbol *sym)
Ingo Molnar07800602009-04-20 15:00:56 +0200771{
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300772 struct sym_entry *syme;
773 const char *name = sym->name;
Anton Blanchard2ab52082009-07-01 09:00:46 +1000774 int i;
Ingo Molnar07800602009-04-20 15:00:56 +0200775
Anton Blanchard3a3393e2009-07-01 09:00:47 +1000776 /*
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
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300783 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"))
Ingo Molnar07800602009-04-20 15:00:56 +0200790 return 1;
791
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200792 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300793 syme->map = map;
Mike Galbraith923c42c2009-07-22 20:36:03 +0200794 pthread_mutex_init(&syme->source_lock, NULL);
795 if (!sym_filter_entry && sym_filter && !strcmp(name, sym_filter))
796 sym_filter_entry = syme;
797
Anton Blanchard2ab52082009-07-01 09:00:46 +1000798 for (i = 0; skip_symbols[i]; i++) {
799 if (!strcmp(skip_symbols[i], name)) {
800 syme->skip = 1;
801 break;
802 }
803 }
Ingo Molnar07800602009-04-20 15:00:56 +0200804
Ingo Molnar07800602009-04-20 15:00:56 +0200805 return 0;
806}
807
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300808static int parse_symbols(void)
Ingo Molnar07800602009-04-20 15:00:56 +0200809{
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200810 if (dsos__load_kernel(vmlinux_name, symbol_filter, 1) <= 0)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300811 return -1;
Ingo Molnar07800602009-04-20 15:00:56 +0200812
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300813 if (dump_symtab)
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300814 dsos__fprintf(stderr);
Ingo Molnar07800602009-04-20 15:00:56 +0200815
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300816 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200817}
818
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200819static void event__process_sample(const event_t *self, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +0200820{
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200821 u64 ip = self->ip.ip;
Arnaldo Carvalho de Melo439d4732009-10-02 03:29:58 -0300822 struct map *map;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200823 struct sym_entry *syme;
824 struct symbol *sym;
Ingo Molnar07800602009-04-20 15:00:56 +0200825
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200826 switch (self->header.misc & PERF_RECORD_MISC_CPUMODE_MASK) {
827 case PERF_RECORD_MISC_USER: {
828 struct thread *thread = threads__findnew(self->ip.pid);
Ingo Molnar07800602009-04-20 15:00:56 +0200829
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200830 if (thread == NULL)
Arnaldo Carvalho de Melode046872009-05-28 14:55:41 -0300831 return;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200832
833 map = thread__find_map(thread, ip);
834 if (map != NULL) {
835 ip = map->map_ip(map, ip);
Arnaldo Carvalho de Melo66bd8422009-10-28 21:51:21 -0200836 sym = map__find_symbol(map, ip, symbol_filter);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200837 if (sym == NULL)
838 return;
839 userspace_samples++;
840 break;
Ingo Molnar07800602009-04-20 15:00:56 +0200841 }
Ingo Molnar07800602009-04-20 15:00:56 +0200842 }
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200843 /*
844 * If this is outside of all known maps,
845 * and is a negative address, try to look it
846 * up in the kernel dso, as it might be a
847 * vsyscall or vdso (which executes in user-mode).
848 */
849 if ((long long)ip >= 0)
850 return;
851 /* Fall thru */
852 case PERF_RECORD_MISC_KERNEL:
853 sym = kernel_maps__find_symbol(ip, &map);
854 if (sym == NULL)
855 return;
856 break;
857 default:
Ingo Molnar07800602009-04-20 15:00:56 +0200858 return;
859 }
860
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200861 syme = symbol__priv(sym);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200862
863 if (!syme->skip) {
864 syme->count[counter]++;
865 record_precise_ip(syme, counter, ip);
866 pthread_mutex_lock(&active_symbols_lock);
867 if (list_empty(&syme->node) || !syme->node.next)
868 __list_insert_active_sym(syme);
869 pthread_mutex_unlock(&active_symbols_lock);
870 ++samples;
871 return;
872 }
873}
874
875static void event__process_mmap(event_t *self)
876{
877 struct thread *thread = threads__findnew(self->mmap.pid);
878
879 if (thread != NULL) {
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -0200880 struct map *map = map__new(&self->mmap, NULL, 0);
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200881 if (map != NULL)
882 thread__insert_map(thread, map);
883 }
884}
885
886static void event__process_comm(event_t *self)
887{
888 struct thread *thread = threads__findnew(self->comm.pid);
889
890 if (thread != NULL)
891 thread__set_comm(thread, self->comm.comm);
892}
893
894static int event__process(event_t *event)
895{
896 switch (event->header.type) {
897 case PERF_RECORD_COMM:
898 event__process_comm(event);
899 break;
900 case PERF_RECORD_MMAP:
901 event__process_mmap(event);
902 break;
903 default:
904 break;
905 }
906
907 return 0;
Ingo Molnar07800602009-04-20 15:00:56 +0200908}
909
Ingo Molnar07800602009-04-20 15:00:56 +0200910struct mmap_data {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200911 int counter;
912 void *base;
Ingo Molnarf37a2912009-07-01 12:37:06 +0200913 int mask;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200914 unsigned int prev;
Ingo Molnar07800602009-04-20 15:00:56 +0200915};
916
917static unsigned int mmap_read_head(struct mmap_data *md)
918{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200919 struct perf_event_mmap_page *pc = md->base;
Ingo Molnar07800602009-04-20 15:00:56 +0200920 int head;
921
922 head = pc->data_head;
923 rmb();
924
925 return head;
926}
927
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200928static void mmap_read_counter(struct mmap_data *md)
Ingo Molnar07800602009-04-20 15:00:56 +0200929{
930 unsigned int head = mmap_read_head(md);
931 unsigned int old = md->prev;
932 unsigned char *data = md->base + page_size;
933 int diff;
934
Ingo Molnar07800602009-04-20 15:00:56 +0200935 /*
936 * If we're further behind than half the buffer, there's a chance
Ingo Molnar2debbc82009-06-05 14:29:10 +0200937 * the writer will bite our tail and mess up the samples under us.
Ingo Molnar07800602009-04-20 15:00:56 +0200938 *
939 * If we somehow ended up ahead of the head, we got messed up.
940 *
941 * In either case, truncate and restart at head.
942 */
943 diff = head - old;
944 if (diff > md->mask / 2 || diff < 0) {
Mike Galbraithf4f0b412009-10-13 14:57:20 +0200945 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
Ingo Molnar07800602009-04-20 15:00:56 +0200946
947 /*
948 * head points to a known good entry, start there.
949 */
950 old = head;
951 }
952
Ingo Molnar07800602009-04-20 15:00:56 +0200953 for (; old != head;) {
Ingo Molnar07800602009-04-20 15:00:56 +0200954 event_t *event = (event_t *)&data[old & md->mask];
955
956 event_t event_copy;
957
Ingo Molnar6f06ccb2009-04-20 15:22:22 +0200958 size_t size = event->header.size;
Ingo Molnar07800602009-04-20 15:00:56 +0200959
960 /*
961 * Event straddles the mmap boundary -- header should always
962 * be inside due to u64 alignment of output.
963 */
964 if ((old & md->mask) + size != ((old + size) & md->mask)) {
965 unsigned int offset = old;
966 unsigned int len = min(sizeof(*event), size), cpy;
967 void *dst = &event_copy;
968
969 do {
970 cpy = min(md->mask + 1 - (offset & md->mask), len);
971 memcpy(dst, &data[offset & md->mask], cpy);
972 offset += cpy;
973 dst += cpy;
974 len -= cpy;
975 } while (len);
976
977 event = &event_copy;
978 }
979
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -0200980 if (event->header.type == PERF_RECORD_SAMPLE)
981 event__process_sample(event, md->counter);
982 else
983 event__process(event);
Ingo Molnar07800602009-04-20 15:00:56 +0200984 old += size;
Ingo Molnar07800602009-04-20 15:00:56 +0200985 }
986
987 md->prev = old;
988}
989
Mike Galbraithc2990a22009-05-24 08:35:49 +0200990static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
991static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
992
Frederic Weisbecker2f011902009-06-06 23:10:43 +0200993static void mmap_read(void)
994{
995 int i, counter;
996
997 for (i = 0; i < nr_cpus; i++) {
998 for (counter = 0; counter < nr_counters; counter++)
999 mmap_read_counter(&mmap_array[i][counter]);
1000 }
1001}
1002
Ingo Molnar716c69f2009-06-07 17:31:52 +02001003int nr_poll;
1004int group_fd;
1005
1006static void start_counter(int i, int counter)
Ingo Molnar07800602009-04-20 15:00:56 +02001007{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001008 struct perf_event_attr *attr;
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001009 int cpu;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001010
1011 cpu = profile_cpu;
1012 if (target_pid == -1 && profile_cpu == -1)
1013 cpu = i;
1014
1015 attr = attrs + counter;
1016
1017 attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001018
1019 if (freq) {
1020 attr->sample_type |= PERF_SAMPLE_PERIOD;
1021 attr->freq = 1;
1022 attr->sample_freq = freq;
1023 }
1024
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001025 attr->inherit = (cpu < 0) && inherit;
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001026 attr->mmap = 1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001027
1028try_again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001029 fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
Ingo Molnar716c69f2009-06-07 17:31:52 +02001030
1031 if (fd[i][counter] < 0) {
1032 int err = errno;
1033
Ingo Molnar716c69f2009-06-07 17:31:52 +02001034 if (err == EPERM)
Ingo Molnar3da297a2009-06-07 17:39:02 +02001035 die("No permission - are you root?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001036 /*
1037 * If it's cycles then fall back to hrtimer
1038 * based cpu-clock-tick sw counter, which
1039 * is always available even if no PMU support:
1040 */
1041 if (attr->type == PERF_TYPE_HARDWARE
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001042 && attr->config == PERF_COUNT_HW_CPU_CYCLES) {
Ingo Molnar716c69f2009-06-07 17:31:52 +02001043
Ingo Molnar3da297a2009-06-07 17:39:02 +02001044 if (verbose)
1045 warning(" ... trying to fall back to cpu-clock-ticks\n");
1046
Ingo Molnar716c69f2009-06-07 17:31:52 +02001047 attr->type = PERF_TYPE_SOFTWARE;
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +02001048 attr->config = PERF_COUNT_SW_CPU_CLOCK;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001049 goto try_again;
1050 }
Ingo Molnar30c806a2009-06-07 17:46:24 +02001051 printf("\n");
1052 error("perfcounter syscall returned with %d (%s)\n",
1053 fd[i][counter], strerror(err));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001054 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
Ingo Molnar716c69f2009-06-07 17:31:52 +02001055 exit(-1);
1056 }
1057 assert(fd[i][counter] >= 0);
1058 fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
1059
1060 /*
1061 * First counter acts as the group leader:
1062 */
1063 if (group && group_fd == -1)
1064 group_fd = fd[i][counter];
1065
1066 event_array[nr_poll].fd = fd[i][counter];
1067 event_array[nr_poll].events = POLLIN;
1068 nr_poll++;
1069
1070 mmap_array[i][counter].counter = counter;
1071 mmap_array[i][counter].prev = 0;
1072 mmap_array[i][counter].mask = mmap_pages*page_size - 1;
1073 mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
1074 PROT_READ, MAP_SHARED, fd[i][counter], 0);
1075 if (mmap_array[i][counter].base == MAP_FAILED)
1076 die("failed to mmap with %d (%s)\n", errno, strerror(errno));
1077}
1078
1079static int __cmd_top(void)
1080{
1081 pthread_t thread;
1082 int i, counter;
Ingo Molnar07800602009-04-20 15:00:56 +02001083 int ret;
1084
Arnaldo Carvalho de Melo5b2bb752009-10-26 19:23:19 -02001085 if (target_pid != -1)
1086 event__synthesize_thread(target_pid, event__process);
1087 else
1088 event__synthesize_threads(event__process);
1089
Ingo Molnar07800602009-04-20 15:00:56 +02001090 for (i = 0; i < nr_cpus; i++) {
1091 group_fd = -1;
Ingo Molnar716c69f2009-06-07 17:31:52 +02001092 for (counter = 0; counter < nr_counters; counter++)
1093 start_counter(i, counter);
Ingo Molnar07800602009-04-20 15:00:56 +02001094 }
1095
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001096 /* Wait for a minimal set of events before starting the snapshot */
1097 poll(event_array, nr_poll, 100);
1098
1099 mmap_read();
1100
Ingo Molnar07800602009-04-20 15:00:56 +02001101 if (pthread_create(&thread, NULL, display_thread, NULL)) {
1102 printf("Could not create display thread.\n");
1103 exit(-1);
1104 }
1105
1106 if (realtime_prio) {
1107 struct sched_param param;
1108
1109 param.sched_priority = realtime_prio;
1110 if (sched_setscheduler(0, SCHED_FIFO, &param)) {
1111 printf("Could not set realtime priority.\n");
1112 exit(-1);
1113 }
1114 }
1115
1116 while (1) {
Ingo Molnar2debbc82009-06-05 14:29:10 +02001117 int hits = samples;
Ingo Molnar07800602009-04-20 15:00:56 +02001118
Frederic Weisbecker2f011902009-06-06 23:10:43 +02001119 mmap_read();
Ingo Molnar07800602009-04-20 15:00:56 +02001120
Ingo Molnar2debbc82009-06-05 14:29:10 +02001121 if (hits == samples)
Ingo Molnar07800602009-04-20 15:00:56 +02001122 ret = poll(event_array, nr_poll, 100);
1123 }
1124
1125 return 0;
1126}
Ingo Molnarb456bae2009-05-26 09:17:18 +02001127
1128static const char * const top_usage[] = {
1129 "perf top [<options>]",
1130 NULL
1131};
1132
Ingo Molnarb456bae2009-05-26 09:17:18 +02001133static const struct option options[] = {
1134 OPT_CALLBACK('e', "event", NULL, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001135 "event selector. use 'perf list' to list available events",
1136 parse_events),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001137 OPT_INTEGER('c', "count", &default_interval,
1138 "event period to sample"),
1139 OPT_INTEGER('p', "pid", &target_pid,
1140 "profile events on existing pid"),
1141 OPT_BOOLEAN('a', "all-cpus", &system_wide,
1142 "system-wide collection from all CPUs"),
1143 OPT_INTEGER('C', "CPU", &profile_cpu,
1144 "CPU to profile on"),
Ingo Molnar83a09442009-08-15 12:26:57 +02001145 OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001146 OPT_INTEGER('m', "mmap-pages", &mmap_pages,
1147 "number of mmap data pages"),
1148 OPT_INTEGER('r', "realtime", &realtime_prio,
1149 "collect data with this RT SCHED_FIFO priority"),
Mike Galbraithdb20c002009-05-26 15:25:34 +02001150 OPT_INTEGER('d', "delay", &delay_secs,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001151 "number of seconds to delay between refreshes"),
1152 OPT_BOOLEAN('D', "dump-symtab", &dump_symtab,
1153 "dump the symbol table used for profiling"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001154 OPT_INTEGER('f', "count-filter", &count_filter,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001155 "only display functions with more events than this"),
1156 OPT_BOOLEAN('g', "group", &group,
1157 "put the counters into a counter group"),
Mike Galbraith0fdc7e62009-07-21 10:30:36 +02001158 OPT_BOOLEAN('i', "inherit", &inherit,
1159 "child tasks inherit counters"),
Mike Galbraith923c42c2009-07-22 20:36:03 +02001160 OPT_STRING('s', "sym-annotate", &sym_filter, "symbol name",
1161 "symbol to annotate - requires -k option"),
Anton Blanchard1f208ea2009-07-01 09:00:44 +10001162 OPT_BOOLEAN('z', "zero", &zero,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001163 "zero history across updates"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001164 OPT_INTEGER('F', "freq", &freq,
Ingo Molnarb456bae2009-05-26 09:17:18 +02001165 "profile at this frequency"),
Ingo Molnar6e53cdf2009-06-04 08:53:05 +02001166 OPT_INTEGER('E', "entries", &print_entries,
1167 "display this many functions"),
Ingo Molnar3da297a2009-06-07 17:39:02 +02001168 OPT_BOOLEAN('v', "verbose", &verbose,
1169 "be more verbose (show counter open errors, etc)"),
Ingo Molnarb456bae2009-05-26 09:17:18 +02001170 OPT_END()
1171};
1172
Ingo Molnarf37a2912009-07-01 12:37:06 +02001173int cmd_top(int argc, const char **argv, const char *prefix __used)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001174{
1175 int counter;
1176
Arnaldo Carvalho de Melo00a192b2009-10-30 16:28:24 -02001177 symbol__init(sizeof(struct sym_entry));
Mike Galbraith42976482009-07-02 08:09:46 +02001178
Ingo Molnarb456bae2009-05-26 09:17:18 +02001179 page_size = sysconf(_SC_PAGE_SIZE);
1180
Ingo Molnarb456bae2009-05-26 09:17:18 +02001181 argc = parse_options(argc, argv, options, top_usage, 0);
1182 if (argc)
1183 usage_with_options(top_usage, options);
1184
Ingo Molnarb456bae2009-05-26 09:17:18 +02001185 /* CPU and PID are mutually exclusive */
1186 if (target_pid != -1 && profile_cpu != -1) {
1187 printf("WARNING: PID switch overriding CPU\n");
1188 sleep(1);
1189 profile_cpu = -1;
1190 }
1191
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001192 if (!nr_counters)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001193 nr_counters = 1;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001194
Frederic Weisbecker2f335a02009-06-05 19:31:01 +02001195 if (delay_secs < 1)
1196 delay_secs = 1;
1197
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001198 parse_symbols();
Mike Galbraith923c42c2009-07-22 20:36:03 +02001199 parse_source(sym_filter_entry);
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001200
Mike Galbraith7e4ff9e2009-10-12 07:56:03 +02001201
1202 /*
1203 * User specified count overrides default frequency.
1204 */
1205 if (default_interval)
1206 freq = 0;
1207 else if (freq) {
1208 default_interval = freq;
1209 } else {
1210 fprintf(stderr, "frequency and count are zero, aborting\n");
1211 exit(EXIT_FAILURE);
1212 }
1213
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001214 /*
1215 * Fill in the ones not specifically initialized via -c:
1216 */
Ingo Molnarb456bae2009-05-26 09:17:18 +02001217 for (counter = 0; counter < nr_counters; counter++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001218 if (attrs[counter].sample_period)
Ingo Molnarb456bae2009-05-26 09:17:18 +02001219 continue;
1220
Ingo Molnara21ca2c2009-06-06 09:58:57 +02001221 attrs[counter].sample_period = default_interval;
Ingo Molnarb456bae2009-05-26 09:17:18 +02001222 }
1223
1224 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1225 assert(nr_cpus <= MAX_NR_CPUS);
1226 assert(nr_cpus >= 0);
1227
1228 if (target_pid != -1 || profile_cpu != -1)
1229 nr_cpus = 1;
1230
Ingo Molnarb456bae2009-05-26 09:17:18 +02001231 return __cmd_top();
1232}