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.
12 #include "util/build-id.h"
13 #include "util/util.h"
14 #include <subcmd/parse-options.h>
15 #include "util/parse-events.h"
17 #include "util/callchain.h"
18 #include "util/cgroup.h"
19 #include "util/header.h"
20 #include "util/event.h"
21 #include "util/evlist.h"
22 #include "util/evsel.h"
23 #include "util/debug.h"
24 #include "util/session.h"
25 #include "util/tool.h"
26 #include "util/symbol.h"
27 #include "util/cpumap.h"
28 #include "util/thread_map.h"
29 #include "util/data.h"
30 #include "util/perf_regs.h"
31 #include "util/auxtrace.h"
33 #include "util/parse-branch-options.h"
34 #include "util/parse-regs-options.h"
35 #include "util/llvm-utils.h"
36 #include "util/bpf-loader.h"
45 struct perf_tool tool;
46 struct record_opts opts;
48 struct perf_data_file file;
49 struct auxtrace_record *itr;
50 struct perf_evlist *evlist;
51 struct perf_session *session;
56 bool no_buildid_cache;
57 bool no_buildid_cache_set;
59 bool timestamp_filename;
60 unsigned long long samples;
63 static int record__write(struct record *rec, void *bf, size_t size)
65 if (perf_data_file__write(rec->session->file, bf, size) < 0) {
66 pr_err("failed to write perf data, error: %m\n");
70 rec->bytes_written += size;
74 static int process_synthesized_event(struct perf_tool *tool,
75 union perf_event *event,
76 struct perf_sample *sample __maybe_unused,
77 struct machine *machine __maybe_unused)
79 struct record *rec = container_of(tool, struct record, tool);
80 return record__write(rec, event, event->header.size);
83 static int record__mmap_read(struct record *rec, int idx)
85 struct perf_mmap *md = &rec->evlist->mmap[idx];
86 u64 head = perf_mmap__read_head(md);
88 unsigned char *data = md->base + page_size;
100 if ((old & md->mask) + size != (head & md->mask)) {
101 buf = &data[old & md->mask];
102 size = md->mask + 1 - (old & md->mask);
105 if (record__write(rec, buf, size) < 0) {
111 buf = &data[old & md->mask];
115 if (record__write(rec, buf, size) < 0) {
121 perf_evlist__mmap_consume(rec->evlist, idx);
126 static volatile int done;
127 static volatile int signr = -1;
128 static volatile int child_finished;
130 static volatile enum {
131 AUXTRACE_SNAPSHOT_OFF = -1,
132 AUXTRACE_SNAPSHOT_DISABLED = 0,
133 AUXTRACE_SNAPSHOT_ENABLED = 1,
134 } auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_OFF;
137 auxtrace_snapshot_on(void)
139 auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_DISABLED;
143 auxtrace_snapshot_enable(void)
145 if (auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_OFF)
147 auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_ENABLED;
151 auxtrace_snapshot_disable(void)
153 if (auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_OFF)
155 auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_DISABLED;
159 auxtrace_snapshot_is_enabled(void)
161 if (auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_OFF)
163 return auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_ENABLED;
166 static volatile int auxtrace_snapshot_err;
167 static volatile int auxtrace_record__snapshot_started;
169 static void sig_handler(int sig)
179 static void record__sig_exit(void)
184 signal(signr, SIG_DFL);
188 #ifdef HAVE_AUXTRACE_SUPPORT
190 static int record__process_auxtrace(struct perf_tool *tool,
191 union perf_event *event, void *data1,
192 size_t len1, void *data2, size_t len2)
194 struct record *rec = container_of(tool, struct record, tool);
195 struct perf_data_file *file = &rec->file;
199 if (!perf_data_file__is_pipe(file)) {
201 int fd = perf_data_file__fd(file);
204 file_offset = lseek(fd, 0, SEEK_CUR);
205 if (file_offset == -1)
207 err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
213 /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
214 padding = (len1 + len2) & 7;
216 padding = 8 - padding;
218 record__write(rec, event, event->header.size);
219 record__write(rec, data1, len1);
221 record__write(rec, data2, len2);
222 record__write(rec, &pad, padding);
227 static int record__auxtrace_mmap_read(struct record *rec,
228 struct auxtrace_mmap *mm)
232 ret = auxtrace_mmap__read(mm, rec->itr, &rec->tool,
233 record__process_auxtrace);
243 static int record__auxtrace_mmap_read_snapshot(struct record *rec,
244 struct auxtrace_mmap *mm)
248 ret = auxtrace_mmap__read_snapshot(mm, rec->itr, &rec->tool,
249 record__process_auxtrace,
250 rec->opts.auxtrace_snapshot_size);
260 static int record__auxtrace_read_snapshot_all(struct record *rec)
265 for (i = 0; i < rec->evlist->nr_mmaps; i++) {
266 struct auxtrace_mmap *mm =
267 &rec->evlist->mmap[i].auxtrace_mmap;
272 if (record__auxtrace_mmap_read_snapshot(rec, mm) != 0) {
281 static void record__read_auxtrace_snapshot(struct record *rec)
283 pr_debug("Recording AUX area tracing snapshot\n");
284 if (record__auxtrace_read_snapshot_all(rec) < 0) {
285 auxtrace_snapshot_err = -1;
287 auxtrace_snapshot_err = auxtrace_record__snapshot_finish(rec->itr);
288 if (!auxtrace_snapshot_err)
289 auxtrace_snapshot_enable();
296 int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
297 struct auxtrace_mmap *mm __maybe_unused)
303 void record__read_auxtrace_snapshot(struct record *rec __maybe_unused)
308 int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
315 static int record__open(struct record *rec)
318 struct perf_evsel *pos;
319 struct perf_evlist *evlist = rec->evlist;
320 struct perf_session *session = rec->session;
321 struct record_opts *opts = &rec->opts;
324 perf_evlist__config(evlist, opts, &callchain_param);
326 evlist__for_each(evlist, pos) {
328 if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) {
329 if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) {
331 ui__warning("%s\n", msg);
336 perf_evsel__open_strerror(pos, &opts->target,
337 errno, msg, sizeof(msg));
338 ui__error("%s\n", msg);
343 if (perf_evlist__apply_filters(evlist, &pos)) {
344 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
345 pos->filter, perf_evsel__name(pos), errno,
346 strerror_r(errno, msg, sizeof(msg)));
351 if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false,
352 opts->auxtrace_mmap_pages,
353 opts->auxtrace_snapshot_mode) < 0) {
354 if (errno == EPERM) {
355 pr_err("Permission error mapping pages.\n"
356 "Consider increasing "
357 "/proc/sys/kernel/perf_event_mlock_kb,\n"
358 "or try again with a smaller value of -m/--mmap_pages.\n"
359 "(current value: %u,%u)\n",
360 opts->mmap_pages, opts->auxtrace_mmap_pages);
363 pr_err("failed to mmap with %d (%s)\n", errno,
364 strerror_r(errno, msg, sizeof(msg)));
373 session->evlist = evlist;
374 perf_session__set_id_hdr_size(session);
379 static int process_sample_event(struct perf_tool *tool,
380 union perf_event *event,
381 struct perf_sample *sample,
382 struct perf_evsel *evsel,
383 struct machine *machine)
385 struct record *rec = container_of(tool, struct record, tool);
389 return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
392 static int process_buildids(struct record *rec)
394 struct perf_data_file *file = &rec->file;
395 struct perf_session *session = rec->session;
401 * During this process, it'll load kernel map and replace the
402 * dso->long_name to a real pathname it found. In this case
403 * we prefer the vmlinux path like
404 * /lib/modules/3.16.4/build/vmlinux
406 * rather than build-id path (in debug directory).
407 * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
409 symbol_conf.ignore_vmlinux_buildid = true;
412 * If --buildid-all is given, it marks all DSO regardless of hits,
413 * so no need to process samples.
415 if (rec->buildid_all)
416 rec->tool.sample = NULL;
418 return perf_session__process_events(session);
421 static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
424 struct perf_tool *tool = data;
426 *As for guest kernel when processing subcommand record&report,
427 *we arrange module mmap prior to guest kernel mmap and trigger
428 *a preload dso because default guest module symbols are loaded
429 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
430 *method is used to avoid symbol missing when the first addr is
431 *in module instead of in guest kernel.
433 err = perf_event__synthesize_modules(tool, process_synthesized_event,
436 pr_err("Couldn't record guest kernel [%d]'s reference"
437 " relocation symbol.\n", machine->pid);
440 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
441 * have no _text sometimes.
443 err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
446 pr_err("Couldn't record guest kernel [%d]'s reference"
447 " relocation symbol.\n", machine->pid);
450 static struct perf_event_header finished_round_event = {
451 .size = sizeof(struct perf_event_header),
452 .type = PERF_RECORD_FINISHED_ROUND,
455 static int record__mmap_read_all(struct record *rec)
457 u64 bytes_written = rec->bytes_written;
461 for (i = 0; i < rec->evlist->nr_mmaps; i++) {
462 struct auxtrace_mmap *mm = &rec->evlist->mmap[i].auxtrace_mmap;
464 if (rec->evlist->mmap[i].base) {
465 if (record__mmap_read(rec, i) != 0) {
471 if (mm->base && !rec->opts.auxtrace_snapshot_mode &&
472 record__auxtrace_mmap_read(rec, mm) != 0) {
479 * Mark the round finished in case we wrote
480 * at least one event.
482 if (bytes_written != rec->bytes_written)
483 rc = record__write(rec, &finished_round_event, sizeof(finished_round_event));
489 static void record__init_features(struct record *rec)
491 struct perf_session *session = rec->session;
494 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
495 perf_header__set_feat(&session->header, feat);
498 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
500 if (!have_tracepoints(&rec->evlist->entries))
501 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
503 if (!rec->opts.branch_stack)
504 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
506 if (!rec->opts.full_auxtrace)
507 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
509 perf_header__clear_feat(&session->header, HEADER_STAT);
513 record__finish_output(struct record *rec)
515 struct perf_data_file *file = &rec->file;
516 int fd = perf_data_file__fd(file);
521 rec->session->header.data_size += rec->bytes_written;
522 file->size = lseek(perf_data_file__fd(file), 0, SEEK_CUR);
524 if (!rec->no_buildid) {
525 process_buildids(rec);
527 if (rec->buildid_all)
528 dsos__hit_all(rec->session);
530 perf_session__write_header(rec->session, rec->evlist, fd, true);
536 record__switch_output(struct record *rec, bool at_exit)
538 struct perf_data_file *file = &rec->file;
541 /* Same Size: "2015122520103046"*/
542 char timestamp[] = "InvalidTimestamp";
545 record__finish_output(rec);
546 err = fetch_current_timestamp(timestamp, sizeof(timestamp));
548 pr_err("Failed to get current timestamp\n");
552 fd = perf_data_file__switch(file, timestamp,
553 rec->session->header.data_offset,
555 if (fd >= 0 && !at_exit) {
556 rec->bytes_written = 0;
557 rec->session->header.data_size = 0;
561 fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
562 file->path, timestamp);
566 static volatile int workload_exec_errno;
569 * perf_evlist__prepare_workload will send a SIGUSR1
570 * if the fork fails, since we asked by setting its
571 * want_signal to true.
573 static void workload_exec_failed_signal(int signo __maybe_unused,
575 void *ucontext __maybe_unused)
577 workload_exec_errno = info->si_value.sival_int;
582 static void snapshot_sig_handler(int sig);
585 perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused,
586 struct perf_tool *tool __maybe_unused,
587 perf_event__handler_t process __maybe_unused,
588 struct machine *machine __maybe_unused)
593 static int record__synthesize(struct record *rec)
595 struct perf_session *session = rec->session;
596 struct machine *machine = &session->machines.host;
597 struct perf_data_file *file = &rec->file;
598 struct record_opts *opts = &rec->opts;
599 struct perf_tool *tool = &rec->tool;
600 int fd = perf_data_file__fd(file);
604 err = perf_event__synthesize_attrs(tool, session,
605 process_synthesized_event);
607 pr_err("Couldn't synthesize attrs.\n");
611 if (have_tracepoints(&rec->evlist->entries)) {
613 * FIXME err <= 0 here actually means that
614 * there were no tracepoints so its not really
615 * an error, just that we don't need to
616 * synthesize anything. We really have to
617 * return this more properly and also
618 * propagate errors that now are calling die()
620 err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist,
621 process_synthesized_event);
623 pr_err("Couldn't record tracing data.\n");
626 rec->bytes_written += err;
630 err = perf_event__synth_time_conv(rec->evlist->mmap[0].base, tool,
631 process_synthesized_event, machine);
635 if (rec->opts.full_auxtrace) {
636 err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
637 session, process_synthesized_event);
642 err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
644 WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
645 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
646 "Check /proc/kallsyms permission or run as root.\n");
648 err = perf_event__synthesize_modules(tool, process_synthesized_event,
650 WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
651 "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
652 "Check /proc/modules permission or run as root.\n");
655 machines__process_guests(&session->machines,
656 perf_event__synthesize_guest_os, tool);
659 err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads,
660 process_synthesized_event, opts->sample_address,
661 opts->proc_map_timeout);
666 static int __cmd_record(struct record *rec, int argc, const char **argv)
670 unsigned long waking = 0;
671 const bool forks = argc > 0;
672 struct machine *machine;
673 struct perf_tool *tool = &rec->tool;
674 struct record_opts *opts = &rec->opts;
675 struct perf_data_file *file = &rec->file;
676 struct perf_session *session;
677 bool disabled = false, draining = false;
680 rec->progname = argv[0];
682 atexit(record__sig_exit);
683 signal(SIGCHLD, sig_handler);
684 signal(SIGINT, sig_handler);
685 signal(SIGTERM, sig_handler);
687 if (rec->opts.auxtrace_snapshot_mode) {
688 signal(SIGUSR2, snapshot_sig_handler);
689 auxtrace_snapshot_on();
691 signal(SIGUSR2, SIG_IGN);
694 session = perf_session__new(file, false, tool);
695 if (session == NULL) {
696 pr_err("Perf session creation failed.\n");
700 fd = perf_data_file__fd(file);
701 rec->session = session;
703 record__init_features(rec);
706 err = perf_evlist__prepare_workload(rec->evlist, &opts->target,
708 workload_exec_failed_signal);
710 pr_err("Couldn't run the workload!\n");
712 goto out_delete_session;
716 if (record__open(rec) != 0) {
721 err = bpf__apply_obj_config();
725 bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
726 pr_err("ERROR: Apply config to BPF failed: %s\n",
732 * Normally perf_session__new would do this, but it doesn't have the
735 if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) {
736 pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
737 rec->tool.ordered_events = false;
740 if (!rec->evlist->nr_groups)
741 perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
744 err = perf_header__write_pipe(fd);
748 err = perf_session__write_header(session, rec->evlist, fd, false);
754 && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
755 pr_err("Couldn't generate buildids. "
756 "Use --no-buildid to profile anyway.\n");
761 machine = &session->machines.host;
763 err = record__synthesize(rec);
767 if (rec->realtime_prio) {
768 struct sched_param param;
770 param.sched_priority = rec->realtime_prio;
771 if (sched_setscheduler(0, SCHED_FIFO, ¶m)) {
772 pr_err("Could not set realtime priority.\n");
779 * When perf is starting the traced process, all the events
780 * (apart from group members) have enable_on_exec=1 set,
781 * so don't spoil it by prematurely enabling them.
783 if (!target__none(&opts->target) && !opts->initial_delay)
784 perf_evlist__enable(rec->evlist);
790 union perf_event *event;
792 event = malloc(sizeof(event->comm) + machine->id_hdr_size);
799 * Some H/W events are generated before COMM event
800 * which is emitted during exec(), so perf script
801 * cannot see a correct process name for those events.
802 * Synthesize COMM event to prevent it.
804 perf_event__synthesize_comm(tool, event,
805 rec->evlist->workload.pid,
806 process_synthesized_event,
810 perf_evlist__start_workload(rec->evlist);
813 if (opts->initial_delay) {
814 usleep(opts->initial_delay * 1000);
815 perf_evlist__enable(rec->evlist);
818 auxtrace_snapshot_enable();
820 unsigned long long hits = rec->samples;
822 if (record__mmap_read_all(rec) < 0) {
823 auxtrace_snapshot_disable();
828 if (auxtrace_record__snapshot_started) {
829 auxtrace_record__snapshot_started = 0;
830 if (!auxtrace_snapshot_err)
831 record__read_auxtrace_snapshot(rec);
832 if (auxtrace_snapshot_err) {
833 pr_err("AUX area tracing snapshot failed\n");
839 if (hits == rec->samples) {
840 if (done || draining)
842 err = perf_evlist__poll(rec->evlist, -1);
844 * Propagate error, only if there's any. Ignore positive
845 * number of returned events and interrupt error.
847 if (err > 0 || (err < 0 && errno == EINTR))
851 if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0)
856 * When perf is starting the traced process, at the end events
857 * die with the process and we wait for that. Thus no need to
858 * disable events in this case.
860 if (done && !disabled && !target__none(&opts->target)) {
861 auxtrace_snapshot_disable();
862 perf_evlist__disable(rec->evlist);
866 auxtrace_snapshot_disable();
868 if (forks && workload_exec_errno) {
869 char msg[STRERR_BUFSIZE];
870 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
871 pr_err("Workload failed: %s\n", emsg);
877 fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
884 kill(rec->evlist->workload.pid, SIGTERM);
890 else if (WIFEXITED(exit_status))
891 status = WEXITSTATUS(exit_status);
892 else if (WIFSIGNALED(exit_status))
893 signr = WTERMSIG(exit_status);
897 /* this will be recalculated during process_buildids() */
901 if (!rec->timestamp_filename) {
902 record__finish_output(rec);
904 fd = record__switch_output(rec, true);
907 goto out_delete_session;
912 if (!err && !quiet) {
914 const char *postfix = rec->timestamp_filename ?
917 if (rec->samples && !rec->opts.full_auxtrace)
918 scnprintf(samples, sizeof(samples),
919 " (%" PRIu64 " samples)", rec->samples);
923 fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s ]\n",
924 perf_data_file__size(file) / 1024.0 / 1024.0,
925 file->path, postfix, samples);
929 perf_session__delete(session);
933 static void callchain_debug(struct callchain_param *callchain)
935 static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
937 pr_debug("callchain: type %s\n", str[callchain->record_mode]);
939 if (callchain->record_mode == CALLCHAIN_DWARF)
940 pr_debug("callchain: stack dump size %d\n",
941 callchain->dump_size);
944 int record_opts__parse_callchain(struct record_opts *record,
945 struct callchain_param *callchain,
946 const char *arg, bool unset)
949 callchain->enabled = !unset;
951 /* --no-call-graph */
953 callchain->record_mode = CALLCHAIN_NONE;
954 pr_debug("callchain: disabled\n");
958 ret = parse_callchain_record_opt(arg, callchain);
960 /* Enable data address sampling for DWARF unwind. */
961 if (callchain->record_mode == CALLCHAIN_DWARF)
962 record->sample_address = true;
963 callchain_debug(callchain);
969 int record_parse_callchain_opt(const struct option *opt,
973 return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
976 int record_callchain_opt(const struct option *opt,
977 const char *arg __maybe_unused,
978 int unset __maybe_unused)
980 struct callchain_param *callchain = opt->value;
982 callchain->enabled = true;
984 if (callchain->record_mode == CALLCHAIN_NONE)
985 callchain->record_mode = CALLCHAIN_FP;
987 callchain_debug(callchain);
991 static int perf_record_config(const char *var, const char *value, void *cb)
993 struct record *rec = cb;
995 if (!strcmp(var, "record.build-id")) {
996 if (!strcmp(value, "cache"))
997 rec->no_buildid_cache = false;
998 else if (!strcmp(value, "no-cache"))
999 rec->no_buildid_cache = true;
1000 else if (!strcmp(value, "skip"))
1001 rec->no_buildid = true;
1006 if (!strcmp(var, "record.call-graph"))
1007 var = "call-graph.record-mode"; /* fall-through */
1009 return perf_default_config(var, value, cb);
1012 struct clockid_map {
1017 #define CLOCKID_MAP(n, c) \
1018 { .name = n, .clockid = (c), }
1020 #define CLOCKID_END { .name = NULL, }
1024 * Add the missing ones, we need to build on many distros...
1026 #ifndef CLOCK_MONOTONIC_RAW
1027 #define CLOCK_MONOTONIC_RAW 4
1029 #ifndef CLOCK_BOOTTIME
1030 #define CLOCK_BOOTTIME 7
1033 #define CLOCK_TAI 11
1036 static const struct clockid_map clockids[] = {
1037 /* available for all events, NMI safe */
1038 CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
1039 CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
1041 /* available for some events */
1042 CLOCKID_MAP("realtime", CLOCK_REALTIME),
1043 CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
1044 CLOCKID_MAP("tai", CLOCK_TAI),
1046 /* available for the lazy */
1047 CLOCKID_MAP("mono", CLOCK_MONOTONIC),
1048 CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
1049 CLOCKID_MAP("real", CLOCK_REALTIME),
1050 CLOCKID_MAP("boot", CLOCK_BOOTTIME),
1055 static int parse_clockid(const struct option *opt, const char *str, int unset)
1057 struct record_opts *opts = (struct record_opts *)opt->value;
1058 const struct clockid_map *cm;
1059 const char *ostr = str;
1062 opts->use_clockid = 0;
1070 /* no setting it twice */
1071 if (opts->use_clockid)
1074 opts->use_clockid = true;
1076 /* if its a number, we're done */
1077 if (sscanf(str, "%d", &opts->clockid) == 1)
1080 /* allow a "CLOCK_" prefix to the name */
1081 if (!strncasecmp(str, "CLOCK_", 6))
1084 for (cm = clockids; cm->name; cm++) {
1085 if (!strcasecmp(str, cm->name)) {
1086 opts->clockid = cm->clockid;
1091 opts->use_clockid = false;
1092 ui__warning("unknown clockid %s, check man page\n", ostr);
1096 static int record__parse_mmap_pages(const struct option *opt,
1098 int unset __maybe_unused)
1100 struct record_opts *opts = opt->value;
1102 unsigned int mmap_pages;
1117 ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s);
1120 opts->mmap_pages = mmap_pages;
1128 ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1);
1132 opts->auxtrace_mmap_pages = mmap_pages;
1139 static const char * const __record_usage[] = {
1140 "perf record [<options>] [<command>]",
1141 "perf record [<options>] -- <command> [<options>]",
1144 const char * const *record_usage = __record_usage;
1147 * XXX Ideally would be local to cmd_record() and passed to a record__new
1148 * because we need to have access to it in record__exit, that is called
1149 * after cmd_record() exits, but since record_options need to be accessible to
1150 * builtin-script, leave it here.
1152 * At least we don't ouch it in all the other functions here directly.
1154 * Just say no to tons of global variables, sigh.
1156 static struct record record = {
1158 .sample_time = true,
1159 .mmap_pages = UINT_MAX,
1160 .user_freq = UINT_MAX,
1161 .user_interval = ULLONG_MAX,
1165 .default_per_cpu = true,
1167 .proc_map_timeout = 500,
1170 .sample = process_sample_event,
1171 .fork = perf_event__process_fork,
1172 .exit = perf_event__process_exit,
1173 .comm = perf_event__process_comm,
1174 .mmap = perf_event__process_mmap,
1175 .mmap2 = perf_event__process_mmap2,
1176 .ordered_events = true,
1180 const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
1181 "\n\t\t\t\tDefault: fp";
1184 * XXX Will stay a global variable till we fix builtin-script.c to stop messing
1185 * with it and switch to use the library functions in perf_evlist that came
1186 * from builtin-record.c, i.e. use record_opts,
1187 * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
1190 struct option __record_options[] = {
1191 OPT_CALLBACK('e', "event", &record.evlist, "event",
1192 "event selector. use 'perf list' to list available events",
1193 parse_events_option),
1194 OPT_CALLBACK(0, "filter", &record.evlist, "filter",
1195 "event filter", parse_filter),
1196 OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
1197 NULL, "don't record events from perf itself",
1199 OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
1200 "record events on existing process id"),
1201 OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
1202 "record events on existing thread id"),
1203 OPT_INTEGER('r', "realtime", &record.realtime_prio,
1204 "collect data with this RT SCHED_FIFO priority"),
1205 OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
1206 "collect data without buffering"),
1207 OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
1208 "collect raw sample records from all opened counters"),
1209 OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
1210 "system-wide collection from all CPUs"),
1211 OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
1212 "list of cpus to monitor"),
1213 OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
1214 OPT_STRING('o', "output", &record.file.path, "file",
1215 "output file name"),
1216 OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
1217 &record.opts.no_inherit_set,
1218 "child tasks do not inherit counters"),
1219 OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
1220 OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
1221 "number of mmap data pages and AUX area tracing mmap pages",
1222 record__parse_mmap_pages),
1223 OPT_BOOLEAN(0, "group", &record.opts.group,
1224 "put the counters into a counter group"),
1225 OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
1226 NULL, "enables call-graph recording" ,
1227 &record_callchain_opt),
1228 OPT_CALLBACK(0, "call-graph", &record.opts,
1229 "record_mode[,record_size]", record_callchain_help,
1230 &record_parse_callchain_opt),
1231 OPT_INCR('v', "verbose", &verbose,
1232 "be more verbose (show counter open errors, etc)"),
1233 OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
1234 OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
1235 "per thread counts"),
1236 OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
1237 OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
1238 &record.opts.sample_time_set,
1239 "Record the sample timestamps"),
1240 OPT_BOOLEAN('P', "period", &record.opts.period, "Record the sample period"),
1241 OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
1243 OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
1244 &record.no_buildid_cache_set,
1245 "do not update the buildid cache"),
1246 OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
1247 &record.no_buildid_set,
1248 "do not collect buildids in perf.data"),
1249 OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
1250 "monitor event in cgroup name only",
1252 OPT_UINTEGER('D', "delay", &record.opts.initial_delay,
1253 "ms to wait before starting measurement after program start"),
1254 OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
1257 OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
1258 "branch any", "sample any taken branches",
1259 parse_branch_stack),
1261 OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
1262 "branch filter mask", "branch stack filter modes",
1263 parse_branch_stack),
1264 OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
1265 "sample by weight (on special events only)"),
1266 OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
1267 "sample transaction flags (special events only)"),
1268 OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
1269 "use per-thread mmaps"),
1270 OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
1271 "sample selected machine registers on interrupt,"
1272 " use -I ? to list register names", parse_regs),
1273 OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
1274 "Record running/enabled time of read (:S) events"),
1275 OPT_CALLBACK('k', "clockid", &record.opts,
1276 "clockid", "clockid to use for events, see clock_gettime()",
1278 OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
1279 "opts", "AUX area tracing Snapshot Mode", ""),
1280 OPT_UINTEGER(0, "proc-map-timeout", &record.opts.proc_map_timeout,
1281 "per thread proc mmap processing timeout in ms"),
1282 OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events,
1283 "Record context switch events"),
1284 OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
1285 "Configure all used events to run in kernel space.",
1286 PARSE_OPT_EXCLUSIVE),
1287 OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
1288 "Configure all used events to run in user space.",
1289 PARSE_OPT_EXCLUSIVE),
1290 OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
1291 "clang binary to use for compiling BPF scriptlets"),
1292 OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
1293 "options passed to clang when compiling BPF scriptlets"),
1294 OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
1295 "file", "vmlinux pathname"),
1296 OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
1297 "Record build-id of all DSOs regardless of hits"),
1298 OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
1299 "append timestamp to output filename"),
1303 struct option *record_options = __record_options;
1305 int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
1308 struct record *rec = &record;
1309 char errbuf[BUFSIZ];
1311 #ifndef HAVE_LIBBPF_SUPPORT
1312 # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
1313 set_nobuild('\0', "clang-path", true);
1314 set_nobuild('\0', "clang-opt", true);
1318 #ifndef HAVE_BPF_PROLOGUE
1319 # if !defined (HAVE_DWARF_SUPPORT)
1320 # define REASON "NO_DWARF=1"
1321 # elif !defined (HAVE_LIBBPF_SUPPORT)
1322 # define REASON "NO_LIBBPF=1"
1324 # define REASON "this architecture doesn't support BPF prologue"
1326 # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
1327 set_nobuild('\0', "vmlinux", true);
1332 rec->evlist = perf_evlist__new();
1333 if (rec->evlist == NULL)
1336 perf_config(perf_record_config, rec);
1338 argc = parse_options(argc, argv, record_options, record_usage,
1339 PARSE_OPT_STOP_AT_NON_OPTION);
1340 if (!argc && target__none(&rec->opts.target))
1341 usage_with_options(record_usage, record_options);
1343 if (nr_cgroups && !rec->opts.target.system_wide) {
1344 usage_with_options_msg(record_usage, record_options,
1345 "cgroup monitoring only available in system-wide mode");
1348 if (rec->opts.record_switch_events &&
1349 !perf_can_record_switch_events()) {
1350 ui__error("kernel does not support recording context switch events\n");
1351 parse_options_usage(record_usage, record_options, "switch-events", 0);
1356 rec->itr = auxtrace_record__init(rec->evlist, &err);
1361 err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
1362 rec->opts.auxtrace_snapshot_opts);
1366 err = bpf__setup_stdout(rec->evlist);
1368 bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
1369 pr_err("ERROR: Setup BPF stdout failed: %s\n",
1378 if (symbol_conf.kptr_restrict)
1380 "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
1381 "check /proc/sys/kernel/kptr_restrict.\n\n"
1382 "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
1383 "file is not found in the buildid cache or in the vmlinux path.\n\n"
1384 "Samples in kernel modules won't be resolved at all.\n\n"
1385 "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
1386 "even with a suitable vmlinux or kallsyms file.\n\n");
1388 if (rec->no_buildid_cache || rec->no_buildid)
1389 disable_buildid_cache();
1391 if (rec->evlist->nr_entries == 0 &&
1392 perf_evlist__add_default(rec->evlist) < 0) {
1393 pr_err("Not enough memory for event selector list\n");
1394 goto out_symbol_exit;
1397 if (rec->opts.target.tid && !rec->opts.no_inherit_set)
1398 rec->opts.no_inherit = true;
1400 err = target__validate(&rec->opts.target);
1402 target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
1403 ui__warning("%s", errbuf);
1406 err = target__parse_uid(&rec->opts.target);
1408 int saved_errno = errno;
1410 target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
1411 ui__error("%s", errbuf);
1414 goto out_symbol_exit;
1418 if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0)
1419 usage_with_options(record_usage, record_options);
1421 err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
1423 goto out_symbol_exit;
1426 * We take all buildids when the file contains
1427 * AUX area tracing data because we do not decode the
1428 * trace because it would take too long.
1430 if (rec->opts.full_auxtrace)
1431 rec->buildid_all = true;
1433 if (record_opts__config(&rec->opts)) {
1435 goto out_symbol_exit;
1438 err = __cmd_record(&record, argc, argv);
1440 perf_evlist__delete(rec->evlist);
1442 auxtrace_record__free(rec->itr);
1446 static void snapshot_sig_handler(int sig __maybe_unused)
1448 if (!auxtrace_snapshot_is_enabled())
1450 auxtrace_snapshot_disable();
1451 auxtrace_snapshot_err = auxtrace_record__snapshot_start(record.itr);
1452 auxtrace_record__snapshot_started = 1;