Commit | Line | Data |
---|---|---|
de9ac07b PZ |
1 | |
2 | ||
3 | #define _GNU_SOURCE | |
4 | #include <sys/types.h> | |
5 | #include <sys/stat.h> | |
6 | #include <sys/time.h> | |
7 | #include <unistd.h> | |
8 | #include <stdint.h> | |
9 | #include <stdlib.h> | |
10 | #include <string.h> | |
11 | #include <limits.h> | |
12 | #include <getopt.h> | |
13 | #include <assert.h> | |
14 | #include <fcntl.h> | |
15 | #include <stdio.h> | |
16 | #include <errno.h> | |
17 | #include <ctype.h> | |
18 | #include <time.h> | |
19 | #include <sched.h> | |
20 | #include <pthread.h> | |
21 | ||
22 | #include <sys/syscall.h> | |
23 | #include <sys/ioctl.h> | |
24 | #include <sys/poll.h> | |
25 | #include <sys/prctl.h> | |
26 | #include <sys/wait.h> | |
27 | #include <sys/uio.h> | |
28 | #include <sys/mman.h> | |
29 | ||
30 | #include <linux/unistd.h> | |
31 | #include <linux/types.h> | |
32 | ||
33 | #include "../../include/linux/perf_counter.h" | |
34 | ||
35 | ||
36 | /* | |
37 | * prctl(PR_TASK_PERF_COUNTERS_DISABLE) will (cheaply) disable all | |
38 | * counters in the current task. | |
39 | */ | |
40 | #define PR_TASK_PERF_COUNTERS_DISABLE 31 | |
41 | #define PR_TASK_PERF_COUNTERS_ENABLE 32 | |
42 | ||
43 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
44 | ||
45 | #define rdclock() \ | |
46 | ({ \ | |
47 | struct timespec ts; \ | |
48 | \ | |
49 | clock_gettime(CLOCK_MONOTONIC, &ts); \ | |
50 | ts.tv_sec * 1000000000ULL + ts.tv_nsec; \ | |
51 | }) | |
52 | ||
53 | /* | |
54 | * Pick up some kernel type conventions: | |
55 | */ | |
56 | #define __user | |
57 | #define asmlinkage | |
58 | ||
59 | #ifdef __x86_64__ | |
e0202f56 | 60 | #define __NR_perf_counter_open 298 |
de9ac07b PZ |
61 | #define rmb() asm volatile("lfence" ::: "memory") |
62 | #define cpu_relax() asm volatile("rep; nop" ::: "memory"); | |
63 | #endif | |
64 | ||
65 | #ifdef __i386__ | |
e0202f56 | 66 | #define __NR_perf_counter_open 336 |
de9ac07b PZ |
67 | #define rmb() asm volatile("lfence" ::: "memory") |
68 | #define cpu_relax() asm volatile("rep; nop" ::: "memory"); | |
69 | #endif | |
70 | ||
71 | #ifdef __powerpc__ | |
72 | #define __NR_perf_counter_open 319 | |
73 | #define rmb() asm volatile ("sync" ::: "memory") | |
74 | #define cpu_relax() asm volatile ("" ::: "memory"); | |
75 | #endif | |
76 | ||
77 | #define unlikely(x) __builtin_expect(!!(x), 0) | |
78 | #define min(x, y) ({ \ | |
79 | typeof(x) _min1 = (x); \ | |
80 | typeof(y) _min2 = (y); \ | |
81 | (void) (&_min1 == &_min2); \ | |
82 | _min1 < _min2 ? _min1 : _min2; }) | |
83 | ||
e33e0a43 | 84 | extern asmlinkage int sys_perf_counter_open( |
de9ac07b PZ |
85 | struct perf_counter_hw_event *hw_event_uptr __user, |
86 | pid_t pid, | |
87 | int cpu, | |
88 | int group_fd, | |
e33e0a43 | 89 | unsigned long flags); |
de9ac07b PZ |
90 | |
91 | #define MAX_COUNTERS 64 | |
92 | #define MAX_NR_CPUS 256 | |
93 | ||
94 | #define EID(type, id) (((__u64)(type) << PERF_COUNTER_TYPE_SHIFT) | (id)) | |
95 | ||
96 | static int nr_counters = 0; | |
97 | static __u64 event_id[MAX_COUNTERS] = { }; | |
98 | static int default_interval = 100000; | |
99 | static int event_count[MAX_COUNTERS]; | |
100 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; | |
101 | static int nr_cpus = 0; | |
102 | static unsigned int page_size; | |
103 | static unsigned int mmap_pages = 16; | |
104 | static int output; | |
105 | static char *output_name = "output.perf"; | |
106 | static int group = 0; | |
107 | static unsigned int realtime_prio = 0; | |
108 | ||
109 | const unsigned int default_count[] = { | |
110 | 1000000, | |
111 | 1000000, | |
112 | 10000, | |
113 | 10000, | |
114 | 1000000, | |
115 | 10000, | |
116 | }; | |
117 | ||
de9ac07b PZ |
118 | struct event_symbol { |
119 | __u64 event; | |
120 | char *symbol; | |
121 | }; | |
122 | ||
123 | static struct event_symbol event_symbols[] = { | |
124 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cpu-cycles", }, | |
125 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES), "cycles", }, | |
126 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS), "instructions", }, | |
127 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES), "cache-references", }, | |
128 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES), "cache-misses", }, | |
129 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branch-instructions", }, | |
130 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_INSTRUCTIONS), "branches", }, | |
131 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BRANCH_MISSES), "branch-misses", }, | |
132 | {EID(PERF_TYPE_HARDWARE, PERF_COUNT_BUS_CYCLES), "bus-cycles", }, | |
133 | ||
134 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK), "cpu-clock", }, | |
135 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK), "task-clock", }, | |
136 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "page-faults", }, | |
137 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS), "faults", }, | |
138 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MIN), "minor-faults", }, | |
139 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS_MAJ), "major-faults", }, | |
140 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "context-switches", }, | |
141 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES), "cs", }, | |
142 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "cpu-migrations", }, | |
143 | {EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS), "migrations", }, | |
144 | }; | |
145 | ||
146 | /* | |
147 | * Each event can have multiple symbolic names. | |
148 | * Symbolic names are (almost) exactly matched. | |
149 | */ | |
150 | static __u64 match_event_symbols(char *str) | |
151 | { | |
152 | __u64 config, id; | |
153 | int type; | |
154 | unsigned int i; | |
155 | ||
156 | if (sscanf(str, "r%llx", &config) == 1) | |
157 | return config | PERF_COUNTER_RAW_MASK; | |
158 | ||
159 | if (sscanf(str, "%d:%llu", &type, &id) == 2) | |
160 | return EID(type, id); | |
161 | ||
162 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { | |
163 | if (!strncmp(str, event_symbols[i].symbol, | |
164 | strlen(event_symbols[i].symbol))) | |
165 | return event_symbols[i].event; | |
166 | } | |
167 | ||
168 | return ~0ULL; | |
169 | } | |
170 | ||
171 | static int parse_events(char *str) | |
172 | { | |
173 | __u64 config; | |
174 | ||
175 | again: | |
176 | if (nr_counters == MAX_COUNTERS) | |
177 | return -1; | |
178 | ||
179 | config = match_event_symbols(str); | |
180 | if (config == ~0ULL) | |
181 | return -1; | |
182 | ||
183 | event_id[nr_counters] = config; | |
184 | nr_counters++; | |
185 | ||
186 | str = strstr(str, ","); | |
187 | if (str) { | |
188 | str++; | |
189 | goto again; | |
190 | } | |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
195 | #define __PERF_COUNTER_FIELD(config, name) \ | |
196 | ((config & PERF_COUNTER_##name##_MASK) >> PERF_COUNTER_##name##_SHIFT) | |
197 | ||
198 | #define PERF_COUNTER_RAW(config) __PERF_COUNTER_FIELD(config, RAW) | |
199 | #define PERF_COUNTER_CONFIG(config) __PERF_COUNTER_FIELD(config, CONFIG) | |
200 | #define PERF_COUNTER_TYPE(config) __PERF_COUNTER_FIELD(config, TYPE) | |
201 | #define PERF_COUNTER_ID(config) __PERF_COUNTER_FIELD(config, EVENT) | |
202 | ||
203 | static void display_events_help(void) | |
204 | { | |
205 | unsigned int i; | |
206 | __u64 e; | |
207 | ||
208 | printf( | |
209 | " -e EVENT --event=EVENT # symbolic-name abbreviations"); | |
210 | ||
211 | for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { | |
212 | int type, id; | |
213 | ||
214 | e = event_symbols[i].event; | |
215 | type = PERF_COUNTER_TYPE(e); | |
216 | id = PERF_COUNTER_ID(e); | |
217 | ||
218 | printf("\n %d:%d: %-20s", | |
219 | type, id, event_symbols[i].symbol); | |
220 | } | |
221 | ||
222 | printf("\n" | |
223 | " rNNN: raw PMU events (eventsel+umask)\n\n"); | |
224 | } | |
225 | ||
226 | static void display_help(void) | |
227 | { | |
228 | printf( | |
229 | "Usage: perf-record [<options>]\n" | |
230 | "perf-record Options (up to %d event types can be specified at once):\n\n", | |
231 | MAX_COUNTERS); | |
232 | ||
233 | display_events_help(); | |
234 | ||
235 | printf( | |
236 | " -c CNT --count=CNT # event period to sample\n" | |
237 | " -m pages --mmap_pages=<pages> # number of mmap data pages\n" | |
238 | " -o file --output=<file> # output file\n" | |
239 | " -r prio --realtime=<prio> # use RT prio\n" | |
240 | ); | |
241 | ||
242 | exit(0); | |
243 | } | |
244 | ||
245 | static void process_options(int argc, char *argv[]) | |
246 | { | |
247 | int error = 0, counter; | |
248 | ||
249 | for (;;) { | |
250 | int option_index = 0; | |
251 | /** Options for getopt */ | |
252 | static struct option long_options[] = { | |
253 | {"count", required_argument, NULL, 'c'}, | |
254 | {"event", required_argument, NULL, 'e'}, | |
255 | {"mmap_pages", required_argument, NULL, 'm'}, | |
256 | {"output", required_argument, NULL, 'o'}, | |
257 | {"realtime", required_argument, NULL, 'r'}, | |
258 | {NULL, 0, NULL, 0 } | |
259 | }; | |
260 | int c = getopt_long(argc, argv, "+:c:e:m:o:r:", | |
261 | long_options, &option_index); | |
262 | if (c == -1) | |
263 | break; | |
264 | ||
265 | switch (c) { | |
266 | case 'c': default_interval = atoi(optarg); break; | |
267 | case 'e': error = parse_events(optarg); break; | |
268 | case 'm': mmap_pages = atoi(optarg); break; | |
269 | case 'o': output_name = strdup(optarg); break; | |
270 | case 'r': realtime_prio = atoi(optarg); break; | |
271 | default: error = 1; break; | |
272 | } | |
273 | } | |
274 | if (error) | |
275 | display_help(); | |
276 | ||
277 | if (!nr_counters) { | |
278 | nr_counters = 1; | |
279 | event_id[0] = 0; | |
280 | } | |
281 | ||
282 | for (counter = 0; counter < nr_counters; counter++) { | |
283 | if (event_count[counter]) | |
284 | continue; | |
285 | ||
286 | event_count[counter] = default_interval; | |
287 | } | |
288 | } | |
289 | ||
290 | struct mmap_data { | |
291 | int counter; | |
292 | void *base; | |
293 | unsigned int mask; | |
294 | unsigned int prev; | |
295 | }; | |
296 | ||
297 | static unsigned int mmap_read_head(struct mmap_data *md) | |
298 | { | |
299 | struct perf_counter_mmap_page *pc = md->base; | |
300 | int head; | |
301 | ||
302 | head = pc->data_head; | |
303 | rmb(); | |
304 | ||
305 | return head; | |
306 | } | |
307 | ||
308 | static long events; | |
309 | static struct timeval last_read, this_read; | |
310 | ||
311 | static void mmap_read(struct mmap_data *md) | |
312 | { | |
313 | unsigned int head = mmap_read_head(md); | |
314 | unsigned int old = md->prev; | |
315 | unsigned char *data = md->base + page_size; | |
316 | unsigned long size; | |
317 | void *buf; | |
318 | int diff; | |
319 | ||
320 | gettimeofday(&this_read, NULL); | |
321 | ||
322 | /* | |
323 | * If we're further behind than half the buffer, there's a chance | |
324 | * the writer will bite our tail and screw up the events under us. | |
325 | * | |
326 | * If we somehow ended up ahead of the head, we got messed up. | |
327 | * | |
328 | * In either case, truncate and restart at head. | |
329 | */ | |
330 | diff = head - old; | |
331 | if (diff > md->mask / 2 || diff < 0) { | |
332 | struct timeval iv; | |
333 | unsigned long msecs; | |
334 | ||
335 | timersub(&this_read, &last_read, &iv); | |
336 | msecs = iv.tv_sec*1000 + iv.tv_usec/1000; | |
337 | ||
338 | fprintf(stderr, "WARNING: failed to keep up with mmap data." | |
339 | " Last read %lu msecs ago.\n", msecs); | |
340 | ||
341 | /* | |
342 | * head points to a known good entry, start there. | |
343 | */ | |
344 | old = head; | |
345 | } | |
346 | ||
347 | last_read = this_read; | |
348 | ||
349 | if (old != head) | |
350 | events++; | |
351 | ||
352 | size = head - old; | |
353 | ||
354 | if ((old & md->mask) + size != (head & md->mask)) { | |
355 | buf = &data[old & md->mask]; | |
356 | size = md->mask + 1 - (old & md->mask); | |
357 | old += size; | |
358 | while (size) { | |
359 | int ret = write(output, buf, size); | |
360 | if (ret < 0) { | |
361 | perror("failed to write"); | |
362 | exit(-1); | |
363 | } | |
364 | size -= ret; | |
365 | buf += ret; | |
366 | } | |
367 | } | |
368 | ||
369 | buf = &data[old & md->mask]; | |
370 | size = head - old; | |
371 | old += size; | |
372 | while (size) { | |
373 | int ret = write(output, buf, size); | |
374 | if (ret < 0) { | |
375 | perror("failed to write"); | |
376 | exit(-1); | |
377 | } | |
378 | size -= ret; | |
379 | buf += ret; | |
380 | } | |
381 | ||
382 | md->prev = old; | |
383 | } | |
384 | ||
385 | static volatile int done = 0; | |
386 | ||
387 | static void sigchld_handler(int sig) | |
388 | { | |
389 | if (sig == SIGCHLD) | |
390 | done = 1; | |
391 | } | |
392 | ||
e33e0a43 | 393 | int cmd_record(int argc, const char **argv) |
de9ac07b PZ |
394 | { |
395 | struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; | |
396 | struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; | |
397 | struct perf_counter_hw_event hw_event; | |
398 | int i, counter, group_fd, nr_poll = 0; | |
399 | pid_t pid; | |
400 | int ret; | |
401 | ||
402 | page_size = sysconf(_SC_PAGE_SIZE); | |
403 | ||
404 | process_options(argc, argv); | |
405 | ||
406 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); | |
407 | assert(nr_cpus <= MAX_NR_CPUS); | |
408 | assert(nr_cpus >= 0); | |
409 | ||
410 | output = open(output_name, O_CREAT|O_RDWR, S_IRWXU); | |
411 | if (output < 0) { | |
412 | perror("failed to create output file"); | |
413 | exit(-1); | |
414 | } | |
415 | ||
416 | argc -= optind; | |
417 | argv += optind; | |
418 | ||
419 | for (i = 0; i < nr_cpus; i++) { | |
420 | group_fd = -1; | |
421 | for (counter = 0; counter < nr_counters; counter++) { | |
422 | ||
423 | memset(&hw_event, 0, sizeof(hw_event)); | |
424 | hw_event.config = event_id[counter]; | |
425 | hw_event.irq_period = event_count[counter]; | |
426 | hw_event.record_type = PERF_RECORD_IP | PERF_RECORD_TID; | |
427 | hw_event.nmi = 1; | |
428 | hw_event.mmap = 1; | |
429 | hw_event.comm = 1; | |
430 | ||
431 | fd[i][counter] = sys_perf_counter_open(&hw_event, -1, i, group_fd, 0); | |
432 | if (fd[i][counter] < 0) { | |
433 | int err = errno; | |
434 | printf("kerneltop error: syscall returned with %d (%s)\n", | |
435 | fd[i][counter], strerror(err)); | |
436 | if (err == EPERM) | |
437 | printf("Are you root?\n"); | |
438 | exit(-1); | |
439 | } | |
440 | assert(fd[i][counter] >= 0); | |
441 | fcntl(fd[i][counter], F_SETFL, O_NONBLOCK); | |
442 | ||
443 | /* | |
444 | * First counter acts as the group leader: | |
445 | */ | |
446 | if (group && group_fd == -1) | |
447 | group_fd = fd[i][counter]; | |
448 | ||
449 | event_array[nr_poll].fd = fd[i][counter]; | |
450 | event_array[nr_poll].events = POLLIN; | |
451 | nr_poll++; | |
452 | ||
453 | mmap_array[i][counter].counter = counter; | |
454 | mmap_array[i][counter].prev = 0; | |
455 | mmap_array[i][counter].mask = mmap_pages*page_size - 1; | |
456 | mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size, | |
457 | PROT_READ, MAP_SHARED, fd[i][counter], 0); | |
458 | if (mmap_array[i][counter].base == MAP_FAILED) { | |
459 | printf("kerneltop error: failed to mmap with %d (%s)\n", | |
460 | errno, strerror(errno)); | |
461 | exit(-1); | |
462 | } | |
463 | } | |
464 | } | |
465 | ||
466 | signal(SIGCHLD, sigchld_handler); | |
467 | ||
468 | pid = fork(); | |
469 | if (pid < 0) | |
470 | perror("failed to fork"); | |
471 | ||
472 | if (!pid) { | |
473 | if (execvp(argv[0], argv)) { | |
474 | perror(argv[0]); | |
475 | exit(-1); | |
476 | } | |
477 | } | |
478 | ||
479 | if (realtime_prio) { | |
480 | struct sched_param param; | |
481 | ||
482 | param.sched_priority = realtime_prio; | |
483 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { | |
484 | printf("Could not set realtime priority.\n"); | |
485 | exit(-1); | |
486 | } | |
487 | } | |
488 | ||
489 | /* | |
490 | * TODO: store the current /proc/$/maps information somewhere | |
491 | */ | |
492 | ||
493 | while (!done) { | |
494 | int hits = events; | |
495 | ||
496 | for (i = 0; i < nr_cpus; i++) { | |
497 | for (counter = 0; counter < nr_counters; counter++) | |
498 | mmap_read(&mmap_array[i][counter]); | |
499 | } | |
500 | ||
501 | if (hits == events) | |
502 | ret = poll(event_array, nr_poll, 100); | |
503 | } | |
504 | ||
505 | return 0; | |
506 | } |