perf record: Decouple record__mmap_read() and evlist.
[cascardo/linux.git] / tools / perf / builtin-record.c
index dc3fcb5..d15517e 100644 (file)
@@ -13,6 +13,7 @@
 #include "util/util.h"
 #include <subcmd/parse-options.h>
 #include "util/parse-events.h"
+#include "util/config.h"
 
 #include "util/callchain.h"
 #include "util/cgroup.h"
@@ -118,11 +119,10 @@ backward_rb_find_range(void *buf, int mask, u64 head, u64 *start, u64 *end)
 }
 
 static int
-rb_find_range(struct perf_evlist *evlist,
-             void *data, int mask, u64 head, u64 old,
-             u64 *start, u64 *end)
+rb_find_range(void *data, int mask, u64 head, u64 old,
+             u64 *start, u64 *end, bool backward)
 {
-       if (!evlist->backward) {
+       if (!backward) {
                *start = old;
                *end = head;
                return 0;
@@ -131,9 +131,10 @@ rb_find_range(struct perf_evlist *evlist,
        return backward_rb_find_range(data, mask, head, start, end);
 }
 
-static int record__mmap_read(struct record *rec, int idx)
+static int
+record__mmap_read(struct record *rec, struct perf_mmap *md,
+                 bool overwrite, bool backward)
 {
-       struct perf_mmap *md = &rec->evlist->mmap[idx];
        u64 head = perf_mmap__read_head(md);
        u64 old = md->prev;
        u64 end = head, start = old;
@@ -142,8 +143,8 @@ static int record__mmap_read(struct record *rec, int idx)
        void *buf;
        int rc = 0;
 
-       if (rb_find_range(rec->evlist, data, md->mask, head,
-                         old, &start, &end))
+       if (rb_find_range(data, md->mask, head,
+                         old, &start, &end, backward))
                return -1;
 
        if (start == end)
@@ -156,7 +157,7 @@ static int record__mmap_read(struct record *rec, int idx)
                WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");
 
                md->prev = head;
-               perf_evlist__mmap_consume(rec->evlist, idx);
+               perf_mmap__consume(md, overwrite || backward);
                return 0;
        }
 
@@ -181,7 +182,7 @@ static int record__mmap_read(struct record *rec, int idx)
        }
 
        md->prev = head;
-       perf_evlist__mmap_consume(rec->evlist, idx);
+       perf_mmap__consume(md, overwrite || backward);
 out:
        return rc;
 }
@@ -341,6 +342,40 @@ int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
 
 #endif
 
+static int record__mmap_evlist(struct record *rec,
+                              struct perf_evlist *evlist)
+{
+       struct record_opts *opts = &rec->opts;
+       char msg[512];
+
+       if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false,
+                                opts->auxtrace_mmap_pages,
+                                opts->auxtrace_snapshot_mode) < 0) {
+               if (errno == EPERM) {
+                       pr_err("Permission error mapping pages.\n"
+                              "Consider increasing "
+                              "/proc/sys/kernel/perf_event_mlock_kb,\n"
+                              "or try again with a smaller value of -m/--mmap_pages.\n"
+                              "(current value: %u,%u)\n",
+                              opts->mmap_pages, opts->auxtrace_mmap_pages);
+                       return -errno;
+               } else {
+                       pr_err("failed to mmap with %d (%s)\n", errno,
+                               str_error_r(errno, msg, sizeof(msg)));
+                       if (errno)
+                               return -errno;
+                       else
+                               return -EINVAL;
+               }
+       }
+       return 0;
+}
+
+static int record__mmap(struct record *rec)
+{
+       return record__mmap_evlist(rec, rec->evlist);
+}
+
 static int record__open(struct record *rec)
 {
        char msg[512];
@@ -352,7 +387,7 @@ static int record__open(struct record *rec)
 
        perf_evlist__config(evlist, opts, &callchain_param);
 
-       evlist__for_each(evlist, pos) {
+       evlist__for_each_entry(evlist, pos) {
 try_again:
                if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) {
                        if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) {
@@ -372,32 +407,14 @@ try_again:
        if (perf_evlist__apply_filters(evlist, &pos)) {
                error("failed to set filter \"%s\" on event %s with %d (%s)\n",
                        pos->filter, perf_evsel__name(pos), errno,
-                       strerror_r(errno, msg, sizeof(msg)));
+                       str_error_r(errno, msg, sizeof(msg)));
                rc = -1;
                goto out;
        }
 
-       if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false,
-                                opts->auxtrace_mmap_pages,
-                                opts->auxtrace_snapshot_mode) < 0) {
-               if (errno == EPERM) {
-                       pr_err("Permission error mapping pages.\n"
-                              "Consider increasing "
-                              "/proc/sys/kernel/perf_event_mlock_kb,\n"
-                              "or try again with a smaller value of -m/--mmap_pages.\n"
-                              "(current value: %u,%u)\n",
-                              opts->mmap_pages, opts->auxtrace_mmap_pages);
-                       rc = -errno;
-               } else {
-                       pr_err("failed to mmap with %d (%s)\n", errno,
-                               strerror_r(errno, msg, sizeof(msg)));
-                       if (errno)
-                               rc = -errno;
-                       else
-                               rc = -EINVAL;
-               }
+       rc = record__mmap(rec);
+       if (rc)
                goto out;
-       }
 
        session->evlist = evlist;
        perf_session__set_id_hdr_size(session);
@@ -481,17 +498,27 @@ static struct perf_event_header finished_round_event = {
        .type = PERF_RECORD_FINISHED_ROUND,
 };
 
-static int record__mmap_read_all(struct record *rec)
+static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist,
+                                   bool backward)
 {
        u64 bytes_written = rec->bytes_written;
        int i;
        int rc = 0;
+       struct perf_mmap *maps;
 
-       for (i = 0; i < rec->evlist->nr_mmaps; i++) {
-               struct auxtrace_mmap *mm = &rec->evlist->mmap[i].auxtrace_mmap;
+       if (!evlist)
+               return 0;
+
+       maps = evlist->mmap;
+       if (!maps)
+               return 0;
 
-               if (rec->evlist->mmap[i].base) {
-                       if (record__mmap_read(rec, i) != 0) {
+       for (i = 0; i < evlist->nr_mmaps; i++) {
+               struct auxtrace_mmap *mm = &maps[i].auxtrace_mmap;
+
+               if (maps[i].base) {
+                       if (record__mmap_read(rec, &maps[i],
+                                             evlist->overwrite, backward) != 0) {
                                rc = -1;
                                goto out;
                        }
@@ -515,6 +542,17 @@ out:
        return rc;
 }
 
+static int record__mmap_read_all(struct record *rec)
+{
+       int err;
+
+       err = record__mmap_read_evlist(rec, rec->evlist, false);
+       if (err)
+               return err;
+
+       return err;
+}
+
 static void record__init_features(struct record *rec)
 {
        struct perf_session *session = rec->session;
@@ -655,6 +693,24 @@ perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused
        return 0;
 }
 
+static const struct perf_event_mmap_page *
+perf_evlist__pick_pc(struct perf_evlist *evlist)
+{
+       if (evlist && evlist->mmap && evlist->mmap[0].base)
+               return evlist->mmap[0].base;
+       return NULL;
+}
+
+static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
+{
+       const struct perf_event_mmap_page *pc;
+
+       pc = perf_evlist__pick_pc(rec->evlist);
+       if (pc)
+               return pc;
+       return NULL;
+}
+
 static int record__synthesize(struct record *rec)
 {
        struct perf_session *session = rec->session;
@@ -692,7 +748,7 @@ static int record__synthesize(struct record *rec)
                }
        }
 
-       err = perf_event__synth_time_conv(rec->evlist->mmap[0].base, tool,
+       err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
                                          process_synthesized_event, machine);
        if (err)
                goto out;
@@ -954,7 +1010,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 
        if (forks && workload_exec_errno) {
                char msg[STRERR_BUFSIZE];
-               const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
+               const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
                pr_err("Workload failed: %s\n", emsg);
                err = -1;
                goto out_child;
@@ -1267,6 +1323,8 @@ static struct record record = {
 const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
        "\n\t\t\t\tDefault: fp";
 
+static bool dry_run;
+
 /*
  * XXX Will stay a global variable till we fix builtin-script.c to stop messing
  * with it and switch to use the library functions in perf_evlist that came
@@ -1386,6 +1444,8 @@ struct option __record_options[] = {
                    "append timestamp to output filename"),
        OPT_BOOLEAN(0, "switch-output", &record.switch_output,
                    "Switch output when receive SIGUSR2"),
+       OPT_BOOLEAN(0, "dry-run", &dry_run,
+                   "Parse options then exit"),
        OPT_END()
 };
 
@@ -1455,6 +1515,9 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
        if (err)
                return err;
 
+       if (dry_run)
+               return 0;
+
        err = bpf__setup_stdout(rec->evlist);
        if (err) {
                bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));