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