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