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