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" |
6eda5838 | 18 | |
7c6a1c65 | 19 | #include "util/header.h" |
66e274f3 | 20 | #include "util/event.h" |
69aad6f1 | 21 | #include "util/evsel.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> |
a41794cd | 29 | #include <sys/mman.h> |
de9ac07b | 30 | |
69aad6f1 ACM |
31 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
32 | ||
7865e817 FW |
33 | enum write_mode_t { |
34 | WRITE_FORCE, | |
35 | WRITE_APPEND | |
36 | }; | |
37 | ||
3de29cab SE |
38 | static u64 user_interval = ULLONG_MAX; |
39 | static u64 default_interval = 0; | |
640c03ce | 40 | static u64 sample_type; |
a21ca2ca | 41 | |
60d567e2 | 42 | static struct cpu_map *cpus; |
de9ac07b | 43 | static unsigned int page_size; |
42e59d7d | 44 | static unsigned int mmap_pages = 128; |
f9212819 | 45 | static unsigned int user_freq = UINT_MAX; |
42e59d7d | 46 | static int freq = 1000; |
de9ac07b | 47 | static int output; |
529870e3 | 48 | static int pipe_output = 0; |
23ac9cbe | 49 | static const char *output_name = "perf.data"; |
42e59d7d | 50 | static int group = 0; |
1967936d | 51 | static int realtime_prio = 0; |
c0555642 | 52 | static bool raw_samples = false; |
9c90a61c | 53 | static bool sample_id_all_avail = true; |
c0555642 | 54 | static bool system_wide = false; |
42e59d7d | 55 | static pid_t target_pid = -1; |
d6d901c2 | 56 | static pid_t target_tid = -1; |
5c98d466 | 57 | static struct thread_map *threads; |
42e59d7d | 58 | static pid_t child_pid = -1; |
2e6cdf99 | 59 | static bool no_inherit = false; |
7865e817 | 60 | static enum write_mode_t write_mode = WRITE_FORCE; |
c0555642 IM |
61 | static bool call_graph = false; |
62 | static bool inherit_stat = false; | |
63 | static bool no_samples = false; | |
64 | static bool sample_address = false; | |
9c90a61c | 65 | static bool sample_time = false; |
a1ac1d3c | 66 | static bool no_buildid = false; |
baa2f6ce | 67 | static bool no_buildid_cache = false; |
42e59d7d IM |
68 | |
69 | static long samples = 0; | |
42e59d7d | 70 | static u64 bytes_written = 0; |
a21ca2ca | 71 | |
d6d901c2 | 72 | static struct pollfd *event_array; |
a21ca2ca | 73 | |
42e59d7d IM |
74 | static int nr_poll = 0; |
75 | static int nr_cpu = 0; | |
a21ca2ca | 76 | |
42e59d7d | 77 | static int file_new = 1; |
6122e4e4 | 78 | static off_t post_processing_offset; |
7c6a1c65 | 79 | |
94c744b6 | 80 | static struct perf_session *session; |
c45c6ea2 | 81 | static const char *cpu_list; |
f5970550 | 82 | |
de9ac07b | 83 | struct mmap_data { |
a21ca2ca IM |
84 | void *base; |
85 | unsigned int mask; | |
86 | unsigned int prev; | |
de9ac07b PZ |
87 | }; |
88 | ||
0e2e63dd | 89 | static struct mmap_data mmap_array[MAX_NR_CPUS]; |
a21ca2ca | 90 | |
9d91a6f7 | 91 | static unsigned long mmap_read_head(struct mmap_data *md) |
de9ac07b | 92 | { |
cdd6c482 | 93 | struct perf_event_mmap_page *pc = md->base; |
9d91a6f7 | 94 | long head; |
de9ac07b PZ |
95 | |
96 | head = pc->data_head; | |
97 | rmb(); | |
98 | ||
99 | return head; | |
100 | } | |
101 | ||
9d91a6f7 PZ |
102 | static void mmap_write_tail(struct mmap_data *md, unsigned long tail) |
103 | { | |
cdd6c482 | 104 | struct perf_event_mmap_page *pc = md->base; |
9d91a6f7 PZ |
105 | |
106 | /* | |
107 | * ensure all reads are done before we write the tail out. | |
108 | */ | |
109 | /* mb(); */ | |
110 | pc->data_tail = tail; | |
111 | } | |
112 | ||
9215545e TZ |
113 | static void advance_output(size_t size) |
114 | { | |
115 | bytes_written += size; | |
116 | } | |
117 | ||
f5970550 PZ |
118 | static void write_output(void *buf, size_t size) |
119 | { | |
120 | while (size) { | |
121 | int ret = write(output, buf, size); | |
122 | ||
123 | if (ret < 0) | |
124 | die("failed to write"); | |
125 | ||
126 | size -= ret; | |
127 | buf += ret; | |
128 | ||
129 | bytes_written += ret; | |
130 | } | |
131 | } | |
132 | ||
d8f66248 | 133 | static int process_synthesized_event(event_t *event, |
640c03ce | 134 | struct sample_data *sample __used, |
d8f66248 | 135 | struct perf_session *self __used) |
234fbbf5 | 136 | { |
6122e4e4 | 137 | write_output(event, event->header.size); |
234fbbf5 ACM |
138 | return 0; |
139 | } | |
140 | ||
de9ac07b PZ |
141 | static void mmap_read(struct mmap_data *md) |
142 | { | |
143 | unsigned int head = mmap_read_head(md); | |
144 | unsigned int old = md->prev; | |
145 | unsigned char *data = md->base + page_size; | |
146 | unsigned long size; | |
147 | void *buf; | |
148 | int diff; | |
149 | ||
de9ac07b PZ |
150 | /* |
151 | * If we're further behind than half the buffer, there's a chance | |
2debbc83 | 152 | * the writer will bite our tail and mess up the samples under us. |
de9ac07b PZ |
153 | * |
154 | * If we somehow ended up ahead of the head, we got messed up. | |
155 | * | |
156 | * In either case, truncate and restart at head. | |
157 | */ | |
158 | diff = head - old; | |
9d91a6f7 | 159 | if (diff < 0) { |
ef365cef | 160 | fprintf(stderr, "WARNING: failed to keep up with mmap data\n"); |
de9ac07b PZ |
161 | /* |
162 | * head points to a known good entry, start there. | |
163 | */ | |
164 | old = head; | |
165 | } | |
166 | ||
de9ac07b | 167 | if (old != head) |
2debbc83 | 168 | samples++; |
de9ac07b PZ |
169 | |
170 | size = head - old; | |
171 | ||
172 | if ((old & md->mask) + size != (head & md->mask)) { | |
173 | buf = &data[old & md->mask]; | |
174 | size = md->mask + 1 - (old & md->mask); | |
175 | old += size; | |
021e9f47 | 176 | |
6122e4e4 | 177 | write_output(buf, size); |
de9ac07b PZ |
178 | } |
179 | ||
180 | buf = &data[old & md->mask]; | |
181 | size = head - old; | |
182 | old += size; | |
021e9f47 | 183 | |
6122e4e4 | 184 | write_output(buf, size); |
de9ac07b PZ |
185 | |
186 | md->prev = old; | |
9d91a6f7 | 187 | mmap_write_tail(md, old); |
de9ac07b PZ |
188 | } |
189 | ||
190 | static volatile int done = 0; | |
f7b7c26e | 191 | static volatile int signr = -1; |
de9ac07b | 192 | |
16c8a109 | 193 | static void sig_handler(int sig) |
de9ac07b | 194 | { |
16c8a109 | 195 | done = 1; |
f7b7c26e PZ |
196 | signr = sig; |
197 | } | |
198 | ||
199 | static void sig_atexit(void) | |
200 | { | |
5ffc8881 | 201 | if (child_pid > 0) |
933da83a CW |
202 | kill(child_pid, SIGTERM); |
203 | ||
18483b81 | 204 | if (signr == -1 || signr == SIGUSR1) |
f7b7c26e PZ |
205 | return; |
206 | ||
207 | signal(signr, SIG_DFL); | |
208 | kill(getpid(), signr); | |
de9ac07b PZ |
209 | } |
210 | ||
f250c030 IM |
211 | static int group_fd; |
212 | ||
cdd6c482 | 213 | static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int nr) |
7c6a1c65 PZ |
214 | { |
215 | struct perf_header_attr *h_attr; | |
216 | ||
94c744b6 ACM |
217 | if (nr < session->header.attrs) { |
218 | h_attr = session->header.attr[nr]; | |
7c6a1c65 PZ |
219 | } else { |
220 | h_attr = perf_header_attr__new(a); | |
dc79c0fc | 221 | if (h_attr != NULL) |
94c744b6 | 222 | if (perf_header__add_attr(&session->header, h_attr) < 0) { |
11deb1f9 ACM |
223 | perf_header_attr__delete(h_attr); |
224 | h_attr = NULL; | |
225 | } | |
7c6a1c65 PZ |
226 | } |
227 | ||
228 | return h_attr; | |
229 | } | |
230 | ||
69aad6f1 | 231 | static void create_counter(struct perf_evsel *evsel, int cpu) |
de9ac07b | 232 | { |
69aad6f1 ACM |
233 | char *filter = evsel->filter; |
234 | struct perf_event_attr *attr = &evsel->attr; | |
7c6a1c65 | 235 | struct perf_header_attr *h_attr; |
69aad6f1 | 236 | int track = !evsel->idx; /* only the first counter needs these */ |
d6d901c2 | 237 | int thread_index; |
c171b552 | 238 | int ret; |
7c6a1c65 PZ |
239 | struct { |
240 | u64 count; | |
241 | u64 time_enabled; | |
242 | u64 time_running; | |
243 | u64 id; | |
244 | } read_data; | |
a43d3f08 ACM |
245 | /* |
246 | * Check if parse_single_tracepoint_event has already asked for | |
247 | * PERF_SAMPLE_TIME. | |
248 | * | |
249 | * XXX this is kludgy but short term fix for problems introduced by | |
250 | * eac23d1c that broke 'perf script' by having different sample_types | |
251 | * when using multiple tracepoint events when we use a perf binary | |
252 | * that tries to use sample_id_all on an older kernel. | |
253 | * | |
254 | * We need to move counter creation to perf_session, support | |
255 | * different sample_types, etc. | |
256 | */ | |
257 | bool time_needed = attr->sample_type & PERF_SAMPLE_TIME; | |
7c6a1c65 PZ |
258 | |
259 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | | |
260 | PERF_FORMAT_TOTAL_TIME_RUNNING | | |
261 | PERF_FORMAT_ID; | |
16c8a109 | 262 | |
3a9f131f | 263 | attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID; |
3efa1cc9 | 264 | |
8907fd60 EM |
265 | if (nr_counters > 1) |
266 | attr->sample_type |= PERF_SAMPLE_ID; | |
267 | ||
f9212819 FW |
268 | /* |
269 | * We default some events to a 1 default interval. But keep | |
270 | * it a weak assumption overridable by the user. | |
271 | */ | |
272 | if (!attr->sample_period || (user_freq != UINT_MAX && | |
3de29cab | 273 | user_interval != ULLONG_MAX)) { |
f9212819 FW |
274 | if (freq) { |
275 | attr->sample_type |= PERF_SAMPLE_PERIOD; | |
276 | attr->freq = 1; | |
277 | attr->sample_freq = freq; | |
278 | } else { | |
279 | attr->sample_period = default_interval; | |
280 | } | |
1dba15e7 | 281 | } |
3efa1cc9 | 282 | |
649c48a9 PZ |
283 | if (no_samples) |
284 | attr->sample_freq = 0; | |
285 | ||
286 | if (inherit_stat) | |
287 | attr->inherit_stat = 1; | |
288 | ||
3af9e859 | 289 | if (sample_address) { |
4bba828d | 290 | attr->sample_type |= PERF_SAMPLE_ADDR; |
3af9e859 EM |
291 | attr->mmap_data = track; |
292 | } | |
4bba828d | 293 | |
3efa1cc9 IM |
294 | if (call_graph) |
295 | attr->sample_type |= PERF_SAMPLE_CALLCHAIN; | |
296 | ||
f60f3593 AS |
297 | if (system_wide) |
298 | attr->sample_type |= PERF_SAMPLE_CPU; | |
299 | ||
a43d3f08 ACM |
300 | if (sample_id_all_avail && |
301 | (sample_time || system_wide || !no_inherit || cpu_list)) | |
9c90a61c ACM |
302 | attr->sample_type |= PERF_SAMPLE_TIME; |
303 | ||
cd6feeea | 304 | if (raw_samples) { |
6ddf259d | 305 | attr->sample_type |= PERF_SAMPLE_TIME; |
daac07b2 | 306 | attr->sample_type |= PERF_SAMPLE_RAW; |
cd6feeea IM |
307 | attr->sample_type |= PERF_SAMPLE_CPU; |
308 | } | |
f413cdb8 | 309 | |
a21ca2ca IM |
310 | attr->mmap = track; |
311 | attr->comm = track; | |
2e6cdf99 SE |
312 | attr->inherit = !no_inherit; |
313 | if (target_pid == -1 && target_tid == -1 && !system_wide) { | |
46be604b | 314 | attr->disabled = 1; |
bedbfdea | 315 | attr->enable_on_exec = 1; |
46be604b | 316 | } |
9c90a61c ACM |
317 | retry_sample_id: |
318 | attr->sample_id_all = sample_id_all_avail ? 1 : 0; | |
bedbfdea | 319 | |
5c98d466 | 320 | for (thread_index = 0; thread_index < threads->nr; thread_index++) { |
3da297a6 | 321 | try_again: |
5c98d466 | 322 | FD(evsel, nr_cpu, thread_index) = sys_perf_event_open(attr, threads->map[thread_index], cpu, group_fd, 0); |
d6d901c2 | 323 | |
69aad6f1 | 324 | if (FD(evsel, nr_cpu, thread_index) < 0) { |
d6d901c2 ZY |
325 | int err = errno; |
326 | ||
327 | if (err == EPERM || err == EACCES) | |
328 | die("Permission error - are you root?\n" | |
329 | "\t Consider tweaking" | |
330 | " /proc/sys/kernel/perf_event_paranoid.\n"); | |
c45c6ea2 | 331 | else if (err == ENODEV && cpu_list) { |
d6d901c2 ZY |
332 | die("No such device - did you specify" |
333 | " an out-of-range profile CPU?\n"); | |
9c90a61c ACM |
334 | } else if (err == EINVAL && sample_id_all_avail) { |
335 | /* | |
336 | * Old kernel, no attr->sample_id_type_all field | |
337 | */ | |
338 | sample_id_all_avail = false; | |
a43d3f08 | 339 | if (!sample_time && !raw_samples && !time_needed) |
eac23d1c IM |
340 | attr->sample_type &= ~PERF_SAMPLE_TIME; |
341 | ||
9c90a61c | 342 | goto retry_sample_id; |
d6d901c2 | 343 | } |
3da297a6 | 344 | |
d6d901c2 ZY |
345 | /* |
346 | * If it's cycles then fall back to hrtimer | |
347 | * based cpu-clock-tick sw counter, which | |
348 | * is always available even if no PMU support: | |
349 | */ | |
350 | if (attr->type == PERF_TYPE_HARDWARE | |
351 | && attr->config == PERF_COUNT_HW_CPU_CYCLES) { | |
352 | ||
353 | if (verbose) | |
354 | warning(" ... trying to fall back to cpu-clock-ticks\n"); | |
355 | attr->type = PERF_TYPE_SOFTWARE; | |
356 | attr->config = PERF_COUNT_SW_CPU_CLOCK; | |
357 | goto try_again; | |
358 | } | |
359 | printf("\n"); | |
d9cf837e | 360 | error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n", |
69aad6f1 | 361 | FD(evsel, nr_cpu, thread_index), strerror(err)); |
bfd45118 SK |
362 | |
363 | #if defined(__i386__) || defined(__x86_64__) | |
d6d901c2 ZY |
364 | if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) |
365 | die("No hardware sampling interrupt available." | |
366 | " No APIC? If so then you can boot the kernel" | |
367 | " with the \"lapic\" boot parameter to" | |
368 | " force-enable it.\n"); | |
bfd45118 SK |
369 | #endif |
370 | ||
d6d901c2 ZY |
371 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
372 | exit(-1); | |
373 | } | |
3da297a6 | 374 | |
69aad6f1 | 375 | h_attr = get_header_attr(attr, evsel->idx); |
d6d901c2 ZY |
376 | if (h_attr == NULL) |
377 | die("nomem\n"); | |
7c6a1c65 | 378 | |
d6d901c2 ZY |
379 | if (!file_new) { |
380 | if (memcmp(&h_attr->attr, attr, sizeof(*attr))) { | |
381 | fprintf(stderr, "incompatible append\n"); | |
382 | exit(-1); | |
383 | } | |
7c6a1c65 | 384 | } |
7c6a1c65 | 385 | |
69aad6f1 | 386 | if (read(FD(evsel, nr_cpu, thread_index), &read_data, sizeof(read_data)) == -1) { |
0ab7368f | 387 | perror("Unable to read perf file descriptor"); |
d6d901c2 ZY |
388 | exit(-1); |
389 | } | |
7c6a1c65 | 390 | |
d6d901c2 ZY |
391 | if (perf_header_attr__add_id(h_attr, read_data.id) < 0) { |
392 | pr_warning("Not enough memory to add id\n"); | |
393 | exit(-1); | |
394 | } | |
7c6a1c65 | 395 | |
69aad6f1 ACM |
396 | assert(FD(evsel, nr_cpu, thread_index) >= 0); |
397 | fcntl(FD(evsel, nr_cpu, thread_index), F_SETFL, O_NONBLOCK); | |
16c8a109 | 398 | |
d6d901c2 ZY |
399 | /* |
400 | * First counter acts as the group leader: | |
401 | */ | |
402 | if (group && group_fd == -1) | |
69aad6f1 ACM |
403 | group_fd = FD(evsel, nr_cpu, thread_index); |
404 | ||
405 | if (evsel->idx || thread_index) { | |
406 | struct perf_evsel *first; | |
407 | first = list_entry(evsel_list.next, struct perf_evsel, node); | |
408 | ret = ioctl(FD(evsel, nr_cpu, thread_index), | |
409 | PERF_EVENT_IOC_SET_OUTPUT, | |
410 | FD(first, nr_cpu, 0)); | |
0e2e63dd PZ |
411 | if (ret) { |
412 | error("failed to set output: %d (%s)\n", errno, | |
413 | strerror(errno)); | |
414 | exit(-1); | |
415 | } | |
416 | } else { | |
0e2e63dd PZ |
417 | mmap_array[nr_cpu].prev = 0; |
418 | mmap_array[nr_cpu].mask = mmap_pages*page_size - 1; | |
419 | mmap_array[nr_cpu].base = mmap(NULL, (mmap_pages+1)*page_size, | |
69aad6f1 | 420 | PROT_READ | PROT_WRITE, MAP_SHARED, FD(evsel, nr_cpu, thread_index), 0); |
0e2e63dd PZ |
421 | if (mmap_array[nr_cpu].base == MAP_FAILED) { |
422 | error("failed to mmap with %d (%s)\n", errno, strerror(errno)); | |
423 | exit(-1); | |
424 | } | |
425 | ||
69aad6f1 | 426 | event_array[nr_poll].fd = FD(evsel, nr_cpu, thread_index); |
0e2e63dd PZ |
427 | event_array[nr_poll].events = POLLIN; |
428 | nr_poll++; | |
ea57c4f5 | 429 | } |
d1302522 | 430 | |
d6d901c2 | 431 | if (filter != NULL) { |
69aad6f1 ACM |
432 | ret = ioctl(FD(evsel, nr_cpu, thread_index), |
433 | PERF_EVENT_IOC_SET_FILTER, filter); | |
d6d901c2 ZY |
434 | if (ret) { |
435 | error("failed to set filter with %d (%s)\n", errno, | |
436 | strerror(errno)); | |
437 | exit(-1); | |
438 | } | |
c171b552 LZ |
439 | } |
440 | } | |
a43d3f08 ACM |
441 | |
442 | if (!sample_type) | |
443 | sample_type = attr->sample_type; | |
f250c030 | 444 | } |
f2521b6e | 445 | |
d6d901c2 | 446 | static void open_counters(int cpu) |
f250c030 | 447 | { |
69aad6f1 | 448 | struct perf_evsel *pos; |
16c8a109 | 449 | |
f250c030 | 450 | group_fd = -1; |
69aad6f1 ACM |
451 | |
452 | list_for_each_entry(pos, &evsel_list, node) | |
453 | create_counter(pos, cpu); | |
f250c030 | 454 | |
16c8a109 PZ |
455 | nr_cpu++; |
456 | } | |
457 | ||
6122e4e4 ACM |
458 | static int process_buildids(void) |
459 | { | |
460 | u64 size = lseek(output, 0, SEEK_CUR); | |
461 | ||
9f591fd7 ACM |
462 | if (size == 0) |
463 | return 0; | |
464 | ||
6122e4e4 ACM |
465 | session->fd = output; |
466 | return __perf_session__process_events(session, post_processing_offset, | |
467 | size - post_processing_offset, | |
468 | size, &build_id__mark_dso_hit_ops); | |
469 | } | |
470 | ||
f5970550 PZ |
471 | static void atexit_header(void) |
472 | { | |
c7929e47 TZ |
473 | if (!pipe_output) { |
474 | session->header.data_size += bytes_written; | |
f5970550 | 475 | |
baa2f6ce ACM |
476 | if (!no_buildid) |
477 | process_buildids(); | |
c7929e47 | 478 | perf_header__write(&session->header, output, true); |
39d17dac | 479 | perf_session__delete(session); |
bd3bfe9e | 480 | perf_evsel_list__delete(); |
d65a458b | 481 | symbol__exit(); |
c7929e47 | 482 | } |
f5970550 PZ |
483 | } |
484 | ||
23346f21 | 485 | static void event__synthesize_guest_os(struct machine *machine, void *data) |
a1645ce1 ZY |
486 | { |
487 | int err; | |
23346f21 | 488 | struct perf_session *psession = data; |
a1645ce1 | 489 | |
23346f21 | 490 | if (machine__is_host(machine)) |
a1645ce1 ZY |
491 | return; |
492 | ||
493 | /* | |
494 | *As for guest kernel when processing subcommand record&report, | |
495 | *we arrange module mmap prior to guest kernel mmap and trigger | |
496 | *a preload dso because default guest module symbols are loaded | |
497 | *from guest kallsyms instead of /lib/modules/XXX/XXX. This | |
498 | *method is used to avoid symbol missing when the first addr is | |
499 | *in module instead of in guest kernel. | |
500 | */ | |
501 | err = event__synthesize_modules(process_synthesized_event, | |
23346f21 | 502 | psession, machine); |
a1645ce1 ZY |
503 | if (err < 0) |
504 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 505 | " relocation symbol.\n", machine->pid); |
a1645ce1 | 506 | |
a1645ce1 ZY |
507 | /* |
508 | * We use _stext for guest kernel because guest kernel's /proc/kallsyms | |
509 | * have no _text sometimes. | |
510 | */ | |
511 | err = event__synthesize_kernel_mmap(process_synthesized_event, | |
23346f21 | 512 | psession, machine, "_text"); |
a1645ce1 ZY |
513 | if (err < 0) |
514 | err = event__synthesize_kernel_mmap(process_synthesized_event, | |
23346f21 | 515 | psession, machine, "_stext"); |
a1645ce1 ZY |
516 | if (err < 0) |
517 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 518 | " relocation symbol.\n", machine->pid); |
a1645ce1 ZY |
519 | } |
520 | ||
98402807 FW |
521 | static struct perf_event_header finished_round_event = { |
522 | .size = sizeof(struct perf_event_header), | |
523 | .type = PERF_RECORD_FINISHED_ROUND, | |
524 | }; | |
525 | ||
526 | static void mmap_read_all(void) | |
527 | { | |
0e2e63dd | 528 | int i; |
98402807 FW |
529 | |
530 | for (i = 0; i < nr_cpu; i++) { | |
0e2e63dd PZ |
531 | if (mmap_array[i].base) |
532 | mmap_read(&mmap_array[i]); | |
98402807 FW |
533 | } |
534 | ||
535 | if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO)) | |
536 | write_output(&finished_round_event, sizeof(finished_round_event)); | |
537 | } | |
538 | ||
d4db3f16 | 539 | static int __cmd_record(int argc, const char **argv) |
16c8a109 | 540 | { |
69aad6f1 | 541 | int i; |
abaff32a | 542 | struct stat st; |
abaff32a | 543 | int flags; |
4dc0a04b | 544 | int err; |
8b412664 | 545 | unsigned long waking = 0; |
856e9660 | 546 | int child_ready_pipe[2], go_pipe[2]; |
46be604b | 547 | const bool forks = argc > 0; |
856e9660 | 548 | char buf; |
23346f21 | 549 | struct machine *machine; |
de9ac07b PZ |
550 | |
551 | page_size = sysconf(_SC_PAGE_SIZE); | |
de9ac07b | 552 | |
f5970550 PZ |
553 | atexit(sig_atexit); |
554 | signal(SIGCHLD, sig_handler); | |
555 | signal(SIGINT, sig_handler); | |
18483b81 | 556 | signal(SIGUSR1, sig_handler); |
f5970550 | 557 | |
d4db3f16 | 558 | if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { |
856e9660 PZ |
559 | perror("failed to create pipes"); |
560 | exit(-1); | |
561 | } | |
562 | ||
529870e3 TZ |
563 | if (!strcmp(output_name, "-")) |
564 | pipe_output = 1; | |
565 | else if (!stat(output_name, &st) && st.st_size) { | |
7865e817 | 566 | if (write_mode == WRITE_FORCE) { |
b38d3464 ACM |
567 | char oldname[PATH_MAX]; |
568 | snprintf(oldname, sizeof(oldname), "%s.old", | |
569 | output_name); | |
570 | unlink(oldname); | |
571 | rename(output_name, oldname); | |
266e0e21 | 572 | } |
7865e817 FW |
573 | } else if (write_mode == WRITE_APPEND) { |
574 | write_mode = WRITE_FORCE; | |
97124d5e PZ |
575 | } |
576 | ||
f887f301 | 577 | flags = O_CREAT|O_RDWR; |
7865e817 | 578 | if (write_mode == WRITE_APPEND) |
f5970550 | 579 | file_new = 0; |
abaff32a IM |
580 | else |
581 | flags |= O_TRUNC; | |
582 | ||
529870e3 TZ |
583 | if (pipe_output) |
584 | output = STDOUT_FILENO; | |
585 | else | |
586 | output = open(output_name, flags, S_IRUSR | S_IWUSR); | |
de9ac07b PZ |
587 | if (output < 0) { |
588 | perror("failed to create output file"); | |
589 | exit(-1); | |
590 | } | |
591 | ||
7865e817 | 592 | session = perf_session__new(output_name, O_WRONLY, |
21ef97f0 | 593 | write_mode == WRITE_FORCE, false, NULL); |
94c744b6 | 594 | if (session == NULL) { |
a9a70bbc ACM |
595 | pr_err("Not enough memory for reading perf file header\n"); |
596 | return -1; | |
597 | } | |
598 | ||
baa2f6ce ACM |
599 | if (!no_buildid) |
600 | perf_header__set_feat(&session->header, HEADER_BUILD_ID); | |
601 | ||
4dc0a04b | 602 | if (!file_new) { |
8dc58101 | 603 | err = perf_header__read(session, output); |
4dc0a04b | 604 | if (err < 0) |
39d17dac | 605 | goto out_delete_session; |
4dc0a04b ACM |
606 | } |
607 | ||
69aad6f1 | 608 | if (have_tracepoints(&evsel_list)) |
94c744b6 | 609 | perf_header__set_feat(&session->header, HEADER_TRACE_INFO); |
03456a15 | 610 | |
39d17dac ACM |
611 | /* |
612 | * perf_session__delete(session) will be called at atexit_header() | |
613 | */ | |
f5970550 PZ |
614 | atexit(atexit_header); |
615 | ||
d4db3f16 | 616 | if (forks) { |
46be604b | 617 | child_pid = fork(); |
2fb750e8 | 618 | if (child_pid < 0) { |
856e9660 PZ |
619 | perror("failed to fork"); |
620 | exit(-1); | |
621 | } | |
7c6a1c65 | 622 | |
46be604b | 623 | if (!child_pid) { |
529870e3 TZ |
624 | if (pipe_output) |
625 | dup2(2, 1); | |
856e9660 PZ |
626 | close(child_ready_pipe[0]); |
627 | close(go_pipe[1]); | |
628 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); | |
629 | ||
630 | /* | |
631 | * Do a dummy execvp to get the PLT entry resolved, | |
632 | * so we avoid the resolver overhead on the real | |
633 | * execvp call. | |
634 | */ | |
635 | execvp("", (char **)argv); | |
636 | ||
637 | /* | |
638 | * Tell the parent we're ready to go | |
639 | */ | |
640 | close(child_ready_pipe[1]); | |
641 | ||
642 | /* | |
643 | * Wait until the parent tells us to go. | |
644 | */ | |
645 | if (read(go_pipe[0], &buf, 1) == -1) | |
646 | perror("unable to read pipe"); | |
647 | ||
648 | execvp(argv[0], (char **)argv); | |
649 | ||
650 | perror(argv[0]); | |
18483b81 | 651 | kill(getppid(), SIGUSR1); |
856e9660 | 652 | exit(-1); |
0a5ac846 | 653 | } |
856e9660 | 654 | |
d6d901c2 | 655 | if (!system_wide && target_tid == -1 && target_pid == -1) |
5c98d466 | 656 | threads->map[0] = child_pid; |
d6d901c2 | 657 | |
856e9660 PZ |
658 | close(child_ready_pipe[1]); |
659 | close(go_pipe[0]); | |
660 | /* | |
661 | * wait for child to settle | |
662 | */ | |
663 | if (read(child_ready_pipe[0], &buf, 1) == -1) { | |
664 | perror("unable to read pipe"); | |
665 | exit(-1); | |
666 | } | |
667 | close(child_ready_pipe[0]); | |
668 | } | |
669 | ||
c45c6ea2 SE |
670 | if (!system_wide && no_inherit && !cpu_list) { |
671 | open_counters(-1); | |
856e9660 | 672 | } else { |
60d567e2 ACM |
673 | for (i = 0; i < cpus->nr; i++) |
674 | open_counters(cpus->map[i]); | |
0a5ac846 | 675 | } |
de9ac07b | 676 | |
640c03ce ACM |
677 | perf_session__set_sample_type(session, sample_type); |
678 | ||
529870e3 TZ |
679 | if (pipe_output) { |
680 | err = perf_header__write_pipe(output); | |
681 | if (err < 0) | |
682 | return err; | |
683 | } else if (file_new) { | |
94c744b6 | 684 | err = perf_header__write(&session->header, output, false); |
d5eed904 ACM |
685 | if (err < 0) |
686 | return err; | |
56b03f3c ACM |
687 | } |
688 | ||
6122e4e4 ACM |
689 | post_processing_offset = lseek(output, 0, SEEK_CUR); |
690 | ||
9c90a61c ACM |
691 | perf_session__set_sample_id_all(session, sample_id_all_avail); |
692 | ||
2c46dbb5 TZ |
693 | if (pipe_output) { |
694 | err = event__synthesize_attrs(&session->header, | |
695 | process_synthesized_event, | |
696 | session); | |
697 | if (err < 0) { | |
698 | pr_err("Couldn't synthesize attrs.\n"); | |
699 | return err; | |
700 | } | |
cd19a035 TZ |
701 | |
702 | err = event__synthesize_event_types(process_synthesized_event, | |
703 | session); | |
704 | if (err < 0) { | |
705 | pr_err("Couldn't synthesize event_types.\n"); | |
706 | return err; | |
707 | } | |
9215545e | 708 | |
69aad6f1 | 709 | if (have_tracepoints(&evsel_list)) { |
63e0c771 TZ |
710 | /* |
711 | * FIXME err <= 0 here actually means that | |
712 | * there were no tracepoints so its not really | |
713 | * an error, just that we don't need to | |
714 | * synthesize anything. We really have to | |
715 | * return this more properly and also | |
716 | * propagate errors that now are calling die() | |
717 | */ | |
69aad6f1 | 718 | err = event__synthesize_tracing_data(output, &evsel_list, |
63e0c771 TZ |
719 | process_synthesized_event, |
720 | session); | |
721 | if (err <= 0) { | |
722 | pr_err("Couldn't record tracing data.\n"); | |
723 | return err; | |
724 | } | |
2c9faa06 | 725 | advance_output(err); |
63e0c771 | 726 | } |
2c46dbb5 TZ |
727 | } |
728 | ||
23346f21 ACM |
729 | machine = perf_session__find_host_machine(session); |
730 | if (!machine) { | |
a1645ce1 ZY |
731 | pr_err("Couldn't find native kernel information.\n"); |
732 | return -1; | |
733 | } | |
734 | ||
56b03f3c | 735 | err = event__synthesize_kernel_mmap(process_synthesized_event, |
23346f21 | 736 | session, machine, "_text"); |
70162138 ACM |
737 | if (err < 0) |
738 | err = event__synthesize_kernel_mmap(process_synthesized_event, | |
23346f21 | 739 | session, machine, "_stext"); |
c1a3a4b9 ACM |
740 | if (err < 0) |
741 | pr_err("Couldn't record kernel reference relocation symbol\n" | |
742 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
743 | "Check /proc/kallsyms permission or run as root.\n"); | |
b7cece76 | 744 | |
a1645ce1 | 745 | err = event__synthesize_modules(process_synthesized_event, |
23346f21 | 746 | session, machine); |
c1a3a4b9 ACM |
747 | if (err < 0) |
748 | pr_err("Couldn't record kernel module information.\n" | |
749 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
750 | "Check /proc/modules permission or run as root.\n"); | |
751 | ||
a1645ce1 | 752 | if (perf_guest) |
23346f21 | 753 | perf_session__process_machines(session, event__synthesize_guest_os); |
7c6a1c65 | 754 | |
cf103a14 | 755 | if (!system_wide) |
d6d901c2 | 756 | event__synthesize_thread(target_tid, process_synthesized_event, |
d8f66248 | 757 | session); |
234fbbf5 | 758 | else |
d8f66248 | 759 | event__synthesize_threads(process_synthesized_event, session); |
7c6a1c65 | 760 | |
de9ac07b PZ |
761 | if (realtime_prio) { |
762 | struct sched_param param; | |
763 | ||
764 | param.sched_priority = realtime_prio; | |
765 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { | |
6beba7ad | 766 | pr_err("Could not set realtime priority.\n"); |
de9ac07b PZ |
767 | exit(-1); |
768 | } | |
769 | } | |
770 | ||
856e9660 PZ |
771 | /* |
772 | * Let the child rip | |
773 | */ | |
d4db3f16 ACM |
774 | if (forks) |
775 | close(go_pipe[1]); | |
856e9660 | 776 | |
649c48a9 | 777 | for (;;) { |
2debbc83 | 778 | int hits = samples; |
d6d901c2 | 779 | int thread; |
de9ac07b | 780 | |
98402807 | 781 | mmap_read_all(); |
de9ac07b | 782 | |
649c48a9 PZ |
783 | if (hits == samples) { |
784 | if (done) | |
785 | break; | |
4dc0a04b | 786 | err = poll(event_array, nr_poll, -1); |
8b412664 PZ |
787 | waking++; |
788 | } | |
789 | ||
790 | if (done) { | |
791 | for (i = 0; i < nr_cpu; i++) { | |
69aad6f1 ACM |
792 | struct perf_evsel *pos; |
793 | ||
794 | list_for_each_entry(pos, &evsel_list, node) { | |
d6d901c2 | 795 | for (thread = 0; |
5c98d466 | 796 | thread < threads->nr; |
d6d901c2 | 797 | thread++) |
69aad6f1 | 798 | ioctl(FD(pos, i, thread), |
d6d901c2 ZY |
799 | PERF_EVENT_IOC_DISABLE); |
800 | } | |
8b412664 | 801 | } |
649c48a9 | 802 | } |
de9ac07b PZ |
803 | } |
804 | ||
18483b81 | 805 | if (quiet || signr == SIGUSR1) |
b44308f5 ACM |
806 | return 0; |
807 | ||
8b412664 PZ |
808 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); |
809 | ||
021e9f47 IM |
810 | /* |
811 | * Approximate RIP event size: 24 bytes. | |
812 | */ | |
813 | fprintf(stderr, | |
2debbc83 | 814 | "[ perf record: Captured and wrote %.3f MB %s (~%lld samples) ]\n", |
021e9f47 IM |
815 | (double)bytes_written / 1024.0 / 1024.0, |
816 | output_name, | |
817 | bytes_written / 24); | |
addc2785 | 818 | |
de9ac07b | 819 | return 0; |
39d17dac ACM |
820 | |
821 | out_delete_session: | |
822 | perf_session__delete(session); | |
823 | return err; | |
de9ac07b | 824 | } |
0e9b20b8 | 825 | |
0e9b20b8 | 826 | static const char * const record_usage[] = { |
9e096753 MG |
827 | "perf record [<options>] [<command>]", |
828 | "perf record [<options>] -- <command> [<options>]", | |
0e9b20b8 IM |
829 | NULL |
830 | }; | |
831 | ||
7865e817 FW |
832 | static bool force, append_file; |
833 | ||
bca647aa | 834 | const struct option record_options[] = { |
0e9b20b8 | 835 | OPT_CALLBACK('e', "event", NULL, "event", |
86847b62 TG |
836 | "event selector. use 'perf list' to list available events", |
837 | parse_events), | |
c171b552 LZ |
838 | OPT_CALLBACK(0, "filter", NULL, "filter", |
839 | "event filter", parse_filter), | |
0e9b20b8 | 840 | OPT_INTEGER('p', "pid", &target_pid, |
d6d901c2 ZY |
841 | "record events on existing process id"), |
842 | OPT_INTEGER('t', "tid", &target_tid, | |
843 | "record events on existing thread id"), | |
0e9b20b8 IM |
844 | OPT_INTEGER('r', "realtime", &realtime_prio, |
845 | "collect data with this RT SCHED_FIFO priority"), | |
daac07b2 FW |
846 | OPT_BOOLEAN('R', "raw-samples", &raw_samples, |
847 | "collect raw sample records from all opened counters"), | |
0e9b20b8 IM |
848 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
849 | "system-wide collection from all CPUs"), | |
abaff32a IM |
850 | OPT_BOOLEAN('A', "append", &append_file, |
851 | "append to the output file to do incremental profiling"), | |
c45c6ea2 SE |
852 | OPT_STRING('C', "cpu", &cpu_list, "cpu", |
853 | "list of cpus to monitor"), | |
97124d5e | 854 | OPT_BOOLEAN('f', "force", &force, |
7865e817 | 855 | "overwrite existing data file (deprecated)"), |
3de29cab | 856 | OPT_U64('c', "count", &user_interval, "event period to sample"), |
abaff32a IM |
857 | OPT_STRING('o', "output", &output_name, "file", |
858 | "output file name"), | |
2e6cdf99 SE |
859 | OPT_BOOLEAN('i', "no-inherit", &no_inherit, |
860 | "child tasks do not inherit counters"), | |
1967936d ACM |
861 | OPT_UINTEGER('F', "freq", &user_freq, "profile at this frequency"), |
862 | OPT_UINTEGER('m', "mmap-pages", &mmap_pages, "number of mmap data pages"), | |
3efa1cc9 IM |
863 | OPT_BOOLEAN('g', "call-graph", &call_graph, |
864 | "do call-graph (stack chain/backtrace) recording"), | |
c0555642 | 865 | OPT_INCR('v', "verbose", &verbose, |
3da297a6 | 866 | "be more verbose (show counter open errors, etc)"), |
b44308f5 | 867 | OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), |
649c48a9 PZ |
868 | OPT_BOOLEAN('s', "stat", &inherit_stat, |
869 | "per thread counts"), | |
4bba828d AB |
870 | OPT_BOOLEAN('d', "data", &sample_address, |
871 | "Sample addresses"), | |
9c90a61c | 872 | OPT_BOOLEAN('T', "timestamp", &sample_time, "Sample timestamps"), |
649c48a9 PZ |
873 | OPT_BOOLEAN('n', "no-samples", &no_samples, |
874 | "don't sample"), | |
baa2f6ce | 875 | OPT_BOOLEAN('N', "no-buildid-cache", &no_buildid_cache, |
a1ac1d3c | 876 | "do not update the buildid cache"), |
baa2f6ce ACM |
877 | OPT_BOOLEAN('B', "no-buildid", &no_buildid, |
878 | "do not collect buildids in perf.data"), | |
0e9b20b8 IM |
879 | OPT_END() |
880 | }; | |
881 | ||
f37a291c | 882 | int cmd_record(int argc, const char **argv, const char *prefix __used) |
0e9b20b8 | 883 | { |
69aad6f1 ACM |
884 | int err = -ENOMEM; |
885 | struct perf_evsel *pos; | |
0e9b20b8 | 886 | |
bca647aa | 887 | argc = parse_options(argc, argv, record_options, record_usage, |
655000e7 | 888 | PARSE_OPT_STOP_AT_NON_OPTION); |
d6d901c2 | 889 | if (!argc && target_pid == -1 && target_tid == -1 && |
c45c6ea2 | 890 | !system_wide && !cpu_list) |
bca647aa | 891 | usage_with_options(record_usage, record_options); |
0e9b20b8 | 892 | |
7865e817 FW |
893 | if (force && append_file) { |
894 | fprintf(stderr, "Can't overwrite and append at the same time." | |
895 | " You need to choose between -f and -A"); | |
bca647aa | 896 | usage_with_options(record_usage, record_options); |
7865e817 FW |
897 | } else if (append_file) { |
898 | write_mode = WRITE_APPEND; | |
899 | } else { | |
900 | write_mode = WRITE_FORCE; | |
901 | } | |
902 | ||
655000e7 | 903 | symbol__init(); |
baa2f6ce ACM |
904 | |
905 | if (no_buildid_cache || no_buildid) | |
a1ac1d3c | 906 | disable_buildid_cache(); |
655000e7 | 907 | |
69aad6f1 ACM |
908 | if (list_empty(&evsel_list) && perf_evsel_list__create_default() < 0) { |
909 | pr_err("Not enough memory for event selector list\n"); | |
910 | goto out_symbol_exit; | |
bbd36e5e | 911 | } |
0e9b20b8 | 912 | |
5c98d466 | 913 | if (target_pid != -1) |
d6d901c2 | 914 | target_tid = target_pid; |
d6d901c2 | 915 | |
5c98d466 ACM |
916 | threads = thread_map__new(target_pid, target_tid); |
917 | if (threads == NULL) { | |
918 | pr_err("Problems finding threads of monitor\n"); | |
919 | usage_with_options(record_usage, record_options); | |
d6d901c2 ZY |
920 | } |
921 | ||
60d567e2 ACM |
922 | cpus = cpu_map__new(cpu_list); |
923 | if (cpus == NULL) { | |
924 | perror("failed to parse CPUs map"); | |
69aad6f1 ACM |
925 | return -1; |
926 | } | |
927 | ||
928 | list_for_each_entry(pos, &evsel_list, node) { | |
5c98d466 | 929 | if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0) |
69aad6f1 | 930 | goto out_free_fd; |
d6d901c2 | 931 | } |
5c98d466 ACM |
932 | event_array = malloc((sizeof(struct pollfd) * MAX_NR_CPUS * |
933 | MAX_COUNTERS * threads->nr)); | |
d6d901c2 | 934 | if (!event_array) |
39d17dac | 935 | goto out_free_fd; |
d6d901c2 | 936 | |
3de29cab | 937 | if (user_interval != ULLONG_MAX) |
f9212819 FW |
938 | default_interval = user_interval; |
939 | if (user_freq != UINT_MAX) | |
940 | freq = user_freq; | |
941 | ||
7e4ff9e3 MG |
942 | /* |
943 | * User specified count overrides default frequency. | |
944 | */ | |
945 | if (default_interval) | |
946 | freq = 0; | |
947 | else if (freq) { | |
948 | default_interval = freq; | |
949 | } else { | |
950 | fprintf(stderr, "frequency and count are zero, aborting\n"); | |
39d17dac ACM |
951 | err = -EINVAL; |
952 | goto out_free_event_array; | |
7e4ff9e3 MG |
953 | } |
954 | ||
39d17dac ACM |
955 | err = __cmd_record(argc, argv); |
956 | ||
957 | out_free_event_array: | |
958 | free(event_array); | |
959 | out_free_fd: | |
5c98d466 ACM |
960 | thread_map__delete(threads); |
961 | threads = NULL; | |
d65a458b ACM |
962 | out_symbol_exit: |
963 | symbol__exit(); | |
39d17dac | 964 | return err; |
0e9b20b8 | 965 | } |