Commit | Line | Data |
---|---|---|
abaff32a | 1 | /* |
bf9e1876 IM |
2 | * builtin-record.c |
3 | * | |
4 | * Builtin record command: Record the profile of a workload | |
5 | * (or a CPU, or a PID) into the perf.data output file - for | |
6 | * later analysis via perf report. | |
abaff32a | 7 | */ |
16f762a2 | 8 | #include "builtin.h" |
bf9e1876 IM |
9 | |
10 | #include "perf.h" | |
11 | ||
6eda5838 | 12 | #include "util/util.h" |
0e9b20b8 | 13 | #include "util/parse-options.h" |
8ad8db37 | 14 | #include "util/parse-events.h" |
a0055ae2 | 15 | #include "util/string.h" |
6eda5838 | 16 | |
97124d5e | 17 | #include <unistd.h> |
de9ac07b | 18 | #include <sched.h> |
de9ac07b | 19 | |
0e9b20b8 IM |
20 | #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) |
21 | #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) | |
1a853e36 | 22 | |
e61078a0 PZ |
23 | static long default_interval = 100000; |
24 | static long event_count[MAX_COUNTERS]; | |
8ad8db37 | 25 | |
de9ac07b | 26 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
3cf165fc | 27 | static int nr_cpus = 0; |
de9ac07b | 28 | static unsigned int page_size; |
3cf165fc | 29 | static unsigned int mmap_pages = 128; |
cf1f4574 | 30 | static int freq = 0; |
de9ac07b | 31 | static int output; |
23ac9cbe | 32 | static const char *output_name = "perf.data"; |
de9ac07b | 33 | static int group = 0; |
16c8a109 PZ |
34 | static unsigned int realtime_prio = 0; |
35 | static int system_wide = 0; | |
1a853e36 | 36 | static pid_t target_pid = -1; |
16c8a109 | 37 | static int inherit = 1; |
97124d5e | 38 | static int force = 0; |
abaff32a | 39 | static int append_file = 0; |
de9ac07b PZ |
40 | |
41 | const unsigned int default_count[] = { | |
42 | 1000000, | |
43 | 1000000, | |
44 | 10000, | |
45 | 10000, | |
46 | 1000000, | |
47 | 10000, | |
48 | }; | |
49 | ||
de9ac07b PZ |
50 | struct mmap_data { |
51 | int counter; | |
52 | void *base; | |
53 | unsigned int mask; | |
54 | unsigned int prev; | |
55 | }; | |
56 | ||
57 | static unsigned int mmap_read_head(struct mmap_data *md) | |
58 | { | |
59 | struct perf_counter_mmap_page *pc = md->base; | |
60 | int head; | |
61 | ||
62 | head = pc->data_head; | |
63 | rmb(); | |
64 | ||
65 | return head; | |
66 | } | |
67 | ||
68 | static long events; | |
69 | static struct timeval last_read, this_read; | |
70 | ||
021e9f47 IM |
71 | static __u64 bytes_written; |
72 | ||
de9ac07b PZ |
73 | static void mmap_read(struct mmap_data *md) |
74 | { | |
75 | unsigned int head = mmap_read_head(md); | |
76 | unsigned int old = md->prev; | |
77 | unsigned char *data = md->base + page_size; | |
78 | unsigned long size; | |
79 | void *buf; | |
80 | int diff; | |
81 | ||
82 | gettimeofday(&this_read, NULL); | |
83 | ||
84 | /* | |
85 | * If we're further behind than half the buffer, there's a chance | |
86 | * the writer will bite our tail and screw up the events under us. | |
87 | * | |
88 | * If we somehow ended up ahead of the head, we got messed up. | |
89 | * | |
90 | * In either case, truncate and restart at head. | |
91 | */ | |
92 | diff = head - old; | |
93 | if (diff > md->mask / 2 || diff < 0) { | |
94 | struct timeval iv; | |
95 | unsigned long msecs; | |
96 | ||
97 | timersub(&this_read, &last_read, &iv); | |
98 | msecs = iv.tv_sec*1000 + iv.tv_usec/1000; | |
99 | ||
100 | fprintf(stderr, "WARNING: failed to keep up with mmap data." | |
101 | " Last read %lu msecs ago.\n", msecs); | |
102 | ||
103 | /* | |
104 | * head points to a known good entry, start there. | |
105 | */ | |
106 | old = head; | |
107 | } | |
108 | ||
109 | last_read = this_read; | |
110 | ||
111 | if (old != head) | |
112 | events++; | |
113 | ||
114 | size = head - old; | |
115 | ||
116 | if ((old & md->mask) + size != (head & md->mask)) { | |
117 | buf = &data[old & md->mask]; | |
118 | size = md->mask + 1 - (old & md->mask); | |
119 | old += size; | |
021e9f47 | 120 | |
de9ac07b PZ |
121 | while (size) { |
122 | int ret = write(output, buf, size); | |
021e9f47 IM |
123 | |
124 | if (ret < 0) | |
125 | die("failed to write"); | |
126 | ||
de9ac07b PZ |
127 | size -= ret; |
128 | buf += ret; | |
021e9f47 IM |
129 | |
130 | bytes_written += ret; | |
de9ac07b PZ |
131 | } |
132 | } | |
133 | ||
134 | buf = &data[old & md->mask]; | |
135 | size = head - old; | |
136 | old += size; | |
021e9f47 | 137 | |
de9ac07b PZ |
138 | while (size) { |
139 | int ret = write(output, buf, size); | |
021e9f47 IM |
140 | |
141 | if (ret < 0) | |
142 | die("failed to write"); | |
143 | ||
de9ac07b PZ |
144 | size -= ret; |
145 | buf += ret; | |
021e9f47 IM |
146 | |
147 | bytes_written += ret; | |
de9ac07b PZ |
148 | } |
149 | ||
150 | md->prev = old; | |
151 | } | |
152 | ||
153 | static volatile int done = 0; | |
154 | ||
16c8a109 | 155 | static void sig_handler(int sig) |
de9ac07b | 156 | { |
16c8a109 | 157 | done = 1; |
de9ac07b PZ |
158 | } |
159 | ||
16c8a109 PZ |
160 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; |
161 | static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; | |
162 | ||
163 | static int nr_poll; | |
164 | static int nr_cpu; | |
165 | ||
1a853e36 | 166 | struct mmap_event { |
16f762a2 IM |
167 | struct perf_event_header header; |
168 | __u32 pid; | |
169 | __u32 tid; | |
170 | __u64 start; | |
171 | __u64 len; | |
172 | __u64 pgoff; | |
173 | char filename[PATH_MAX]; | |
1a853e36 | 174 | }; |
16f762a2 | 175 | |
1a853e36 | 176 | struct comm_event { |
16f762a2 IM |
177 | struct perf_event_header header; |
178 | __u32 pid; | |
179 | __u32 tid; | |
180 | char comm[16]; | |
1a853e36 ACM |
181 | }; |
182 | ||
f70e87d7 | 183 | static void pid_synthesize_comm_event(pid_t pid, int full) |
1a853e36 | 184 | { |
16f762a2 | 185 | struct comm_event comm_ev; |
1a853e36 ACM |
186 | char filename[PATH_MAX]; |
187 | char bf[BUFSIZ]; | |
a0055ae2 | 188 | int fd, ret; |
1a853e36 | 189 | size_t size; |
a0055ae2 | 190 | char *field, *sep; |
f70e87d7 PZ |
191 | DIR *tasks; |
192 | struct dirent dirent, *next; | |
1a853e36 ACM |
193 | |
194 | snprintf(filename, sizeof(filename), "/proc/%d/stat", pid); | |
195 | ||
196 | fd = open(filename, O_RDONLY); | |
197 | if (fd < 0) { | |
198 | fprintf(stderr, "couldn't open %s\n", filename); | |
199 | exit(EXIT_FAILURE); | |
200 | } | |
201 | if (read(fd, bf, sizeof(bf)) < 0) { | |
202 | fprintf(stderr, "couldn't read %s\n", filename); | |
203 | exit(EXIT_FAILURE); | |
204 | } | |
205 | close(fd); | |
206 | ||
a0055ae2 | 207 | /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */ |
1a853e36 | 208 | memset(&comm_ev, 0, sizeof(comm_ev)); |
a0055ae2 ACM |
209 | field = strchr(bf, '('); |
210 | if (field == NULL) | |
211 | goto out_failure; | |
212 | sep = strchr(++field, ')'); | |
213 | if (sep == NULL) | |
214 | goto out_failure; | |
215 | size = sep - field; | |
216 | memcpy(comm_ev.comm, field, size++); | |
f70e87d7 PZ |
217 | |
218 | comm_ev.pid = pid; | |
1a853e36 | 219 | comm_ev.header.type = PERF_EVENT_COMM; |
1a853e36 ACM |
220 | size = ALIGN(size, sizeof(uint64_t)); |
221 | comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size); | |
16f762a2 | 222 | |
f70e87d7 PZ |
223 | if (!full) { |
224 | comm_ev.tid = pid; | |
225 | ||
226 | ret = write(output, &comm_ev, comm_ev.header.size); | |
227 | if (ret < 0) { | |
228 | perror("failed to write"); | |
229 | exit(-1); | |
230 | } | |
231 | return; | |
232 | } | |
233 | ||
234 | snprintf(filename, sizeof(filename), "/proc/%d/task", pid); | |
235 | ||
236 | tasks = opendir(filename); | |
237 | while (!readdir_r(tasks, &dirent, &next) && next) { | |
238 | char *end; | |
239 | pid = strtol(dirent.d_name, &end, 10); | |
240 | if (*end) | |
241 | continue; | |
242 | ||
243 | comm_ev.tid = pid; | |
244 | ||
245 | ret = write(output, &comm_ev, comm_ev.header.size); | |
246 | if (ret < 0) { | |
247 | perror("failed to write"); | |
248 | exit(-1); | |
249 | } | |
1a853e36 | 250 | } |
f70e87d7 PZ |
251 | closedir(tasks); |
252 | return; | |
253 | ||
a0055ae2 ACM |
254 | out_failure: |
255 | fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n", | |
256 | filename); | |
257 | exit(EXIT_FAILURE); | |
1a853e36 ACM |
258 | } |
259 | ||
f70e87d7 | 260 | static void pid_synthesize_mmap_events(pid_t pid) |
1a853e36 ACM |
261 | { |
262 | char filename[PATH_MAX]; | |
263 | FILE *fp; | |
264 | ||
265 | snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); | |
266 | ||
267 | fp = fopen(filename, "r"); | |
268 | if (fp == NULL) { | |
269 | fprintf(stderr, "couldn't open %s\n", filename); | |
270 | exit(EXIT_FAILURE); | |
271 | } | |
272 | while (1) { | |
a0055ae2 | 273 | char bf[BUFSIZ], *pbf = bf; |
1a853e36 ACM |
274 | struct mmap_event mmap_ev = { |
275 | .header.type = PERF_EVENT_MMAP, | |
276 | }; | |
a0055ae2 | 277 | int n; |
1a853e36 ACM |
278 | size_t size; |
279 | if (fgets(bf, sizeof(bf), fp) == NULL) | |
280 | break; | |
281 | ||
282 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ | |
a0055ae2 ACM |
283 | n = hex2u64(pbf, &mmap_ev.start); |
284 | if (n < 0) | |
285 | continue; | |
286 | pbf += n + 1; | |
287 | n = hex2u64(pbf, &mmap_ev.len); | |
288 | if (n < 0) | |
289 | continue; | |
290 | pbf += n + 3; | |
291 | if (*pbf == 'x') { /* vm_exec */ | |
1a853e36 ACM |
292 | char *execname = strrchr(bf, ' '); |
293 | ||
294 | if (execname == NULL || execname[1] != '/') | |
295 | continue; | |
296 | ||
297 | execname += 1; | |
298 | size = strlen(execname); | |
299 | execname[size - 1] = '\0'; /* Remove \n */ | |
300 | memcpy(mmap_ev.filename, execname, size); | |
301 | size = ALIGN(size, sizeof(uint64_t)); | |
302 | mmap_ev.len -= mmap_ev.start; | |
303 | mmap_ev.header.size = (sizeof(mmap_ev) - | |
304 | (sizeof(mmap_ev.filename) - size)); | |
f70e87d7 | 305 | mmap_ev.pid = pid; |
1a853e36 ACM |
306 | mmap_ev.tid = pid; |
307 | ||
308 | if (write(output, &mmap_ev, mmap_ev.header.size) < 0) { | |
309 | perror("failed to write"); | |
310 | exit(-1); | |
311 | } | |
312 | } | |
313 | } | |
314 | ||
315 | fclose(fp); | |
316 | } | |
317 | ||
f70e87d7 PZ |
318 | static void synthesize_events(void) |
319 | { | |
320 | DIR *proc; | |
321 | struct dirent dirent, *next; | |
322 | ||
323 | proc = opendir("/proc"); | |
324 | ||
325 | while (!readdir_r(proc, &dirent, &next) && next) { | |
326 | char *end; | |
327 | pid_t pid; | |
328 | ||
329 | pid = strtol(dirent.d_name, &end, 10); | |
330 | if (*end) /* only interested in proper numerical dirents */ | |
331 | continue; | |
332 | ||
333 | pid_synthesize_comm_event(pid, 1); | |
334 | pid_synthesize_mmap_events(pid); | |
335 | } | |
336 | ||
337 | closedir(proc); | |
338 | } | |
339 | ||
f250c030 IM |
340 | static int group_fd; |
341 | ||
342 | static void create_counter(int counter, int cpu, pid_t pid) | |
de9ac07b | 343 | { |
c70975bc | 344 | struct perf_counter_attr attr; |
16c8a109 | 345 | int track = 1; |
16c8a109 | 346 | |
f250c030 IM |
347 | memset(&attr, 0, sizeof(attr)); |
348 | attr.config = event_id[counter]; | |
349 | attr.sample_period = event_count[counter]; | |
350 | attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; | |
cf1f4574 | 351 | attr.freq = freq; |
f250c030 IM |
352 | attr.mmap = track; |
353 | attr.comm = track; | |
cf1f4574 | 354 | attr.inherit = (cpu < 0) && inherit; |
16c8a109 | 355 | |
f250c030 | 356 | track = 0; /* only the first counter needs these */ |
16c8a109 | 357 | |
f250c030 | 358 | fd[nr_cpu][counter] = sys_perf_counter_open(&attr, pid, cpu, group_fd, 0); |
16c8a109 | 359 | |
f250c030 IM |
360 | if (fd[nr_cpu][counter] < 0) { |
361 | int err = errno; | |
16c8a109 | 362 | |
f250c030 IM |
363 | error("syscall returned with %d (%s)\n", |
364 | fd[nr_cpu][counter], strerror(err)); | |
365 | if (err == EPERM) | |
366 | printf("Are you root?\n"); | |
367 | exit(-1); | |
368 | } | |
369 | assert(fd[nr_cpu][counter] >= 0); | |
370 | fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); | |
16c8a109 | 371 | |
f250c030 IM |
372 | /* |
373 | * First counter acts as the group leader: | |
374 | */ | |
375 | if (group && group_fd == -1) | |
376 | group_fd = fd[nr_cpu][counter]; | |
377 | ||
378 | event_array[nr_poll].fd = fd[nr_cpu][counter]; | |
379 | event_array[nr_poll].events = POLLIN; | |
380 | nr_poll++; | |
381 | ||
382 | mmap_array[nr_cpu][counter].counter = counter; | |
383 | mmap_array[nr_cpu][counter].prev = 0; | |
384 | mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1; | |
385 | mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size, | |
386 | PROT_READ, MAP_SHARED, fd[nr_cpu][counter], 0); | |
387 | if (mmap_array[nr_cpu][counter].base == MAP_FAILED) { | |
388 | error("failed to mmap with %d (%s)\n", errno, strerror(errno)); | |
389 | exit(-1); | |
390 | } | |
391 | } | |
f2521b6e | 392 | |
f250c030 IM |
393 | static void open_counters(int cpu, pid_t pid) |
394 | { | |
395 | int counter; | |
16c8a109 | 396 | |
f250c030 IM |
397 | if (pid > 0) { |
398 | pid_synthesize_comm_event(pid, 0); | |
399 | pid_synthesize_mmap_events(pid); | |
16c8a109 | 400 | } |
f250c030 IM |
401 | |
402 | group_fd = -1; | |
403 | for (counter = 0; counter < nr_counters; counter++) | |
404 | create_counter(counter, cpu, pid); | |
405 | ||
16c8a109 PZ |
406 | nr_cpu++; |
407 | } | |
408 | ||
0e9b20b8 | 409 | static int __cmd_record(int argc, const char **argv) |
16c8a109 PZ |
410 | { |
411 | int i, counter; | |
abaff32a | 412 | struct stat st; |
de9ac07b | 413 | pid_t pid; |
abaff32a | 414 | int flags; |
de9ac07b PZ |
415 | int ret; |
416 | ||
417 | page_size = sysconf(_SC_PAGE_SIZE); | |
de9ac07b PZ |
418 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
419 | assert(nr_cpus <= MAX_NR_CPUS); | |
420 | assert(nr_cpus >= 0); | |
421 | ||
abaff32a IM |
422 | if (!stat(output_name, &st) && !force && !append_file) { |
423 | fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n", | |
97124d5e PZ |
424 | output_name); |
425 | exit(-1); | |
426 | } | |
427 | ||
abaff32a IM |
428 | flags = O_CREAT|O_RDWR; |
429 | if (append_file) | |
430 | flags |= O_APPEND; | |
431 | else | |
432 | flags |= O_TRUNC; | |
433 | ||
434 | output = open(output_name, flags, S_IRUSR|S_IWUSR); | |
de9ac07b PZ |
435 | if (output < 0) { |
436 | perror("failed to create output file"); | |
437 | exit(-1); | |
438 | } | |
439 | ||
1a853e36 | 440 | if (!system_wide) { |
df97992c | 441 | open_counters(-1, target_pid != -1 ? target_pid : getpid()); |
1a853e36 ACM |
442 | } else for (i = 0; i < nr_cpus; i++) |
443 | open_counters(i, target_pid); | |
de9ac07b | 444 | |
16c8a109 PZ |
445 | signal(SIGCHLD, sig_handler); |
446 | signal(SIGINT, sig_handler); | |
de9ac07b | 447 | |
ef65b2a0 | 448 | if (target_pid == -1 && argc) { |
1a853e36 ACM |
449 | pid = fork(); |
450 | if (pid < 0) | |
451 | perror("failed to fork"); | |
de9ac07b | 452 | |
1a853e36 | 453 | if (!pid) { |
0e9b20b8 | 454 | if (execvp(argv[0], (char **)argv)) { |
1a853e36 ACM |
455 | perror(argv[0]); |
456 | exit(-1); | |
457 | } | |
de9ac07b PZ |
458 | } |
459 | } | |
460 | ||
461 | if (realtime_prio) { | |
462 | struct sched_param param; | |
463 | ||
464 | param.sched_priority = realtime_prio; | |
465 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { | |
466 | printf("Could not set realtime priority.\n"); | |
467 | exit(-1); | |
468 | } | |
469 | } | |
470 | ||
f70e87d7 PZ |
471 | if (system_wide) |
472 | synthesize_events(); | |
de9ac07b PZ |
473 | |
474 | while (!done) { | |
475 | int hits = events; | |
476 | ||
16c8a109 | 477 | for (i = 0; i < nr_cpu; i++) { |
de9ac07b PZ |
478 | for (counter = 0; counter < nr_counters; counter++) |
479 | mmap_read(&mmap_array[i][counter]); | |
480 | } | |
481 | ||
482 | if (hits == events) | |
483 | ret = poll(event_array, nr_poll, 100); | |
484 | } | |
485 | ||
021e9f47 IM |
486 | /* |
487 | * Approximate RIP event size: 24 bytes. | |
488 | */ | |
489 | fprintf(stderr, | |
490 | "[ perf record: Captured and wrote %.3f MB %s (~%lld events) ]\n", | |
491 | (double)bytes_written / 1024.0 / 1024.0, | |
492 | output_name, | |
493 | bytes_written / 24); | |
addc2785 | 494 | |
de9ac07b PZ |
495 | return 0; |
496 | } | |
0e9b20b8 | 497 | |
0e9b20b8 | 498 | static const char * const record_usage[] = { |
9e096753 MG |
499 | "perf record [<options>] [<command>]", |
500 | "perf record [<options>] -- <command> [<options>]", | |
0e9b20b8 IM |
501 | NULL |
502 | }; | |
503 | ||
8ad8db37 IM |
504 | static char events_help_msg[EVENTS_HELP_MAX]; |
505 | ||
5242519b | 506 | static const struct option options[] = { |
0e9b20b8 | 507 | OPT_CALLBACK('e', "event", NULL, "event", |
8ad8db37 | 508 | events_help_msg, parse_events), |
0e9b20b8 IM |
509 | OPT_INTEGER('p', "pid", &target_pid, |
510 | "record events on existing pid"), | |
511 | OPT_INTEGER('r', "realtime", &realtime_prio, | |
512 | "collect data with this RT SCHED_FIFO priority"), | |
513 | OPT_BOOLEAN('a', "all-cpus", &system_wide, | |
514 | "system-wide collection from all CPUs"), | |
abaff32a IM |
515 | OPT_BOOLEAN('A', "append", &append_file, |
516 | "append to the output file to do incremental profiling"), | |
97124d5e PZ |
517 | OPT_BOOLEAN('f', "force", &force, |
518 | "overwrite existing data file"), | |
e61078a0 | 519 | OPT_LONG('c', "count", &default_interval, |
abaff32a IM |
520 | "event period to sample"), |
521 | OPT_STRING('o', "output", &output_name, "file", | |
522 | "output file name"), | |
523 | OPT_BOOLEAN('i', "inherit", &inherit, | |
524 | "child tasks inherit counters"), | |
cf1f4574 IM |
525 | OPT_INTEGER('F', "freq", &freq, |
526 | "profile at this frequency"), | |
abaff32a IM |
527 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
528 | "number of mmap data pages"), | |
0e9b20b8 IM |
529 | OPT_END() |
530 | }; | |
531 | ||
532 | int cmd_record(int argc, const char **argv, const char *prefix) | |
533 | { | |
534 | int counter; | |
535 | ||
8ad8db37 | 536 | create_events_help(events_help_msg); |
0e9b20b8 IM |
537 | |
538 | argc = parse_options(argc, argv, options, record_usage, 0); | |
ef65b2a0 | 539 | if (!argc && target_pid == -1 && !system_wide) |
0e9b20b8 IM |
540 | usage_with_options(record_usage, options); |
541 | ||
542 | if (!nr_counters) { | |
543 | nr_counters = 1; | |
544 | event_id[0] = 0; | |
545 | } | |
546 | ||
cf1f4574 IM |
547 | if (freq) { |
548 | default_interval = freq; | |
549 | freq = 1; | |
550 | } | |
0e9b20b8 IM |
551 | for (counter = 0; counter < nr_counters; counter++) { |
552 | if (event_count[counter]) | |
553 | continue; | |
554 | ||
555 | event_count[counter] = default_interval; | |
556 | } | |
557 | ||
558 | return __cmd_record(argc, argv); | |
559 | } |