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