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