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 | 17 | #include "util/header.h" |
66e274f3 | 18 | #include "util/event.h" |
8f28827a | 19 | #include "util/debug.h" |
94c744b6 | 20 | #include "util/session.h" |
8d06367f | 21 | #include "util/symbol.h" |
7c6a1c65 | 22 | |
97124d5e | 23 | #include <unistd.h> |
de9ac07b | 24 | #include <sched.h> |
de9ac07b | 25 | |
de9ac07b | 26 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
a21ca2ca | 27 | |
7e4ff9e3 | 28 | static long default_interval = 0; |
a21ca2ca | 29 | |
42e59d7d | 30 | static int nr_cpus = 0; |
de9ac07b | 31 | static unsigned int page_size; |
42e59d7d IM |
32 | static unsigned int mmap_pages = 128; |
33 | static int freq = 1000; | |
de9ac07b | 34 | static int output; |
23ac9cbe | 35 | static const char *output_name = "perf.data"; |
42e59d7d IM |
36 | static int group = 0; |
37 | static unsigned int realtime_prio = 0; | |
38 | static int raw_samples = 0; | |
39 | static int system_wide = 0; | |
40 | static int profile_cpu = -1; | |
41 | static pid_t target_pid = -1; | |
42 | static pid_t child_pid = -1; | |
43 | static int inherit = 1; | |
44 | static int force = 0; | |
45 | static int append_file = 0; | |
46 | static int call_graph = 0; | |
47 | static int inherit_stat = 0; | |
48 | static int no_samples = 0; | |
49 | static int sample_address = 0; | |
50 | static int multiplex = 0; | |
51 | static int multiplex_fd = -1; | |
52 | ||
53 | static long samples = 0; | |
a21ca2ca IM |
54 | static struct timeval last_read; |
55 | static struct timeval this_read; | |
56 | ||
42e59d7d | 57 | static u64 bytes_written = 0; |
a21ca2ca IM |
58 | |
59 | static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS]; | |
60 | ||
42e59d7d IM |
61 | static int nr_poll = 0; |
62 | static int nr_cpu = 0; | |
a21ca2ca | 63 | |
42e59d7d | 64 | static int file_new = 1; |
7c6a1c65 | 65 | |
94c744b6 | 66 | static struct perf_session *session; |
f5970550 | 67 | |
de9ac07b | 68 | struct mmap_data { |
a21ca2ca IM |
69 | int counter; |
70 | void *base; | |
71 | unsigned int mask; | |
72 | unsigned int prev; | |
de9ac07b PZ |
73 | }; |
74 | ||
a21ca2ca IM |
75 | static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; |
76 | ||
9d91a6f7 | 77 | static unsigned long mmap_read_head(struct mmap_data *md) |
de9ac07b | 78 | { |
cdd6c482 | 79 | struct perf_event_mmap_page *pc = md->base; |
9d91a6f7 | 80 | long head; |
de9ac07b PZ |
81 | |
82 | head = pc->data_head; | |
83 | rmb(); | |
84 | ||
85 | return head; | |
86 | } | |
87 | ||
9d91a6f7 PZ |
88 | static void mmap_write_tail(struct mmap_data *md, unsigned long tail) |
89 | { | |
cdd6c482 | 90 | struct perf_event_mmap_page *pc = md->base; |
9d91a6f7 PZ |
91 | |
92 | /* | |
93 | * ensure all reads are done before we write the tail out. | |
94 | */ | |
95 | /* mb(); */ | |
96 | pc->data_tail = tail; | |
97 | } | |
98 | ||
f5970550 PZ |
99 | static void write_output(void *buf, size_t size) |
100 | { | |
101 | while (size) { | |
102 | int ret = write(output, buf, size); | |
103 | ||
104 | if (ret < 0) | |
105 | die("failed to write"); | |
106 | ||
107 | size -= ret; | |
108 | buf += ret; | |
109 | ||
110 | bytes_written += ret; | |
111 | } | |
112 | } | |
113 | ||
8d06367f ACM |
114 | static void write_event(event_t *buf, size_t size) |
115 | { | |
f5a2c3dc ACM |
116 | size_t processed_size = buf->header.size; |
117 | event_t *ev = buf; | |
118 | ||
119 | do { | |
120 | /* | |
121 | * Add it to the list of DSOs, so that when we finish this | |
122 | * record session we can pick the available build-ids. | |
123 | */ | |
124 | if (ev->header.type == PERF_RECORD_MMAP) { | |
125 | struct list_head *head = &dsos__user; | |
126 | if (ev->header.misc == 1) | |
127 | head = &dsos__kernel; | |
128 | __dsos__findnew(head, ev->mmap.filename); | |
129 | } | |
130 | ||
131 | ev = ((void *)ev) + ev->header.size; | |
132 | processed_size += ev->header.size; | |
133 | } while (processed_size < size); | |
8d06367f ACM |
134 | |
135 | write_output(buf, size); | |
136 | } | |
137 | ||
d8f66248 ACM |
138 | static int process_synthesized_event(event_t *event, |
139 | struct perf_session *self __used) | |
234fbbf5 | 140 | { |
8d06367f | 141 | write_event(event, event->header.size); |
234fbbf5 ACM |
142 | return 0; |
143 | } | |
144 | ||
de9ac07b PZ |
145 | static void mmap_read(struct mmap_data *md) |
146 | { | |
147 | unsigned int head = mmap_read_head(md); | |
148 | unsigned int old = md->prev; | |
149 | unsigned char *data = md->base + page_size; | |
150 | unsigned long size; | |
151 | void *buf; | |
152 | int diff; | |
153 | ||
154 | gettimeofday(&this_read, NULL); | |
155 | ||
156 | /* | |
157 | * If we're further behind than half the buffer, there's a chance | |
2debbc83 | 158 | * the writer will bite our tail and mess up the samples under us. |
de9ac07b PZ |
159 | * |
160 | * If we somehow ended up ahead of the head, we got messed up. | |
161 | * | |
162 | * In either case, truncate and restart at head. | |
163 | */ | |
164 | diff = head - old; | |
9d91a6f7 | 165 | if (diff < 0) { |
de9ac07b PZ |
166 | struct timeval iv; |
167 | unsigned long msecs; | |
168 | ||
169 | timersub(&this_read, &last_read, &iv); | |
170 | msecs = iv.tv_sec*1000 + iv.tv_usec/1000; | |
171 | ||
172 | fprintf(stderr, "WARNING: failed to keep up with mmap data." | |
173 | " Last read %lu msecs ago.\n", msecs); | |
174 | ||
175 | /* | |
176 | * head points to a known good entry, start there. | |
177 | */ | |
178 | old = head; | |
179 | } | |
180 | ||
181 | last_read = this_read; | |
182 | ||
183 | if (old != head) | |
2debbc83 | 184 | samples++; |
de9ac07b PZ |
185 | |
186 | size = head - old; | |
187 | ||
188 | if ((old & md->mask) + size != (head & md->mask)) { | |
189 | buf = &data[old & md->mask]; | |
190 | size = md->mask + 1 - (old & md->mask); | |
191 | old += size; | |
021e9f47 | 192 | |
8d06367f | 193 | write_event(buf, size); |
de9ac07b PZ |
194 | } |
195 | ||
196 | buf = &data[old & md->mask]; | |
197 | size = head - old; | |
198 | old += size; | |
021e9f47 | 199 | |
8d06367f | 200 | write_event(buf, size); |
de9ac07b PZ |
201 | |
202 | md->prev = old; | |
9d91a6f7 | 203 | mmap_write_tail(md, old); |
de9ac07b PZ |
204 | } |
205 | ||
206 | static volatile int done = 0; | |
f7b7c26e | 207 | static volatile int signr = -1; |
de9ac07b | 208 | |
16c8a109 | 209 | static void sig_handler(int sig) |
de9ac07b | 210 | { |
16c8a109 | 211 | done = 1; |
f7b7c26e PZ |
212 | signr = sig; |
213 | } | |
214 | ||
215 | static void sig_atexit(void) | |
216 | { | |
933da83a CW |
217 | if (child_pid != -1) |
218 | kill(child_pid, SIGTERM); | |
219 | ||
f7b7c26e PZ |
220 | if (signr == -1) |
221 | return; | |
222 | ||
223 | signal(signr, SIG_DFL); | |
224 | kill(getpid(), signr); | |
de9ac07b PZ |
225 | } |
226 | ||
f250c030 IM |
227 | static int group_fd; |
228 | ||
cdd6c482 | 229 | static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr) |
7c6a1c65 PZ |
230 | { |
231 | struct perf_header_attr *h_attr; | |
232 | ||
94c744b6 ACM |
233 | if (nr < session->header.attrs) { |
234 | h_attr = session->header.attr[nr]; | |
7c6a1c65 PZ |
235 | } else { |
236 | h_attr = perf_header_attr__new(a); | |
dc79c0fc | 237 | if (h_attr != NULL) |
94c744b6 | 238 | if (perf_header__add_attr(&session->header, h_attr) < 0) { |
11deb1f9 ACM |
239 | perf_header_attr__delete(h_attr); |
240 | h_attr = NULL; | |
241 | } | |
7c6a1c65 PZ |
242 | } |
243 | ||
244 | return h_attr; | |
245 | } | |
246 | ||
f250c030 | 247 | static void create_counter(int counter, int cpu, pid_t pid) |
de9ac07b | 248 | { |
c171b552 | 249 | char *filter = filters[counter]; |
cdd6c482 | 250 | struct perf_event_attr *attr = attrs + counter; |
7c6a1c65 PZ |
251 | struct perf_header_attr *h_attr; |
252 | int track = !counter; /* only the first counter needs these */ | |
c171b552 | 253 | int ret; |
7c6a1c65 PZ |
254 | struct { |
255 | u64 count; | |
256 | u64 time_enabled; | |
257 | u64 time_running; | |
258 | u64 id; | |
259 | } read_data; | |
260 | ||
261 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | | |
262 | PERF_FORMAT_TOTAL_TIME_RUNNING | | |
263 | PERF_FORMAT_ID; | |
16c8a109 | 264 | |
3a9f131f | 265 | attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
3efa1cc9 | 266 | |
1dba15e7 | 267 | if (freq) { |
ea1900e5 | 268 | attr->sample_type |= PERF_SAMPLE_PERIOD; |
a21ca2ca IM |
269 | attr->freq = 1; |
270 | attr->sample_freq = freq; | |
1dba15e7 | 271 | } |
3efa1cc9 | 272 | |
649c48a9 PZ |
273 | if (no_samples) |
274 | attr->sample_freq = 0; | |
275 | ||
276 | if (inherit_stat) | |
277 | attr->inherit_stat = 1; | |
278 | ||
4bba828d AB |
279 | if (sample_address) |
280 | attr->sample_type |= PERF_SAMPLE_ADDR; | |
281 | ||
3efa1cc9 IM |
282 | if (call_graph) |
283 | attr->sample_type |= PERF_SAMPLE_CALLCHAIN; | |
284 | ||
cd6feeea | 285 | if (raw_samples) { |
6ddf259d | 286 | attr->sample_type |= PERF_SAMPLE_TIME; |
daac07b2 | 287 | attr->sample_type |= PERF_SAMPLE_RAW; |
cd6feeea IM |
288 | attr->sample_type |= PERF_SAMPLE_CPU; |
289 | } | |
f413cdb8 | 290 | |
a21ca2ca IM |
291 | attr->mmap = track; |
292 | attr->comm = track; | |
60ab2716 | 293 | attr->inherit = inherit; |
4502d77c | 294 | attr->disabled = 1; |
16c8a109 | 295 | |
3da297a6 | 296 | try_again: |
cdd6c482 | 297 | fd[nr_cpu][counter] = sys_perf_event_open(attr, pid, cpu, group_fd, 0); |
16c8a109 | 298 | |
f250c030 IM |
299 | if (fd[nr_cpu][counter] < 0) { |
300 | int err = errno; | |
16c8a109 | 301 | |
c10edee2 | 302 | if (err == EPERM || err == EACCES) |
3da297a6 | 303 | die("Permission error - are you root?\n"); |
0a5ac846 JA |
304 | else if (err == ENODEV && profile_cpu != -1) |
305 | die("No such device - did you specify an out-of-range profile CPU?\n"); | |
3da297a6 IM |
306 | |
307 | /* | |
308 | * If it's cycles then fall back to hrtimer | |
309 | * based cpu-clock-tick sw counter, which | |
310 | * is always available even if no PMU support: | |
311 | */ | |
312 | if (attr->type == PERF_TYPE_HARDWARE | |
f4dbfa8f | 313 | && attr->config == PERF_COUNT_HW_CPU_CYCLES) { |
3da297a6 IM |
314 | |
315 | if (verbose) | |
316 | warning(" ... trying to fall back to cpu-clock-ticks\n"); | |
317 | attr->type = PERF_TYPE_SOFTWARE; | |
f4dbfa8f | 318 | attr->config = PERF_COUNT_SW_CPU_CLOCK; |
3da297a6 IM |
319 | goto try_again; |
320 | } | |
30c806a0 IM |
321 | printf("\n"); |
322 | error("perfcounter syscall returned with %d (%s)\n", | |
323 | fd[nr_cpu][counter], strerror(err)); | |
bfd45118 SK |
324 | |
325 | #if defined(__i386__) || defined(__x86_64__) | |
326 | if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) | |
327 | die("No hardware sampling interrupt available. No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.\n"); | |
328 | #endif | |
329 | ||
cdd6c482 | 330 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
f250c030 IM |
331 | exit(-1); |
332 | } | |
3da297a6 | 333 | |
7c6a1c65 | 334 | h_attr = get_header_attr(attr, counter); |
dc79c0fc ACM |
335 | if (h_attr == NULL) |
336 | die("nomem\n"); | |
7c6a1c65 PZ |
337 | |
338 | if (!file_new) { | |
339 | if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { | |
340 | fprintf(stderr, "incompatible append\n"); | |
341 | exit(-1); | |
342 | } | |
343 | } | |
344 | ||
3928ddbe FW |
345 | if (read(fd[nr_cpu][counter], &read_data, sizeof(read_data)) == -1) { |
346 | perror("Unable to read perf file descriptor\n"); | |
347 | exit(-1); | |
348 | } | |
7c6a1c65 | 349 | |
58754121 ACM |
350 | if (perf_header_attr__add_id(h_attr, read_data.id) < 0) { |
351 | pr_warning("Not enough memory to add id\n"); | |
352 | exit(-1); | |
353 | } | |
7c6a1c65 | 354 | |
f250c030 IM |
355 | assert(fd[nr_cpu][counter] >= 0); |
356 | fcntl(fd[nr_cpu][counter], F_SETFL, O_NONBLOCK); | |
16c8a109 | 357 | |
f250c030 IM |
358 | /* |
359 | * First counter acts as the group leader: | |
360 | */ | |
361 | if (group && group_fd == -1) | |
362 | group_fd = fd[nr_cpu][counter]; | |
ea57c4f5 IM |
363 | if (multiplex && multiplex_fd == -1) |
364 | multiplex_fd = fd[nr_cpu][counter]; | |
f250c030 | 365 | |
ea57c4f5 | 366 | if (multiplex && fd[nr_cpu][counter] != multiplex_fd) { |
4502d77c | 367 | |
cdd6c482 | 368 | ret = ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd); |
ea57c4f5 IM |
369 | assert(ret != -1); |
370 | } else { | |
371 | event_array[nr_poll].fd = fd[nr_cpu][counter]; | |
372 | event_array[nr_poll].events = POLLIN; | |
373 | nr_poll++; | |
374 | ||
375 | mmap_array[nr_cpu][counter].counter = counter; | |
376 | mmap_array[nr_cpu][counter].prev = 0; | |
377 | mmap_array[nr_cpu][counter].mask = mmap_pages*page_size - 1; | |
378 | mmap_array[nr_cpu][counter].base = mmap(NULL, (mmap_pages+1)*page_size, | |
379 | PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter], 0); | |
380 | if (mmap_array[nr_cpu][counter].base == MAP_FAILED) { | |
381 | error("failed to mmap with %d (%s)\n", errno, strerror(errno)); | |
382 | exit(-1); | |
383 | } | |
384 | } | |
d1302522 | 385 | |
c171b552 LZ |
386 | if (filter != NULL) { |
387 | ret = ioctl(fd[nr_cpu][counter], | |
388 | PERF_EVENT_IOC_SET_FILTER, filter); | |
389 | if (ret) { | |
390 | error("failed to set filter with %d (%s)\n", errno, | |
391 | strerror(errno)); | |
392 | exit(-1); | |
393 | } | |
394 | } | |
395 | ||
cdd6c482 | 396 | ioctl(fd[nr_cpu][counter], PERF_EVENT_IOC_ENABLE); |
f250c030 | 397 | } |
f2521b6e | 398 | |
f250c030 IM |
399 | static void open_counters(int cpu, pid_t pid) |
400 | { | |
401 | int counter; | |
16c8a109 | 402 | |
f250c030 IM |
403 | group_fd = -1; |
404 | for (counter = 0; counter < nr_counters; counter++) | |
405 | create_counter(counter, cpu, pid); | |
406 | ||
16c8a109 PZ |
407 | nr_cpu++; |
408 | } | |
409 | ||
f5970550 PZ |
410 | static void atexit_header(void) |
411 | { | |
94c744b6 | 412 | session->header.data_size += bytes_written; |
f5970550 | 413 | |
94c744b6 | 414 | perf_header__write(&session->header, output, true); |
f5970550 PZ |
415 | } |
416 | ||
d4db3f16 | 417 | static int __cmd_record(int argc, const char **argv) |
16c8a109 PZ |
418 | { |
419 | int i, counter; | |
abaff32a | 420 | struct stat st; |
7c6a1c65 | 421 | pid_t pid = 0; |
abaff32a | 422 | int flags; |
4dc0a04b | 423 | int err; |
8b412664 | 424 | unsigned long waking = 0; |
856e9660 | 425 | int child_ready_pipe[2], go_pipe[2]; |
d4db3f16 | 426 | const bool forks = target_pid == -1 && argc > 0; |
856e9660 | 427 | char buf; |
de9ac07b PZ |
428 | |
429 | page_size = sysconf(_SC_PAGE_SIZE); | |
de9ac07b PZ |
430 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
431 | assert(nr_cpus <= MAX_NR_CPUS); | |
432 | assert(nr_cpus >= 0); | |
433 | ||
f5970550 PZ |
434 | atexit(sig_atexit); |
435 | signal(SIGCHLD, sig_handler); | |
436 | signal(SIGINT, sig_handler); | |
437 | ||
d4db3f16 | 438 | if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { |
856e9660 PZ |
439 | perror("failed to create pipes"); |
440 | exit(-1); | |
441 | } | |
442 | ||
266e0e21 | 443 | if (!stat(output_name, &st) && st.st_size) { |
b38d3464 ACM |
444 | if (!force) { |
445 | if (!append_file) { | |
446 | pr_err("Error, output file %s exists, use -A " | |
447 | "to append or -f to overwrite.\n", | |
448 | output_name); | |
449 | exit(-1); | |
450 | } | |
451 | } else { | |
452 | char oldname[PATH_MAX]; | |
453 | snprintf(oldname, sizeof(oldname), "%s.old", | |
454 | output_name); | |
455 | unlink(oldname); | |
456 | rename(output_name, oldname); | |
266e0e21 PH |
457 | } |
458 | } else { | |
459 | append_file = 0; | |
97124d5e PZ |
460 | } |
461 | ||
abaff32a IM |
462 | flags = O_CREAT|O_RDWR; |
463 | if (append_file) | |
f5970550 | 464 | file_new = 0; |
abaff32a IM |
465 | else |
466 | flags |= O_TRUNC; | |
467 | ||
468 | output = open(output_name, flags, S_IRUSR|S_IWUSR); | |
de9ac07b PZ |
469 | if (output < 0) { |
470 | perror("failed to create output file"); | |
471 | exit(-1); | |
472 | } | |
473 | ||
75be6cf4 | 474 | session = perf_session__new(output_name, O_WRONLY, force); |
94c744b6 | 475 | if (session == NULL) { |
a9a70bbc ACM |
476 | pr_err("Not enough memory for reading perf file header\n"); |
477 | return -1; | |
478 | } | |
479 | ||
b7cece76 ACM |
480 | if (perf_session__create_kernel_maps(session) < 0) { |
481 | pr_err("Problems creating kernel maps\n"); | |
482 | return -1; | |
483 | } | |
484 | ||
4dc0a04b | 485 | if (!file_new) { |
94c744b6 | 486 | err = perf_header__read(&session->header, output); |
4dc0a04b ACM |
487 | if (err < 0) |
488 | return err; | |
489 | } | |
490 | ||
9df37ddd | 491 | if (raw_samples) { |
94c744b6 | 492 | perf_header__set_feat(&session->header, HEADER_TRACE_INFO); |
9df37ddd FW |
493 | } else { |
494 | for (i = 0; i < nr_counters; i++) { | |
495 | if (attrs[i].sample_type & PERF_SAMPLE_RAW) { | |
94c744b6 | 496 | perf_header__set_feat(&session->header, HEADER_TRACE_INFO); |
9df37ddd FW |
497 | break; |
498 | } | |
499 | } | |
500 | } | |
03456a15 | 501 | |
f5970550 PZ |
502 | atexit(atexit_header); |
503 | ||
d4db3f16 | 504 | if (forks) { |
856e9660 PZ |
505 | pid = fork(); |
506 | if (pid < 0) { | |
507 | perror("failed to fork"); | |
508 | exit(-1); | |
509 | } | |
7c6a1c65 | 510 | |
856e9660 PZ |
511 | if (!pid) { |
512 | close(child_ready_pipe[0]); | |
513 | close(go_pipe[1]); | |
514 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); | |
515 | ||
516 | /* | |
517 | * Do a dummy execvp to get the PLT entry resolved, | |
518 | * so we avoid the resolver overhead on the real | |
519 | * execvp call. | |
520 | */ | |
521 | execvp("", (char **)argv); | |
522 | ||
523 | /* | |
524 | * Tell the parent we're ready to go | |
525 | */ | |
526 | close(child_ready_pipe[1]); | |
527 | ||
528 | /* | |
529 | * Wait until the parent tells us to go. | |
530 | */ | |
531 | if (read(go_pipe[0], &buf, 1) == -1) | |
532 | perror("unable to read pipe"); | |
533 | ||
534 | execvp(argv[0], (char **)argv); | |
535 | ||
536 | perror(argv[0]); | |
537 | exit(-1); | |
0a5ac846 | 538 | } |
856e9660 PZ |
539 | |
540 | child_pid = pid; | |
541 | ||
542 | if (!system_wide) | |
543 | target_pid = pid; | |
544 | ||
545 | close(child_ready_pipe[1]); | |
546 | close(go_pipe[0]); | |
547 | /* | |
548 | * wait for child to settle | |
549 | */ | |
550 | if (read(child_ready_pipe[0], &buf, 1) == -1) { | |
551 | perror("unable to read pipe"); | |
552 | exit(-1); | |
553 | } | |
554 | close(child_ready_pipe[0]); | |
555 | } | |
556 | ||
557 | ||
60ab2716 | 558 | if ((!system_wide && !inherit) || profile_cpu != -1) { |
856e9660 PZ |
559 | open_counters(profile_cpu, target_pid); |
560 | } else { | |
561 | for (i = 0; i < nr_cpus; i++) | |
562 | open_counters(i, target_pid); | |
0a5ac846 | 563 | } |
de9ac07b | 564 | |
d5eed904 | 565 | if (file_new) { |
94c744b6 | 566 | err = perf_header__write(&session->header, output, false); |
d5eed904 ACM |
567 | if (err < 0) |
568 | return err; | |
56b03f3c ACM |
569 | } |
570 | ||
571 | err = event__synthesize_kernel_mmap(process_synthesized_event, | |
572 | session, "_text"); | |
573 | if (err < 0) { | |
574 | pr_err("Couldn't record kernel reference relocation symbol.\n"); | |
575 | return err; | |
b7cece76 ACM |
576 | } |
577 | ||
578 | err = event__synthesize_modules(process_synthesized_event, session); | |
579 | if (err < 0) { | |
580 | pr_err("Couldn't record kernel reference relocation symbol.\n"); | |
581 | return err; | |
d5eed904 | 582 | } |
7c6a1c65 | 583 | |
d4db3f16 | 584 | if (!system_wide && profile_cpu == -1) |
d8f66248 ACM |
585 | event__synthesize_thread(pid, process_synthesized_event, |
586 | session); | |
234fbbf5 | 587 | else |
d8f66248 | 588 | event__synthesize_threads(process_synthesized_event, session); |
7c6a1c65 | 589 | |
de9ac07b PZ |
590 | if (realtime_prio) { |
591 | struct sched_param param; | |
592 | ||
593 | param.sched_priority = realtime_prio; | |
594 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { | |
6beba7ad | 595 | pr_err("Could not set realtime priority.\n"); |
de9ac07b PZ |
596 | exit(-1); |
597 | } | |
598 | } | |
599 | ||
856e9660 PZ |
600 | /* |
601 | * Let the child rip | |
602 | */ | |
d4db3f16 ACM |
603 | if (forks) |
604 | close(go_pipe[1]); | |
856e9660 | 605 | |
649c48a9 | 606 | for (;;) { |
2debbc83 | 607 | int hits = samples; |
de9ac07b | 608 | |
16c8a109 | 609 | for (i = 0; i < nr_cpu; i++) { |
ea57c4f5 IM |
610 | for (counter = 0; counter < nr_counters; counter++) { |
611 | if (mmap_array[i][counter].base) | |
612 | mmap_read(&mmap_array[i][counter]); | |
613 | } | |
de9ac07b PZ |
614 | } |
615 | ||
649c48a9 PZ |
616 | if (hits == samples) { |
617 | if (done) | |
618 | break; | |
4dc0a04b | 619 | err = poll(event_array, nr_poll, -1); |
8b412664 PZ |
620 | waking++; |
621 | } | |
622 | ||
623 | if (done) { | |
624 | for (i = 0; i < nr_cpu; i++) { | |
625 | for (counter = 0; counter < nr_counters; counter++) | |
cdd6c482 | 626 | ioctl(fd[i][counter], PERF_EVENT_IOC_DISABLE); |
8b412664 | 627 | } |
649c48a9 | 628 | } |
de9ac07b PZ |
629 | } |
630 | ||
8b412664 PZ |
631 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); |
632 | ||
021e9f47 IM |
633 | /* |
634 | * Approximate RIP event size: 24 bytes. | |
635 | */ | |
636 | fprintf(stderr, | |
2debbc83 | 637 | "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", |
021e9f47 IM |
638 | (double)bytes_written / 1024.0 / 1024.0, |
639 | output_name, | |
640 | bytes_written / 24); | |
addc2785 | 641 | |
de9ac07b PZ |
642 | return 0; |
643 | } | |
0e9b20b8 | 644 | |
0e9b20b8 | 645 | static const char * const record_usage[] = { |
9e096753 MG |
646 | "perf record [<options>] [<command>]", |
647 | "perf record [<options>] -- <command> [<options>]", | |
0e9b20b8 IM |
648 | NULL |
649 | }; | |
650 | ||
5242519b | 651 | static const struct option options[] = { |
0e9b20b8 | 652 | OPT_CALLBACK('e', "event", NULL, "event", |
86847b62 TG |
653 | "event selector. use 'perf list' to list available events", |
654 | parse_events), | |
c171b552 LZ |
655 | OPT_CALLBACK(0, "filter", NULL, "filter", |
656 | "event filter", parse_filter), | |
0e9b20b8 IM |
657 | OPT_INTEGER('p', "pid", &target_pid, |
658 | "record events on existing pid"), | |
659 | OPT_INTEGER('r', "realtime", &realtime_prio, | |
660 | "collect data with this RT SCHED_FIFO priority"), | |
daac07b2 FW |
661 | OPT_BOOLEAN('R', "raw-samples", &raw_samples, |
662 | "collect raw sample records from all opened counters"), | |
0e9b20b8 IM |
663 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
664 | "system-wide collection from all CPUs"), | |
abaff32a IM |
665 | OPT_BOOLEAN('A', "append", &append_file, |
666 | "append to the output file to do incremental profiling"), | |
0a5ac846 JA |
667 | OPT_INTEGER('C', "profile_cpu", &profile_cpu, |
668 | "CPU to profile on"), | |
97124d5e PZ |
669 | OPT_BOOLEAN('f', "force", &force, |
670 | "overwrite existing data file"), | |
e61078a0 | 671 | OPT_LONG('c', "count", &default_interval, |
abaff32a IM |
672 | "event period to sample"), |
673 | OPT_STRING('o', "output", &output_name, "file", | |
674 | "output file name"), | |
675 | OPT_BOOLEAN('i', "inherit", &inherit, | |
676 | "child tasks inherit counters"), | |
cf1f4574 IM |
677 | OPT_INTEGER('F', "freq", &freq, |
678 | "profile at this frequency"), | |
abaff32a IM |
679 | OPT_INTEGER('m', "mmap-pages", &mmap_pages, |
680 | "number of mmap data pages"), | |
3efa1cc9 IM |
681 | OPT_BOOLEAN('g', "call-graph", &call_graph, |
682 | "do call-graph (stack chain/backtrace) recording"), | |
3da297a6 IM |
683 | OPT_BOOLEAN('v', "verbose", &verbose, |
684 | "be more verbose (show counter open errors, etc)"), | |
649c48a9 PZ |
685 | OPT_BOOLEAN('s', "stat", &inherit_stat, |
686 | "per thread counts"), | |
4bba828d AB |
687 | OPT_BOOLEAN('d', "data", &sample_address, |
688 | "Sample addresses"), | |
649c48a9 PZ |
689 | OPT_BOOLEAN('n', "no-samples", &no_samples, |
690 | "don't sample"), | |
d1302522 FW |
691 | OPT_BOOLEAN('M', "multiplex", &multiplex, |
692 | "multiplex counter output in a single channel"), | |
0e9b20b8 IM |
693 | OPT_END() |
694 | }; | |
695 | ||
f37a291c | 696 | int cmd_record(int argc, const char **argv, const char *prefix __used) |
0e9b20b8 IM |
697 | { |
698 | int counter; | |
699 | ||
a0541234 | 700 | argc = parse_options(argc, argv, options, record_usage, |
655000e7 | 701 | PARSE_OPT_STOP_AT_NON_OPTION); |
d4db3f16 | 702 | if (!argc && target_pid == -1 && !system_wide && profile_cpu == -1) |
0e9b20b8 IM |
703 | usage_with_options(record_usage, options); |
704 | ||
655000e7 ACM |
705 | symbol__init(); |
706 | ||
bbd36e5e PZ |
707 | if (!nr_counters) { |
708 | nr_counters = 1; | |
709 | attrs[0].type = PERF_TYPE_HARDWARE; | |
710 | attrs[0].config = PERF_COUNT_HW_CPU_CYCLES; | |
711 | } | |
0e9b20b8 | 712 | |
7e4ff9e3 MG |
713 | /* |
714 | * User specified count overrides default frequency. | |
715 | */ | |
716 | if (default_interval) | |
717 | freq = 0; | |
718 | else if (freq) { | |
719 | default_interval = freq; | |
720 | } else { | |
721 | fprintf(stderr, "frequency and count are zero, aborting\n"); | |
722 | exit(EXIT_FAILURE); | |
723 | } | |
724 | ||
0e9b20b8 | 725 | for (counter = 0; counter < nr_counters; counter++) { |
a21ca2ca | 726 | if (attrs[counter].sample_period) |
0e9b20b8 IM |
727 | continue; |
728 | ||
a21ca2ca | 729 | attrs[counter].sample_period = default_interval; |
0e9b20b8 IM |
730 | } |
731 | ||
732 | return __cmd_record(argc, argv); | |
733 | } |