]>
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 | |
7c6a1c65 PZ |
17 | #include "util/header.h" |
18 | ||
97124d5e | 19 | #include <unistd.h> |
de9ac07b | 20 | #include <sched.h> |
de9ac07b | 21 | |
0e9b20b8 IM |
22 | #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) |
23 | #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) | |
1a853e36 | 24 | |
de9ac07b | 25 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
a21ca2ca IM |
26 | |
27 | static long default_interval = 100000; | |
28 | ||
3cf165fc | 29 | static int nr_cpus = 0; |
de9ac07b | 30 | static unsigned int page_size; |
3cf165fc | 31 | static unsigned int mmap_pages = 128; |
cf1f4574 | 32 | static int freq = 0; |
de9ac07b | 33 | static int output; |
23ac9cbe | 34 | static const char *output_name = "perf.data"; |
de9ac07b | 35 | static int group = 0; |
16c8a109 PZ |
36 | static unsigned int realtime_prio = 0; |
37 | static int system_wide = 0; | |
1a853e36 | 38 | static pid_t target_pid = -1; |
16c8a109 | 39 | static int inherit = 1; |
97124d5e | 40 | static int force = 0; |
abaff32a | 41 | static int append_file = 0; |
3efa1cc9 | 42 | static int call_graph = 0; |
3da297a6 | 43 | static int verbose = 0; |
649c48a9 PZ |
44 | static int inherit_stat = 0; |
45 | static int no_samples = 0; | |
de9ac07b | 46 | |
a21ca2ca IM |
47 | static long samples; |
48 | static struct timeval last_read; | |
49 | static struct timeval this_read; | |
50 | ||
9cffa8d5 | 51 | static u64 bytes_written; |
a21ca2ca IM |
52 | |
53 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; | |
54 | ||
55 | static int nr_poll; | |
56 | static int nr_cpu; | |
57 | ||
f5970550 | 58 | static int file_new = 1; |
7c6a1c65 PZ |
59 | |
60 | struct perf_header *header; | |
f5970550 | 61 | |
a21ca2ca IM |
62 | struct mmap_event { |
63 | struct perf_event_header header; | |
9cffa8d5 PM |
64 | u32 pid; |
65 | u32 tid; | |
66 | u64 start; | |
67 | u64 len; | |
68 | u64 pgoff; | |
a21ca2ca IM |
69 | char filename[PATH_MAX]; |
70 | }; | |
71 | ||
72 | struct comm_event { | |
73 | struct perf_event_header header; | |
9cffa8d5 PM |
74 | u32 pid; |
75 | u32 tid; | |
a21ca2ca | 76 | char comm[16]; |
de9ac07b PZ |
77 | }; |
78 | ||
a21ca2ca | 79 | |
de9ac07b | 80 | struct mmap_data { |
a21ca2ca IM |
81 | int counter; |
82 | void *base; | |
83 | unsigned int mask; | |
84 | unsigned int prev; | |
de9ac07b PZ |
85 | }; |
86 | ||
a21ca2ca IM |
87 | static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; |
88 | ||
9d91a6f7 | 89 | static unsigned long mmap_read_head(struct mmap_data *md) |
de9ac07b PZ |
90 | { |
91 | struct perf_counter_mmap_page *pc = md->base; | |
9d91a6f7 | 92 | long head; |
de9ac07b PZ |
93 | |
94 | head = pc->data_head; | |
95 | rmb(); | |
96 | ||
97 | return head; | |
98 | } | |
99 | ||
9d91a6f7 PZ |
100 | static void mmap_write_tail(struct mmap_data *md, unsigned long tail) |
101 | { | |
102 | struct perf_counter_mmap_page *pc = md->base; | |
103 | ||
104 | /* | |
105 | * ensure all reads are done before we write the tail out. | |
106 | */ | |
107 | /* mb(); */ | |
108 | pc->data_tail = tail; | |
109 | } | |
110 | ||
f5970550 PZ |
111 | static void write_output(void *buf, size_t size) |
112 | { | |
113 | while (size) { | |
114 | int ret = write(output, buf, size); | |
115 | ||
116 | if (ret < 0) | |
117 | die("failed to write"); | |
118 | ||
119 | size -= ret; | |
120 | buf += ret; | |
121 | ||
122 | bytes_written += ret; | |
123 | } | |
124 | } | |
125 | ||
de9ac07b PZ |
126 | static void mmap_read(struct mmap_data *md) |
127 | { | |
128 | unsigned int head = mmap_read_head(md); | |
129 | unsigned int old = md->prev; | |
130 | unsigned char *data = md->base + page_size; | |
131 | unsigned long size; | |
132 | void *buf; | |
133 | int diff; | |
134 | ||
135 | gettimeofday(&this_read, NULL); | |
136 | ||
137 | /* | |
138 | * If we're further behind than half the buffer, there's a chance | |
2debbc83 | 139 | * the writer will bite our tail and mess up the samples under us. |
de9ac07b PZ |
140 | * |
141 | * If we somehow ended up ahead of the head, we got messed up. | |
142 | * | |
143 | * In either case, truncate and restart at head. | |
144 | */ | |
145 | diff = head - old; | |
9d91a6f7 | 146 | if (diff < 0) { |
de9ac07b PZ |
147 | struct timeval iv; |
148 | unsigned long msecs; | |
149 | ||
150 | timersub(&this_read, &last_read, &iv); | |
151 | msecs = iv.tv_sec*1000 + iv.tv_usec/1000; | |
152 | ||
153 | fprintf(stderr, "WARNING: failed to keep up with mmap data." | |
154 | " Last read %lu msecs ago.\n", msecs); | |
155 | ||
156 | /* | |
157 | * head points to a known good entry, start there. | |
158 | */ | |
159 | old = head; | |
160 | } | |
161 | ||
162 | last_read = this_read; | |
163 | ||
164 | if (old != head) | |
2debbc83 | 165 | samples++; |
de9ac07b PZ |
166 | |
167 | size = head - old; | |
168 | ||
169 | if ((old & md->mask) + size != (head & md->mask)) { | |
170 | buf = &data[old & md->mask]; | |
171 | size = md->mask + 1 - (old & md->mask); | |
172 | old += size; | |
021e9f47 | 173 | |
f5970550 | 174 | write_output(buf, size); |
de9ac07b PZ |
175 | } |
176 | ||
177 | buf = &data[old & md->mask]; | |
178 | size = head - old; | |
179 | old += size; | |
021e9f47 | 180 | |
f5970550 | 181 | write_output(buf, size); |
de9ac07b PZ |
182 | |
183 | md->prev = old; | |
9d91a6f7 | 184 | mmap_write_tail(md, old); |
de9ac07b PZ |
185 | } |
186 | ||
187 | static volatile int done = 0; | |
f7b7c26e | 188 | static volatile int signr = -1; |
de9ac07b | 189 | |
16c8a109 | 190 | static void sig_handler(int sig) |
de9ac07b | 191 | { |
16c8a109 | 192 | done = 1; |
f7b7c26e PZ |
193 | signr = sig; |
194 | } | |
195 | ||
196 | static void sig_atexit(void) | |
197 | { | |
198 | if (signr == -1) | |
199 | return; | |
200 | ||
201 | signal(signr, SIG_DFL); | |
202 | kill(getpid(), signr); | |
de9ac07b PZ |
203 | } |
204 | ||
f70e87d7 | 205 | static void pid_synthesize_comm_event(pid_t pid, int full) |
1a853e36 | 206 | { |
16f762a2 | 207 | struct comm_event comm_ev; |
1a853e36 ACM |
208 | char filename[PATH_MAX]; |
209 | char bf[BUFSIZ]; | |
f5970550 | 210 | int fd; |
1a853e36 | 211 | size_t size; |
a0055ae2 | 212 | char *field, *sep; |
f70e87d7 PZ |
213 | DIR *tasks; |
214 | struct dirent dirent, *next; | |
1a853e36 ACM |
215 | |
216 | snprintf(filename, sizeof(filename), "/proc/%d/stat", pid); | |
217 | ||
218 | fd = open(filename, O_RDONLY); | |
219 | if (fd < 0) { | |
613d8602 IM |
220 | /* |
221 | * We raced with a task exiting - just return: | |
222 | */ | |
223 | if (verbose) | |
224 | fprintf(stderr, "couldn't open %s\n", filename); | |
225 | return; | |
1a853e36 ACM |
226 | } |
227 | if (read(fd, bf, sizeof(bf)) < 0) { | |
228 | fprintf(stderr, "couldn't read %s\n", filename); | |
229 | exit(EXIT_FAILURE); | |
230 | } | |
231 | close(fd); | |
232 | ||
a0055ae2 | 233 | /* 9027 (cat) R 6747 9027 6747 34816 9027 ... */ |
1a853e36 | 234 | memset(&comm_ev, 0, sizeof(comm_ev)); |
a0055ae2 ACM |
235 | field = strchr(bf, '('); |
236 | if (field == NULL) | |
237 | goto out_failure; | |
238 | sep = strchr(++field, ')'); | |
239 | if (sep == NULL) | |
240 | goto out_failure; | |
241 | size = sep - field; | |
242 | memcpy(comm_ev.comm, field, size++); | |
f70e87d7 PZ |
243 | |
244 | comm_ev.pid = pid; | |
1a853e36 | 245 | comm_ev.header.type = PERF_EVENT_COMM; |
9cffa8d5 | 246 | size = ALIGN(size, sizeof(u64)); |
1a853e36 | 247 | comm_ev.header.size = sizeof(comm_ev) - (sizeof(comm_ev.comm) - size); |
16f762a2 | 248 | |
f70e87d7 PZ |
249 | if (!full) { |
250 | comm_ev.tid = pid; | |
251 | ||
f5970550 | 252 | write_output(&comm_ev, comm_ev.header.size); |
f70e87d7 PZ |
253 | return; |
254 | } | |
255 | ||
256 | snprintf(filename, sizeof(filename), "/proc/%d/task", pid); | |
257 | ||
258 | tasks = opendir(filename); | |
259 | while (!readdir_r(tasks, &dirent, &next) && next) { | |
260 | char *end; | |
261 | pid = strtol(dirent.d_name, &end, 10); | |
262 | if (*end) | |
263 | continue; | |
264 | ||
265 | comm_ev.tid = pid; | |
266 | ||
f5970550 | 267 | write_output(&comm_ev, comm_ev.header.size); |
1a853e36 | 268 | } |
f70e87d7 PZ |
269 | closedir(tasks); |
270 | return; | |
271 | ||
a0055ae2 ACM |
272 | out_failure: |
273 | fprintf(stderr, "couldn't get COMM and pgid, malformed %s\n", | |
274 | filename); | |
275 | exit(EXIT_FAILURE); | |
1a853e36 ACM |
276 | } |
277 | ||
2debbc83 | 278 | static void pid_synthesize_mmap_samples(pid_t pid) |
1a853e36 ACM |
279 | { |
280 | char filename[PATH_MAX]; | |
281 | FILE *fp; | |
282 | ||
283 | snprintf(filename, sizeof(filename), "/proc/%d/maps", pid); | |
284 | ||
285 | fp = fopen(filename, "r"); | |
286 | if (fp == NULL) { | |
613d8602 IM |
287 | /* |
288 | * We raced with a task exiting - just return: | |
289 | */ | |
290 | if (verbose) | |
291 | fprintf(stderr, "couldn't open %s\n", filename); | |
292 | return; | |
1a853e36 ACM |
293 | } |
294 | while (1) { | |
a0055ae2 | 295 | char bf[BUFSIZ], *pbf = bf; |
1a853e36 | 296 | struct mmap_event mmap_ev = { |
f37a291c | 297 | .header = { .type = PERF_EVENT_MMAP }, |
1a853e36 | 298 | }; |
a0055ae2 | 299 | int n; |
1a853e36 ACM |
300 | size_t size; |
301 | if (fgets(bf, sizeof(bf), fp) == NULL) | |
302 | break; | |
303 | ||
304 | /* 00400000-0040c000 r-xp 00000000 fd:01 41038 /bin/cat */ | |
a0055ae2 ACM |
305 | n = hex2u64(pbf, &mmap_ev.start); |
306 | if (n < 0) | |
307 | continue; | |
308 | pbf += n + 1; | |
309 | n = hex2u64(pbf, &mmap_ev.len); | |
310 | if (n < 0) | |
311 | continue; | |
312 | pbf += n + 3; | |
313 | if (*pbf == 'x') { /* vm_exec */ | |
76c64c5e | 314 | char *execname = strchr(bf, '/'); |
1a853e36 | 315 | |
76c64c5e | 316 | if (execname == NULL) |
1a853e36 ACM |
317 | continue; |
318 | ||
1a853e36 ACM |
319 | size = strlen(execname); |
320 | execname[size - 1] = '\0'; /* Remove \n */ | |
321 | memcpy(mmap_ev.filename, execname, size); | |
9cffa8d5 | 322 | size = ALIGN(size, sizeof(u64)); |
1a853e36 ACM |
323 | mmap_ev.len -= mmap_ev.start; |
324 | mmap_ev.header.size = (sizeof(mmap_ev) - | |
325 | (sizeof(mmap_ev.filename) - size)); | |
f70e87d7 | 326 | mmap_ev.pid = pid; |
1a853e36 ACM |
327 | mmap_ev.tid = pid; |
328 | ||
f5970550 | 329 | write_output(&mmap_ev, mmap_ev.header.size); |
1a853e36 ACM |
330 | } |
331 | } | |
332 | ||
333 | fclose(fp); | |
334 | } | |
335 | ||
7c6a1c65 | 336 | static void synthesize_all(void) |
f70e87d7 PZ |
337 | { |
338 | DIR *proc; | |
339 | struct dirent dirent, *next; | |
340 | ||
341 | proc = opendir("/proc"); | |
342 | ||
343 | while (!readdir_r(proc, &dirent, &next) && next) { | |
344 | char *end; | |
345 | pid_t pid; | |
346 | ||
347 | pid = strtol(dirent.d_name, &end, 10); | |
348 | if (*end) /* only interested in proper numerical dirents */ | |
349 | continue; | |
350 | ||
351 | pid_synthesize_comm_event(pid, 1); | |
2debbc83 | 352 | pid_synthesize_mmap_samples(pid); |
f70e87d7 PZ |
353 | } |
354 | ||
355 | closedir(proc); | |
356 | } | |
357 | ||
f250c030 IM |
358 | static int group_fd; |
359 | ||
7c6a1c65 PZ |
360 | static struct perf_header_attr *get_header_attr(struct perf_counter_attr *a, int nr) |
361 | { | |
362 | struct perf_header_attr *h_attr; | |
363 | ||
364 | if (nr < header->attrs) { | |
365 | h_attr = header->attr[nr]; | |
366 | } else { | |
367 | h_attr = perf_header_attr__new(a); | |
368 | perf_header__add_attr(header, h_attr); | |
369 | } | |
370 | ||
371 | return h_attr; | |
372 | } | |
373 | ||
f250c030 | 374 | static void create_counter(int counter, int cpu, pid_t pid) |
de9ac07b | 375 | { |
a21ca2ca | 376 | struct perf_counter_attr *attr = attrs + counter; |
7c6a1c65 PZ |
377 | struct perf_header_attr *h_attr; |
378 | int track = !counter; /* only the first counter needs these */ | |
379 | struct { | |
380 | u64 count; | |
381 | u64 time_enabled; | |
382 | u64 time_running; | |
383 | u64 id; | |
384 | } read_data; | |
385 | ||
386 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | | |
387 | PERF_FORMAT_TOTAL_TIME_RUNNING | | |
388 | PERF_FORMAT_ID; | |
16c8a109 | 389 | |
ea1900e5 | 390 | attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
3efa1cc9 | 391 | |
1dba15e7 | 392 | if (freq) { |
ea1900e5 | 393 | attr->sample_type |= PERF_SAMPLE_PERIOD; |
a21ca2ca IM |
394 | attr->freq = 1; |
395 | attr->sample_freq = freq; | |
1dba15e7 | 396 | } |
3efa1cc9 | 397 | |
649c48a9 PZ |
398 | if (no_samples) |
399 | attr->sample_freq = 0; | |
400 | ||
401 | if (inherit_stat) | |
402 | attr->inherit_stat = 1; | |
403 | ||
3efa1cc9 IM |
404 | if (call_graph) |
405 | attr->sample_type |= PERF_SAMPLE_CALLCHAIN; | |
406 | ||
a21ca2ca IM |
407 | attr->mmap = track; |
408 | attr->comm = track; | |
409 | attr->inherit = (cpu < 0) && inherit; | |
4502d77c | 410 | attr->disabled = 1; |
16c8a109 | 411 | |
3da297a6 | 412 | try_again: |
a21ca2ca | 413 | fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0); |
16c8a109 | 414 | |
f250c030 IM |
415 | if (fd[nr_cpu][counter] < 0) { |
416 | int err = errno; | |
16c8a109 | 417 | |
f250c030 | 418 | if (err == EPERM) |
3da297a6 IM |
419 | die("Permission error - are you root?\n"); |
420 | ||
421 | /* | |
422 | * If it's cycles then fall back to hrtimer | |
423 | * based cpu-clock-tick sw counter, which | |
424 | * is always available even if no PMU support: | |
425 | */ | |
426 | if (attr->type == PERF_TYPE_HARDWARE | |
f4dbfa8f | 427 | && attr->config == PERF_COUNT_HW_CPU_CYCLES) { |
3da297a6 IM |
428 | |
429 | if (verbose) | |
430 | warning(" ... trying to fall back to cpu-clock-ticks\n"); | |
431 | attr->type = PERF_TYPE_SOFTWARE; | |
f4dbfa8f | 432 | attr->config = PERF_COUNT_SW_CPU_CLOCK; |
3da297a6 IM |
433 | goto try_again; |
434 | } | |
30c806a0 IM |
435 | printf("\n"); |
436 | error("perfcounter syscall returned with %d (%s)\n", | |
437 | fd[nr_cpu][counter], strerror(err)); | |
438 | die("No CONFIG_PERF_COUNTERS=y kernel support configured?\n"); | |
f250c030 IM |
439 | exit(-1); |
440 | } | |
3da297a6 | 441 | |
7c6a1c65 PZ |
442 | h_attr = get_header_attr(attr, counter); |
443 | ||
444 | if (!file_new) { | |
445 | if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { | |
446 | fprintf(stderr, "incompatible append\n"); | |
447 | exit(-1); | |
448 | } | |
449 | } | |
450 | ||
3928ddbe FW |
451 | if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) { |
452 | perror("Unable to read perf file descriptor\n"); | |
453 | exit(-1); | |
454 | } | |
7c6a1c65 PZ |
455 | |
456 | perf_header_attr__add_id(h_attr, read_data.id); | |
457 | ||
f250c030 IM |
458 | assert(fd[nr_cpu][counter] >= 0); |
459 | fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); | |
16c8a109 | 460 | |
f250c030 IM |
461 | /* |
462 | * First counter acts as the group leader: | |
463 | */ | |
464 | if (group && group_fd == -1) | |
465 | group_fd = fd[nr_cpu][counter]; | |
466 | ||
467 | event_array[nr_poll].fd = fd[nr_cpu][counter]; | |
468 | event_array[nr_poll].events = POLLIN; | |
469 | nr_poll++; | |
470 | ||
471 | mmap_array[nr_cpu][counter].counter = counter; | |
472 | mmap_array[nr_cpu][counter].prev = 0; | |
473 | mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1; | |
474 | mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size, | |
9d91a6f7 | 475 | PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0); |
f250c030 IM |
476 | if (mmap_array[nr_cpu][counter].base == MAP_FAILED) { |
477 | error("failed to mmap with %d (%s)\n", errno, strerror(errno)); | |
478 | exit(-1); | |
479 | } | |
4502d77c PZ |
480 | |
481 | ioctl(fd[nr_cpu][counter], PERF_COUNTER_IOC_ENABLE); | |
f250c030 | 482 | } |
f2521b6e | 483 | |
f250c030 IM |
484 | static void open_counters(int cpu, pid_t pid) |
485 | { | |
486 | int counter; | |
16c8a109 | 487 | |
f250c030 IM |
488 | group_fd = -1; |
489 | for (counter = 0; counter < nr_counters; counter++) | |
490 | create_counter(counter, cpu, pid); | |
491 | ||
16c8a109 PZ |
492 | nr_cpu++; |
493 | } | |
494 | ||
f5970550 PZ |
495 | static void atexit_header(void) |
496 | { | |
7c6a1c65 | 497 | header->data_size += bytes_written; |
f5970550 | 498 | |
7c6a1c65 | 499 | perf_header__write(header, output); |
f5970550 PZ |
500 | } |
501 | ||
0e9b20b8 | 502 | static int __cmd_record(int argc, const char **argv) |
16c8a109 PZ |
503 | { |
504 | int i, counter; | |
abaff32a | 505 | struct stat st; |
7c6a1c65 | 506 | pid_t pid = 0; |
abaff32a | 507 | int flags; |
de9ac07b PZ |
508 | int ret; |
509 | ||
510 | page_size = sysconf(_SC_PAGE_SIZE); | |
de9ac07b PZ |
511 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
512 | assert(nr_cpus <= MAX_NR_CPUS); | |
513 | assert(nr_cpus >= 0); | |
514 | ||
f5970550 PZ |
515 | atexit(sig_atexit); |
516 | signal(SIGCHLD, sig_handler); | |
517 | signal(SIGINT, sig_handler); | |
518 | ||
abaff32a IM |
519 | if (!stat(output_name, &st) && !force && !append_file) { |
520 | fprintf(stderr, "Error, output file %s exists, use -A to append or -f to overwrite.\n", | |
97124d5e PZ |
521 | output_name); |
522 | exit(-1); | |
523 | } | |
524 | ||
abaff32a IM |
525 | flags = O_CREAT|O_RDWR; |
526 | if (append_file) | |
f5970550 | 527 | file_new = 0; |
abaff32a IM |
528 | else |
529 | flags |= O_TRUNC; | |
530 | ||
531 | output = open(output_name, flags, S_IRUSR|S_IWUSR); | |
de9ac07b PZ |
532 | if (output < 0) { |
533 | perror("failed to create output file"); | |
534 | exit(-1); | |
535 | } | |
536 | ||
7c6a1c65 PZ |
537 | if (!file_new) |
538 | header = perf_header__read(output); | |
539 | else | |
540 | header = perf_header__new(); | |
f5970550 PZ |
541 | |
542 | atexit(atexit_header); | |
543 | ||
1a853e36 | 544 | if (!system_wide) { |
7c6a1c65 PZ |
545 | pid = target_pid; |
546 | if (pid == -1) | |
547 | pid = getpid(); | |
548 | ||
549 | open_counters(-1, pid); | |
1a853e36 ACM |
550 | } else for (i = 0; i < nr_cpus; i++) |
551 | open_counters(i, target_pid); | |
de9ac07b | 552 | |
7c6a1c65 PZ |
553 | if (file_new) |
554 | perf_header__write(header, output); | |
555 | ||
556 | if (!system_wide) { | |
557 | pid_synthesize_comm_event(pid, 0); | |
558 | pid_synthesize_mmap_samples(pid); | |
559 | } else | |
560 | synthesize_all(); | |
561 | ||
ef65b2a0 | 562 | if (target_pid == -1 && argc) { |
1a853e36 ACM |
563 | pid = fork(); |
564 | if (pid < 0) | |
565 | perror("failed to fork"); | |
de9ac07b | 566 | |
1a853e36 | 567 | if (!pid) { |
0e9b20b8 | 568 | if (execvp(argv[0], (char **)argv)) { |
1a853e36 ACM |
569 | perror(argv[0]); |
570 | exit(-1); | |
571 | } | |
de9ac07b PZ |
572 | } |
573 | } | |
574 | ||
575 | if (realtime_prio) { | |
576 | struct sched_param param; | |
577 | ||
578 | param.sched_priority = realtime_prio; | |
579 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { | |
580 | printf("Could not set realtime priority.\n"); | |
581 | exit(-1); | |
582 | } | |
583 | } | |
584 | ||
649c48a9 | 585 | for (;;) { |
2debbc83 | 586 | int hits = samples; |
de9ac07b | 587 | |
16c8a109 | 588 | for (i = 0; i < nr_cpu; i++) { |
de9ac07b PZ |
589 | for (counter = 0; counter < nr_counters; counter++) |
590 | mmap_read(&mmap_array[i][counter]); | |
591 | } | |
592 | ||
649c48a9 PZ |
593 | if (hits == samples) { |
594 | if (done) | |
595 | break; | |
de9ac07b | 596 | ret = poll(event_array, nr_poll, 100); |
649c48a9 | 597 | } |
de9ac07b PZ |
598 | } |
599 | ||
021e9f47 IM |
600 | /* |
601 | * Approximate RIP event size: 24 bytes. | |
602 | */ | |
603 | fprintf(stderr, | |
2debbc83 | 604 | "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", |
021e9f47 IM |
605 | (double)bytes_written / 1024.0 / 1024.0, |
606 | output_name, | |
607 | bytes_written / 24); | |
addc2785 | 608 | |
de9ac07b PZ |
609 | return 0; |
610 | } | |
0e9b20b8 | 611 | |
0e9b20b8 | 612 | static const char * const record_usage[] = { |
9e096753 MG |
613 | "perf record [<options>] [<command>]", |
614 | "perf record [<options>] -- <command> [<options>]", | |
0e9b20b8 IM |
615 | NULL |
616 | }; | |
617 | ||
5242519b | 618 | static const struct option options[] = { |
0e9b20b8 | 619 | OPT_CALLBACK('e', "event", NULL, "event", |
86847b62 TG |
620 | "event selector. use 'perf list' to list available events", |
621 | parse_events), | |
0e9b20b8 IM |
622 | OPT_INTEGER('p', "pid", &target_pid, |
623 | "record events on existing pid"), | |
624 | OPT_INTEGER('r', "realtime", &realtime_prio, | |
625 | "collect data with this RT SCHED_FIFO priority"), | |
626 | OPT_BOOLEAN('a', "all-cpus", &system_wide, | |
627 | "system-wide collection from all CPUs"), | |
abaff32a IM |
628 | OPT_BOOLEAN('A', "append", &append_file, |
629 | "append to the output file to do incremental profiling"), | |
97124d5e PZ |
630 | OPT_BOOLEAN('f', "force", &force, |
631 | "overwrite existing data file"), | |
e61078a0 | 632 | OPT_LONG('c', "count", &default_interval, |
abaff32a IM |
633 | "event period to sample"), |
634 | OPT_STRING('o', "output", &output_name, "file", | |
635 | "output file name"), | |
636 | OPT_BOOLEAN('i', "inherit", &inherit, | |
637 | "child tasks inherit counters"), | |
cf1f4574 IM |
638 | OPT_INTEGER('F', "freq", &freq, |
639 | "profile at this frequency"), | |
abaff32a IM |
640 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
641 | "number of mmap data pages"), | |
3efa1cc9 IM |
642 | OPT_BOOLEAN('g', "call-graph", &call_graph, |
643 | "do call-graph (stack chain/backtrace) recording"), | |
3da297a6 IM |
644 | OPT_BOOLEAN('v', "verbose", &verbose, |
645 | "be more verbose (show counter open errors, etc)"), | |
649c48a9 PZ |
646 | OPT_BOOLEAN('s', "stat", &inherit_stat, |
647 | "per thread counts"), | |
648 | OPT_BOOLEAN('n', "no-samples", &no_samples, | |
649 | "don't sample"), | |
0e9b20b8 IM |
650 | OPT_END() |
651 | }; | |
652 | ||
f37a291c | 653 | int cmd_record(int argc, const char **argv, const char *prefix __used) |
0e9b20b8 IM |
654 | { |
655 | int counter; | |
656 | ||
0e9b20b8 | 657 | argc = parse_options(argc, argv, options, record_usage, 0); |
ef65b2a0 | 658 | if (!argc && target_pid == -1 && !system_wide) |
0e9b20b8 IM |
659 | usage_with_options(record_usage, options); |
660 | ||
bbd36e5e PZ |
661 | if (!nr_counters) { |
662 | nr_counters = 1; | |
663 | attrs[0].type = PERF_TYPE_HARDWARE; | |
664 | attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; | |
665 | } | |
0e9b20b8 IM |
666 | |
667 | for (counter = 0; counter < nr_counters; counter++) { | |
a21ca2ca | 668 | if (attrs[counter].sample_period) |
0e9b20b8 IM |
669 | continue; |
670 | ||
a21ca2ca | 671 | attrs[counter].sample_period = default_interval; |
0e9b20b8 IM |
672 | } |
673 | ||
674 | return __cmd_record(argc, argv); | |
675 | } |