8 #define _FILE_OFFSET_BITS 64
71 pr_err(
"failed to write\n");
84 static int process_synthesized_event(
struct perf_tool *tool,
90 if (write_output(rec, event, event->
header.size) < 0)
96 static int perf_record__mmap_read(
struct perf_record *rec,
99 unsigned int head = perf_mmap__read_head(md);
100 unsigned int old = md->
prev;
113 if ((old & md->
mask) + size != (head & md->
mask)) {
114 buf = &data[old & md->
mask];
115 size = md->
mask + 1 - (old & md->
mask);
118 if (write_output(rec, buf, size) < 0) {
124 buf = &data[old & md->
mask];
128 if (write_output(rec, buf, size) < 0) {
134 perf_mmap__write_tail(md, old);
140 static volatile int done = 0;
141 static volatile int signr = -1;
142 static volatile int child_finished = 0;
153 static void perf_record__sig_exit(
int exit_status __maybe_unused,
void *
arg)
158 if (rec->
evlist->workload.pid > 0) {
163 if (WIFSIGNALED(status))
164 psignal(WTERMSIG(status), rec->
progname);
167 if (signr == -1 || signr ==
SIGUSR1)
171 kill(getpid(), signr);
182 pair = perf_evlist__first(other);
187 pair = perf_evsel__next(pair);
193 static int perf_record__open(
struct perf_record *rec)
222 fallback_missing_features:
236 pr_err(
"No such device - did you specify"
237 " an out-of-range profile CPU?\n");
240 }
else if (err ==
EINVAL) {
243 pr_debug(
"Old kernel, cannot exclude "
244 "guest or host samples.\n");
246 goto fallback_missing_features;
255 goto retry_sample_id;
273 "trying to fall back to cpu-clock-ticks\n");
284 ui__error(
"The %s event is not supported.\n",
291 error(
"sys_perf_event_open() syscall returned with %d "
292 "(%s) for event %s. /bin/dmesg may provide "
293 "additional information.\n",
296 #if defined(__i386__) || defined(__x86_64__)
299 pr_err(
"No hardware sampling interrupt available."
300 " No APIC? If so then you can boot the kernel"
301 " with the \"lapic\" boot parameter to"
302 " force-enable it.\n");
308 pr_err(
"No CONFIG_PERF_EVENTS=y kernel support configured?\n");
315 error(
"failed to set filter with %d (%s)\n", errno,
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"
330 pr_err(
"--mmap_pages/-m value must be a power of two.");
342 if (!perf_evlist__equal(session->
evlist, evlist)) {
343 fprintf(stderr,
"incompatible append\n");
354 static int process_buildids(
struct perf_record *rec)
367 static void perf_record__exit(
int status,
void *arg)
374 if (!rec->
opts.pipe_output) {
378 process_buildids(rec);
387 static void perf_event__synthesize_guest_os(
struct machine *
machine,
void *data)
392 if (machine__is_host(machine))
406 pr_err(
"Couldn't record guest kernel [%d]'s reference"
407 " relocation symbol.\n", machine->
pid);
419 pr_err(
"Couldn't record guest kernel [%d]'s reference"
420 " relocation symbol.\n", machine->
pid);
428 static int perf_record__mmap_read_all(
struct perf_record *rec)
433 for (i = 0; i < rec->
evlist->nr_mmaps; i++) {
434 if (rec->
evlist->mmap[i].base) {
435 if (perf_record__mmap_read(rec, &rec->
evlist->mmap[i]) != 0) {
443 rc = write_output(rec, &finished_round_event,
444 sizeof(finished_round_event));
450 static int __cmd_record(
struct perf_record *rec,
int argc,
const char **argv)
455 unsigned long waking = 0;
456 const bool forks = argc > 0;
468 on_exit(perf_record__sig_exit, rec);
480 if (!
strcmp(output_name,
"-"))
482 else if (!
stat(output_name, &
st) &&
st.st_size) {
485 snprintf(oldname,
sizeof(oldname),
"%s.old",
488 rename(output_name, oldname);
502 output = STDOUT_FILENO;
506 perror(
"failed to create output file");
514 if (session ==
NULL) {
515 pr_err(
"Not enough memory for reading perf file header\n");
530 if (!rec->
opts.branch_stack)
536 goto out_delete_session;
542 pr_err(
"Couldn't run the workload!\n");
543 goto out_delete_session;
547 if (perf_record__open(rec) != 0) {
549 goto out_delete_session;
555 on_exit(perf_record__exit, rec);
560 goto out_delete_session;
565 goto out_delete_session;
570 pr_err(
"Couldn't generate buildids. "
571 "Use --no-buildid to profile anyway.\n");
573 goto out_delete_session;
578 machine = perf_session__find_host_machine(session);
580 pr_err(
"Couldn't find native kernel information.\n");
582 goto out_delete_session;
587 process_synthesized_event);
589 pr_err(
"Couldn't synthesize attrs.\n");
590 goto out_delete_session;
596 pr_err(
"Couldn't synthesize event_types.\n");
597 goto out_delete_session;
610 process_synthesized_event);
612 pr_err(
"Couldn't record tracing data.\n");
613 goto out_delete_session;
615 advance_output(rec, err);
625 pr_err(
"Couldn't record kernel reference relocation symbol\n"
626 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
627 "Check /proc/kallsyms permission or run as root.\n");
632 pr_err(
"Couldn't record kernel module information.\n"
633 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
634 "Check /proc/modules permission or run as root.\n");
637 perf_session__process_machines(session, tool,
638 perf_event__synthesize_guest_os);
640 if (!opts->
target.system_wide)
642 process_synthesized_event,
649 goto out_delete_session;
656 pr_err(
"Could not set realtime priority.\n");
658 goto out_delete_session;
673 if (perf_record__mmap_read_all(rec) < 0) {
675 goto out_delete_session;
692 fprintf(stderr,
"[ perf record: Woken up %ld times to write data ]\n", waking);
698 "[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n",
710 #define BRANCH_OPT(n, m) \
711 { .name = n, .mode = (m) }
713 #define BRANCH_END { .name = NULL }
732 parse_branch_stack(
const struct option *
opt,
const char *
str,
int unset)
735 (PERF_SAMPLE_BRANCH_USER |\
736 PERF_SAMPLE_BRANCH_KERNEL |\
737 PERF_SAMPLE_BRANCH_HV)
756 s =
os = strdup(str);
765 for (br = branch_modes; br->
name; br++) {
771 " check man page\n", s);
786 if ((*mode & ~ONLY_PLM) == 0) {
794 #ifdef LIBUNWIND_SUPPORT
795 static int get_stack_size(
char *str,
unsigned long *
_size)
801 size = strtoul(str, &endptr, 0);
808 if (!size || size > max_size)
816 pr_err(
"callchain: Incorrect stack dump size (max %ld): %s\n",
823 parse_callchain_opt(
const struct option *opt __maybe_unused,
const char *arg,
845 tok = strtok_r((
char *)buf,
",", &saveptr);
846 name = tok ? : (
char *)buf;
851 if (!strtok_r(
NULL,
",", &saveptr)) {
855 pr_err(
"callchain: No more arguments "
856 "needed for -g fp\n");
859 #ifdef LIBUNWIND_SUPPORT
861 }
else if (!
strncmp(
name,
"dwarf",
sizeof(
"dwarf"))) {
862 const unsigned long default_stack_dump_size = 8192;
866 rec->
opts.stack_dump_size = default_stack_dump_size;
868 tok = strtok_r(
NULL,
",", &saveptr);
870 unsigned long size = 0;
872 ret = get_stack_size(tok, &size);
877 pr_debug(
"callchain: stack dump size %d\n",
878 rec->
opts.stack_dump_size);
881 pr_err(
"callchain: Unknown -g option "
896 static const char *
const record_usage[] = {
897 "perf record [<options>] [<command>]",
898 "perf record [<options>] -- <command> [<options>]",
926 #define CALLCHAIN_HELP "do call-graph (stack chain/backtrace) recording: "
928 #ifdef LIBUNWIND_SUPPORT
943 "event selector. use 'perf list' to list available events",
948 "record events on existing process id"),
950 "record events on existing thread id"),
952 "collect data with this RT SCHED_FIFO priority"),
954 "collect data without buffering"),
956 "collect raw sample records from all opened counters"),
958 "system-wide collection from all CPUs"),
960 "append to the output file to do incremental profiling"),
962 "list of cpus to monitor"),
964 "overwrite existing data file (deprecated)"),
965 OPT_U64(
'c',
"count", &record.
opts.user_interval,
"event period to sample"),
969 "child tasks do not inherit counters"),
970 OPT_UINTEGER(
'F',
"freq", &record.
opts.user_freq,
"profile at this frequency"),
972 "number of mmap data pages"),
974 "put the counters into a counter group"),
976 callchain_help, &parse_callchain_opt,
979 "be more verbose (show counter open errors, etc)"),
982 "per thread counts"),
985 OPT_BOOLEAN(
'T',
"timestamp", &record.
opts.sample_time,
"Sample timestamps"),
990 "do not update the buildid cache"),
992 "do not collect buildids in perf.data"),
994 "monitor event in cgroup name only",
1000 "branch any",
"sample any taken branches",
1001 parse_branch_stack),
1004 "branch filter mask",
"branch stack filter modes",
1005 parse_branch_stack),
1015 char errbuf[BUFSIZ];
1018 if (evsel_list ==
NULL)
1021 rec->
evlist = evsel_list;
1023 argc =
parse_options(argc, argv, record_options, record_usage,
1025 if (!argc && perf_target__none(&rec->
opts.target))
1029 ui__error(
"Can't overwrite and append at the same time."
1030 " You need to choose between -f and -A");
1039 ui__error(
"cgroup monitoring only available in"
1040 " system-wide mode\n");
1048 "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
1049 "check /proc/sys/kernel/kptr_restrict.\n\n"
1050 "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
1051 "file is not found in the buildid cache or in the vmlinux path.\n\n"
1052 "Samples in kernel modules won't be resolved at all.\n\n"
1053 "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
1054 "even with a suitable vmlinux or kallsyms file.\n\n");
1061 pr_err(
"Not enough memory for event selector list\n");
1062 goto out_symbol_exit;
1092 rec->
opts.default_interval = rec->
opts.user_interval;
1094 rec->
opts.freq = rec->
opts.user_freq;
1099 if (rec->
opts.default_interval)
1101 else if (rec->
opts.freq) {
1102 rec->
opts.default_interval = rec->
opts.freq;
1104 ui__error(
"frequency and count are zero, aborting\n");
1109 err = __cmd_record(&record, argc, argv);