]>
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" |
0e9b20b8 | 14 | #include "util/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" |
ef149c25 | 30 | #include "util/auxtrace.h" |
7c6a1c65 | 31 | |
97124d5e | 32 | #include <unistd.h> |
de9ac07b | 33 | #include <sched.h> |
a41794cd | 34 | #include <sys/mman.h> |
de9ac07b | 35 | |
78da39fa | 36 | |
8c6f45a7 | 37 | struct record { |
45694aa7 | 38 | struct perf_tool tool; |
b4006796 | 39 | struct record_opts opts; |
d20deb64 | 40 | u64 bytes_written; |
f5fc1412 | 41 | struct perf_data_file file; |
ef149c25 | 42 | struct auxtrace_record *itr; |
d20deb64 ACM |
43 | struct perf_evlist *evlist; |
44 | struct perf_session *session; | |
45 | const char *progname; | |
d20deb64 | 46 | int realtime_prio; |
d20deb64 ACM |
47 | bool no_buildid; |
48 | bool no_buildid_cache; | |
d20deb64 | 49 | long samples; |
0f82ebc4 | 50 | }; |
a21ca2ca | 51 | |
8c6f45a7 | 52 | static int record__write(struct record *rec, void *bf, size_t size) |
f5970550 | 53 | { |
cf8b2e69 | 54 | if (perf_data_file__write(rec->session->file, bf, size) < 0) { |
50a9b868 JO |
55 | pr_err("failed to write perf data, error: %m\n"); |
56 | return -1; | |
f5970550 | 57 | } |
8d3eca20 | 58 | |
cf8b2e69 | 59 | rec->bytes_written += size; |
8d3eca20 | 60 | return 0; |
f5970550 PZ |
61 | } |
62 | ||
45694aa7 | 63 | static int process_synthesized_event(struct perf_tool *tool, |
d20deb64 | 64 | union perf_event *event, |
1d037ca1 IT |
65 | struct perf_sample *sample __maybe_unused, |
66 | struct machine *machine __maybe_unused) | |
234fbbf5 | 67 | { |
8c6f45a7 ACM |
68 | struct record *rec = container_of(tool, struct record, tool); |
69 | return record__write(rec, event, event->header.size); | |
234fbbf5 ACM |
70 | } |
71 | ||
e5685730 | 72 | static int record__mmap_read(struct record *rec, int idx) |
de9ac07b | 73 | { |
e5685730 | 74 | struct perf_mmap *md = &rec->evlist->mmap[idx]; |
7b8283b5 DA |
75 | u64 head = perf_mmap__read_head(md); |
76 | u64 old = md->prev; | |
918512b4 | 77 | unsigned char *data = md->base + page_size; |
de9ac07b PZ |
78 | unsigned long size; |
79 | void *buf; | |
8d3eca20 | 80 | int rc = 0; |
de9ac07b | 81 | |
dc82009a | 82 | if (old == head) |
8d3eca20 | 83 | return 0; |
dc82009a | 84 | |
d20deb64 | 85 | rec->samples++; |
de9ac07b PZ |
86 | |
87 | size = head - old; | |
88 | ||
89 | if ((old & md->mask) + size != (head & md->mask)) { | |
90 | buf = &data[old & md->mask]; | |
91 | size = md->mask + 1 - (old & md->mask); | |
92 | old += size; | |
021e9f47 | 93 | |
8c6f45a7 | 94 | if (record__write(rec, buf, size) < 0) { |
8d3eca20 DA |
95 | rc = -1; |
96 | goto out; | |
97 | } | |
de9ac07b PZ |
98 | } |
99 | ||
100 | buf = &data[old & md->mask]; | |
101 | size = head - old; | |
102 | old += size; | |
021e9f47 | 103 | |
8c6f45a7 | 104 | if (record__write(rec, buf, size) < 0) { |
8d3eca20 DA |
105 | rc = -1; |
106 | goto out; | |
107 | } | |
de9ac07b PZ |
108 | |
109 | md->prev = old; | |
e5685730 | 110 | perf_evlist__mmap_consume(rec->evlist, idx); |
8d3eca20 DA |
111 | out: |
112 | return rc; | |
de9ac07b PZ |
113 | } |
114 | ||
ef149c25 AH |
115 | static int record__process_auxtrace(struct perf_tool *tool, |
116 | union perf_event *event, void *data1, | |
117 | size_t len1, void *data2, size_t len2) | |
118 | { | |
119 | struct record *rec = container_of(tool, struct record, tool); | |
99fa2984 | 120 | struct perf_data_file *file = &rec->file; |
ef149c25 AH |
121 | size_t padding; |
122 | u8 pad[8] = {0}; | |
123 | ||
99fa2984 AH |
124 | if (!perf_data_file__is_pipe(file)) { |
125 | off_t file_offset; | |
126 | int fd = perf_data_file__fd(file); | |
127 | int err; | |
128 | ||
129 | file_offset = lseek(fd, 0, SEEK_CUR); | |
130 | if (file_offset == -1) | |
131 | return -1; | |
132 | err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, | |
133 | event, file_offset); | |
134 | if (err) | |
135 | return err; | |
136 | } | |
137 | ||
ef149c25 AH |
138 | /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ |
139 | padding = (len1 + len2) & 7; | |
140 | if (padding) | |
141 | padding = 8 - padding; | |
142 | ||
143 | record__write(rec, event, event->header.size); | |
144 | record__write(rec, data1, len1); | |
145 | if (len2) | |
146 | record__write(rec, data2, len2); | |
147 | record__write(rec, &pad, padding); | |
148 | ||
149 | return 0; | |
150 | } | |
151 | ||
152 | static int record__auxtrace_mmap_read(struct record *rec, | |
153 | struct auxtrace_mmap *mm) | |
154 | { | |
155 | int ret; | |
156 | ||
157 | ret = auxtrace_mmap__read(mm, rec->itr, &rec->tool, | |
158 | record__process_auxtrace); | |
159 | if (ret < 0) | |
160 | return ret; | |
161 | ||
162 | if (ret) | |
163 | rec->samples++; | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
de9ac07b | 168 | static volatile int done = 0; |
f7b7c26e | 169 | static volatile int signr = -1; |
33e49ea7 | 170 | static volatile int child_finished = 0; |
de9ac07b | 171 | |
16c8a109 | 172 | static void sig_handler(int sig) |
de9ac07b | 173 | { |
33e49ea7 AK |
174 | if (sig == SIGCHLD) |
175 | child_finished = 1; | |
45604710 NK |
176 | else |
177 | signr = sig; | |
33e49ea7 | 178 | |
16c8a109 | 179 | done = 1; |
f7b7c26e PZ |
180 | } |
181 | ||
45604710 | 182 | static void record__sig_exit(void) |
f7b7c26e | 183 | { |
45604710 | 184 | if (signr == -1) |
f7b7c26e PZ |
185 | return; |
186 | ||
187 | signal(signr, SIG_DFL); | |
45604710 | 188 | raise(signr); |
de9ac07b PZ |
189 | } |
190 | ||
8c6f45a7 | 191 | static int record__open(struct record *rec) |
dd7927f4 | 192 | { |
56e52e85 | 193 | char msg[512]; |
6a4bb04c | 194 | struct perf_evsel *pos; |
d20deb64 ACM |
195 | struct perf_evlist *evlist = rec->evlist; |
196 | struct perf_session *session = rec->session; | |
b4006796 | 197 | struct record_opts *opts = &rec->opts; |
8d3eca20 | 198 | int rc = 0; |
dd7927f4 | 199 | |
f77a9518 | 200 | perf_evlist__config(evlist, opts); |
cac21425 | 201 | |
0050f7aa | 202 | evlist__for_each(evlist, pos) { |
dd7927f4 | 203 | try_again: |
6a4bb04c | 204 | if (perf_evsel__open(pos, evlist->cpus, evlist->threads) < 0) { |
56e52e85 | 205 | if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) { |
d6d901c2 | 206 | if (verbose) |
c0a54341 | 207 | ui__warning("%s\n", msg); |
d6d901c2 ZY |
208 | goto try_again; |
209 | } | |
ca6a4258 | 210 | |
56e52e85 ACM |
211 | rc = -errno; |
212 | perf_evsel__open_strerror(pos, &opts->target, | |
213 | errno, msg, sizeof(msg)); | |
214 | ui__error("%s\n", msg); | |
8d3eca20 | 215 | goto out; |
c171b552 LZ |
216 | } |
217 | } | |
a43d3f08 | 218 | |
23d4aad4 ACM |
219 | if (perf_evlist__apply_filters(evlist, &pos)) { |
220 | error("failed to set filter \"%s\" on event %s with %d (%s)\n", | |
221 | pos->filter, perf_evsel__name(pos), errno, | |
35550da3 | 222 | strerror_r(errno, msg, sizeof(msg))); |
8d3eca20 DA |
223 | rc = -1; |
224 | goto out; | |
0a102479 FW |
225 | } |
226 | ||
ef149c25 AH |
227 | if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false, |
228 | opts->auxtrace_mmap_pages, false) < 0) { | |
8d3eca20 DA |
229 | if (errno == EPERM) { |
230 | pr_err("Permission error mapping pages.\n" | |
231 | "Consider increasing " | |
232 | "/proc/sys/kernel/perf_event_mlock_kb,\n" | |
233 | "or try again with a smaller value of -m/--mmap_pages.\n" | |
ef149c25 AH |
234 | "(current value: %u,%u)\n", |
235 | opts->mmap_pages, opts->auxtrace_mmap_pages); | |
8d3eca20 | 236 | rc = -errno; |
8d3eca20 | 237 | } else { |
35550da3 MH |
238 | pr_err("failed to mmap with %d (%s)\n", errno, |
239 | strerror_r(errno, msg, sizeof(msg))); | |
8d3eca20 DA |
240 | rc = -errno; |
241 | } | |
242 | goto out; | |
18e60939 | 243 | } |
0a27d7f9 | 244 | |
563aecb2 | 245 | session->evlist = evlist; |
7b56cce2 | 246 | perf_session__set_id_hdr_size(session); |
8d3eca20 DA |
247 | out: |
248 | return rc; | |
16c8a109 PZ |
249 | } |
250 | ||
e3d59112 NK |
251 | static int process_sample_event(struct perf_tool *tool, |
252 | union perf_event *event, | |
253 | struct perf_sample *sample, | |
254 | struct perf_evsel *evsel, | |
255 | struct machine *machine) | |
256 | { | |
257 | struct record *rec = container_of(tool, struct record, tool); | |
258 | ||
259 | rec->samples++; | |
260 | ||
261 | return build_id__mark_dso_hit(tool, event, sample, evsel, machine); | |
262 | } | |
263 | ||
8c6f45a7 | 264 | static int process_buildids(struct record *rec) |
6122e4e4 | 265 | { |
f5fc1412 JO |
266 | struct perf_data_file *file = &rec->file; |
267 | struct perf_session *session = rec->session; | |
6122e4e4 | 268 | |
42aa276f | 269 | u64 size = lseek(perf_data_file__fd(file), 0, SEEK_CUR); |
9f591fd7 ACM |
270 | if (size == 0) |
271 | return 0; | |
272 | ||
4ac30cf7 NK |
273 | file->size = size; |
274 | ||
00dc8657 NK |
275 | /* |
276 | * During this process, it'll load kernel map and replace the | |
277 | * dso->long_name to a real pathname it found. In this case | |
278 | * we prefer the vmlinux path like | |
279 | * /lib/modules/3.16.4/build/vmlinux | |
280 | * | |
281 | * rather than build-id path (in debug directory). | |
282 | * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 | |
283 | */ | |
284 | symbol_conf.ignore_vmlinux_buildid = true; | |
285 | ||
b7b61cbe | 286 | return perf_session__process_events(session); |
6122e4e4 ACM |
287 | } |
288 | ||
8115d60c | 289 | static void perf_event__synthesize_guest_os(struct machine *machine, void *data) |
a1645ce1 ZY |
290 | { |
291 | int err; | |
45694aa7 | 292 | struct perf_tool *tool = data; |
a1645ce1 ZY |
293 | /* |
294 | *As for guest kernel when processing subcommand record&report, | |
295 | *we arrange module mmap prior to guest kernel mmap and trigger | |
296 | *a preload dso because default guest module symbols are loaded | |
297 | *from guest kallsyms instead of /lib/modules/XXX/XXX. This | |
298 | *method is used to avoid symbol missing when the first addr is | |
299 | *in module instead of in guest kernel. | |
300 | */ | |
45694aa7 | 301 | err = perf_event__synthesize_modules(tool, process_synthesized_event, |
743eb868 | 302 | machine); |
a1645ce1 ZY |
303 | if (err < 0) |
304 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 305 | " relocation symbol.\n", machine->pid); |
a1645ce1 | 306 | |
a1645ce1 ZY |
307 | /* |
308 | * We use _stext for guest kernel because guest kernel's /proc/kallsyms | |
309 | * have no _text sometimes. | |
310 | */ | |
45694aa7 | 311 | err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, |
0ae617be | 312 | machine); |
a1645ce1 ZY |
313 | if (err < 0) |
314 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 315 | " relocation symbol.\n", machine->pid); |
a1645ce1 ZY |
316 | } |
317 | ||
98402807 FW |
318 | static struct perf_event_header finished_round_event = { |
319 | .size = sizeof(struct perf_event_header), | |
320 | .type = PERF_RECORD_FINISHED_ROUND, | |
321 | }; | |
322 | ||
8c6f45a7 | 323 | static int record__mmap_read_all(struct record *rec) |
98402807 | 324 | { |
dcabb507 | 325 | u64 bytes_written = rec->bytes_written; |
0e2e63dd | 326 | int i; |
8d3eca20 | 327 | int rc = 0; |
98402807 | 328 | |
d20deb64 | 329 | for (i = 0; i < rec->evlist->nr_mmaps; i++) { |
ef149c25 AH |
330 | struct auxtrace_mmap *mm = &rec->evlist->mmap[i].auxtrace_mmap; |
331 | ||
8d3eca20 | 332 | if (rec->evlist->mmap[i].base) { |
e5685730 | 333 | if (record__mmap_read(rec, i) != 0) { |
8d3eca20 DA |
334 | rc = -1; |
335 | goto out; | |
336 | } | |
337 | } | |
ef149c25 AH |
338 | |
339 | if (mm->base && | |
340 | record__auxtrace_mmap_read(rec, mm) != 0) { | |
341 | rc = -1; | |
342 | goto out; | |
343 | } | |
98402807 FW |
344 | } |
345 | ||
dcabb507 JO |
346 | /* |
347 | * Mark the round finished in case we wrote | |
348 | * at least one event. | |
349 | */ | |
350 | if (bytes_written != rec->bytes_written) | |
351 | rc = record__write(rec, &finished_round_event, sizeof(finished_round_event)); | |
8d3eca20 DA |
352 | |
353 | out: | |
354 | return rc; | |
98402807 FW |
355 | } |
356 | ||
8c6f45a7 | 357 | static void record__init_features(struct record *rec) |
57706abc | 358 | { |
57706abc DA |
359 | struct perf_session *session = rec->session; |
360 | int feat; | |
361 | ||
362 | for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) | |
363 | perf_header__set_feat(&session->header, feat); | |
364 | ||
365 | if (rec->no_buildid) | |
366 | perf_header__clear_feat(&session->header, HEADER_BUILD_ID); | |
367 | ||
3e2be2da | 368 | if (!have_tracepoints(&rec->evlist->entries)) |
57706abc DA |
369 | perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); |
370 | ||
371 | if (!rec->opts.branch_stack) | |
372 | perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); | |
ef149c25 AH |
373 | |
374 | if (!rec->opts.full_auxtrace) | |
375 | perf_header__clear_feat(&session->header, HEADER_AUXTRACE); | |
57706abc DA |
376 | } |
377 | ||
f33cbe72 ACM |
378 | static volatile int workload_exec_errno; |
379 | ||
380 | /* | |
381 | * perf_evlist__prepare_workload will send a SIGUSR1 | |
382 | * if the fork fails, since we asked by setting its | |
383 | * want_signal to true. | |
384 | */ | |
45604710 NK |
385 | static void workload_exec_failed_signal(int signo __maybe_unused, |
386 | siginfo_t *info, | |
f33cbe72 ACM |
387 | void *ucontext __maybe_unused) |
388 | { | |
389 | workload_exec_errno = info->si_value.sival_int; | |
390 | done = 1; | |
f33cbe72 ACM |
391 | child_finished = 1; |
392 | } | |
393 | ||
8c6f45a7 | 394 | static int __cmd_record(struct record *rec, int argc, const char **argv) |
16c8a109 | 395 | { |
57706abc | 396 | int err; |
45604710 | 397 | int status = 0; |
8b412664 | 398 | unsigned long waking = 0; |
46be604b | 399 | const bool forks = argc > 0; |
23346f21 | 400 | struct machine *machine; |
45694aa7 | 401 | struct perf_tool *tool = &rec->tool; |
b4006796 | 402 | struct record_opts *opts = &rec->opts; |
f5fc1412 | 403 | struct perf_data_file *file = &rec->file; |
d20deb64 | 404 | struct perf_session *session; |
6dcf45ef | 405 | bool disabled = false, draining = false; |
42aa276f | 406 | int fd; |
de9ac07b | 407 | |
d20deb64 | 408 | rec->progname = argv[0]; |
33e49ea7 | 409 | |
45604710 | 410 | atexit(record__sig_exit); |
f5970550 PZ |
411 | signal(SIGCHLD, sig_handler); |
412 | signal(SIGINT, sig_handler); | |
804f7ac7 | 413 | signal(SIGTERM, sig_handler); |
f5970550 | 414 | |
b7b61cbe | 415 | session = perf_session__new(file, false, tool); |
94c744b6 | 416 | if (session == NULL) { |
ffa91880 | 417 | pr_err("Perf session creation failed.\n"); |
a9a70bbc ACM |
418 | return -1; |
419 | } | |
420 | ||
42aa276f | 421 | fd = perf_data_file__fd(file); |
d20deb64 ACM |
422 | rec->session = session; |
423 | ||
8c6f45a7 | 424 | record__init_features(rec); |
330aa675 | 425 | |
d4db3f16 | 426 | if (forks) { |
3e2be2da | 427 | err = perf_evlist__prepare_workload(rec->evlist, &opts->target, |
f5fc1412 | 428 | argv, file->is_pipe, |
735f7e0b | 429 | workload_exec_failed_signal); |
35b9d88e ACM |
430 | if (err < 0) { |
431 | pr_err("Couldn't run the workload!\n"); | |
45604710 | 432 | status = err; |
35b9d88e | 433 | goto out_delete_session; |
856e9660 | 434 | } |
856e9660 PZ |
435 | } |
436 | ||
8c6f45a7 | 437 | if (record__open(rec) != 0) { |
8d3eca20 | 438 | err = -1; |
45604710 | 439 | goto out_child; |
8d3eca20 | 440 | } |
de9ac07b | 441 | |
3e2be2da | 442 | if (!rec->evlist->nr_groups) |
a8bb559b NK |
443 | perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); |
444 | ||
f5fc1412 | 445 | if (file->is_pipe) { |
42aa276f | 446 | err = perf_header__write_pipe(fd); |
529870e3 | 447 | if (err < 0) |
45604710 | 448 | goto out_child; |
563aecb2 | 449 | } else { |
42aa276f | 450 | err = perf_session__write_header(session, rec->evlist, fd, false); |
d5eed904 | 451 | if (err < 0) |
45604710 | 452 | goto out_child; |
56b03f3c ACM |
453 | } |
454 | ||
d3665498 | 455 | if (!rec->no_buildid |
e20960c0 | 456 | && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { |
d3665498 | 457 | pr_err("Couldn't generate buildids. " |
e20960c0 | 458 | "Use --no-buildid to profile anyway.\n"); |
8d3eca20 | 459 | err = -1; |
45604710 | 460 | goto out_child; |
e20960c0 RR |
461 | } |
462 | ||
34ba5122 | 463 | machine = &session->machines.host; |
743eb868 | 464 | |
f5fc1412 | 465 | if (file->is_pipe) { |
45694aa7 | 466 | err = perf_event__synthesize_attrs(tool, session, |
d20deb64 | 467 | process_synthesized_event); |
2c46dbb5 TZ |
468 | if (err < 0) { |
469 | pr_err("Couldn't synthesize attrs.\n"); | |
45604710 | 470 | goto out_child; |
2c46dbb5 | 471 | } |
cd19a035 | 472 | |
3e2be2da | 473 | if (have_tracepoints(&rec->evlist->entries)) { |
63e0c771 TZ |
474 | /* |
475 | * FIXME err <= 0 here actually means that | |
476 | * there were no tracepoints so its not really | |
477 | * an error, just that we don't need to | |
478 | * synthesize anything. We really have to | |
479 | * return this more properly and also | |
480 | * propagate errors that now are calling die() | |
481 | */ | |
42aa276f | 482 | err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist, |
743eb868 | 483 | process_synthesized_event); |
63e0c771 TZ |
484 | if (err <= 0) { |
485 | pr_err("Couldn't record tracing data.\n"); | |
45604710 | 486 | goto out_child; |
63e0c771 | 487 | } |
f34b9001 | 488 | rec->bytes_written += err; |
63e0c771 | 489 | } |
2c46dbb5 TZ |
490 | } |
491 | ||
ef149c25 AH |
492 | if (rec->opts.full_auxtrace) { |
493 | err = perf_event__synthesize_auxtrace_info(rec->itr, tool, | |
494 | session, process_synthesized_event); | |
495 | if (err) | |
496 | goto out_delete_session; | |
497 | } | |
498 | ||
45694aa7 | 499 | err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, |
0ae617be | 500 | machine); |
c1a3a4b9 ACM |
501 | if (err < 0) |
502 | pr_err("Couldn't record kernel reference relocation symbol\n" | |
503 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
504 | "Check /proc/kallsyms permission or run as root.\n"); | |
b7cece76 | 505 | |
45694aa7 | 506 | err = perf_event__synthesize_modules(tool, process_synthesized_event, |
743eb868 | 507 | machine); |
c1a3a4b9 ACM |
508 | if (err < 0) |
509 | pr_err("Couldn't record kernel module information.\n" | |
510 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
511 | "Check /proc/modules permission or run as root.\n"); | |
512 | ||
7e383de4 | 513 | if (perf_guest) { |
876650e6 ACM |
514 | machines__process_guests(&session->machines, |
515 | perf_event__synthesize_guest_os, tool); | |
7e383de4 | 516 | } |
7c6a1c65 | 517 | |
3e2be2da | 518 | err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads, |
a33fbd56 | 519 | process_synthesized_event, opts->sample_address); |
8d3eca20 | 520 | if (err != 0) |
45604710 | 521 | goto out_child; |
8d3eca20 | 522 | |
d20deb64 | 523 | if (rec->realtime_prio) { |
de9ac07b PZ |
524 | struct sched_param param; |
525 | ||
d20deb64 | 526 | param.sched_priority = rec->realtime_prio; |
de9ac07b | 527 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
6beba7ad | 528 | pr_err("Could not set realtime priority.\n"); |
8d3eca20 | 529 | err = -1; |
45604710 | 530 | goto out_child; |
de9ac07b PZ |
531 | } |
532 | } | |
533 | ||
774cb499 JO |
534 | /* |
535 | * When perf is starting the traced process, all the events | |
536 | * (apart from group members) have enable_on_exec=1 set, | |
537 | * so don't spoil it by prematurely enabling them. | |
538 | */ | |
6619a53e | 539 | if (!target__none(&opts->target) && !opts->initial_delay) |
3e2be2da | 540 | perf_evlist__enable(rec->evlist); |
764e16a3 | 541 | |
856e9660 PZ |
542 | /* |
543 | * Let the child rip | |
544 | */ | |
735f7e0b | 545 | if (forks) |
3e2be2da | 546 | perf_evlist__start_workload(rec->evlist); |
856e9660 | 547 | |
6619a53e AK |
548 | if (opts->initial_delay) { |
549 | usleep(opts->initial_delay * 1000); | |
550 | perf_evlist__enable(rec->evlist); | |
551 | } | |
552 | ||
649c48a9 | 553 | for (;;) { |
d20deb64 | 554 | int hits = rec->samples; |
de9ac07b | 555 | |
8c6f45a7 | 556 | if (record__mmap_read_all(rec) < 0) { |
8d3eca20 | 557 | err = -1; |
45604710 | 558 | goto out_child; |
8d3eca20 | 559 | } |
de9ac07b | 560 | |
d20deb64 | 561 | if (hits == rec->samples) { |
6dcf45ef | 562 | if (done || draining) |
649c48a9 | 563 | break; |
f66a889d | 564 | err = perf_evlist__poll(rec->evlist, -1); |
a515114f JO |
565 | /* |
566 | * Propagate error, only if there's any. Ignore positive | |
567 | * number of returned events and interrupt error. | |
568 | */ | |
569 | if (err > 0 || (err < 0 && errno == EINTR)) | |
45604710 | 570 | err = 0; |
8b412664 | 571 | waking++; |
6dcf45ef ACM |
572 | |
573 | if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) | |
574 | draining = true; | |
8b412664 PZ |
575 | } |
576 | ||
774cb499 JO |
577 | /* |
578 | * When perf is starting the traced process, at the end events | |
579 | * die with the process and we wait for that. Thus no need to | |
580 | * disable events in this case. | |
581 | */ | |
602ad878 | 582 | if (done && !disabled && !target__none(&opts->target)) { |
3e2be2da | 583 | perf_evlist__disable(rec->evlist); |
2711926a JO |
584 | disabled = true; |
585 | } | |
de9ac07b PZ |
586 | } |
587 | ||
f33cbe72 | 588 | if (forks && workload_exec_errno) { |
35550da3 | 589 | char msg[STRERR_BUFSIZE]; |
f33cbe72 ACM |
590 | const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg)); |
591 | pr_err("Workload failed: %s\n", emsg); | |
592 | err = -1; | |
45604710 | 593 | goto out_child; |
f33cbe72 ACM |
594 | } |
595 | ||
e3d59112 | 596 | if (!quiet) |
45604710 | 597 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); |
b44308f5 | 598 | |
45604710 NK |
599 | out_child: |
600 | if (forks) { | |
601 | int exit_status; | |
addc2785 | 602 | |
45604710 NK |
603 | if (!child_finished) |
604 | kill(rec->evlist->workload.pid, SIGTERM); | |
605 | ||
606 | wait(&exit_status); | |
607 | ||
608 | if (err < 0) | |
609 | status = err; | |
610 | else if (WIFEXITED(exit_status)) | |
611 | status = WEXITSTATUS(exit_status); | |
612 | else if (WIFSIGNALED(exit_status)) | |
613 | signr = WTERMSIG(exit_status); | |
614 | } else | |
615 | status = err; | |
616 | ||
e3d59112 NK |
617 | /* this will be recalculated during process_buildids() */ |
618 | rec->samples = 0; | |
619 | ||
45604710 NK |
620 | if (!err && !file->is_pipe) { |
621 | rec->session->header.data_size += rec->bytes_written; | |
622 | ||
cd10b289 | 623 | if (!rec->no_buildid) { |
45604710 | 624 | process_buildids(rec); |
cd10b289 AH |
625 | /* |
626 | * We take all buildids when the file contains | |
627 | * AUX area tracing data because we do not decode the | |
628 | * trace because it would take too long. | |
629 | */ | |
630 | if (rec->opts.full_auxtrace) | |
631 | dsos__hit_all(rec->session); | |
632 | } | |
42aa276f | 633 | perf_session__write_header(rec->session, rec->evlist, fd, true); |
45604710 | 634 | } |
39d17dac | 635 | |
e3d59112 NK |
636 | if (!err && !quiet) { |
637 | char samples[128]; | |
638 | ||
ef149c25 | 639 | if (rec->samples && !rec->opts.full_auxtrace) |
e3d59112 NK |
640 | scnprintf(samples, sizeof(samples), |
641 | " (%" PRIu64 " samples)", rec->samples); | |
642 | else | |
643 | samples[0] = '\0'; | |
644 | ||
645 | fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s ]\n", | |
646 | perf_data_file__size(file) / 1024.0 / 1024.0, | |
647 | file->path, samples); | |
648 | } | |
649 | ||
39d17dac ACM |
650 | out_delete_session: |
651 | perf_session__delete(session); | |
45604710 | 652 | return status; |
de9ac07b | 653 | } |
0e9b20b8 | 654 | |
bdfebd84 RAV |
655 | #define BRANCH_OPT(n, m) \ |
656 | { .name = n, .mode = (m) } | |
657 | ||
658 | #define BRANCH_END { .name = NULL } | |
659 | ||
660 | struct branch_mode { | |
661 | const char *name; | |
662 | int mode; | |
663 | }; | |
664 | ||
665 | static const struct branch_mode branch_modes[] = { | |
666 | BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER), | |
667 | BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL), | |
668 | BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV), | |
669 | BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY), | |
670 | BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL), | |
671 | BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN), | |
672 | BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL), | |
0126d493 AK |
673 | BRANCH_OPT("abort_tx", PERF_SAMPLE_BRANCH_ABORT_TX), |
674 | BRANCH_OPT("in_tx", PERF_SAMPLE_BRANCH_IN_TX), | |
675 | BRANCH_OPT("no_tx", PERF_SAMPLE_BRANCH_NO_TX), | |
0fffa5df | 676 | BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_COND), |
bdfebd84 RAV |
677 | BRANCH_END |
678 | }; | |
679 | ||
680 | static int | |
a5aabdac | 681 | parse_branch_stack(const struct option *opt, const char *str, int unset) |
bdfebd84 RAV |
682 | { |
683 | #define ONLY_PLM \ | |
684 | (PERF_SAMPLE_BRANCH_USER |\ | |
685 | PERF_SAMPLE_BRANCH_KERNEL |\ | |
686 | PERF_SAMPLE_BRANCH_HV) | |
687 | ||
688 | uint64_t *mode = (uint64_t *)opt->value; | |
689 | const struct branch_mode *br; | |
a5aabdac | 690 | char *s, *os = NULL, *p; |
bdfebd84 RAV |
691 | int ret = -1; |
692 | ||
a5aabdac SE |
693 | if (unset) |
694 | return 0; | |
bdfebd84 | 695 | |
a5aabdac SE |
696 | /* |
697 | * cannot set it twice, -b + --branch-filter for instance | |
698 | */ | |
699 | if (*mode) | |
bdfebd84 RAV |
700 | return -1; |
701 | ||
a5aabdac SE |
702 | /* str may be NULL in case no arg is passed to -b */ |
703 | if (str) { | |
704 | /* because str is read-only */ | |
705 | s = os = strdup(str); | |
706 | if (!s) | |
707 | return -1; | |
708 | ||
709 | for (;;) { | |
710 | p = strchr(s, ','); | |
711 | if (p) | |
712 | *p = '\0'; | |
713 | ||
714 | for (br = branch_modes; br->name; br++) { | |
715 | if (!strcasecmp(s, br->name)) | |
716 | break; | |
717 | } | |
718 | if (!br->name) { | |
719 | ui__warning("unknown branch filter %s," | |
720 | " check man page\n", s); | |
721 | goto error; | |
722 | } | |
bdfebd84 | 723 | |
a5aabdac | 724 | *mode |= br->mode; |
bdfebd84 | 725 | |
a5aabdac SE |
726 | if (!p) |
727 | break; | |
bdfebd84 | 728 | |
a5aabdac SE |
729 | s = p + 1; |
730 | } | |
bdfebd84 RAV |
731 | } |
732 | ret = 0; | |
733 | ||
a5aabdac | 734 | /* default to any branch */ |
bdfebd84 | 735 | if ((*mode & ~ONLY_PLM) == 0) { |
a5aabdac | 736 | *mode = PERF_SAMPLE_BRANCH_ANY; |
bdfebd84 RAV |
737 | } |
738 | error: | |
739 | free(os); | |
740 | return ret; | |
741 | } | |
742 | ||
72a128aa | 743 | static void callchain_debug(void) |
09b0fd45 | 744 | { |
aad2b21c | 745 | static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; |
a601fdff | 746 | |
72a128aa | 747 | pr_debug("callchain: type %s\n", str[callchain_param.record_mode]); |
26d33022 | 748 | |
72a128aa | 749 | if (callchain_param.record_mode == CALLCHAIN_DWARF) |
09b0fd45 | 750 | pr_debug("callchain: stack dump size %d\n", |
72a128aa | 751 | callchain_param.dump_size); |
09b0fd45 JO |
752 | } |
753 | ||
72a128aa | 754 | int record_parse_callchain_opt(const struct option *opt __maybe_unused, |
09b0fd45 JO |
755 | const char *arg, |
756 | int unset) | |
757 | { | |
09b0fd45 JO |
758 | int ret; |
759 | ||
72a128aa | 760 | callchain_param.enabled = !unset; |
eb853e80 | 761 | |
09b0fd45 JO |
762 | /* --no-call-graph */ |
763 | if (unset) { | |
72a128aa | 764 | callchain_param.record_mode = CALLCHAIN_NONE; |
09b0fd45 JO |
765 | pr_debug("callchain: disabled\n"); |
766 | return 0; | |
767 | } | |
768 | ||
f7f084f4 | 769 | ret = parse_callchain_record_opt(arg); |
26d33022 | 770 | if (!ret) |
72a128aa | 771 | callchain_debug(); |
26d33022 JO |
772 | |
773 | return ret; | |
774 | } | |
775 | ||
72a128aa | 776 | int record_callchain_opt(const struct option *opt __maybe_unused, |
09b0fd45 JO |
777 | const char *arg __maybe_unused, |
778 | int unset __maybe_unused) | |
779 | { | |
72a128aa | 780 | callchain_param.enabled = true; |
09b0fd45 | 781 | |
72a128aa NK |
782 | if (callchain_param.record_mode == CALLCHAIN_NONE) |
783 | callchain_param.record_mode = CALLCHAIN_FP; | |
eb853e80 | 784 | |
72a128aa | 785 | callchain_debug(); |
09b0fd45 JO |
786 | return 0; |
787 | } | |
788 | ||
eb853e80 JO |
789 | static int perf_record_config(const char *var, const char *value, void *cb) |
790 | { | |
eb853e80 | 791 | if (!strcmp(var, "record.call-graph")) |
5a2e5e85 | 792 | var = "call-graph.record-mode"; /* fall-through */ |
eb853e80 JO |
793 | |
794 | return perf_default_config(var, value, cb); | |
795 | } | |
796 | ||
814c8c38 PZ |
797 | struct clockid_map { |
798 | const char *name; | |
799 | int clockid; | |
800 | }; | |
801 | ||
802 | #define CLOCKID_MAP(n, c) \ | |
803 | { .name = n, .clockid = (c), } | |
804 | ||
805 | #define CLOCKID_END { .name = NULL, } | |
806 | ||
807 | ||
808 | /* | |
809 | * Add the missing ones, we need to build on many distros... | |
810 | */ | |
811 | #ifndef CLOCK_MONOTONIC_RAW | |
812 | #define CLOCK_MONOTONIC_RAW 4 | |
813 | #endif | |
814 | #ifndef CLOCK_BOOTTIME | |
815 | #define CLOCK_BOOTTIME 7 | |
816 | #endif | |
817 | #ifndef CLOCK_TAI | |
818 | #define CLOCK_TAI 11 | |
819 | #endif | |
820 | ||
821 | static const struct clockid_map clockids[] = { | |
822 | /* available for all events, NMI safe */ | |
823 | CLOCKID_MAP("monotonic", CLOCK_MONOTONIC), | |
824 | CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW), | |
825 | ||
826 | /* available for some events */ | |
827 | CLOCKID_MAP("realtime", CLOCK_REALTIME), | |
828 | CLOCKID_MAP("boottime", CLOCK_BOOTTIME), | |
829 | CLOCKID_MAP("tai", CLOCK_TAI), | |
830 | ||
831 | /* available for the lazy */ | |
832 | CLOCKID_MAP("mono", CLOCK_MONOTONIC), | |
833 | CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW), | |
834 | CLOCKID_MAP("real", CLOCK_REALTIME), | |
835 | CLOCKID_MAP("boot", CLOCK_BOOTTIME), | |
836 | ||
837 | CLOCKID_END, | |
838 | }; | |
839 | ||
840 | static int parse_clockid(const struct option *opt, const char *str, int unset) | |
841 | { | |
842 | struct record_opts *opts = (struct record_opts *)opt->value; | |
843 | const struct clockid_map *cm; | |
844 | const char *ostr = str; | |
845 | ||
846 | if (unset) { | |
847 | opts->use_clockid = 0; | |
848 | return 0; | |
849 | } | |
850 | ||
851 | /* no arg passed */ | |
852 | if (!str) | |
853 | return 0; | |
854 | ||
855 | /* no setting it twice */ | |
856 | if (opts->use_clockid) | |
857 | return -1; | |
858 | ||
859 | opts->use_clockid = true; | |
860 | ||
861 | /* if its a number, we're done */ | |
862 | if (sscanf(str, "%d", &opts->clockid) == 1) | |
863 | return 0; | |
864 | ||
865 | /* allow a "CLOCK_" prefix to the name */ | |
866 | if (!strncasecmp(str, "CLOCK_", 6)) | |
867 | str += 6; | |
868 | ||
869 | for (cm = clockids; cm->name; cm++) { | |
870 | if (!strcasecmp(str, cm->name)) { | |
871 | opts->clockid = cm->clockid; | |
872 | return 0; | |
873 | } | |
874 | } | |
875 | ||
876 | opts->use_clockid = false; | |
877 | ui__warning("unknown clockid %s, check man page\n", ostr); | |
878 | return -1; | |
879 | } | |
880 | ||
e9db1310 AH |
881 | static int record__parse_mmap_pages(const struct option *opt, |
882 | const char *str, | |
883 | int unset __maybe_unused) | |
884 | { | |
885 | struct record_opts *opts = opt->value; | |
886 | char *s, *p; | |
887 | unsigned int mmap_pages; | |
888 | int ret; | |
889 | ||
890 | if (!str) | |
891 | return -EINVAL; | |
892 | ||
893 | s = strdup(str); | |
894 | if (!s) | |
895 | return -ENOMEM; | |
896 | ||
897 | p = strchr(s, ','); | |
898 | if (p) | |
899 | *p = '\0'; | |
900 | ||
901 | if (*s) { | |
902 | ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s); | |
903 | if (ret) | |
904 | goto out_free; | |
905 | opts->mmap_pages = mmap_pages; | |
906 | } | |
907 | ||
908 | if (!p) { | |
909 | ret = 0; | |
910 | goto out_free; | |
911 | } | |
912 | ||
913 | ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1); | |
914 | if (ret) | |
915 | goto out_free; | |
916 | ||
917 | opts->auxtrace_mmap_pages = mmap_pages; | |
918 | ||
919 | out_free: | |
920 | free(s); | |
921 | return ret; | |
922 | } | |
923 | ||
e5b2c207 | 924 | static const char * const __record_usage[] = { |
9e096753 MG |
925 | "perf record [<options>] [<command>]", |
926 | "perf record [<options>] -- <command> [<options>]", | |
0e9b20b8 IM |
927 | NULL |
928 | }; | |
e5b2c207 | 929 | const char * const *record_usage = __record_usage; |
0e9b20b8 | 930 | |
d20deb64 | 931 | /* |
8c6f45a7 ACM |
932 | * XXX Ideally would be local to cmd_record() and passed to a record__new |
933 | * because we need to have access to it in record__exit, that is called | |
d20deb64 ACM |
934 | * after cmd_record() exits, but since record_options need to be accessible to |
935 | * builtin-script, leave it here. | |
936 | * | |
937 | * At least we don't ouch it in all the other functions here directly. | |
938 | * | |
939 | * Just say no to tons of global variables, sigh. | |
940 | */ | |
8c6f45a7 | 941 | static struct record record = { |
d20deb64 | 942 | .opts = { |
8affc2b8 | 943 | .sample_time = true, |
d20deb64 ACM |
944 | .mmap_pages = UINT_MAX, |
945 | .user_freq = UINT_MAX, | |
946 | .user_interval = ULLONG_MAX, | |
447a6013 | 947 | .freq = 4000, |
d1cb9fce NK |
948 | .target = { |
949 | .uses_mmap = true, | |
3aa5939d | 950 | .default_per_cpu = true, |
d1cb9fce | 951 | }, |
d20deb64 | 952 | }, |
e3d59112 NK |
953 | .tool = { |
954 | .sample = process_sample_event, | |
955 | .fork = perf_event__process_fork, | |
956 | .comm = perf_event__process_comm, | |
957 | .mmap = perf_event__process_mmap, | |
958 | .mmap2 = perf_event__process_mmap2, | |
959 | }, | |
d20deb64 | 960 | }; |
7865e817 | 961 | |
09b0fd45 | 962 | #define CALLCHAIN_HELP "setup and enables call-graph (stack chain/backtrace) recording: " |
61eaa3be | 963 | |
9ff125d1 | 964 | #ifdef HAVE_DWARF_UNWIND_SUPPORT |
aad2b21c | 965 | const char record_callchain_help[] = CALLCHAIN_HELP "fp dwarf lbr"; |
61eaa3be | 966 | #else |
aad2b21c | 967 | const char record_callchain_help[] = CALLCHAIN_HELP "fp lbr"; |
61eaa3be ACM |
968 | #endif |
969 | ||
d20deb64 ACM |
970 | /* |
971 | * XXX Will stay a global variable till we fix builtin-script.c to stop messing | |
972 | * with it and switch to use the library functions in perf_evlist that came | |
b4006796 | 973 | * from builtin-record.c, i.e. use record_opts, |
d20deb64 ACM |
974 | * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record', |
975 | * using pipes, etc. | |
976 | */ | |
e5b2c207 | 977 | struct option __record_options[] = { |
d20deb64 | 978 | OPT_CALLBACK('e', "event", &record.evlist, "event", |
86847b62 | 979 | "event selector. use 'perf list' to list available events", |
f120f9d5 | 980 | parse_events_option), |
d20deb64 | 981 | OPT_CALLBACK(0, "filter", &record.evlist, "filter", |
c171b552 | 982 | "event filter", parse_filter), |
bea03405 | 983 | OPT_STRING('p', "pid", &record.opts.target.pid, "pid", |
d6d901c2 | 984 | "record events on existing process id"), |
bea03405 | 985 | OPT_STRING('t', "tid", &record.opts.target.tid, "tid", |
d6d901c2 | 986 | "record events on existing thread id"), |
d20deb64 | 987 | OPT_INTEGER('r', "realtime", &record.realtime_prio, |
0e9b20b8 | 988 | "collect data with this RT SCHED_FIFO priority"), |
509051ea | 989 | OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, |
acac03fa | 990 | "collect data without buffering"), |
d20deb64 | 991 | OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, |
daac07b2 | 992 | "collect raw sample records from all opened counters"), |
bea03405 | 993 | OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, |
0e9b20b8 | 994 | "system-wide collection from all CPUs"), |
bea03405 | 995 | OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", |
c45c6ea2 | 996 | "list of cpus to monitor"), |
d20deb64 | 997 | OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), |
f5fc1412 | 998 | OPT_STRING('o', "output", &record.file.path, "file", |
abaff32a | 999 | "output file name"), |
69e7e5b0 AH |
1000 | OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, |
1001 | &record.opts.no_inherit_set, | |
1002 | "child tasks do not inherit counters"), | |
d20deb64 | 1003 | OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"), |
e9db1310 AH |
1004 | OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", |
1005 | "number of mmap data pages and AUX area tracing mmap pages", | |
1006 | record__parse_mmap_pages), | |
d20deb64 | 1007 | OPT_BOOLEAN(0, "group", &record.opts.group, |
43bece79 | 1008 | "put the counters into a counter group"), |
09b0fd45 JO |
1009 | OPT_CALLBACK_NOOPT('g', NULL, &record.opts, |
1010 | NULL, "enables call-graph recording" , | |
1011 | &record_callchain_opt), | |
1012 | OPT_CALLBACK(0, "call-graph", &record.opts, | |
1013 | "mode[,dump_size]", record_callchain_help, | |
1014 | &record_parse_callchain_opt), | |
c0555642 | 1015 | OPT_INCR('v', "verbose", &verbose, |
3da297a6 | 1016 | "be more verbose (show counter open errors, etc)"), |
b44308f5 | 1017 | OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), |
d20deb64 | 1018 | OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, |
649c48a9 | 1019 | "per thread counts"), |
d20deb64 | 1020 | OPT_BOOLEAN('d', "data", &record.opts.sample_address, |
4bba828d | 1021 | "Sample addresses"), |
d20deb64 | 1022 | OPT_BOOLEAN('T', "timestamp", &record.opts.sample_time, "Sample timestamps"), |
3e76ac78 | 1023 | OPT_BOOLEAN('P', "period", &record.opts.period, "Sample period"), |
d20deb64 | 1024 | OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, |
649c48a9 | 1025 | "don't sample"), |
d20deb64 | 1026 | OPT_BOOLEAN('N', "no-buildid-cache", &record.no_buildid_cache, |
a1ac1d3c | 1027 | "do not update the buildid cache"), |
d20deb64 | 1028 | OPT_BOOLEAN('B', "no-buildid", &record.no_buildid, |
baa2f6ce | 1029 | "do not collect buildids in perf.data"), |
d20deb64 | 1030 | OPT_CALLBACK('G', "cgroup", &record.evlist, "name", |
023695d9 SE |
1031 | "monitor event in cgroup name only", |
1032 | parse_cgroups), | |
a6205a35 | 1033 | OPT_UINTEGER('D', "delay", &record.opts.initial_delay, |
6619a53e | 1034 | "ms to wait before starting measurement after program start"), |
bea03405 NK |
1035 | OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", |
1036 | "user to profile"), | |
a5aabdac SE |
1037 | |
1038 | OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, | |
1039 | "branch any", "sample any taken branches", | |
1040 | parse_branch_stack), | |
1041 | ||
1042 | OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, | |
1043 | "branch filter mask", "branch stack filter modes", | |
bdfebd84 | 1044 | parse_branch_stack), |
05484298 AK |
1045 | OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, |
1046 | "sample by weight (on special events only)"), | |
475eeab9 AK |
1047 | OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, |
1048 | "sample transaction flags (special events only)"), | |
3aa5939d AH |
1049 | OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, |
1050 | "use per-thread mmaps"), | |
4b6c5177 SE |
1051 | OPT_BOOLEAN('I', "intr-regs", &record.opts.sample_intr_regs, |
1052 | "Sample machine registers on interrupt"), | |
85c273d2 AK |
1053 | OPT_BOOLEAN(0, "running-time", &record.opts.running_time, |
1054 | "Record running/enabled time of read (:S) events"), | |
814c8c38 PZ |
1055 | OPT_CALLBACK('k', "clockid", &record.opts, |
1056 | "clockid", "clockid to use for events, see clock_gettime()", | |
1057 | parse_clockid), | |
0e9b20b8 IM |
1058 | OPT_END() |
1059 | }; | |
1060 | ||
e5b2c207 NK |
1061 | struct option *record_options = __record_options; |
1062 | ||
1d037ca1 | 1063 | int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused) |
0e9b20b8 | 1064 | { |
ef149c25 | 1065 | int err; |
8c6f45a7 | 1066 | struct record *rec = &record; |
16ad2ffb | 1067 | char errbuf[BUFSIZ]; |
0e9b20b8 | 1068 | |
3e2be2da ACM |
1069 | rec->evlist = perf_evlist__new(); |
1070 | if (rec->evlist == NULL) | |
361c99a6 ACM |
1071 | return -ENOMEM; |
1072 | ||
eb853e80 JO |
1073 | perf_config(perf_record_config, rec); |
1074 | ||
bca647aa | 1075 | argc = parse_options(argc, argv, record_options, record_usage, |
655000e7 | 1076 | PARSE_OPT_STOP_AT_NON_OPTION); |
602ad878 | 1077 | if (!argc && target__none(&rec->opts.target)) |
bca647aa | 1078 | usage_with_options(record_usage, record_options); |
0e9b20b8 | 1079 | |
bea03405 | 1080 | if (nr_cgroups && !rec->opts.target.system_wide) { |
3780f488 NK |
1081 | ui__error("cgroup monitoring only available in" |
1082 | " system-wide mode\n"); | |
023695d9 SE |
1083 | usage_with_options(record_usage, record_options); |
1084 | } | |
1085 | ||
ef149c25 AH |
1086 | if (!rec->itr) { |
1087 | rec->itr = auxtrace_record__init(rec->evlist, &err); | |
1088 | if (err) | |
1089 | return err; | |
1090 | } | |
1091 | ||
1092 | err = -ENOMEM; | |
1093 | ||
0a7e6d1b | 1094 | symbol__init(NULL); |
baa2f6ce | 1095 | |
ec80fde7 | 1096 | if (symbol_conf.kptr_restrict) |
646aaea6 ACM |
1097 | pr_warning( |
1098 | "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" | |
1099 | "check /proc/sys/kernel/kptr_restrict.\n\n" | |
1100 | "Samples in kernel functions may not be resolved if a suitable vmlinux\n" | |
1101 | "file is not found in the buildid cache or in the vmlinux path.\n\n" | |
1102 | "Samples in kernel modules won't be resolved at all.\n\n" | |
1103 | "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" | |
1104 | "even with a suitable vmlinux or kallsyms file.\n\n"); | |
ec80fde7 | 1105 | |
d20deb64 | 1106 | if (rec->no_buildid_cache || rec->no_buildid) |
a1ac1d3c | 1107 | disable_buildid_cache(); |
655000e7 | 1108 | |
3e2be2da ACM |
1109 | if (rec->evlist->nr_entries == 0 && |
1110 | perf_evlist__add_default(rec->evlist) < 0) { | |
69aad6f1 ACM |
1111 | pr_err("Not enough memory for event selector list\n"); |
1112 | goto out_symbol_exit; | |
bbd36e5e | 1113 | } |
0e9b20b8 | 1114 | |
69e7e5b0 AH |
1115 | if (rec->opts.target.tid && !rec->opts.no_inherit_set) |
1116 | rec->opts.no_inherit = true; | |
1117 | ||
602ad878 | 1118 | err = target__validate(&rec->opts.target); |
16ad2ffb | 1119 | if (err) { |
602ad878 | 1120 | target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); |
16ad2ffb NK |
1121 | ui__warning("%s", errbuf); |
1122 | } | |
1123 | ||
602ad878 | 1124 | err = target__parse_uid(&rec->opts.target); |
16ad2ffb NK |
1125 | if (err) { |
1126 | int saved_errno = errno; | |
4bd0f2d2 | 1127 | |
602ad878 | 1128 | target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); |
3780f488 | 1129 | ui__error("%s", errbuf); |
16ad2ffb NK |
1130 | |
1131 | err = -saved_errno; | |
8fa60e1f | 1132 | goto out_symbol_exit; |
16ad2ffb | 1133 | } |
0d37aa34 | 1134 | |
16ad2ffb | 1135 | err = -ENOMEM; |
3e2be2da | 1136 | if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) |
dd7927f4 | 1137 | usage_with_options(record_usage, record_options); |
69aad6f1 | 1138 | |
ef149c25 AH |
1139 | err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); |
1140 | if (err) | |
1141 | goto out_symbol_exit; | |
1142 | ||
b4006796 | 1143 | if (record_opts__config(&rec->opts)) { |
39d17dac | 1144 | err = -EINVAL; |
03ad9747 | 1145 | goto out_symbol_exit; |
7e4ff9e3 MG |
1146 | } |
1147 | ||
d20deb64 | 1148 | err = __cmd_record(&record, argc, argv); |
d65a458b | 1149 | out_symbol_exit: |
45604710 | 1150 | perf_evlist__delete(rec->evlist); |
d65a458b | 1151 | symbol__exit(); |
ef149c25 | 1152 | auxtrace_record__free(rec->itr); |
39d17dac | 1153 | return err; |
0e9b20b8 | 1154 | } |