[PATCH v5 0/8] perf tools: Support overwritable ring buffer

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH v5 0/8] perf tools: Support overwritable ring buffer

Wang Nan-2
This patch set enables daemonized perf recording by utilizing
overwritable backward ring buffer. With this feature one can
put perf background, and dump ring buffer records by a SIGUSR2
when he/she find something unusual. For example, following
command record system calls, schedule events and samples on cpu cycles
continously:

 # perf record -g -e cycles -e raw_syscalls:*/call-graph=no/ \
                  -e sched:sched_switch/call-graph=no/ \
                  --switch-output --overwrite -a

Then by sending SIGUSR2 to perf when lagging is happen, we get multiple
perf.data output, each of them correspond a abnormal event, and the data
size is reasonable:

 # ls -l ./perf.data*
 -rw------- 1 root root 5122165 May 13 23:51 ./perf.data.2016051323511683
 -rw------- 1 root root 5135093 May 13 23:51 ./perf.data.2016051323512107
 -rw------- 1 root root 5135213 May 13 23:51 ./perf.data.2016051323512215
 -rw------- 1 root root 5135157 May 13 23:51 ./perf.data.2016051323512387

v1 -> v2: Totally redesign: drop the principle of 'channal', use
          auxiliary evlist instead. Fix missing documentation.

v2 -> v3: Rename perf_evlist__toggle_paused() to perf_evlist__pause/resume.

v3 -> v4: Update commit message to describe auxiliary evlist more clearly.

v4 -> v5: Reorder commits, ensure '--overwrite' works right after perf
          support the option.
          Add test cases for auxiliary evlist.
          Avoid bug if main evlist is empty.

Wang Nan (8):
  perf tools: Check 'base' pointer before checking refcnt when put a
    mmap
  perf tools: Choose correct reading direction according to
    evlist->backward
  perf tests: Add testcase for auxiliary evlist
  perf record: Introduce rec->overwrite_evlist for overwritable events
  perf record: Toggle overwrite ring buffer for reading
  perf tools: Enable overwrite settings
  perf tools: Don't warn about out of order event if write_backward is
    used
  perf tools: Check write_backward during evlist config

 tools/perf/Documentation/perf-record.txt |  14 ++
 tools/perf/builtin-record.c              | 285 +++++++++++++++++++++++++++----
 tools/perf/perf.h                        |   1 +
 tools/perf/tests/backward-ring-buffer.c  |  86 +++++++---
 tools/perf/util/evlist.c                 |  15 +-
 tools/perf/util/evlist.h                 |   2 +
 tools/perf/util/evsel.c                  |  27 +--
 tools/perf/util/evsel.h                  |  15 ++
 tools/perf/util/parse-events.c           |  20 ++-
 tools/perf/util/parse-events.h           |   2 +
 tools/perf/util/parse-events.l           |   2 +
 tools/perf/util/record.c                 |  17 ++
 tools/perf/util/session.c                |  22 ++-
 13 files changed, 435 insertions(+), 73 deletions(-)

Cc: Arnaldo Carvalho de Melo <[hidden email]>
Cc: He Kuang <[hidden email]>
Cc: Jiri Olsa <[hidden email]>
Cc: Masami Hiramatsu <[hidden email]>
Cc: Namhyung Kim <[hidden email]>
Cc: Peter Zijlstra <[hidden email]>
Cc: Wang Nan <[hidden email]>
Cc: Zefan Li <[hidden email]>
Cc: [hidden email]

--
1.8.3.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v5 1/8] perf tools: Check 'base' pointer before checking refcnt when put a mmap

Wang Nan-2
evlist->mmap[i]->refcnt could be 0 if an evlist has no evsel or all
evsels don't match the evlist during mmap. For example, when all evsels
are overwritable but the evlist itself is normal. To avoid crashing,
perf should check 'base' pointer before checking refcnt, and raise bug
only when base is not NULL.

Signed-off-by: Wang Nan <[hidden email]>
Cc: He Kuang <[hidden email]>
Cc: Arnaldo Carvalho de Melo <[hidden email]>
Cc: Jiri Olsa <[hidden email]>
Cc: Masami Hiramatsu <[hidden email]>
Cc: Namhyung Kim <[hidden email]>
Cc: Zefan Li <[hidden email]>
Cc: [hidden email]
---
 tools/perf/util/evlist.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index fbd0d47..f916d25 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -857,9 +857,11 @@ static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
 
 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
 {
- BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0);
+ struct perf_mmap *mmap = &evlist->mmap[idx];
 
- if (atomic_dec_and_test(&evlist->mmap[idx].refcnt))
+ BUG_ON(mmap->base && atomic_read(&mmap->refcnt) == 0);
+
+ if (atomic_dec_and_test(&mmap->refcnt))
  __perf_evlist__munmap(evlist, idx);
 }
 
--
1.8.3.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v5 2/8] perf tools: Choose correct reading direction according to evlist->backward

Wang Nan-2
In reply to this post by Wang Nan-2
Now we have evlist->backward to indicate the mmap direction. Make
perf_evlist__mmap_read() choose right direction automatically.

Signed-off-by: Wang Nan <[hidden email]>
Cc: Arnaldo Carvalho de Melo <[hidden email]>
Cc: Jiri Olsa <[hidden email]>
Cc: Masami Hiramatsu <[hidden email]>
Cc: Namhyung Kim <[hidden email]>
Cc: Zefan Li <[hidden email]>
Cc: He Kuang <[hidden email]>
---
 tools/perf/util/evlist.c | 9 ++++++++-
 tools/perf/util/evlist.h | 2 ++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index f916d25..8f7b44e 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -778,7 +778,7 @@ broken_event:
  return event;
 }
 
-union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
+union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx)
 {
  struct perf_mmap *md = &evlist->mmap[idx];
  u64 head;
@@ -833,6 +833,13 @@ perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx)
  return perf_mmap__read(md, false, start, end, &md->prev);
 }
 
+union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
+{
+ if (!evlist->backward)
+ return perf_evlist__mmap_read_forward(evlist, idx);
+ return perf_evlist__mmap_read_backward(evlist, idx);
+}
+
 void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx)
 {
  struct perf_mmap *md = &evlist->mmap[idx];
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 0505012..41e65ac 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -134,6 +134,8 @@ struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id);
 
 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx);
 
+union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist,
+ int idx);
 union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist,
   int idx);
 void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx);
--
1.8.3.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v5 3/8] perf tests: Add testcase for auxiliary evlist

Wang Nan-2
In reply to this post by Wang Nan-2
Improve test backward-ring-buffer, trace both enter and exit event of
prctl() syscall, utilize auxiliary evlist to mmap enter and exit event
into separated mmaps.

Signed-off-by: Wang Nan <[hidden email]>
Cc: Arnaldo Carvalho de Melo <[hidden email]>
Cc: Jiri Olsa <[hidden email]>
Cc: Masami Hiramatsu <[hidden email]>
Cc: Namhyung Kim <[hidden email]>
Cc: Zefan Li <[hidden email]>
Cc: He Kuang <[hidden email]>
---
 tools/perf/tests/backward-ring-buffer.c | 87 ++++++++++++++++++++++++++-------
 1 file changed, 68 insertions(+), 19 deletions(-)

diff --git a/tools/perf/tests/backward-ring-buffer.c b/tools/perf/tests/backward-ring-buffer.c
index d9ba991..76e34c0 100644
--- a/tools/perf/tests/backward-ring-buffer.c
+++ b/tools/perf/tests/backward-ring-buffer.c
@@ -31,16 +31,19 @@ static int count_samples(struct perf_evlist *evlist, int *sample_count,
  for (i = 0; i < evlist->nr_mmaps; i++) {
  union perf_event *event;
 
- perf_evlist__mmap_read_catchup(evlist, i);
- while ((event = perf_evlist__mmap_read_backward(evlist, i)) != NULL) {
+ if (evlist->backward)
+ perf_evlist__mmap_read_catchup(evlist, i);
+ while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
  const u32 type = event->header.type;
 
  switch (type) {
  case PERF_RECORD_SAMPLE:
- (*sample_count)++;
+ if (sample_count)
+ (*sample_count)++;
  break;
  case PERF_RECORD_COMM:
- (*comm_count)++;
+ if (comm_count)
+ (*comm_count)++;
  break;
  default:
  pr_err("Unexpected record of type %d\n", type);
@@ -51,34 +54,53 @@ static int count_samples(struct perf_evlist *evlist, int *sample_count,
  return TEST_OK;
 }
 
-static int do_test(struct perf_evlist *evlist, int mmap_pages,
-   int *sample_count, int *comm_count)
+static int do_test(struct perf_evlist *evlist,
+   struct perf_evlist *aux_evlist,
+   int mmap_pages,
+   int *enter_sample_count,
+   int *exit_sample_count,
+   int *comm_count)
 {
  int err;
  char sbuf[STRERR_BUFSIZE];
 
- err = perf_evlist__mmap(evlist, mmap_pages, true);
+ err = perf_evlist__mmap(evlist, mmap_pages, false);
  if (err < 0) {
  pr_debug("perf_evlist__mmap: %s\n",
  strerror_r(errno, sbuf, sizeof(sbuf)));
  return TEST_FAIL;
  }
 
+ err = perf_evlist__mmap(aux_evlist, mmap_pages, true);
+ if (err < 0) {
+ pr_debug("perf_evlist__mmap for aux_evlist: %s\n",
+ strerror_r(errno, sbuf, sizeof(sbuf)));
+ return TEST_FAIL;
+ }
+
  perf_evlist__enable(evlist);
  testcase();
  perf_evlist__disable(evlist);
 
- err = count_samples(evlist, sample_count, comm_count);
+ err = count_samples(aux_evlist, exit_sample_count, comm_count);
+ if (err)
+ goto errout;
+ err = count_samples(evlist, enter_sample_count, NULL);
+ if (err)
+ goto errout;
+errout:
  perf_evlist__munmap(evlist);
+ perf_evlist__munmap(aux_evlist);
  return err;
 }
 
 
 int test__backward_ring_buffer(int subtest __maybe_unused)
 {
- int ret = TEST_SKIP, err, sample_count = 0, comm_count = 0;
+ int ret = TEST_SKIP, err;
+ int enter_sample_count = 0, exit_sample_count = 0, comm_count = 0;
  char pid[16], sbuf[STRERR_BUFSIZE];
- struct perf_evlist *evlist;
+ struct perf_evlist *evlist, *aux_evlist = NULL;
  struct perf_evsel *evsel __maybe_unused;
  struct parse_events_error parse_error;
  struct record_opts opts = {
@@ -115,11 +137,22 @@ int test__backward_ring_buffer(int subtest __maybe_unused)
  goto out_delete_evlist;
  }
 
- perf_evlist__config(evlist, &opts, NULL);
+ /*
+ * Set backward bit, ring buffer should be writing from end. Record
+ * it in aux evlist
+ */
+ perf_evlist__last(evlist)->overwrite = true;
+ perf_evlist__last(evlist)->attr.write_backward = 1;
 
- /* Set backward bit, ring buffer should be writing from end */
- evlist__for_each(evlist, evsel)
- evsel->attr.write_backward = 1;
+ err = parse_events(evlist, "syscalls:sys_exit_prctl", &parse_error);
+ if (err) {
+ pr_debug("Failed to parse tracepoint event, try use root\n");
+ ret = TEST_SKIP;
+ goto out_delete_evlist;
+ }
+ /* Don't set backward bit for exit event. Record it in main evlist */
+
+ perf_evlist__config(evlist, &opts, NULL);
 
  err = perf_evlist__open(evlist);
  if (err < 0) {
@@ -128,24 +161,40 @@ int test__backward_ring_buffer(int subtest __maybe_unused)
  goto out_delete_evlist;
  }
 
+ aux_evlist = perf_evlist__new_aux(evlist);
+ if (!aux_evlist) {
+ pr_debug("perf_evlist__new_aux failed\n");
+ goto out_delete_evlist;
+ }
+ aux_evlist->backward = true;
+
  ret = TEST_FAIL;
- err = do_test(evlist, opts.mmap_pages, &sample_count,
+ err = do_test(evlist, aux_evlist, opts.mmap_pages,
+      &enter_sample_count, &exit_sample_count,
       &comm_count);
  if (err != TEST_OK)
  goto out_delete_evlist;
 
- if ((sample_count != NR_ITERS) || (comm_count != NR_ITERS)) {
- pr_err("Unexpected counter: sample_count=%d, comm_count=%d\n",
-       sample_count, comm_count);
+ if (enter_sample_count != exit_sample_count) {
+ pr_err("Unexpected counter: enter_sample_count=%d, exit_sample_count=%d\n",
+       enter_sample_count, exit_sample_count);
+ goto out_delete_evlist;
+ }
+
+ if ((exit_sample_count != NR_ITERS) || (comm_count != NR_ITERS)) {
+ pr_err("Unexpected counter: exit_sample_count=%d, comm_count=%d\n",
+       exit_sample_count, comm_count);
  goto out_delete_evlist;
  }
 
- err = do_test(evlist, 1, &sample_count, &comm_count);
+ err = do_test(evlist, aux_evlist, 1, NULL, NULL, NULL);
  if (err != TEST_OK)
  goto out_delete_evlist;
 
  ret = TEST_OK;
 out_delete_evlist:
+ if (aux_evlist)
+ perf_evlist__delete(aux_evlist);
  perf_evlist__delete(evlist);
  return ret;
 }
--
1.8.3.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v5 4/8] perf record: Introduce rec->overwrite_evlist for overwritable events

Wang Nan-2
In reply to this post by Wang Nan-2
Create an auxiliary evlist for overwritable events.

Before mmap, build this evlist and set 'overwrite' and 'backward'
attribute. Since perf_evlist__mmap_ex() only maps events when
evsel->overwrite matches evlist's corresponding attributes, with
these two evlists an event goes to either rec->evlist or
rec->overwrite_evlist.

Signed-off-by: Wang Nan <[hidden email]>
Cc: He Kuang <[hidden email]>
Cc: Arnaldo Carvalho de Melo <[hidden email]>
Cc: Jiri Olsa <[hidden email]>
Cc: Masami Hiramatsu <[hidden email]>
Cc: Namhyung Kim <[hidden email]>
Cc: Zefan Li <[hidden email]>
Cc: [hidden email]
---
 tools/perf/builtin-record.c | 138 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 114 insertions(+), 24 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index d4cf1b0..e7be4e1 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -50,6 +50,7 @@ struct record {
  struct perf_data_file file;
  struct auxtrace_record *itr;
  struct perf_evlist *evlist;
+ struct perf_evlist *overwrite_evlist;
  struct perf_session *session;
  const char *progname;
  int realtime_prio;
@@ -341,6 +342,84 @@ int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
 
 #endif
 
+static int record__create_overwrite_evlist(struct record *rec)
+{
+ struct perf_evlist *evlist = rec->evlist;
+ struct perf_evsel *pos;
+
+ evlist__for_each(evlist, pos) {
+ if (!pos->overwrite)
+ continue;
+
+ if (!rec->overwrite_evlist) {
+ rec->overwrite_evlist = perf_evlist__new_aux(evlist);
+ if (rec->overwrite_evlist) {
+ rec->overwrite_evlist->backward = true;
+ rec->overwrite_evlist->overwrite = true;
+ return 0;
+ } else
+ return -ENOMEM;
+ }
+ }
+ return 0;
+}
+
+static int record__mmap_evlist(struct record *rec,
+       struct perf_evlist *evlist,
+       bool overwrite)
+{
+ struct record_opts *opts = &rec->opts;
+ char msg[512];
+
+ /*
+ * Don't use evlist->overwrite because it is logically an
+ * internal attribute and is set by perf_evlist__mmap_ex().
+ * Avoid circular dependency.
+ */
+ if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, overwrite,
+ 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,
+ strerror_r(errno, msg, sizeof(msg)));
+ if (errno)
+ return -errno;
+ else
+ return -EINVAL;
+ }
+ }
+ return 0;
+}
+
+static int record__mmap(struct record *rec)
+{
+ int err;
+
+ err = record__create_overwrite_evlist(rec);
+ if (err)
+ return err;
+
+ err = record__mmap_evlist(rec, rec->evlist, false);
+ if (err)
+ return err;
+
+ if (!rec->overwrite_evlist)
+ return 0;
+
+ err = record__mmap_evlist(rec, rec->overwrite_evlist, true);
+ if (err)
+ return err;
+ return 0;
+}
+
 static int record__open(struct record *rec)
 {
  char msg[512];
@@ -353,6 +432,13 @@ static int record__open(struct record *rec)
  perf_evlist__config(evlist, opts, &callchain_param);
 
  evlist__for_each(evlist, pos) {
+ if (pos->overwrite) {
+ if (!pos->attr.write_backward) {
+ ui__warning("Unable to read from overwrite ring buffer\n\n");
+ rc = -ENOSYS;
+ goto out;
+ }
+ }
 try_again:
  if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) {
  if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) {
@@ -377,28 +463,9 @@ try_again:
  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);
 out:
@@ -655,10 +722,26 @@ 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)
 {
- if (rec->evlist && rec->evlist->mmap && rec->evlist->mmap[0].base)
- return rec->evlist->mmap[0].base;
+ const struct perf_event_mmap_page *pc;
+
+ /* Change it to a loop if a new aux evlist is added */
+ pc = perf_evlist__pick_pc(rec->evlist);
+ if (pc)
+ return pc;
+ pc = perf_evlist__pick_pc(rec->overwrite_evlist);
+ if (pc)
+ return pc;
+
  return NULL;
 }
 
@@ -930,6 +1013,8 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
  }
 
  if (hits == rec->samples) {
+ int active_evts;
+
  if (done || draining)
  break;
  err = perf_evlist__poll(rec->evlist, -1);
@@ -941,7 +1026,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
  err = 0;
  waking++;
 
- if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0)
+ active_evts = perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP);
+ active_evts += perf_evlist__filter_pollfd(rec->overwrite_evlist, POLLERR | POLLHUP);
+
+ if (active_evts == 0)
  draining = true;
  }
 
@@ -1565,6 +1653,8 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
  err = __cmd_record(&record, argc, argv);
 out_symbol_exit:
  perf_evlist__delete(rec->evlist);
+ if (rec->overwrite_evlist)
+ perf_evlist__delete(rec->overwrite_evlist);
  symbol__exit();
  auxtrace_record__free(rec->itr);
  return err;
--
1.8.3.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v5 5/8] perf record: Toggle overwrite ring buffer for reading

Wang Nan-2
In reply to this post by Wang Nan-2
overwrite_evt_state is introduced to reflect the state of overwritable
ring buffers. It is a state machine with 3 states:

 RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
    ^                  ^                 |
    |                  |___(disallow)___/|
    |                                    |
     \_________________(3)______________/

 RUNNING      : Overwritable ring buffers are recording
 DATA_PENDING : We are required to collect overwritable ring buffers
 EMPTY        : We have collected data from those ring buffers.

 (1): Pause ring buffers for reading
 (2): Read from ring buffers
 (3): Resume ring buffers for recording

We can't avoid this complexity. Because we deliberately drop records from
overwritable ring buffer, we can't detect remaining data by checking head
and old pointers. Therefore, DATA_PENDING state is mandatory.

Signed-off-by: Wang Nan <[hidden email]>
Signed-off-by: He Kuang <[hidden email]>
Cc: Arnaldo Carvalho de Melo <[hidden email]>
Cc: Jiri Olsa <[hidden email]>
Cc: Masami Hiramatsu <[hidden email]>
Cc: Namhyung Kim <[hidden email]>
Cc: Zefan Li <[hidden email]>
Cc: [hidden email]
---
 tools/perf/builtin-record.c | 146 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 136 insertions(+), 10 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index e7be4e1..d5bb947 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -42,6 +42,28 @@
 #include <sys/mman.h>
 #include <asm/bug.h>
 
+/*
+ * State machine of overwrite_evt_state:
+ *
+ * RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
+ *    ^                  ^                 |
+ *    |                  |___(disallow)___/|
+ *    |                                    |
+ *     \_________________(3)______________/
+ *
+ * RUNNING      : Overwritable ring buffers are recording
+ * DATA_PENDING : We are required to collect overwritable ring buffers
+ * EMPTY        : We have collected data from those ring buffers.
+ *
+ * (1): Pause ring buffers for reading
+ * (2): Read from ring buffers
+ * (3): Resume ring buffers for recording
+ */
+enum overwrite_evt_state {
+ OVERWRITE_EVT_RUNNING,
+ OVERWRITE_EVT_DATA_PENDING,
+ OVERWRITE_EVT_EMPTY,
+};
 
 struct record {
  struct perf_tool tool;
@@ -61,6 +83,7 @@ struct record {
  bool buildid_all;
  bool timestamp_filename;
  bool switch_output;
+ enum overwrite_evt_state overwrite_evt_state;
  unsigned long long samples;
 };
 
@@ -132,9 +155,9 @@ 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_evlist *evlist, int idx)
 {
- struct perf_mmap *md = &rec->evlist->mmap[idx];
+ struct perf_mmap *md = &evlist->mmap[idx];
  u64 head = perf_mmap__read_head(md);
  u64 old = md->prev;
  u64 end = head, start = old;
@@ -143,7 +166,7 @@ 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,
+ if (rb_find_range(evlist, data, md->mask, head,
   old, &start, &end))
  return -1;
 
@@ -157,7 +180,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_evlist__mmap_consume(evlist, idx);
  return 0;
  }
 
@@ -182,7 +205,7 @@ static int record__mmap_read(struct record *rec, int idx)
  }
 
  md->prev = head;
- perf_evlist__mmap_consume(rec->evlist, idx);
+ perf_evlist__mmap_consume(evlist, idx);
 out:
  return rc;
 }
@@ -468,6 +491,7 @@ try_again:
  goto out;
  session->evlist = evlist;
  perf_session__set_id_hdr_size(session);
+ rec->overwrite_evt_state = OVERWRITE_EVT_RUNNING;
 out:
  return rc;
 }
@@ -548,17 +572,72 @@ static struct perf_event_header finished_round_event = {
  .type = PERF_RECORD_FINISHED_ROUND,
 };
 
-static int record__mmap_read_all(struct record *rec)
+static void
+record__toggle_overwrite_evsels(struct record *rec,
+ enum overwrite_evt_state state)
+{
+ struct perf_evlist *evlist = rec->overwrite_evlist;
+ enum overwrite_evt_state old_state = rec->overwrite_evt_state;
+ enum action {
+ NONE,
+ PAUSE,
+ RESUME,
+ } action = NONE;
+
+ switch (old_state) {
+ case OVERWRITE_EVT_RUNNING:
+ if (state != OVERWRITE_EVT_RUNNING)
+ action = PAUSE;
+ break;
+ case OVERWRITE_EVT_DATA_PENDING:
+ if (state == OVERWRITE_EVT_RUNNING)
+ action = RESUME;
+ break;
+ case OVERWRITE_EVT_EMPTY:
+ if (state == OVERWRITE_EVT_RUNNING)
+ action = RESUME;
+ if (state == OVERWRITE_EVT_DATA_PENDING)
+ state = OVERWRITE_EVT_EMPTY;
+ break;
+ default:
+ WARN_ONCE(1, "Shouldn't get there\n");
+ }
+
+ rec->overwrite_evt_state = state;
+
+ if (action == NONE)
+ return;
+
+ if (!evlist)
+ return;
+
+ switch (action) {
+ case PAUSE:
+ perf_evlist__pause(evlist);
+ break;
+ case RESUME:
+ perf_evlist__resume(evlist);
+ break;
+ case NONE:
+ default:
+ break;
+ }
+}
+
+static int __record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist)
 {
  u64 bytes_written = rec->bytes_written;
  int i;
  int rc = 0;
 
- for (i = 0; i < rec->evlist->nr_mmaps; i++) {
- struct auxtrace_mmap *mm = &rec->evlist->mmap[i].auxtrace_mmap;
+ if (!evlist)
+ return 0;
+
+ for (i = 0; i < evlist->nr_mmaps; i++) {
+ struct auxtrace_mmap *mm = &evlist->mmap[i].auxtrace_mmap;
 
- if (rec->evlist->mmap[i].base) {
- if (record__mmap_read(rec, i) != 0) {
+ if (evlist->mmap[i].base) {
+ if (record__mmap_read(rec, evlist, i) != 0) {
  rc = -1;
  goto out;
  }
@@ -582,6 +661,23 @@ out:
  return rc;
 }
 
+static int record__mmap_read_all(struct record *rec)
+{
+ int err;
+
+ err = __record__mmap_read_evlist(rec, rec->evlist);
+ if (err)
+ return err;
+
+ if (rec->overwrite_evt_state == OVERWRITE_EVT_DATA_PENDING) {
+ err = __record__mmap_read_evlist(rec, rec->overwrite_evlist);
+ if (err)
+ return err;
+ record__toggle_overwrite_evsels(rec, OVERWRITE_EVT_EMPTY);
+ }
+ return 0;
+}
+
 static void record__init_features(struct record *rec)
 {
  struct perf_session *session = rec->session;
@@ -978,6 +1074,17 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
  for (;;) {
  unsigned long long hits = rec->samples;
 
+ /*
+ * rec->overwrite_evt_state is possible to be
+ * OVERWRITE_EVT_EMPTY here: when done == true and
+ * hits != rec->samples after previous reading.
+ *
+ * record__toggle_overwrite_evsels ensure we never
+ * convert OVERWRITE_EVT_EMPTY to OVERWRITE_EVT_DATA_PENDING.
+ */
+ if (trigger_is_hit(&switch_output_trigger) || done || draining)
+ record__toggle_overwrite_evsels(rec, OVERWRITE_EVT_DATA_PENDING);
+
  if (record__mmap_read_all(rec) < 0) {
  trigger_error(&auxtrace_snapshot_trigger);
  trigger_error(&switch_output_trigger);
@@ -997,8 +1104,27 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
  }
 
  if (trigger_is_hit(&switch_output_trigger)) {
+ /*
+ * If switch_output_trigger is hit, the data in
+ * overwritable ring buffer should have been collected,
+ * so overwrite_evt_state should be set to
+ * OVERWRITE_EVT_EMPTY.
+ *
+ * If SIGUSR2 raise after or during record__mmap_read_all(),
+ * record__mmap_read_all() didn't collect data from
+ * overwritable ring buffer. Read again.
+ */
+ if (rec->overwrite_evt_state == OVERWRITE_EVT_RUNNING)
+ continue;
  trigger_ready(&switch_output_trigger);
 
+ /*
+ * Reenable events in overwrite ring buffer after
+ * record__mmap_read_all(): we should have collected
+ * data from it.
+ */
+ record__toggle_overwrite_evsels(rec, OVERWRITE_EVT_RUNNING);
+
  if (!quiet)
  fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
  waking);
--
1.8.3.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v5 6/8] perf tools: Enable overwrite settings

Wang Nan-2
In reply to this post by Wang Nan-2
This patch allows following config terms and option:

Globally setting events to overwrite;

 # perf record --overwrite ...

Set specific events to be overwrite or no-overwrite.

 # perf record --event cycles/overwrite/ ...
 # perf record --event cycles/no-overwrite/ ...

Add missing config terms and update config term array size because the
longest string length is changed.

For overwritable events, automatically select attr.write_backward since
perf requires it to be backward for reading.

Test result:
 # perf record --overwrite -e syscalls:*enter_nanosleep* usleep 1
 [ perf record: Woken up 2 times to write data ]
 [ perf record: Captured and wrote 0.011 MB perf.data (1 samples) ]
 # perf evlist -v
 syscalls:sys_enter_nanosleep: type: 2, size: 112, config: 0x134, { sample_period, sample_freq }: 1, sample_type: IP|TID|TIME|CPU|PERIOD|RAW, disabled: 1, inherit: 1, mmap: 1, comm: 1, enable_on_exec: 1, task: 1, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1, write_backward: 1
 # Tip: use 'perf evlist --trace-fields' to show fields for tracepoint events

Signed-off-by: Wang Nan <[hidden email]>
Signed-off-by: He Kuang <[hidden email]>
Cc: Arnaldo Carvalho de Melo <[hidden email]>
Cc: Jiri Olsa <[hidden email]>
Cc: Masami Hiramatsu <[hidden email]>
Cc: Namhyung Kim <[hidden email]>
Cc: Zefan Li <[hidden email]>
Cc: [hidden email]
---
 tools/perf/Documentation/perf-record.txt | 14 ++++++++++++++
 tools/perf/builtin-record.c              |  1 +
 tools/perf/perf.h                        |  1 +
 tools/perf/tests/backward-ring-buffer.c  | 15 ++++++---------
 tools/perf/util/evsel.c                  | 12 ++++++++++++
 tools/perf/util/evsel.h                  |  2 ++
 tools/perf/util/parse-events.c           | 20 ++++++++++++++++++--
 tools/perf/util/parse-events.h           |  2 ++
 tools/perf/util/parse-events.l           |  2 ++
 9 files changed, 58 insertions(+), 11 deletions(-)

diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt
index 8dbee83..f5cb932 100644
--- a/tools/perf/Documentation/perf-record.txt
+++ b/tools/perf/Documentation/perf-record.txt
@@ -360,6 +360,20 @@ particular perf.data snapshot should be kept or not.
 
 Implies --timestamp-filename, --no-buildid and --no-buildid-cache.
 
+--overwrite::
+Makes all events use overwritable ring buffer. Event with overwritable ring
+buffer works like a flight recorder: when buffer gets full, instead of dumping
+records into output file, kernel overwrites old records silently. Perf dumps
+data from overwritable ring buffer when switching output (see --switch-output)
+and before terminate.
+
+Perf behaves like a daemon when '--overwrite' and '--switch-output' are
+provided. It record and drop events in background, and dumps data when
+something unusual is detected.
+
+'overwrite' attribute can also be set or canceled for specific event using
+config terms like 'cycles/overwrite/' and 'instructions/no-overwrite/'.
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-list[1]
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index d5bb947..bf63515 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -1524,6 +1524,7 @@ struct option __record_options[] = {
  OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
  &record.opts.no_inherit_set,
  "child tasks do not inherit counters"),
+ OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
  OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
  OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
      "number of mmap data pages and AUX area tracing mmap pages",
diff --git a/tools/perf/perf.h b/tools/perf/perf.h
index cd8f1b1..608b42b 100644
--- a/tools/perf/perf.h
+++ b/tools/perf/perf.h
@@ -59,6 +59,7 @@ struct record_opts {
  bool     record_switch_events;
  bool     all_kernel;
  bool     all_user;
+ bool     overwrite;
  unsigned int freq;
  unsigned int mmap_pages;
  unsigned int auxtrace_mmap_pages;
diff --git a/tools/perf/tests/backward-ring-buffer.c b/tools/perf/tests/backward-ring-buffer.c
index 76e34c0..bba0b83 100644
--- a/tools/perf/tests/backward-ring-buffer.c
+++ b/tools/perf/tests/backward-ring-buffer.c
@@ -130,27 +130,24 @@ int test__backward_ring_buffer(int subtest __maybe_unused)
  }
 
  bzero(&parse_error, sizeof(parse_error));
- err = parse_events(evlist, "syscalls:sys_enter_prctl", &parse_error);
+ /*
+ * Set backward bit, ring buffer should be writing from end. Record
+ * it in aux evlist
+ */
+ err = parse_events(evlist, "syscalls:sys_enter_prctl/overwrite/", &parse_error);
  if (err) {
  pr_debug("Failed to parse tracepoint event, try use root\n");
  ret = TEST_SKIP;
  goto out_delete_evlist;
  }
 
- /*
- * Set backward bit, ring buffer should be writing from end. Record
- * it in aux evlist
- */
- perf_evlist__last(evlist)->overwrite = true;
- perf_evlist__last(evlist)->attr.write_backward = 1;
-
+ /* Don't set backward bit for exit event. Record it in main evlist */
  err = parse_events(evlist, "syscalls:sys_exit_prctl", &parse_error);
  if (err) {
  pr_debug("Failed to parse tracepoint event, try use root\n");
  ret = TEST_SKIP;
  goto out_delete_evlist;
  }
- /* Don't set backward bit for exit event. Record it in main evlist */
 
  perf_evlist__config(evlist, &opts, NULL);
 
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 02c177d..6330a4f 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -671,11 +671,22 @@ static void apply_config_terms(struct perf_evsel *evsel,
  */
  attr->inherit = term->val.inherit ? 1 : 0;
  break;
+ case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
+ evsel->overwrite = term->val.overwrite ? 1 : 0;
+ break;
  default:
  break;
  }
  }
 
+ /*
+ * Set backward after config term processing because it is
+ * possible to set overwrite globally, without config
+ * terms.
+ */
+ if (evsel->overwrite)
+ attr->write_backward = 1;
+
  /* User explicitly set per-event callgraph, clear the old setting and reset. */
  if ((callgraph_buf != NULL) || (dump_size > 0)) {
 
@@ -747,6 +758,7 @@ void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,
 
  attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
  attr->inherit    = !opts->no_inherit;
+ evsel->overwrite    = opts->overwrite;
 
  perf_evsel__set_sample_bit(evsel, IP);
  perf_evsel__set_sample_bit(evsel, TID);
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index c1f1015..bce99fa 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -44,6 +44,7 @@ enum {
  PERF_EVSEL__CONFIG_TERM_CALLGRAPH,
  PERF_EVSEL__CONFIG_TERM_STACK_USER,
  PERF_EVSEL__CONFIG_TERM_INHERIT,
+ PERF_EVSEL__CONFIG_TERM_OVERWRITE,
  PERF_EVSEL__CONFIG_TERM_MAX,
 };
 
@@ -57,6 +58,7 @@ struct perf_evsel_config_term {
  char *callgraph;
  u64 stack_user;
  bool inherit;
+ bool overwrite;
  } val;
 };
 
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index bcbc983..85f813d 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -900,6 +900,8 @@ static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
  [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
  [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
  [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
+ [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
+ [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
 };
 
 static bool config_term_shrinked;
@@ -992,6 +994,12 @@ do {   \
  case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
  CHECK_TYPE_VAL(NUM);
  break;
+ case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
+ CHECK_TYPE_VAL(NUM);
+ break;
+ case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
+ CHECK_TYPE_VAL(NUM);
+ break;
  case PARSE_EVENTS__TERM_TYPE_NAME:
  CHECK_TYPE_VAL(STR);
  break;
@@ -1040,6 +1048,8 @@ static int config_term_tracepoint(struct perf_event_attr *attr,
  case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
  case PARSE_EVENTS__TERM_TYPE_INHERIT:
  case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
+ case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
+ case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
  return config_term_common(attr, term, err);
  default:
  if (err) {
@@ -1109,6 +1119,12 @@ do { \
  case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
  ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
  break;
+ case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
+ ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
+ break;
+ case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
+ ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
+ break;
  default:
  break;
  }
@@ -2322,9 +2338,9 @@ static void config_terms_list(char *buf, size_t buf_sz)
 char *parse_events_formats_error_string(char *additional_terms)
 {
  char *str;
- /* "branch_type" is the longest name */
+ /* "no-overwrite" is the longest name */
  char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
-  (sizeof("branch_type") - 1)];
+  (sizeof("no-overwrite") - 1)];
 
  config_terms_list(static_terms, sizeof(static_terms));
  /* valid terms */
diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
index d740c3c..f341d9d 100644
--- a/tools/perf/util/parse-events.h
+++ b/tools/perf/util/parse-events.h
@@ -68,6 +68,8 @@ enum {
  PARSE_EVENTS__TERM_TYPE_STACKSIZE,
  PARSE_EVENTS__TERM_TYPE_NOINHERIT,
  PARSE_EVENTS__TERM_TYPE_INHERIT,
+ PARSE_EVENTS__TERM_TYPE_NOOVERWRITE,
+ PARSE_EVENTS__TERM_TYPE_OVERWRITE,
  __PARSE_EVENTS__TERM_TYPE_NR,
 };
 
diff --git a/tools/perf/util/parse-events.l b/tools/perf/util/parse-events.l
index 1477fbc..cc4c426 100644
--- a/tools/perf/util/parse-events.l
+++ b/tools/perf/util/parse-events.l
@@ -201,6 +201,8 @@ call-graph { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
 stack-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
 inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
 no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
+overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
+no-overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
 , { return ','; }
 "/" { BEGIN(INITIAL); return '/'; }
 {name_minus} { return str(yyscanner, PE_NAME); }
--
1.8.3.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v5 7/8] perf tools: Don't warn about out of order event if write_backward is used

Wang Nan-2
In reply to this post by Wang Nan-2
If write_backward attribute is set, records are written into kernel
ring buffer from end to beginning, but read from beginning to end.
To avoid 'XX out of order events recorded' warning message (timestamps
of records is in reverse order when using write_backward), suppress the
warning message if write_backward is selected by at lease one event.

Result:

Before this patch:
 # perf record -m 1 -e raw_syscalls:sys_exit/overwrite/ \
                    -e raw_syscalls:sys_enter \
                    dd if=/dev/zero of=/dev/null count=300
 300+0 records in
 300+0 records out
 153600 bytes (154 kB) copied, 0.000601617 s, 255 MB/s
 [ perf record: Woken up 5 times to write data ]
 Warning:
 40 out of order events recorded.
 [ perf record: Captured and wrote 0.096 MB perf.data (696 samples) ]

After this patch:
 # perf record -m 1 -e raw_syscalls:sys_exit/overwrite/ \
                    -e raw_syscalls:sys_enter \
                    dd if=/dev/zero of=/dev/null count=300
 300+0 records in
 300+0 records out
 153600 bytes (154 kB) copied, 0.000644873 s, 238 MB/s
 [ perf record: Woken up 5 times to write data ]
 [ perf record: Captured and wrote 0.096 MB perf.data (696 samples) ]

Signed-off-by: Wang Nan <[hidden email]>
Signed-off-by: He Kuang <[hidden email]>
Cc: Arnaldo Carvalho de Melo <[hidden email]>
Cc: Jiri Olsa <[hidden email]>
Cc: Masami Hiramatsu <[hidden email]>
Cc: Namhyung Kim <[hidden email]>
Cc: Zefan Li <[hidden email]>
Cc: [hidden email]
---
 tools/perf/util/session.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 2335b28..8e3d9d4 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -1495,10 +1495,27 @@ int perf_session__register_idle_thread(struct perf_session *session)
  return err;
 }
 
+static void
+perf_session__warn_order(const struct perf_session *session)
+{
+ const struct ordered_events *oe = &session->ordered_events;
+ struct perf_evsel *evsel;
+ bool should_warn = true;
+
+ evlist__for_each(session->evlist, evsel) {
+ if (evsel->attr.write_backward)
+ should_warn = false;
+ }
+
+ if (!should_warn)
+ return;
+ if (oe->nr_unordered_events != 0)
+ ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
+}
+
 static void perf_session__warn_about_errors(const struct perf_session *session)
 {
  const struct events_stats *stats = &session->evlist->stats;
- const struct ordered_events *oe = &session->ordered_events;
 
  if (session->tool->lost == perf_event__process_lost &&
     stats->nr_events[PERF_RECORD_LOST] != 0) {
@@ -1555,8 +1572,7 @@ static void perf_session__warn_about_errors(const struct perf_session *session)
     stats->nr_unprocessable_samples);
  }
 
- if (oe->nr_unordered_events != 0)
- ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
+ perf_session__warn_order(session);
 
  events_stats__auxtrace_error_warn(stats);
 
--
1.8.3.4

Reply | Threaded
Open this post in threaded view
|

[PATCH v5 8/8] perf tools: Check write_backward during evlist config

Wang Nan-2
In reply to this post by Wang Nan-2
Before this patch, when using overwritable ring buffer on an old
kernel, error message is misleading:

 # ~/perf record -m 1 -e raw_syscalls:*/overwrite/ -a
 Error:
 The raw_syscalls:sys_enter event is not supported.

This patch output clear error message to tell user his/her kernel
is too old:

 # ~/perf record -m 1 -e raw_syscalls:*/overwrite/ -a
 Reading from overwrite event is not supported by this kernel
 Error:
 The raw_syscalls:sys_enter event is not supported.

Signed-off-by: Wang Nan <[hidden email]>
Cc: He Kuang <[hidden email]>
Cc: Arnaldo Carvalho de Melo <[hidden email]>
Cc: Jiri Olsa <[hidden email]>
Cc: Masami Hiramatsu <[hidden email]>
Cc: Namhyung Kim <[hidden email]>
Cc: Zefan Li <[hidden email]>
Cc: [hidden email]
---
 tools/perf/util/evsel.c  | 17 +++++------------
 tools/perf/util/evsel.h  | 13 +++++++++++++
 tools/perf/util/record.c | 17 +++++++++++++++++
 3 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 6330a4f..994310f 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -29,17 +29,7 @@
 #include "trace-event.h"
 #include "stat.h"
 
-static struct {
- bool sample_id_all;
- bool exclude_guest;
- bool mmap2;
- bool cloexec;
- bool clockid;
- bool clockid_wrong;
- bool lbr_flags;
- bool write_backward;
-} perf_missing_features;
-
+struct perf_missing_features perf_missing_features;
 static clockid_t clockid;
 
 static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
@@ -684,8 +674,11 @@ static void apply_config_terms(struct perf_evsel *evsel,
  * possible to set overwrite globally, without config
  * terms.
  */
- if (evsel->overwrite)
+ if (evsel->overwrite) {
+ WARN_ONCE(perf_missing_features.write_backward,
+  "Reading from overwrite event is not supported by this kernel\n");
  attr->write_backward = 1;
+ }
 
  /* User explicitly set per-event callgraph, clear the old setting and reset. */
  if ((callgraph_buf != NULL) || (dump_size > 0)) {
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index bce99fa..c9b6716 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -11,6 +11,19 @@
 #include "cpumap.h"
 #include "counts.h"
 
+struct perf_missing_features {
+ bool sample_id_all;
+ bool exclude_guest;
+ bool mmap2;
+ bool cloexec;
+ bool clockid;
+ bool clockid_wrong;
+ bool lbr_flags;
+ bool write_backward;
+};
+
+extern struct perf_missing_features perf_missing_features;
+
 struct perf_evsel;
 
 /*
diff --git a/tools/perf/util/record.c b/tools/perf/util/record.c
index 481792c..e3ab812 100644
--- a/tools/perf/util/record.c
+++ b/tools/perf/util/record.c
@@ -90,6 +90,11 @@ static void perf_probe_context_switch(struct perf_evsel *evsel)
  evsel->attr.context_switch = 1;
 }
 
+static void perf_probe_write_backward(struct perf_evsel *evsel)
+{
+ evsel->attr.write_backward = 1;
+}
+
 bool perf_can_sample_identifier(void)
 {
  return perf_probe_api(perf_probe_sample_identifier);
@@ -129,6 +134,17 @@ bool perf_can_record_cpu_wide(void)
  return true;
 }
 
+static void perf_check_write_backward(void)
+{
+ static bool checked = false;
+
+ if (!checked) {
+ perf_missing_features.write_backward =
+ !perf_probe_api(perf_probe_write_backward);
+ checked = true;
+ }
+}
+
 void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
  struct callchain_param *callchain)
 {
@@ -136,6 +152,7 @@ void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
  bool use_sample_identifier = false;
  bool use_comm_exec;
 
+ perf_check_write_backward();
  /*
  * Set the evsel leader links before we configure attributes,
  * since some might depend on this info.
--
1.8.3.4

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v5 4/8] perf record: Introduce rec->overwrite_evlist for overwritable events

Wang Nan-2
In reply to this post by Wang Nan-2


On 2016/5/25 13:03, Wang Nan wrote:

> Create an auxiliary evlist for overwritable events.
>
> Before mmap, build this evlist and set 'overwrite' and 'backward'
> attribute. Since perf_evlist__mmap_ex() only maps events when
> evsel->overwrite matches evlist's corresponding attributes, with
> these two evlists an event goes to either rec->evlist or
> rec->overwrite_evlist.
>
> Signed-off-by: Wang Nan <[hidden email]>
> Cc: He Kuang <[hidden email]>
> Cc: Arnaldo Carvalho de Melo <[hidden email]>
> Cc: Jiri Olsa <[hidden email]>
> Cc: Masami Hiramatsu <[hidden email]>
> Cc: Namhyung Kim <[hidden email]>
> Cc: Zefan Li <[hidden email]>
> Cc: [hidden email]
> ---
>   tools/perf/builtin-record.c | 138 ++++++++++++++++++++++++++++++++++++--------
>   1 file changed, 114 insertions(+), 24 deletions(-)
>

[SNIP]

> @@ -941,7 +1026,10 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
>   err = 0;
>   waking++;
>  
> - if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0)
> + active_evts = perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP);
> + active_evts += perf_evlist__filter_pollfd(rec->overwrite_evlist, POLLERR | POLLHUP);

Sorry, before calling filter here should check if rec->overwrite_evlist
is not NULL.
I'll resend this patch with this problem fixed soon.

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v5 4/8] perf record: Introduce rec->overwrite_evlist for overwritable events

Wang Nan-2

On 2016/5/25 18:16, Wangnan (F) wrote:

>
>
> On 2016/5/25 13:03, Wang Nan wrote:
>> Create an auxiliary evlist for overwritable events.
>>
>> Before mmap, build this evlist and set 'overwrite' and 'backward'
>> attribute. Since perf_evlist__mmap_ex() only maps events when
>> evsel->overwrite matches evlist's corresponding attributes, with
>> these two evlists an event goes to either rec->evlist or
>> rec->overwrite_evlist.
>>
>> Signed-off-by: Wang Nan <[hidden email]>
>> Cc: He Kuang <[hidden email]>
>> Cc: Arnaldo Carvalho de Melo <[hidden email]>
>> Cc: Jiri Olsa <[hidden email]>
>> Cc: Masami Hiramatsu <[hidden email]>
>> Cc: Namhyung Kim <[hidden email]>
>> Cc: Zefan Li <[hidden email]>
>> Cc: [hidden email]
>> ---
>>   tools/perf/builtin-record.c | 138
>> ++++++++++++++++++++++++++++++++++++--------
>>   1 file changed, 114 insertions(+), 24 deletions(-)
>>
>
> [SNIP]
>
>> @@ -941,7 +1026,10 @@ static int __cmd_record(struct record *rec, int
>> argc, const char **argv)
>>                   err = 0;
>>               waking++;
>>   -            if (perf_evlist__filter_pollfd(rec->evlist, POLLERR |
>> POLLHUP) == 0)
>> +            active_evts = perf_evlist__filter_pollfd(rec->evlist,
>> POLLERR | POLLHUP);
>> +            active_evts +=
>> perf_evlist__filter_pollfd(rec->overwrite_evlist, POLLERR | POLLHUP);
>
> Sorry, before calling filter here should check if
> rec->overwrite_evlist is not NULL.
> I'll resend this patch with this problem fixed soon.

Please ignore the full v5 series and see v6 instead. The final two patches
(9/10 and 10/10) in v6 solve the problem.

Thank you.

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v5 4/8] perf record: Introduce rec->overwrite_evlist for overwritable events

Arnaldo Carvalho de Melo-7
Em Wed, May 25, 2016 at 09:47:37PM +0800, Wangnan (F) escreveu:
> > Sorry, before calling filter here should check if rec->overwrite_evlist
> > is not NULL.
> > I'll resend this patch with this problem fixed soon.
>
> Please ignore the full v5 series and see v6 instead. The final two patches
> (9/10 and 10/10) in v6 solve the problem.

Ok

- Arnaldo