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 | */ |
16f762a2 | 8 | #include "builtin.h" |
bf9e1876 IM |
9 | |
10 | #include "perf.h" | |
11 | ||
6122e4e4 | 12 | #include "util/build-id.h" |
6eda5838 | 13 | #include "util/util.h" |
4b6ab94e | 14 | #include <subcmd/parse-options.h> |
8ad8db37 | 15 | #include "util/parse-events.h" |
6eda5838 | 16 | |
8f651eae | 17 | #include "util/callchain.h" |
f14d5707 | 18 | #include "util/cgroup.h" |
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" |
45694aa7 | 25 | #include "util/tool.h" |
8d06367f | 26 | #include "util/symbol.h" |
a12b51c4 | 27 | #include "util/cpumap.h" |
fd78260b | 28 | #include "util/thread_map.h" |
f5fc1412 | 29 | #include "util/data.h" |
bcc84ec6 | 30 | #include "util/perf_regs.h" |
ef149c25 | 31 | #include "util/auxtrace.h" |
46bc29b9 | 32 | #include "util/tsc.h" |
f00898f4 | 33 | #include "util/parse-branch-options.h" |
bcc84ec6 | 34 | #include "util/parse-regs-options.h" |
71dc2326 | 35 | #include "util/llvm-utils.h" |
8690a2a7 | 36 | #include "util/bpf-loader.h" |
5f9cf599 | 37 | #include "util/trigger.h" |
d8871ea7 | 38 | #include "asm/bug.h" |
7c6a1c65 | 39 | |
97124d5e | 40 | #include <unistd.h> |
de9ac07b | 41 | #include <sched.h> |
a41794cd | 42 | #include <sys/mman.h> |
de9ac07b | 43 | |
78da39fa | 44 | |
8c6f45a7 | 45 | struct record { |
45694aa7 | 46 | struct perf_tool tool; |
b4006796 | 47 | struct record_opts opts; |
d20deb64 | 48 | u64 bytes_written; |
f5fc1412 | 49 | struct perf_data_file file; |
ef149c25 | 50 | struct auxtrace_record *itr; |
d20deb64 ACM |
51 | struct perf_evlist *evlist; |
52 | struct perf_session *session; | |
53 | const char *progname; | |
d20deb64 | 54 | int realtime_prio; |
d20deb64 | 55 | bool no_buildid; |
d2db9a98 | 56 | bool no_buildid_set; |
d20deb64 | 57 | bool no_buildid_cache; |
d2db9a98 | 58 | bool no_buildid_cache_set; |
6156681b | 59 | bool buildid_all; |
ecfd7a9c | 60 | bool timestamp_filename; |
3c1cb7e3 | 61 | bool switch_output; |
9f065194 | 62 | unsigned long long samples; |
0f82ebc4 | 63 | }; |
a21ca2ca | 64 | |
8c6f45a7 | 65 | static int record__write(struct record *rec, void *bf, size_t size) |
f5970550 | 66 | { |
cf8b2e69 | 67 | if (perf_data_file__write(rec->session->file, bf, size) < 0) { |
50a9b868 JO |
68 | pr_err("failed to write perf data, error: %m\n"); |
69 | return -1; | |
f5970550 | 70 | } |
8d3eca20 | 71 | |
cf8b2e69 | 72 | rec->bytes_written += size; |
8d3eca20 | 73 | return 0; |
f5970550 PZ |
74 | } |
75 | ||
45694aa7 | 76 | static int process_synthesized_event(struct perf_tool *tool, |
d20deb64 | 77 | union perf_event *event, |
1d037ca1 IT |
78 | struct perf_sample *sample __maybe_unused, |
79 | struct machine *machine __maybe_unused) | |
234fbbf5 | 80 | { |
8c6f45a7 ACM |
81 | struct record *rec = container_of(tool, struct record, tool); |
82 | return record__write(rec, event, event->header.size); | |
234fbbf5 ACM |
83 | } |
84 | ||
e5685730 | 85 | static int record__mmap_read(struct record *rec, int idx) |
de9ac07b | 86 | { |
e5685730 | 87 | struct perf_mmap *md = &rec->evlist->mmap[idx]; |
7b8283b5 DA |
88 | u64 head = perf_mmap__read_head(md); |
89 | u64 old = md->prev; | |
918512b4 | 90 | unsigned char *data = md->base + page_size; |
de9ac07b PZ |
91 | unsigned long size; |
92 | void *buf; | |
8d3eca20 | 93 | int rc = 0; |
de9ac07b | 94 | |
dc82009a | 95 | if (old == head) |
8d3eca20 | 96 | return 0; |
dc82009a | 97 | |
d20deb64 | 98 | rec->samples++; |
de9ac07b PZ |
99 | |
100 | size = head - old; | |
101 | ||
102 | if ((old & md->mask) + size != (head & md->mask)) { | |
103 | buf = &data[old & md->mask]; | |
104 | size = md->mask + 1 - (old & md->mask); | |
105 | old += size; | |
021e9f47 | 106 | |
8c6f45a7 | 107 | if (record__write(rec, buf, size) < 0) { |
8d3eca20 DA |
108 | rc = -1; |
109 | goto out; | |
110 | } | |
de9ac07b PZ |
111 | } |
112 | ||
113 | buf = &data[old & md->mask]; | |
114 | size = head - old; | |
115 | old += size; | |
021e9f47 | 116 | |
8c6f45a7 | 117 | if (record__write(rec, buf, size) < 0) { |
8d3eca20 DA |
118 | rc = -1; |
119 | goto out; | |
120 | } | |
de9ac07b PZ |
121 | |
122 | md->prev = old; | |
e5685730 | 123 | perf_evlist__mmap_consume(rec->evlist, idx); |
8d3eca20 DA |
124 | out: |
125 | return rc; | |
de9ac07b PZ |
126 | } |
127 | ||
2dd6d8a1 AH |
128 | static volatile int done; |
129 | static volatile int signr = -1; | |
130 | static volatile int child_finished; | |
c0bdc1c4 | 131 | |
2dd6d8a1 | 132 | static volatile int auxtrace_record__snapshot_started; |
5f9cf599 | 133 | static DEFINE_TRIGGER(auxtrace_snapshot_trigger); |
3c1cb7e3 | 134 | static DEFINE_TRIGGER(switch_output_trigger); |
2dd6d8a1 AH |
135 | |
136 | static void sig_handler(int sig) | |
137 | { | |
138 | if (sig == SIGCHLD) | |
139 | child_finished = 1; | |
140 | else | |
141 | signr = sig; | |
142 | ||
143 | done = 1; | |
144 | } | |
145 | ||
146 | static void record__sig_exit(void) | |
147 | { | |
148 | if (signr == -1) | |
149 | return; | |
150 | ||
151 | signal(signr, SIG_DFL); | |
152 | raise(signr); | |
153 | } | |
154 | ||
e31f0d01 AH |
155 | #ifdef HAVE_AUXTRACE_SUPPORT |
156 | ||
ef149c25 AH |
157 | static int record__process_auxtrace(struct perf_tool *tool, |
158 | union perf_event *event, void *data1, | |
159 | size_t len1, void *data2, size_t len2) | |
160 | { | |
161 | struct record *rec = container_of(tool, struct record, tool); | |
99fa2984 | 162 | struct perf_data_file *file = &rec->file; |
ef149c25 AH |
163 | size_t padding; |
164 | u8 pad[8] = {0}; | |
165 | ||
99fa2984 AH |
166 | if (!perf_data_file__is_pipe(file)) { |
167 | off_t file_offset; | |
168 | int fd = perf_data_file__fd(file); | |
169 | int err; | |
170 | ||
171 | file_offset = lseek(fd, 0, SEEK_CUR); | |
172 | if (file_offset == -1) | |
173 | return -1; | |
174 | err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, | |
175 | event, file_offset); | |
176 | if (err) | |
177 | return err; | |
178 | } | |
179 | ||
ef149c25 AH |
180 | /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ |
181 | padding = (len1 + len2) & 7; | |
182 | if (padding) | |
183 | padding = 8 - padding; | |
184 | ||
185 | record__write(rec, event, event->header.size); | |
186 | record__write(rec, data1, len1); | |
187 | if (len2) | |
188 | record__write(rec, data2, len2); | |
189 | record__write(rec, &pad, padding); | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
194 | static int record__auxtrace_mmap_read(struct record *rec, | |
195 | struct auxtrace_mmap *mm) | |
196 | { | |
197 | int ret; | |
198 | ||
199 | ret = auxtrace_mmap__read(mm, rec->itr, &rec->tool, | |
200 | record__process_auxtrace); | |
201 | if (ret < 0) | |
202 | return ret; | |
203 | ||
204 | if (ret) | |
205 | rec->samples++; | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
2dd6d8a1 AH |
210 | static int record__auxtrace_mmap_read_snapshot(struct record *rec, |
211 | struct auxtrace_mmap *mm) | |
212 | { | |
213 | int ret; | |
214 | ||
215 | ret = auxtrace_mmap__read_snapshot(mm, rec->itr, &rec->tool, | |
216 | record__process_auxtrace, | |
217 | rec->opts.auxtrace_snapshot_size); | |
218 | if (ret < 0) | |
219 | return ret; | |
220 | ||
221 | if (ret) | |
222 | rec->samples++; | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
227 | static int record__auxtrace_read_snapshot_all(struct record *rec) | |
228 | { | |
229 | int i; | |
230 | int rc = 0; | |
231 | ||
232 | for (i = 0; i < rec->evlist->nr_mmaps; i++) { | |
233 | struct auxtrace_mmap *mm = | |
234 | &rec->evlist->mmap[i].auxtrace_mmap; | |
235 | ||
236 | if (!mm->base) | |
237 | continue; | |
238 | ||
239 | if (record__auxtrace_mmap_read_snapshot(rec, mm) != 0) { | |
240 | rc = -1; | |
241 | goto out; | |
242 | } | |
243 | } | |
244 | out: | |
245 | return rc; | |
246 | } | |
247 | ||
248 | static void record__read_auxtrace_snapshot(struct record *rec) | |
249 | { | |
250 | pr_debug("Recording AUX area tracing snapshot\n"); | |
251 | if (record__auxtrace_read_snapshot_all(rec) < 0) { | |
5f9cf599 | 252 | trigger_error(&auxtrace_snapshot_trigger); |
2dd6d8a1 | 253 | } else { |
5f9cf599 WN |
254 | if (auxtrace_record__snapshot_finish(rec->itr)) |
255 | trigger_error(&auxtrace_snapshot_trigger); | |
256 | else | |
257 | trigger_ready(&auxtrace_snapshot_trigger); | |
2dd6d8a1 AH |
258 | } |
259 | } | |
260 | ||
e31f0d01 AH |
261 | #else |
262 | ||
263 | static inline | |
264 | int record__auxtrace_mmap_read(struct record *rec __maybe_unused, | |
265 | struct auxtrace_mmap *mm __maybe_unused) | |
266 | { | |
267 | return 0; | |
268 | } | |
269 | ||
2dd6d8a1 AH |
270 | static inline |
271 | void record__read_auxtrace_snapshot(struct record *rec __maybe_unused) | |
de9ac07b | 272 | { |
f7b7c26e PZ |
273 | } |
274 | ||
2dd6d8a1 AH |
275 | static inline |
276 | int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) | |
f7b7c26e | 277 | { |
2dd6d8a1 | 278 | return 0; |
de9ac07b PZ |
279 | } |
280 | ||
2dd6d8a1 AH |
281 | #endif |
282 | ||
8c6f45a7 | 283 | static int record__open(struct record *rec) |
dd7927f4 | 284 | { |
56e52e85 | 285 | char msg[512]; |
6a4bb04c | 286 | struct perf_evsel *pos; |
d20deb64 ACM |
287 | struct perf_evlist *evlist = rec->evlist; |
288 | struct perf_session *session = rec->session; | |
b4006796 | 289 | struct record_opts *opts = &rec->opts; |
8d3eca20 | 290 | int rc = 0; |
dd7927f4 | 291 | |
e68ae9cf | 292 | perf_evlist__config(evlist, opts, &callchain_param); |
cac21425 | 293 | |
0050f7aa | 294 | evlist__for_each(evlist, pos) { |
dd7927f4 | 295 | try_again: |
d988d5ee | 296 | if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) { |
56e52e85 | 297 | if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) { |
d6d901c2 | 298 | if (verbose) |
c0a54341 | 299 | ui__warning("%s\n", msg); |
d6d901c2 ZY |
300 | goto try_again; |
301 | } | |
ca6a4258 | 302 | |
56e52e85 ACM |
303 | rc = -errno; |
304 | perf_evsel__open_strerror(pos, &opts->target, | |
305 | errno, msg, sizeof(msg)); | |
306 | ui__error("%s\n", msg); | |
8d3eca20 | 307 | goto out; |
c171b552 LZ |
308 | } |
309 | } | |
a43d3f08 | 310 | |
23d4aad4 ACM |
311 | if (perf_evlist__apply_filters(evlist, &pos)) { |
312 | error("failed to set filter \"%s\" on event %s with %d (%s)\n", | |
313 | pos->filter, perf_evsel__name(pos), errno, | |
35550da3 | 314 | strerror_r(errno, msg, sizeof(msg))); |
8d3eca20 DA |
315 | rc = -1; |
316 | goto out; | |
0a102479 FW |
317 | } |
318 | ||
ef149c25 | 319 | if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false, |
2dd6d8a1 AH |
320 | opts->auxtrace_mmap_pages, |
321 | opts->auxtrace_snapshot_mode) < 0) { | |
8d3eca20 DA |
322 | if (errno == EPERM) { |
323 | pr_err("Permission error mapping pages.\n" | |
324 | "Consider increasing " | |
325 | "/proc/sys/kernel/perf_event_mlock_kb,\n" | |
326 | "or try again with a smaller value of -m/--mmap_pages.\n" | |
ef149c25 AH |
327 | "(current value: %u,%u)\n", |
328 | opts->mmap_pages, opts->auxtrace_mmap_pages); | |
8d3eca20 | 329 | rc = -errno; |
8d3eca20 | 330 | } else { |
35550da3 MH |
331 | pr_err("failed to mmap with %d (%s)\n", errno, |
332 | strerror_r(errno, msg, sizeof(msg))); | |
95c36561 WN |
333 | if (errno) |
334 | rc = -errno; | |
335 | else | |
336 | rc = -EINVAL; | |
8d3eca20 DA |
337 | } |
338 | goto out; | |
18e60939 | 339 | } |
0a27d7f9 | 340 | |
563aecb2 | 341 | session->evlist = evlist; |
7b56cce2 | 342 | perf_session__set_id_hdr_size(session); |
8d3eca20 DA |
343 | out: |
344 | return rc; | |
16c8a109 PZ |
345 | } |
346 | ||
e3d59112 NK |
347 | static int process_sample_event(struct perf_tool *tool, |
348 | union perf_event *event, | |
349 | struct perf_sample *sample, | |
350 | struct perf_evsel *evsel, | |
351 | struct machine *machine) | |
352 | { | |
353 | struct record *rec = container_of(tool, struct record, tool); | |
354 | ||
355 | rec->samples++; | |
356 | ||
357 | return build_id__mark_dso_hit(tool, event, sample, evsel, machine); | |
358 | } | |
359 | ||
8c6f45a7 | 360 | static int process_buildids(struct record *rec) |
6122e4e4 | 361 | { |
f5fc1412 JO |
362 | struct perf_data_file *file = &rec->file; |
363 | struct perf_session *session = rec->session; | |
6122e4e4 | 364 | |
457ae94a | 365 | if (file->size == 0) |
9f591fd7 ACM |
366 | return 0; |
367 | ||
00dc8657 NK |
368 | /* |
369 | * During this process, it'll load kernel map and replace the | |
370 | * dso->long_name to a real pathname it found. In this case | |
371 | * we prefer the vmlinux path like | |
372 | * /lib/modules/3.16.4/build/vmlinux | |
373 | * | |
374 | * rather than build-id path (in debug directory). | |
375 | * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 | |
376 | */ | |
377 | symbol_conf.ignore_vmlinux_buildid = true; | |
378 | ||
6156681b NK |
379 | /* |
380 | * If --buildid-all is given, it marks all DSO regardless of hits, | |
381 | * so no need to process samples. | |
382 | */ | |
383 | if (rec->buildid_all) | |
384 | rec->tool.sample = NULL; | |
385 | ||
b7b61cbe | 386 | return perf_session__process_events(session); |
6122e4e4 ACM |
387 | } |
388 | ||
8115d60c | 389 | static void perf_event__synthesize_guest_os(struct machine *machine, void *data) |
a1645ce1 ZY |
390 | { |
391 | int err; | |
45694aa7 | 392 | struct perf_tool *tool = data; |
a1645ce1 ZY |
393 | /* |
394 | *As for guest kernel when processing subcommand record&report, | |
395 | *we arrange module mmap prior to guest kernel mmap and trigger | |
396 | *a preload dso because default guest module symbols are loaded | |
397 | *from guest kallsyms instead of /lib/modules/XXX/XXX. This | |
398 | *method is used to avoid symbol missing when the first addr is | |
399 | *in module instead of in guest kernel. | |
400 | */ | |
45694aa7 | 401 | err = perf_event__synthesize_modules(tool, process_synthesized_event, |
743eb868 | 402 | machine); |
a1645ce1 ZY |
403 | if (err < 0) |
404 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 405 | " relocation symbol.\n", machine->pid); |
a1645ce1 | 406 | |
a1645ce1 ZY |
407 | /* |
408 | * We use _stext for guest kernel because guest kernel's /proc/kallsyms | |
409 | * have no _text sometimes. | |
410 | */ | |
45694aa7 | 411 | err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, |
0ae617be | 412 | machine); |
a1645ce1 ZY |
413 | if (err < 0) |
414 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 415 | " relocation symbol.\n", machine->pid); |
a1645ce1 ZY |
416 | } |
417 | ||
98402807 FW |
418 | static struct perf_event_header finished_round_event = { |
419 | .size = sizeof(struct perf_event_header), | |
420 | .type = PERF_RECORD_FINISHED_ROUND, | |
421 | }; | |
422 | ||
8c6f45a7 | 423 | static int record__mmap_read_all(struct record *rec) |
98402807 | 424 | { |
dcabb507 | 425 | u64 bytes_written = rec->bytes_written; |
0e2e63dd | 426 | int i; |
8d3eca20 | 427 | int rc = 0; |
98402807 | 428 | |
d20deb64 | 429 | for (i = 0; i < rec->evlist->nr_mmaps; i++) { |
ef149c25 AH |
430 | struct auxtrace_mmap *mm = &rec->evlist->mmap[i].auxtrace_mmap; |
431 | ||
8d3eca20 | 432 | if (rec->evlist->mmap[i].base) { |
e5685730 | 433 | if (record__mmap_read(rec, i) != 0) { |
8d3eca20 DA |
434 | rc = -1; |
435 | goto out; | |
436 | } | |
437 | } | |
ef149c25 | 438 | |
2dd6d8a1 | 439 | if (mm->base && !rec->opts.auxtrace_snapshot_mode && |
ef149c25 AH |
440 | record__auxtrace_mmap_read(rec, mm) != 0) { |
441 | rc = -1; | |
442 | goto out; | |
443 | } | |
98402807 FW |
444 | } |
445 | ||
dcabb507 JO |
446 | /* |
447 | * Mark the round finished in case we wrote | |
448 | * at least one event. | |
449 | */ | |
450 | if (bytes_written != rec->bytes_written) | |
451 | rc = record__write(rec, &finished_round_event, sizeof(finished_round_event)); | |
8d3eca20 DA |
452 | |
453 | out: | |
454 | return rc; | |
98402807 FW |
455 | } |
456 | ||
8c6f45a7 | 457 | static void record__init_features(struct record *rec) |
57706abc | 458 | { |
57706abc DA |
459 | struct perf_session *session = rec->session; |
460 | int feat; | |
461 | ||
462 | for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) | |
463 | perf_header__set_feat(&session->header, feat); | |
464 | ||
465 | if (rec->no_buildid) | |
466 | perf_header__clear_feat(&session->header, HEADER_BUILD_ID); | |
467 | ||
3e2be2da | 468 | if (!have_tracepoints(&rec->evlist->entries)) |
57706abc DA |
469 | perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); |
470 | ||
471 | if (!rec->opts.branch_stack) | |
472 | perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); | |
ef149c25 AH |
473 | |
474 | if (!rec->opts.full_auxtrace) | |
475 | perf_header__clear_feat(&session->header, HEADER_AUXTRACE); | |
ffa517ad JO |
476 | |
477 | perf_header__clear_feat(&session->header, HEADER_STAT); | |
57706abc DA |
478 | } |
479 | ||
e1ab48ba WN |
480 | static void |
481 | record__finish_output(struct record *rec) | |
482 | { | |
483 | struct perf_data_file *file = &rec->file; | |
484 | int fd = perf_data_file__fd(file); | |
485 | ||
486 | if (file->is_pipe) | |
487 | return; | |
488 | ||
489 | rec->session->header.data_size += rec->bytes_written; | |
490 | file->size = lseek(perf_data_file__fd(file), 0, SEEK_CUR); | |
491 | ||
492 | if (!rec->no_buildid) { | |
493 | process_buildids(rec); | |
494 | ||
495 | if (rec->buildid_all) | |
496 | dsos__hit_all(rec->session); | |
497 | } | |
498 | perf_session__write_header(rec->session, rec->evlist, fd, true); | |
499 | ||
500 | return; | |
501 | } | |
502 | ||
3c1cb7e3 WN |
503 | static int record__synthesize(struct record *rec); |
504 | ||
ecfd7a9c WN |
505 | static int |
506 | record__switch_output(struct record *rec, bool at_exit) | |
507 | { | |
508 | struct perf_data_file *file = &rec->file; | |
509 | int fd, err; | |
510 | ||
511 | /* Same Size: "2015122520103046"*/ | |
512 | char timestamp[] = "InvalidTimestamp"; | |
513 | ||
514 | rec->samples = 0; | |
515 | record__finish_output(rec); | |
516 | err = fetch_current_timestamp(timestamp, sizeof(timestamp)); | |
517 | if (err) { | |
518 | pr_err("Failed to get current timestamp\n"); | |
519 | return -EINVAL; | |
520 | } | |
521 | ||
522 | fd = perf_data_file__switch(file, timestamp, | |
523 | rec->session->header.data_offset, | |
524 | at_exit); | |
525 | if (fd >= 0 && !at_exit) { | |
526 | rec->bytes_written = 0; | |
527 | rec->session->header.data_size = 0; | |
528 | } | |
529 | ||
530 | if (!quiet) | |
531 | fprintf(stderr, "[ perf record: Dump %s.%s ]\n", | |
532 | file->path, timestamp); | |
3c1cb7e3 WN |
533 | |
534 | /* Output tracking events */ | |
535 | if (!at_exit) | |
536 | record__synthesize(rec); | |
537 | ||
ecfd7a9c WN |
538 | return fd; |
539 | } | |
540 | ||
f33cbe72 ACM |
541 | static volatile int workload_exec_errno; |
542 | ||
543 | /* | |
544 | * perf_evlist__prepare_workload will send a SIGUSR1 | |
545 | * if the fork fails, since we asked by setting its | |
546 | * want_signal to true. | |
547 | */ | |
45604710 NK |
548 | static void workload_exec_failed_signal(int signo __maybe_unused, |
549 | siginfo_t *info, | |
f33cbe72 ACM |
550 | void *ucontext __maybe_unused) |
551 | { | |
552 | workload_exec_errno = info->si_value.sival_int; | |
553 | done = 1; | |
f33cbe72 ACM |
554 | child_finished = 1; |
555 | } | |
556 | ||
2dd6d8a1 AH |
557 | static void snapshot_sig_handler(int sig); |
558 | ||
46bc29b9 AH |
559 | int __weak |
560 | perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused, | |
561 | struct perf_tool *tool __maybe_unused, | |
562 | perf_event__handler_t process __maybe_unused, | |
563 | struct machine *machine __maybe_unused) | |
564 | { | |
565 | return 0; | |
566 | } | |
567 | ||
c45c86eb WN |
568 | static int record__synthesize(struct record *rec) |
569 | { | |
570 | struct perf_session *session = rec->session; | |
571 | struct machine *machine = &session->machines.host; | |
572 | struct perf_data_file *file = &rec->file; | |
573 | struct record_opts *opts = &rec->opts; | |
574 | struct perf_tool *tool = &rec->tool; | |
575 | int fd = perf_data_file__fd(file); | |
576 | int err = 0; | |
577 | ||
578 | if (file->is_pipe) { | |
579 | err = perf_event__synthesize_attrs(tool, session, | |
580 | process_synthesized_event); | |
581 | if (err < 0) { | |
582 | pr_err("Couldn't synthesize attrs.\n"); | |
583 | goto out; | |
584 | } | |
585 | ||
586 | if (have_tracepoints(&rec->evlist->entries)) { | |
587 | /* | |
588 | * FIXME err <= 0 here actually means that | |
589 | * there were no tracepoints so its not really | |
590 | * an error, just that we don't need to | |
591 | * synthesize anything. We really have to | |
592 | * return this more properly and also | |
593 | * propagate errors that now are calling die() | |
594 | */ | |
595 | err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist, | |
596 | process_synthesized_event); | |
597 | if (err <= 0) { | |
598 | pr_err("Couldn't record tracing data.\n"); | |
599 | goto out; | |
600 | } | |
601 | rec->bytes_written += err; | |
602 | } | |
603 | } | |
604 | ||
46bc29b9 AH |
605 | err = perf_event__synth_time_conv(rec->evlist->mmap[0].base, tool, |
606 | process_synthesized_event, machine); | |
607 | if (err) | |
608 | goto out; | |
609 | ||
c45c86eb WN |
610 | if (rec->opts.full_auxtrace) { |
611 | err = perf_event__synthesize_auxtrace_info(rec->itr, tool, | |
612 | session, process_synthesized_event); | |
613 | if (err) | |
614 | goto out; | |
615 | } | |
616 | ||
617 | err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, | |
618 | machine); | |
619 | WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" | |
620 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
621 | "Check /proc/kallsyms permission or run as root.\n"); | |
622 | ||
623 | err = perf_event__synthesize_modules(tool, process_synthesized_event, | |
624 | machine); | |
625 | WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" | |
626 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
627 | "Check /proc/modules permission or run as root.\n"); | |
628 | ||
629 | if (perf_guest) { | |
630 | machines__process_guests(&session->machines, | |
631 | perf_event__synthesize_guest_os, tool); | |
632 | } | |
633 | ||
634 | err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads, | |
635 | process_synthesized_event, opts->sample_address, | |
636 | opts->proc_map_timeout); | |
637 | out: | |
638 | return err; | |
639 | } | |
640 | ||
8c6f45a7 | 641 | static int __cmd_record(struct record *rec, int argc, const char **argv) |
16c8a109 | 642 | { |
57706abc | 643 | int err; |
45604710 | 644 | int status = 0; |
8b412664 | 645 | unsigned long waking = 0; |
46be604b | 646 | const bool forks = argc > 0; |
23346f21 | 647 | struct machine *machine; |
45694aa7 | 648 | struct perf_tool *tool = &rec->tool; |
b4006796 | 649 | struct record_opts *opts = &rec->opts; |
f5fc1412 | 650 | struct perf_data_file *file = &rec->file; |
d20deb64 | 651 | struct perf_session *session; |
6dcf45ef | 652 | bool disabled = false, draining = false; |
42aa276f | 653 | int fd; |
de9ac07b | 654 | |
d20deb64 | 655 | rec->progname = argv[0]; |
33e49ea7 | 656 | |
45604710 | 657 | atexit(record__sig_exit); |
f5970550 PZ |
658 | signal(SIGCHLD, sig_handler); |
659 | signal(SIGINT, sig_handler); | |
804f7ac7 | 660 | signal(SIGTERM, sig_handler); |
c0bdc1c4 | 661 | |
3c1cb7e3 | 662 | if (rec->opts.auxtrace_snapshot_mode || rec->switch_output) { |
2dd6d8a1 | 663 | signal(SIGUSR2, snapshot_sig_handler); |
3c1cb7e3 WN |
664 | if (rec->opts.auxtrace_snapshot_mode) |
665 | trigger_on(&auxtrace_snapshot_trigger); | |
666 | if (rec->switch_output) | |
667 | trigger_on(&switch_output_trigger); | |
c0bdc1c4 | 668 | } else { |
2dd6d8a1 | 669 | signal(SIGUSR2, SIG_IGN); |
c0bdc1c4 | 670 | } |
f5970550 | 671 | |
b7b61cbe | 672 | session = perf_session__new(file, false, tool); |
94c744b6 | 673 | if (session == NULL) { |
ffa91880 | 674 | pr_err("Perf session creation failed.\n"); |
a9a70bbc ACM |
675 | return -1; |
676 | } | |
677 | ||
42aa276f | 678 | fd = perf_data_file__fd(file); |
d20deb64 ACM |
679 | rec->session = session; |
680 | ||
8c6f45a7 | 681 | record__init_features(rec); |
330aa675 | 682 | |
d4db3f16 | 683 | if (forks) { |
3e2be2da | 684 | err = perf_evlist__prepare_workload(rec->evlist, &opts->target, |
f5fc1412 | 685 | argv, file->is_pipe, |
735f7e0b | 686 | workload_exec_failed_signal); |
35b9d88e ACM |
687 | if (err < 0) { |
688 | pr_err("Couldn't run the workload!\n"); | |
45604710 | 689 | status = err; |
35b9d88e | 690 | goto out_delete_session; |
856e9660 | 691 | } |
856e9660 PZ |
692 | } |
693 | ||
8c6f45a7 | 694 | if (record__open(rec) != 0) { |
8d3eca20 | 695 | err = -1; |
45604710 | 696 | goto out_child; |
8d3eca20 | 697 | } |
de9ac07b | 698 | |
8690a2a7 WN |
699 | err = bpf__apply_obj_config(); |
700 | if (err) { | |
701 | char errbuf[BUFSIZ]; | |
702 | ||
703 | bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); | |
704 | pr_err("ERROR: Apply config to BPF failed: %s\n", | |
705 | errbuf); | |
706 | goto out_child; | |
707 | } | |
708 | ||
cca8482c AH |
709 | /* |
710 | * Normally perf_session__new would do this, but it doesn't have the | |
711 | * evlist. | |
712 | */ | |
713 | if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) { | |
714 | pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); | |
715 | rec->tool.ordered_events = false; | |
716 | } | |
717 | ||
3e2be2da | 718 | if (!rec->evlist->nr_groups) |
a8bb559b NK |
719 | perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); |
720 | ||
f5fc1412 | 721 | if (file->is_pipe) { |
42aa276f | 722 | err = perf_header__write_pipe(fd); |
529870e3 | 723 | if (err < 0) |
45604710 | 724 | goto out_child; |
563aecb2 | 725 | } else { |
42aa276f | 726 | err = perf_session__write_header(session, rec->evlist, fd, false); |
d5eed904 | 727 | if (err < 0) |
45604710 | 728 | goto out_child; |
56b03f3c ACM |
729 | } |
730 | ||
d3665498 | 731 | if (!rec->no_buildid |
e20960c0 | 732 | && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { |
d3665498 | 733 | pr_err("Couldn't generate buildids. " |
e20960c0 | 734 | "Use --no-buildid to profile anyway.\n"); |
8d3eca20 | 735 | err = -1; |
45604710 | 736 | goto out_child; |
e20960c0 RR |
737 | } |
738 | ||
34ba5122 | 739 | machine = &session->machines.host; |
743eb868 | 740 | |
c45c86eb WN |
741 | err = record__synthesize(rec); |
742 | if (err < 0) | |
45604710 | 743 | goto out_child; |
8d3eca20 | 744 | |
d20deb64 | 745 | if (rec->realtime_prio) { |
de9ac07b PZ |
746 | struct sched_param param; |
747 | ||
d20deb64 | 748 | param.sched_priority = rec->realtime_prio; |
de9ac07b | 749 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
6beba7ad | 750 | pr_err("Could not set realtime priority.\n"); |
8d3eca20 | 751 | err = -1; |
45604710 | 752 | goto out_child; |
de9ac07b PZ |
753 | } |
754 | } | |
755 | ||
774cb499 JO |
756 | /* |
757 | * When perf is starting the traced process, all the events | |
758 | * (apart from group members) have enable_on_exec=1 set, | |
759 | * so don't spoil it by prematurely enabling them. | |
760 | */ | |
6619a53e | 761 | if (!target__none(&opts->target) && !opts->initial_delay) |
3e2be2da | 762 | perf_evlist__enable(rec->evlist); |
764e16a3 | 763 | |
856e9660 PZ |
764 | /* |
765 | * Let the child rip | |
766 | */ | |
e803cf97 | 767 | if (forks) { |
e5bed564 NK |
768 | union perf_event *event; |
769 | ||
770 | event = malloc(sizeof(event->comm) + machine->id_hdr_size); | |
771 | if (event == NULL) { | |
772 | err = -ENOMEM; | |
773 | goto out_child; | |
774 | } | |
775 | ||
e803cf97 NK |
776 | /* |
777 | * Some H/W events are generated before COMM event | |
778 | * which is emitted during exec(), so perf script | |
779 | * cannot see a correct process name for those events. | |
780 | * Synthesize COMM event to prevent it. | |
781 | */ | |
e5bed564 | 782 | perf_event__synthesize_comm(tool, event, |
e803cf97 NK |
783 | rec->evlist->workload.pid, |
784 | process_synthesized_event, | |
785 | machine); | |
e5bed564 | 786 | free(event); |
e803cf97 | 787 | |
3e2be2da | 788 | perf_evlist__start_workload(rec->evlist); |
e803cf97 | 789 | } |
856e9660 | 790 | |
6619a53e AK |
791 | if (opts->initial_delay) { |
792 | usleep(opts->initial_delay * 1000); | |
793 | perf_evlist__enable(rec->evlist); | |
794 | } | |
795 | ||
5f9cf599 | 796 | trigger_ready(&auxtrace_snapshot_trigger); |
3c1cb7e3 | 797 | trigger_ready(&switch_output_trigger); |
649c48a9 | 798 | for (;;) { |
9f065194 | 799 | unsigned long long hits = rec->samples; |
de9ac07b | 800 | |
8c6f45a7 | 801 | if (record__mmap_read_all(rec) < 0) { |
5f9cf599 | 802 | trigger_error(&auxtrace_snapshot_trigger); |
3c1cb7e3 | 803 | trigger_error(&switch_output_trigger); |
8d3eca20 | 804 | err = -1; |
45604710 | 805 | goto out_child; |
8d3eca20 | 806 | } |
de9ac07b | 807 | |
2dd6d8a1 AH |
808 | if (auxtrace_record__snapshot_started) { |
809 | auxtrace_record__snapshot_started = 0; | |
5f9cf599 | 810 | if (!trigger_is_error(&auxtrace_snapshot_trigger)) |
2dd6d8a1 | 811 | record__read_auxtrace_snapshot(rec); |
5f9cf599 | 812 | if (trigger_is_error(&auxtrace_snapshot_trigger)) { |
2dd6d8a1 AH |
813 | pr_err("AUX area tracing snapshot failed\n"); |
814 | err = -1; | |
815 | goto out_child; | |
816 | } | |
817 | } | |
818 | ||
3c1cb7e3 WN |
819 | if (trigger_is_hit(&switch_output_trigger)) { |
820 | trigger_ready(&switch_output_trigger); | |
821 | ||
822 | if (!quiet) | |
823 | fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", | |
824 | waking); | |
825 | waking = 0; | |
826 | fd = record__switch_output(rec, false); | |
827 | if (fd < 0) { | |
828 | pr_err("Failed to switch to new file\n"); | |
829 | trigger_error(&switch_output_trigger); | |
830 | err = fd; | |
831 | goto out_child; | |
832 | } | |
833 | } | |
834 | ||
d20deb64 | 835 | if (hits == rec->samples) { |
6dcf45ef | 836 | if (done || draining) |
649c48a9 | 837 | break; |
f66a889d | 838 | err = perf_evlist__poll(rec->evlist, -1); |
a515114f JO |
839 | /* |
840 | * Propagate error, only if there's any. Ignore positive | |
841 | * number of returned events and interrupt error. | |
842 | */ | |
843 | if (err > 0 || (err < 0 && errno == EINTR)) | |
45604710 | 844 | err = 0; |
8b412664 | 845 | waking++; |
6dcf45ef ACM |
846 | |
847 | if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) | |
848 | draining = true; | |
8b412664 PZ |
849 | } |
850 | ||
774cb499 JO |
851 | /* |
852 | * When perf is starting the traced process, at the end events | |
853 | * die with the process and we wait for that. Thus no need to | |
854 | * disable events in this case. | |
855 | */ | |
602ad878 | 856 | if (done && !disabled && !target__none(&opts->target)) { |
5f9cf599 | 857 | trigger_off(&auxtrace_snapshot_trigger); |
3e2be2da | 858 | perf_evlist__disable(rec->evlist); |
2711926a JO |
859 | disabled = true; |
860 | } | |
de9ac07b | 861 | } |
5f9cf599 | 862 | trigger_off(&auxtrace_snapshot_trigger); |
3c1cb7e3 | 863 | trigger_off(&switch_output_trigger); |
de9ac07b | 864 | |
f33cbe72 | 865 | if (forks && workload_exec_errno) { |
35550da3 | 866 | char msg[STRERR_BUFSIZE]; |
f33cbe72 ACM |
867 | const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg)); |
868 | pr_err("Workload failed: %s\n", emsg); | |
869 | err = -1; | |
45604710 | 870 | goto out_child; |
f33cbe72 ACM |
871 | } |
872 | ||
e3d59112 | 873 | if (!quiet) |
45604710 | 874 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); |
b44308f5 | 875 | |
45604710 NK |
876 | out_child: |
877 | if (forks) { | |
878 | int exit_status; | |
addc2785 | 879 | |
45604710 NK |
880 | if (!child_finished) |
881 | kill(rec->evlist->workload.pid, SIGTERM); | |
882 | ||
883 | wait(&exit_status); | |
884 | ||
885 | if (err < 0) | |
886 | status = err; | |
887 | else if (WIFEXITED(exit_status)) | |
888 | status = WEXITSTATUS(exit_status); | |
889 | else if (WIFSIGNALED(exit_status)) | |
890 | signr = WTERMSIG(exit_status); | |
891 | } else | |
892 | status = err; | |
893 | ||
e3d59112 NK |
894 | /* this will be recalculated during process_buildids() */ |
895 | rec->samples = 0; | |
896 | ||
ecfd7a9c WN |
897 | if (!err) { |
898 | if (!rec->timestamp_filename) { | |
899 | record__finish_output(rec); | |
900 | } else { | |
901 | fd = record__switch_output(rec, true); | |
902 | if (fd < 0) { | |
903 | status = fd; | |
904 | goto out_delete_session; | |
905 | } | |
906 | } | |
907 | } | |
39d17dac | 908 | |
e3d59112 NK |
909 | if (!err && !quiet) { |
910 | char samples[128]; | |
ecfd7a9c WN |
911 | const char *postfix = rec->timestamp_filename ? |
912 | ".<timestamp>" : ""; | |
e3d59112 | 913 | |
ef149c25 | 914 | if (rec->samples && !rec->opts.full_auxtrace) |
e3d59112 NK |
915 | scnprintf(samples, sizeof(samples), |
916 | " (%" PRIu64 " samples)", rec->samples); | |
917 | else | |
918 | samples[0] = '\0'; | |
919 | ||
ecfd7a9c | 920 | fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s ]\n", |
e3d59112 | 921 | perf_data_file__size(file) / 1024.0 / 1024.0, |
ecfd7a9c | 922 | file->path, postfix, samples); |
e3d59112 NK |
923 | } |
924 | ||
39d17dac ACM |
925 | out_delete_session: |
926 | perf_session__delete(session); | |
45604710 | 927 | return status; |
de9ac07b | 928 | } |
0e9b20b8 | 929 | |
0883e820 | 930 | static void callchain_debug(struct callchain_param *callchain) |
09b0fd45 | 931 | { |
aad2b21c | 932 | static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; |
a601fdff | 933 | |
0883e820 | 934 | pr_debug("callchain: type %s\n", str[callchain->record_mode]); |
26d33022 | 935 | |
0883e820 | 936 | if (callchain->record_mode == CALLCHAIN_DWARF) |
09b0fd45 | 937 | pr_debug("callchain: stack dump size %d\n", |
0883e820 | 938 | callchain->dump_size); |
09b0fd45 JO |
939 | } |
940 | ||
0883e820 ACM |
941 | int record_opts__parse_callchain(struct record_opts *record, |
942 | struct callchain_param *callchain, | |
943 | const char *arg, bool unset) | |
09b0fd45 | 944 | { |
09b0fd45 | 945 | int ret; |
0883e820 | 946 | callchain->enabled = !unset; |
eb853e80 | 947 | |
09b0fd45 JO |
948 | /* --no-call-graph */ |
949 | if (unset) { | |
0883e820 | 950 | callchain->record_mode = CALLCHAIN_NONE; |
09b0fd45 JO |
951 | pr_debug("callchain: disabled\n"); |
952 | return 0; | |
953 | } | |
954 | ||
0883e820 | 955 | ret = parse_callchain_record_opt(arg, callchain); |
5c0cf224 JO |
956 | if (!ret) { |
957 | /* Enable data address sampling for DWARF unwind. */ | |
0883e820 | 958 | if (callchain->record_mode == CALLCHAIN_DWARF) |
5c0cf224 | 959 | record->sample_address = true; |
0883e820 | 960 | callchain_debug(callchain); |
5c0cf224 | 961 | } |
26d33022 JO |
962 | |
963 | return ret; | |
964 | } | |
965 | ||
0883e820 ACM |
966 | int record_parse_callchain_opt(const struct option *opt, |
967 | const char *arg, | |
968 | int unset) | |
969 | { | |
970 | return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); | |
971 | } | |
972 | ||
c421e80b | 973 | int record_callchain_opt(const struct option *opt, |
09b0fd45 JO |
974 | const char *arg __maybe_unused, |
975 | int unset __maybe_unused) | |
976 | { | |
2ddd5c04 | 977 | struct callchain_param *callchain = opt->value; |
c421e80b | 978 | |
2ddd5c04 | 979 | callchain->enabled = true; |
09b0fd45 | 980 | |
2ddd5c04 ACM |
981 | if (callchain->record_mode == CALLCHAIN_NONE) |
982 | callchain->record_mode = CALLCHAIN_FP; | |
eb853e80 | 983 | |
2ddd5c04 | 984 | callchain_debug(callchain); |
09b0fd45 JO |
985 | return 0; |
986 | } | |
987 | ||
eb853e80 JO |
988 | static int perf_record_config(const char *var, const char *value, void *cb) |
989 | { | |
7a29c087 NK |
990 | struct record *rec = cb; |
991 | ||
992 | if (!strcmp(var, "record.build-id")) { | |
993 | if (!strcmp(value, "cache")) | |
994 | rec->no_buildid_cache = false; | |
995 | else if (!strcmp(value, "no-cache")) | |
996 | rec->no_buildid_cache = true; | |
997 | else if (!strcmp(value, "skip")) | |
998 | rec->no_buildid = true; | |
999 | else | |
1000 | return -1; | |
1001 | return 0; | |
1002 | } | |
eb853e80 | 1003 | if (!strcmp(var, "record.call-graph")) |
5a2e5e85 | 1004 | var = "call-graph.record-mode"; /* fall-through */ |
eb853e80 JO |
1005 | |
1006 | return perf_default_config(var, value, cb); | |
1007 | } | |
1008 | ||
814c8c38 PZ |
1009 | struct clockid_map { |
1010 | const char *name; | |
1011 | int clockid; | |
1012 | }; | |
1013 | ||
1014 | #define CLOCKID_MAP(n, c) \ | |
1015 | { .name = n, .clockid = (c), } | |
1016 | ||
1017 | #define CLOCKID_END { .name = NULL, } | |
1018 | ||
1019 | ||
1020 | /* | |
1021 | * Add the missing ones, we need to build on many distros... | |
1022 | */ | |
1023 | #ifndef CLOCK_MONOTONIC_RAW | |
1024 | #define CLOCK_MONOTONIC_RAW 4 | |
1025 | #endif | |
1026 | #ifndef CLOCK_BOOTTIME | |
1027 | #define CLOCK_BOOTTIME 7 | |
1028 | #endif | |
1029 | #ifndef CLOCK_TAI | |
1030 | #define CLOCK_TAI 11 | |
1031 | #endif | |
1032 | ||
1033 | static const struct clockid_map clockids[] = { | |
1034 | /* available for all events, NMI safe */ | |
1035 | CLOCKID_MAP("monotonic", CLOCK_MONOTONIC), | |
1036 | CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW), | |
1037 | ||
1038 | /* available for some events */ | |
1039 | CLOCKID_MAP("realtime", CLOCK_REALTIME), | |
1040 | CLOCKID_MAP("boottime", CLOCK_BOOTTIME), | |
1041 | CLOCKID_MAP("tai", CLOCK_TAI), | |
1042 | ||
1043 | /* available for the lazy */ | |
1044 | CLOCKID_MAP("mono", CLOCK_MONOTONIC), | |
1045 | CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW), | |
1046 | CLOCKID_MAP("real", CLOCK_REALTIME), | |
1047 | CLOCKID_MAP("boot", CLOCK_BOOTTIME), | |
1048 | ||
1049 | CLOCKID_END, | |
1050 | }; | |
1051 | ||
1052 | static int parse_clockid(const struct option *opt, const char *str, int unset) | |
1053 | { | |
1054 | struct record_opts *opts = (struct record_opts *)opt->value; | |
1055 | const struct clockid_map *cm; | |
1056 | const char *ostr = str; | |
1057 | ||
1058 | if (unset) { | |
1059 | opts->use_clockid = 0; | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | /* no arg passed */ | |
1064 | if (!str) | |
1065 | return 0; | |
1066 | ||
1067 | /* no setting it twice */ | |
1068 | if (opts->use_clockid) | |
1069 | return -1; | |
1070 | ||
1071 | opts->use_clockid = true; | |
1072 | ||
1073 | /* if its a number, we're done */ | |
1074 | if (sscanf(str, "%d", &opts->clockid) == 1) | |
1075 | return 0; | |
1076 | ||
1077 | /* allow a "CLOCK_" prefix to the name */ | |
1078 | if (!strncasecmp(str, "CLOCK_", 6)) | |
1079 | str += 6; | |
1080 | ||
1081 | for (cm = clockids; cm->name; cm++) { | |
1082 | if (!strcasecmp(str, cm->name)) { | |
1083 | opts->clockid = cm->clockid; | |
1084 | return 0; | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | opts->use_clockid = false; | |
1089 | ui__warning("unknown clockid %s, check man page\n", ostr); | |
1090 | return -1; | |
1091 | } | |
1092 | ||
e9db1310 AH |
1093 | static int record__parse_mmap_pages(const struct option *opt, |
1094 | const char *str, | |
1095 | int unset __maybe_unused) | |
1096 | { | |
1097 | struct record_opts *opts = opt->value; | |
1098 | char *s, *p; | |
1099 | unsigned int mmap_pages; | |
1100 | int ret; | |
1101 | ||
1102 | if (!str) | |
1103 | return -EINVAL; | |
1104 | ||
1105 | s = strdup(str); | |
1106 | if (!s) | |
1107 | return -ENOMEM; | |
1108 | ||
1109 | p = strchr(s, ','); | |
1110 | if (p) | |
1111 | *p = '\0'; | |
1112 | ||
1113 | if (*s) { | |
1114 | ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s); | |
1115 | if (ret) | |
1116 | goto out_free; | |
1117 | opts->mmap_pages = mmap_pages; | |
1118 | } | |
1119 | ||
1120 | if (!p) { | |
1121 | ret = 0; | |
1122 | goto out_free; | |
1123 | } | |
1124 | ||
1125 | ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1); | |
1126 | if (ret) | |
1127 | goto out_free; | |
1128 | ||
1129 | opts->auxtrace_mmap_pages = mmap_pages; | |
1130 | ||
1131 | out_free: | |
1132 | free(s); | |
1133 | return ret; | |
1134 | } | |
1135 | ||
e5b2c207 | 1136 | static const char * const __record_usage[] = { |
9e096753 MG |
1137 | "perf record [<options>] [<command>]", |
1138 | "perf record [<options>] -- <command> [<options>]", | |
0e9b20b8 IM |
1139 | NULL |
1140 | }; | |
e5b2c207 | 1141 | const char * const *record_usage = __record_usage; |
0e9b20b8 | 1142 | |
d20deb64 | 1143 | /* |
8c6f45a7 ACM |
1144 | * XXX Ideally would be local to cmd_record() and passed to a record__new |
1145 | * because we need to have access to it in record__exit, that is called | |
d20deb64 ACM |
1146 | * after cmd_record() exits, but since record_options need to be accessible to |
1147 | * builtin-script, leave it here. | |
1148 | * | |
1149 | * At least we don't ouch it in all the other functions here directly. | |
1150 | * | |
1151 | * Just say no to tons of global variables, sigh. | |
1152 | */ | |
8c6f45a7 | 1153 | static struct record record = { |
d20deb64 | 1154 | .opts = { |
8affc2b8 | 1155 | .sample_time = true, |
d20deb64 ACM |
1156 | .mmap_pages = UINT_MAX, |
1157 | .user_freq = UINT_MAX, | |
1158 | .user_interval = ULLONG_MAX, | |
447a6013 | 1159 | .freq = 4000, |
d1cb9fce NK |
1160 | .target = { |
1161 | .uses_mmap = true, | |
3aa5939d | 1162 | .default_per_cpu = true, |
d1cb9fce | 1163 | }, |
9d9cad76 | 1164 | .proc_map_timeout = 500, |
d20deb64 | 1165 | }, |
e3d59112 NK |
1166 | .tool = { |
1167 | .sample = process_sample_event, | |
1168 | .fork = perf_event__process_fork, | |
cca8482c | 1169 | .exit = perf_event__process_exit, |
e3d59112 NK |
1170 | .comm = perf_event__process_comm, |
1171 | .mmap = perf_event__process_mmap, | |
1172 | .mmap2 = perf_event__process_mmap2, | |
cca8482c | 1173 | .ordered_events = true, |
e3d59112 | 1174 | }, |
d20deb64 | 1175 | }; |
7865e817 | 1176 | |
76a26549 NK |
1177 | const char record_callchain_help[] = CALLCHAIN_RECORD_HELP |
1178 | "\n\t\t\t\tDefault: fp"; | |
61eaa3be | 1179 | |
d20deb64 ACM |
1180 | /* |
1181 | * XXX Will stay a global variable till we fix builtin-script.c to stop messing | |
1182 | * with it and switch to use the library functions in perf_evlist that came | |
b4006796 | 1183 | * from builtin-record.c, i.e. use record_opts, |
d20deb64 ACM |
1184 | * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record', |
1185 | * using pipes, etc. | |
1186 | */ | |
e5b2c207 | 1187 | struct option __record_options[] = { |
d20deb64 | 1188 | OPT_CALLBACK('e', "event", &record.evlist, "event", |
86847b62 | 1189 | "event selector. use 'perf list' to list available events", |
f120f9d5 | 1190 | parse_events_option), |
d20deb64 | 1191 | OPT_CALLBACK(0, "filter", &record.evlist, "filter", |
c171b552 | 1192 | "event filter", parse_filter), |
4ba1faa1 WN |
1193 | OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, |
1194 | NULL, "don't record events from perf itself", | |
1195 | exclude_perf), | |
bea03405 | 1196 | OPT_STRING('p', "pid", &record.opts.target.pid, "pid", |
d6d901c2 | 1197 | "record events on existing process id"), |
bea03405 | 1198 | OPT_STRING('t', "tid", &record.opts.target.tid, "tid", |
d6d901c2 | 1199 | "record events on existing thread id"), |
d20deb64 | 1200 | OPT_INTEGER('r', "realtime", &record.realtime_prio, |
0e9b20b8 | 1201 | "collect data with this RT SCHED_FIFO priority"), |
509051ea | 1202 | OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, |
acac03fa | 1203 | "collect data without buffering"), |
d20deb64 | 1204 | OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, |
daac07b2 | 1205 | "collect raw sample records from all opened counters"), |
bea03405 | 1206 | OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, |
0e9b20b8 | 1207 | "system-wide collection from all CPUs"), |
bea03405 | 1208 | OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", |
c45c6ea2 | 1209 | "list of cpus to monitor"), |
d20deb64 | 1210 | OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), |
f5fc1412 | 1211 | OPT_STRING('o', "output", &record.file.path, "file", |
abaff32a | 1212 | "output file name"), |
69e7e5b0 AH |
1213 | OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, |
1214 | &record.opts.no_inherit_set, | |
1215 | "child tasks do not inherit counters"), | |
d20deb64 | 1216 | OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"), |
e9db1310 AH |
1217 | OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", |
1218 | "number of mmap data pages and AUX area tracing mmap pages", | |
1219 | record__parse_mmap_pages), | |
d20deb64 | 1220 | OPT_BOOLEAN(0, "group", &record.opts.group, |
43bece79 | 1221 | "put the counters into a counter group"), |
2ddd5c04 | 1222 | OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, |
09b0fd45 JO |
1223 | NULL, "enables call-graph recording" , |
1224 | &record_callchain_opt), | |
1225 | OPT_CALLBACK(0, "call-graph", &record.opts, | |
76a26549 | 1226 | "record_mode[,record_size]", record_callchain_help, |
09b0fd45 | 1227 | &record_parse_callchain_opt), |
c0555642 | 1228 | OPT_INCR('v', "verbose", &verbose, |
3da297a6 | 1229 | "be more verbose (show counter open errors, etc)"), |
b44308f5 | 1230 | OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), |
d20deb64 | 1231 | OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, |
649c48a9 | 1232 | "per thread counts"), |
56100321 | 1233 | OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), |
3abebc55 AH |
1234 | OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, |
1235 | &record.opts.sample_time_set, | |
1236 | "Record the sample timestamps"), | |
56100321 | 1237 | OPT_BOOLEAN('P', "period", &record.opts.period, "Record the sample period"), |
d20deb64 | 1238 | OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, |
649c48a9 | 1239 | "don't sample"), |
d2db9a98 WN |
1240 | OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, |
1241 | &record.no_buildid_cache_set, | |
1242 | "do not update the buildid cache"), | |
1243 | OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, | |
1244 | &record.no_buildid_set, | |
1245 | "do not collect buildids in perf.data"), | |
d20deb64 | 1246 | OPT_CALLBACK('G', "cgroup", &record.evlist, "name", |
023695d9 SE |
1247 | "monitor event in cgroup name only", |
1248 | parse_cgroups), | |
a6205a35 | 1249 | OPT_UINTEGER('D', "delay", &record.opts.initial_delay, |
6619a53e | 1250 | "ms to wait before starting measurement after program start"), |
bea03405 NK |
1251 | OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", |
1252 | "user to profile"), | |
a5aabdac SE |
1253 | |
1254 | OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, | |
1255 | "branch any", "sample any taken branches", | |
1256 | parse_branch_stack), | |
1257 | ||
1258 | OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, | |
1259 | "branch filter mask", "branch stack filter modes", | |
bdfebd84 | 1260 | parse_branch_stack), |
05484298 AK |
1261 | OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, |
1262 | "sample by weight (on special events only)"), | |
475eeab9 AK |
1263 | OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, |
1264 | "sample transaction flags (special events only)"), | |
3aa5939d AH |
1265 | OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, |
1266 | "use per-thread mmaps"), | |
bcc84ec6 SE |
1267 | OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", |
1268 | "sample selected machine registers on interrupt," | |
1269 | " use -I ? to list register names", parse_regs), | |
85c273d2 AK |
1270 | OPT_BOOLEAN(0, "running-time", &record.opts.running_time, |
1271 | "Record running/enabled time of read (:S) events"), | |
814c8c38 PZ |
1272 | OPT_CALLBACK('k', "clockid", &record.opts, |
1273 | "clockid", "clockid to use for events, see clock_gettime()", | |
1274 | parse_clockid), | |
2dd6d8a1 AH |
1275 | OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, |
1276 | "opts", "AUX area tracing Snapshot Mode", ""), | |
9d9cad76 KL |
1277 | OPT_UINTEGER(0, "proc-map-timeout", &record.opts.proc_map_timeout, |
1278 | "per thread proc mmap processing timeout in ms"), | |
b757bb09 AH |
1279 | OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events, |
1280 | "Record context switch events"), | |
85723885 JO |
1281 | OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, |
1282 | "Configure all used events to run in kernel space.", | |
1283 | PARSE_OPT_EXCLUSIVE), | |
1284 | OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, | |
1285 | "Configure all used events to run in user space.", | |
1286 | PARSE_OPT_EXCLUSIVE), | |
71dc2326 WN |
1287 | OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", |
1288 | "clang binary to use for compiling BPF scriptlets"), | |
1289 | OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", | |
1290 | "options passed to clang when compiling BPF scriptlets"), | |
7efe0e03 HK |
1291 | OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, |
1292 | "file", "vmlinux pathname"), | |
6156681b NK |
1293 | OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, |
1294 | "Record build-id of all DSOs regardless of hits"), | |
ecfd7a9c WN |
1295 | OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, |
1296 | "append timestamp to output filename"), | |
3c1cb7e3 WN |
1297 | OPT_BOOLEAN(0, "switch-output", &record.switch_output, |
1298 | "Switch output when receive SIGUSR2"), | |
0e9b20b8 IM |
1299 | OPT_END() |
1300 | }; | |
1301 | ||
e5b2c207 NK |
1302 | struct option *record_options = __record_options; |
1303 | ||
1d037ca1 | 1304 | int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused) |
0e9b20b8 | 1305 | { |
ef149c25 | 1306 | int err; |
8c6f45a7 | 1307 | struct record *rec = &record; |
16ad2ffb | 1308 | char errbuf[BUFSIZ]; |
0e9b20b8 | 1309 | |
48e1cab1 WN |
1310 | #ifndef HAVE_LIBBPF_SUPPORT |
1311 | # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) | |
1312 | set_nobuild('\0', "clang-path", true); | |
1313 | set_nobuild('\0', "clang-opt", true); | |
1314 | # undef set_nobuild | |
7efe0e03 HK |
1315 | #endif |
1316 | ||
1317 | #ifndef HAVE_BPF_PROLOGUE | |
1318 | # if !defined (HAVE_DWARF_SUPPORT) | |
1319 | # define REASON "NO_DWARF=1" | |
1320 | # elif !defined (HAVE_LIBBPF_SUPPORT) | |
1321 | # define REASON "NO_LIBBPF=1" | |
1322 | # else | |
1323 | # define REASON "this architecture doesn't support BPF prologue" | |
1324 | # endif | |
1325 | # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) | |
1326 | set_nobuild('\0', "vmlinux", true); | |
1327 | # undef set_nobuild | |
1328 | # undef REASON | |
48e1cab1 WN |
1329 | #endif |
1330 | ||
3e2be2da ACM |
1331 | rec->evlist = perf_evlist__new(); |
1332 | if (rec->evlist == NULL) | |
361c99a6 ACM |
1333 | return -ENOMEM; |
1334 | ||
eb853e80 JO |
1335 | perf_config(perf_record_config, rec); |
1336 | ||
bca647aa | 1337 | argc = parse_options(argc, argv, record_options, record_usage, |
655000e7 | 1338 | PARSE_OPT_STOP_AT_NON_OPTION); |
602ad878 | 1339 | if (!argc && target__none(&rec->opts.target)) |
bca647aa | 1340 | usage_with_options(record_usage, record_options); |
0e9b20b8 | 1341 | |
bea03405 | 1342 | if (nr_cgroups && !rec->opts.target.system_wide) { |
c7118369 NK |
1343 | usage_with_options_msg(record_usage, record_options, |
1344 | "cgroup monitoring only available in system-wide mode"); | |
1345 | ||
023695d9 | 1346 | } |
b757bb09 AH |
1347 | if (rec->opts.record_switch_events && |
1348 | !perf_can_record_switch_events()) { | |
c7118369 NK |
1349 | ui__error("kernel does not support recording context switch events\n"); |
1350 | parse_options_usage(record_usage, record_options, "switch-events", 0); | |
1351 | return -EINVAL; | |
b757bb09 | 1352 | } |
023695d9 | 1353 | |
ef149c25 AH |
1354 | if (!rec->itr) { |
1355 | rec->itr = auxtrace_record__init(rec->evlist, &err); | |
1356 | if (err) | |
1357 | return err; | |
1358 | } | |
1359 | ||
2dd6d8a1 AH |
1360 | err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, |
1361 | rec->opts.auxtrace_snapshot_opts); | |
1362 | if (err) | |
1363 | return err; | |
1364 | ||
d7888573 WN |
1365 | err = bpf__setup_stdout(rec->evlist); |
1366 | if (err) { | |
1367 | bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); | |
1368 | pr_err("ERROR: Setup BPF stdout failed: %s\n", | |
1369 | errbuf); | |
1370 | return err; | |
1371 | } | |
1372 | ||
ef149c25 AH |
1373 | err = -ENOMEM; |
1374 | ||
0a7e6d1b | 1375 | symbol__init(NULL); |
baa2f6ce | 1376 | |
ec80fde7 | 1377 | if (symbol_conf.kptr_restrict) |
646aaea6 ACM |
1378 | pr_warning( |
1379 | "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" | |
1380 | "check /proc/sys/kernel/kptr_restrict.\n\n" | |
1381 | "Samples in kernel functions may not be resolved if a suitable vmlinux\n" | |
1382 | "file is not found in the buildid cache or in the vmlinux path.\n\n" | |
1383 | "Samples in kernel modules won't be resolved at all.\n\n" | |
1384 | "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" | |
1385 | "even with a suitable vmlinux or kallsyms file.\n\n"); | |
ec80fde7 | 1386 | |
d20deb64 | 1387 | if (rec->no_buildid_cache || rec->no_buildid) |
a1ac1d3c | 1388 | disable_buildid_cache(); |
655000e7 | 1389 | |
3e2be2da ACM |
1390 | if (rec->evlist->nr_entries == 0 && |
1391 | perf_evlist__add_default(rec->evlist) < 0) { | |
69aad6f1 ACM |
1392 | pr_err("Not enough memory for event selector list\n"); |
1393 | goto out_symbol_exit; | |
bbd36e5e | 1394 | } |
0e9b20b8 | 1395 | |
69e7e5b0 AH |
1396 | if (rec->opts.target.tid && !rec->opts.no_inherit_set) |
1397 | rec->opts.no_inherit = true; | |
1398 | ||
602ad878 | 1399 | err = target__validate(&rec->opts.target); |
16ad2ffb | 1400 | if (err) { |
602ad878 | 1401 | target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); |
16ad2ffb NK |
1402 | ui__warning("%s", errbuf); |
1403 | } | |
1404 | ||
602ad878 | 1405 | err = target__parse_uid(&rec->opts.target); |
16ad2ffb NK |
1406 | if (err) { |
1407 | int saved_errno = errno; | |
4bd0f2d2 | 1408 | |
602ad878 | 1409 | target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); |
3780f488 | 1410 | ui__error("%s", errbuf); |
16ad2ffb NK |
1411 | |
1412 | err = -saved_errno; | |
8fa60e1f | 1413 | goto out_symbol_exit; |
16ad2ffb | 1414 | } |
0d37aa34 | 1415 | |
16ad2ffb | 1416 | err = -ENOMEM; |
3e2be2da | 1417 | if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) |
dd7927f4 | 1418 | usage_with_options(record_usage, record_options); |
69aad6f1 | 1419 | |
ef149c25 AH |
1420 | err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); |
1421 | if (err) | |
1422 | goto out_symbol_exit; | |
1423 | ||
6156681b NK |
1424 | /* |
1425 | * We take all buildids when the file contains | |
1426 | * AUX area tracing data because we do not decode the | |
1427 | * trace because it would take too long. | |
1428 | */ | |
1429 | if (rec->opts.full_auxtrace) | |
1430 | rec->buildid_all = true; | |
1431 | ||
b4006796 | 1432 | if (record_opts__config(&rec->opts)) { |
39d17dac | 1433 | err = -EINVAL; |
03ad9747 | 1434 | goto out_symbol_exit; |
7e4ff9e3 MG |
1435 | } |
1436 | ||
d20deb64 | 1437 | err = __cmd_record(&record, argc, argv); |
d65a458b | 1438 | out_symbol_exit: |
45604710 | 1439 | perf_evlist__delete(rec->evlist); |
d65a458b | 1440 | symbol__exit(); |
ef149c25 | 1441 | auxtrace_record__free(rec->itr); |
39d17dac | 1442 | return err; |
0e9b20b8 | 1443 | } |
2dd6d8a1 AH |
1444 | |
1445 | static void snapshot_sig_handler(int sig __maybe_unused) | |
1446 | { | |
5f9cf599 WN |
1447 | if (trigger_is_ready(&auxtrace_snapshot_trigger)) { |
1448 | trigger_hit(&auxtrace_snapshot_trigger); | |
1449 | auxtrace_record__snapshot_started = 1; | |
1450 | if (auxtrace_record__snapshot_start(record.itr)) | |
1451 | trigger_error(&auxtrace_snapshot_trigger); | |
1452 | } | |
3c1cb7e3 WN |
1453 | |
1454 | if (trigger_is_ready(&switch_output_trigger)) | |
1455 | trigger_hit(&switch_output_trigger); | |
2dd6d8a1 | 1456 | } |