linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wang Nan <wangnan0@huawei.com>
To: <acme@kernel.org>
Cc: <lizefan@huawei.com>, <linux-kernel@vger.kernel.org>,
	<pi3orama@163.com>, Wang Nan <wangnan0@huawei.com>,
	He Kuang <hekuang@huawei.com>,
	"Arnaldo Carvalho de Melo" <acme@redhat.com>,
	Jiri Olsa <jolsa@kernel.org>,
	"Masami Hiramatsu" <mhiramat@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	"Nilay Vaish" <nilayvaish@gmail.com>
Subject: [PATCH v16 10/15] perf tools: Setup backward mmap state machine
Date: Thu, 14 Jul 2016 08:34:42 +0000	[thread overview]
Message-ID: <1468485287-33422-11-git-send-email-wangnan0@huawei.com> (raw)
In-Reply-To: <1468485287-33422-1-git-send-email-wangnan0@huawei.com>

Introduce a bkw_mmap_state state machine to evlist:

                     .________________(forbid)_____________.
                     |                                     V
 NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
                     ^  ^              |   ^               |
                     |  |__(forbid)____/   |___(forbid)___/|
                     |                                     |
                      \_________________(3)_______________/

 NOTREADY     : Backward ring buffers are not ready
 RUNNING      : Backward ring buffers are recording
 DATA_PENDING : We are required to collect data from backward ring buffers
 EMPTY        : We have collected data from backward ring buffers.

 (0): Setup backward ring buffer
 (1): Pause ring buffers for reading
 (2): Read from ring buffers
 (3): Resume ring buffers for recording

We can't avoid this complexity. Since we deliberately drop records from
overwritable ring buffer, there's no way for us to check remaining from
ring buffer itself (by checking head and old pointers). Therefore, we
need DATA_PENDING and EMPTY state to help us recording what we have done
to the ring buffer.

In record__mmap_read_evlist(), drive this state machine from DATA_PENDING
to EMPTY.

In perf_evlist__mmap_per_evsel(), drive this state machine from NOTREADY
to RUNNING when creating backward mmap.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: Nilay Vaish <nilayvaish@gmail.com>
Cc: pi3orama@163.com
---
 tools/perf/builtin-record.c |  5 ++++
 tools/perf/util/evlist.c    | 63 +++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/evlist.h    | 32 +++++++++++++++++++++++
 3 files changed, 100 insertions(+)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index dbcb223..d4f15e7 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -513,6 +513,9 @@ static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli
 	if (!maps)
 		return 0;
 
+	if (backward && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
+		return 0;
+
 	for (i = 0; i < evlist->nr_mmaps; i++) {
 		struct auxtrace_mmap *mm = &maps[i].auxtrace_mmap;
 
@@ -538,6 +541,8 @@ static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evli
 	if (bytes_written != rec->bytes_written)
 		rc = record__write(rec, &finished_round_event, sizeof(finished_round_event));
 
+	if (backward)
+		perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
 out:
 	return rc;
 }
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 5beb44f..400cb22 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -15,6 +15,7 @@
 #include "evlist.h"
 #include "evsel.h"
 #include "debug.h"
+#include "asm/bug.h"
 #include <unistd.h>
 
 #include "parse-events.h"
@@ -44,6 +45,7 @@ void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
 	perf_evlist__set_maps(evlist, cpus, threads);
 	fdarray__init(&evlist->pollfd, 64);
 	evlist->workload.pid = -1;
+	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
 }
 
 struct perf_evlist *perf_evlist__new(void)
@@ -1068,6 +1070,8 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
 				if (!maps)
 					return -1;
 				evlist->backward_mmap = maps;
+				if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
+					perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
 			}
 		}
 
@@ -1972,3 +1976,62 @@ perf_evlist__find_evsel_by_str(struct perf_evlist *evlist,
 
 	return NULL;
 }
+
+void
+perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist,
+			     enum bkw_mmap_state state)
+{
+	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
+	enum action {
+		NONE,
+		PAUSE,
+		RESUME,
+	} action = NONE;
+
+	if (!evlist->backward_mmap)
+		return;
+
+	switch (old_state) {
+	case BKW_MMAP_NOTREADY: {
+		if (state != BKW_MMAP_RUNNING)
+			goto state_err;;
+		break;
+	}
+	case BKW_MMAP_RUNNING: {
+		if (state != BKW_MMAP_DATA_PENDING)
+			goto state_err;
+		action = PAUSE;
+		break;
+	}
+	case BKW_MMAP_DATA_PENDING: {
+		if (state != BKW_MMAP_EMPTY)
+			goto state_err;
+		break;
+	}
+	case BKW_MMAP_EMPTY: {
+		if (state != BKW_MMAP_RUNNING)
+			goto state_err;
+		action = RESUME;
+		break;
+	}
+	default:
+		WARN_ONCE(1, "Shouldn't get there\n");
+	}
+
+	evlist->bkw_mmap_state = state;
+
+	switch (action) {
+	case PAUSE:
+		perf_evlist__pause(evlist);
+		break;
+	case RESUME:
+		perf_evlist__resume(evlist);
+		break;
+	case NONE:
+	default:
+		break;
+	}
+
+state_err:
+	return;
+}
diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h
index 6a3d9bd..a2b84dd 100644
--- a/tools/perf/util/evlist.h
+++ b/tools/perf/util/evlist.h
@@ -41,6 +41,34 @@ perf_mmap__mmap_len(struct perf_mmap *map)
 	return map->mask + 1 + page_size;
 }
 
+/*
+ * State machine of bkw_mmap_state:
+ *
+ *                     .________________(forbid)_____________.
+ *                     |                                     V
+ * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
+ *                     ^  ^              |   ^               |
+ *                     |  |__(forbid)____/   |___(forbid)___/|
+ *                     |                                     |
+ *                      \_________________(3)_______________/
+ *
+ * NOTREADY     : Backward ring buffers are not ready
+ * RUNNING      : Backward ring buffers are recording
+ * DATA_PENDING : We are required to collect data from backward ring buffers
+ * EMPTY        : We have collected data from backward ring buffers.
+ *
+ * (0): Setup backward ring buffer
+ * (1): Pause ring buffers for reading
+ * (2): Read from ring buffers
+ * (3): Resume ring buffers for recording
+ */
+enum bkw_mmap_state {
+	BKW_MMAP_NOTREADY,
+	BKW_MMAP_RUNNING,
+	BKW_MMAP_DATA_PENDING,
+	BKW_MMAP_EMPTY,
+};
+
 struct perf_evlist {
 	struct list_head entries;
 	struct hlist_head heads[PERF_EVLIST__HLIST_SIZE];
@@ -54,6 +82,7 @@ struct perf_evlist {
 	int		 id_pos;
 	int		 is_pos;
 	u64		 combined_sample_type;
+	enum bkw_mmap_state bkw_mmap_state;
 	struct {
 		int	cork_fd;
 		pid_t	pid;
@@ -135,6 +164,9 @@ struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
 
 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id);
 
+void
+perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist, enum bkw_mmap_state state);
+
 union perf_event *perf_mmap__read_forward(struct perf_mmap *map, bool check_messup);
 union perf_event *perf_mmap__read_backward(struct perf_mmap *map);
 
-- 
1.8.3.4

  parent reply	other threads:[~2016-07-14  8:36 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-14  8:34 [PATCH v16 00/15] perf tools: Support overwritable ring buffer Wang Nan
2016-07-14  8:34 ` [PATCH v16 01/15] perf tools: Drop redundant evsel->overwrite indicator Wang Nan
2016-07-16 20:45   ` [tip:perf/core] perf evlist: " tip-bot for Arnaldo Carvalho de Melo
2016-07-14  8:34 ` [PATCH v16 02/15] tools lib fd array: Allow associating a pointer cookie with each entry Wang Nan
2016-07-16 20:46   ` [tip:perf/core] " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 03/15] perf tools: Update perf evlist mmap related APIs and helpers Wang Nan
2016-07-16 20:46   ` [tip:perf/core] perf evlist: Update " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 04/15] perf tools: Decouple record__mmap_read() and evlist Wang Nan
2016-07-16 20:47   ` [tip:perf/core] perf record: " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 05/15] perf tools: Record mmap cookie into fdarray private field Wang Nan
2016-07-15 14:59   ` Jiri Olsa
2016-07-16 20:47   ` [tip:perf/core] perf evlist: " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 06/15] perf tools: Extract common code in mmap failure processing Wang Nan
2016-07-16 20:48   ` [tip:perf/core] perf evlist: " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 07/15] perf tools: Introduce backward_mmap array for evlist Wang Nan
2016-07-16 20:48   ` [tip:perf/core] perf evlist: " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 08/15] perf tools: Map backward events to backward_mmap Wang Nan
2016-07-16 20:48   ` [tip:perf/core] perf evlist: " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 09/15] perf tools: Drop evlist->backward Wang Nan
2016-07-16 20:49   ` [tip:perf/core] perf evlist: " tip-bot for Wang Nan
2016-07-14  8:34 ` Wang Nan [this message]
2016-07-16 20:49   ` [tip:perf/core] perf evlist: Setup backward mmap state machine tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 11/15] perf record: Read from overwritable ring buffer Wang Nan
2016-07-16 20:50   ` [tip:perf/core] " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 12/15] perf tools: Make perf_evlist__{pause,resume} internal helpers Wang Nan
2016-07-16 20:50   ` [tip:perf/core] perf evlist: Make {pause,resume} " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 13/15] perf tools: Enable overwrite settings Wang Nan
2016-07-16 20:50   ` [tip:perf/core] " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 14/15] perf tools: Don't warn about out of order event if write_backward is used Wang Nan
2016-07-15 14:59   ` Jiri Olsa
2016-07-16 20:51   ` [tip:perf/core] perf session: " tip-bot for Wang Nan
2016-07-14  8:34 ` [PATCH v16 15/15] perf tools: Add --tail-synthesize option Wang Nan
2016-07-16 20:51   ` [tip:perf/core] perf record: " tip-bot for Wang Nan
2016-07-15 15:00 ` [PATCH v16 00/15] perf tools: Support overwritable ring buffer Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1468485287-33422-11-git-send-email-wangnan0@huawei.com \
    --to=wangnan0@huawei.com \
    --cc=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=hekuang@huawei.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lizefan@huawei.com \
    --cc=mhiramat@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=nilayvaish@gmail.com \
    --cc=pi3orama@163.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).