diff mbox

[v4,1/6] perf tools: Derive trigger class from auxtrace_snapshot

Message ID 1460961133-182746-2-git-send-email-wangnan0@huawei.com
State Superseded
Headers show

Commit Message

Wang Nan April 18, 2016, 6:32 a.m. UTC
Use 'trigger' to model operations which need to be executed when
an event (a signal, for example) is observed.

States and transits:

 OFF--(on)--> READY --(toggle)--> TOGGLED --(process)--> PROCESSING
                ^                    |                      |
                |                    |                      |
                |                 (ready)                (ready)
                |                    |                      |
                 \__________________/______________________/

is_toggled and is_ready are two key functions to query the state of
a trigger. is_toggled means the event already happen; is_ready means the
trigger is waiting for the event.

'PROCESSING' represents a state the event happens and be observed, and
the processing is on the way so can't accept a new event immediately.

auxtrace_record__snapshot_started and auxtrace_snapshot_err are removed
and switch to trigger.

Signed-off-by: Wang Nan <wangnan0@huawei.com>

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: He Kuang <hekuang@huawei.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: pi3orama@163.com
---
 tools/perf/builtin-record.c |  73 +++++++-------------------
 tools/perf/util/trigger.h   | 123 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 142 insertions(+), 54 deletions(-)
 create mode 100644 tools/perf/util/trigger.h

-- 
1.8.3.4

Comments

Wang Nan April 18, 2016, 2:20 p.m. UTC | #1
On 2016/4/18 21:45, Jiri Olsa wrote:
> On Mon, Apr 18, 2016 at 06:32:08AM +0000, Wang Nan wrote:

>> Use 'trigger' to model operations which need to be executed when

>> an event (a signal, for example) is observed.

>>

>> States and transits:

>>

>>   OFF--(on)--> READY --(toggle)--> TOGGLED --(process)--> PROCESSING

>>                  ^                    |                      |

>>                  |                    |                      |

>>                  |                 (ready)                (ready)

>>                  |                    |                      |

>>                   \__________________/______________________/

>>

>> is_toggled and is_ready are two key functions to query the state of

>> a trigger. is_toggled means the event already happen; is_ready means the

>> trigger is waiting for the event.

>>

>> 'PROCESSING' represents a state the event happens and be observed, and

>> the processing is on the way so can't accept a new event immediately.

> hum, I must be missing something.. but I dont see how you're

> using this state except for smal window within:

>

>                  if (auxtrace_snapshot_is_toggled()) {

>   ->                     auxtrace_snapshot_process();

>                          if (!auxtrace_snapshot_is_error())

>   ->                              record__read_auxtrace_snapshot(rec);

>

> but no other place queries or depends on this state


Right.

Since we are creating a new class, I think we can make code simpler by 
merging
all state variables into trigger.

Without this state we must keep 'auxtrace_record__snapshot_started'.

I think merging it into trigger class makes the whole program a little
bit simpler. Or do you think keeping trigger class simpler whould be better?

Thank you.
Wang Nan April 18, 2016, 2:37 p.m. UTC | #2
On 2016/4/18 22:29, Jiri Olsa wrote:
> On Mon, Apr 18, 2016 at 10:20:23PM +0800, Wangnan (F) wrote:

>>

>> On 2016/4/18 21:45, Jiri Olsa wrote:

>>> On Mon, Apr 18, 2016 at 06:32:08AM +0000, Wang Nan wrote:

>>>> Use 'trigger' to model operations which need to be executed when

>>>> an event (a signal, for example) is observed.

>>>>

>>>> States and transits:

>>>>

>>>>   OFF--(on)--> READY --(toggle)--> TOGGLED --(process)--> PROCESSING

>>>>                  ^                    |                      |

>>>>                  |                    |                      |

>>>>                  |                 (ready)                (ready)

>>>>                  |                    |                      |

>>>>                   \__________________/______________________/

>>>>

>>>> is_toggled and is_ready are two key functions to query the state of

>>>> a trigger. is_toggled means the event already happen; is_ready means the

>>>> trigger is waiting for the event.

>>>>

>>>> 'PROCESSING' represents a state the event happens and be observed, and

>>>> the processing is on the way so can't accept a new event immediately.

>>> hum, I must be missing something.. but I dont see how you're

>>> using this state except for smal window within:

>>>

>>>                  if (auxtrace_snapshot_is_toggled()) {

>>>   ->                     auxtrace_snapshot_process();

>>>                          if (!auxtrace_snapshot_is_error())

>>>   ->                              record__read_auxtrace_snapshot(rec);

>>>

>>> but no other place queries or depends on this state

>> Right.

>>

>> Since we are creating a new class, I think we can make code simpler by

>> merging

>> all state variables into trigger.

>>

>> Without this state we must keep 'auxtrace_record__snapshot_started'.

> do we? we dont need this for switch_output code apparently


I think it is a good chance to clean existing code.

However, number of lines introduced to .h in patch 1/6 is more than
lines removed from .c. So I think removing the 'PROCESSING' is okay.
If in future we find switch_output also need this state we can add it
back at that time.

Please wait for v5.

Thank you.

> jirka

>

>> I think merging it into trigger class makes the whole program a little

>> bit simpler. Or do you think keeping trigger class simpler whould be better?

>>

>> Thank you.

>>

>>
diff mbox

Patch

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 5b4758a..0ff2422 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -34,6 +34,7 @@ 
 #include "util/parse-regs-options.h"
 #include "util/llvm-utils.h"
 #include "util/bpf-loader.h"
+#include "util/trigger.h"
 #include "asm/bug.h"
 
 #include <unistd.h>
@@ -127,44 +128,7 @@  static volatile int done;
 static volatile int signr = -1;
 static volatile int child_finished;
 
-static volatile enum {
-	AUXTRACE_SNAPSHOT_OFF = -1,
-	AUXTRACE_SNAPSHOT_DISABLED = 0,
-	AUXTRACE_SNAPSHOT_ENABLED = 1,
-} auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_OFF;
-
-static inline void
-auxtrace_snapshot_on(void)
-{
-	auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_DISABLED;
-}
-
-static inline void
-auxtrace_snapshot_enable(void)
-{
-	if (auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_OFF)
-		return;
-	auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_ENABLED;
-}
-
-static inline void
-auxtrace_snapshot_disable(void)
-{
-	if (auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_OFF)
-		return;
-	auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_DISABLED;
-}
-
-static inline bool
-auxtrace_snapshot_is_enabled(void)
-{
-	if (auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_OFF)
-		return false;
-	return auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_ENABLED;
-}
-
-static volatile int auxtrace_snapshot_err;
-static volatile int auxtrace_record__snapshot_started;
+static DEFINE_TRIGGER(auxtrace_snapshot);
 
 static void sig_handler(int sig)
 {
@@ -282,11 +246,12 @@  static void record__read_auxtrace_snapshot(struct record *rec)
 {
 	pr_debug("Recording AUX area tracing snapshot\n");
 	if (record__auxtrace_read_snapshot_all(rec) < 0) {
-		auxtrace_snapshot_err = -1;
+		auxtrace_snapshot_error();
 	} else {
-		auxtrace_snapshot_err = auxtrace_record__snapshot_finish(rec->itr);
-		if (!auxtrace_snapshot_err)
-			auxtrace_snapshot_enable();
+		if (auxtrace_record__snapshot_finish(rec->itr))
+			auxtrace_snapshot_error();
+		else
+			auxtrace_snapshot_ready();
 	}
 }
 
@@ -815,21 +780,21 @@  static int __cmd_record(struct record *rec, int argc, const char **argv)
 		perf_evlist__enable(rec->evlist);
 	}
 
-	auxtrace_snapshot_enable();
+	auxtrace_snapshot_ready();
 	for (;;) {
 		unsigned long long hits = rec->samples;
 
 		if (record__mmap_read_all(rec) < 0) {
-			auxtrace_snapshot_disable();
+			auxtrace_snapshot_error();
 			err = -1;
 			goto out_child;
 		}
 
-		if (auxtrace_record__snapshot_started) {
-			auxtrace_record__snapshot_started = 0;
-			if (!auxtrace_snapshot_err)
+		if (auxtrace_snapshot_is_toggled()) {
+			auxtrace_snapshot_process();
+			if (!auxtrace_snapshot_is_error())
 				record__read_auxtrace_snapshot(rec);
-			if (auxtrace_snapshot_err) {
+			if (auxtrace_snapshot_is_error()) {
 				pr_err("AUX area tracing snapshot failed\n");
 				err = -1;
 				goto out_child;
@@ -858,12 +823,12 @@  static int __cmd_record(struct record *rec, int argc, const char **argv)
 		 * disable events in this case.
 		 */
 		if (done && !disabled && !target__none(&opts->target)) {
-			auxtrace_snapshot_disable();
+			auxtrace_snapshot_off();
 			perf_evlist__disable(rec->evlist);
 			disabled = true;
 		}
 	}
-	auxtrace_snapshot_disable();
+	auxtrace_snapshot_off();
 
 	if (forks && workload_exec_errno) {
 		char msg[STRERR_BUFSIZE];
@@ -1447,9 +1412,9 @@  out_symbol_exit:
 
 static void snapshot_sig_handler(int sig __maybe_unused)
 {
-	if (!auxtrace_snapshot_is_enabled())
+	if (!auxtrace_snapshot_is_ready())
 		return;
-	auxtrace_snapshot_disable();
-	auxtrace_snapshot_err = auxtrace_record__snapshot_start(record.itr);
-	auxtrace_record__snapshot_started = 1;
+	auxtrace_snapshot_toggle();
+	if (auxtrace_record__snapshot_start(record.itr))
+		auxtrace_snapshot_error();
 }
diff --git a/tools/perf/util/trigger.h b/tools/perf/util/trigger.h
new file mode 100644
index 0000000..852d876
--- /dev/null
+++ b/tools/perf/util/trigger.h
@@ -0,0 +1,123 @@ 
+#ifndef __TRIGGER_H_
+#define __TRIGGER_H_ 1
+
+#include "util/debug.h"
+#include "asm/bug.h"
+
+/*
+ * Use trigger to model operations which need to be executed when
+ * an event (a signal, for example) is observed.
+ *
+ * States and transits:
+ *
+ *
+ *  OFF--(on)--> READY --(toggle)--> TOGGLED --(process)--> PROCESSING
+ *                 ^                    |                      |
+ *                 |                    |                      |
+ *                 |                 (ready)                (ready)
+ *                 |                    |                      |
+ *                  \__________________/______________________/
+ *
+ * is_toggled and is_ready are two key functions to query the state of
+ * a trigger. is_toggled means the event already happen; is_ready means the
+ * trigger is waiting for the event.
+ *
+ * 'PROCESSING' represents a state the event happens and be observed, and
+ * the processing is on the way so can't accept a new event immediately.
+ */
+
+struct trigger {
+	volatile enum {
+		TRIGGER_ERROR		= -2,
+		TRIGGER_OFF		= -1,
+		TRIGGER_READY		= 0,
+		TRIGGER_TOGGLED		= 1,
+		TRIGGER_PROCESSING	= 2,
+	} state;
+	const char *name;
+};
+
+#define TRIGGER_WARN_ONCE(t, exp) \
+	WARN_ONCE(t->state != exp, "trigger '%s' state transist error: %d in %s()\n", \
+		  t->name, t->state, __func__)
+
+static inline bool trigger_is_available(struct trigger *t)
+{
+	return t->state >= 0;
+}
+
+static inline bool trigger_is_error(struct trigger *t)
+{
+	return t->state <= TRIGGER_ERROR;
+}
+
+static inline void trigger_on(struct trigger *t)
+{
+	TRIGGER_WARN_ONCE(t, TRIGGER_OFF);
+	t->state = TRIGGER_READY;
+}
+
+static inline void trigger_ready(struct trigger *t)
+{
+	if (!trigger_is_available(t))
+		return;
+	t->state = TRIGGER_READY;
+}
+
+static inline void trigger_toggle(struct trigger *t)
+{
+	if (!trigger_is_available(t))
+		return;
+	TRIGGER_WARN_ONCE(t, TRIGGER_READY);
+	t->state = TRIGGER_TOGGLED;
+}
+
+static inline void trigger_process(struct trigger *t)
+{
+	if (!trigger_is_available(t))
+		return;
+	TRIGGER_WARN_ONCE(t, TRIGGER_TOGGLED);
+	t->state = TRIGGER_PROCESSING;
+}
+
+static inline void trigger_off(struct trigger *t)
+{
+	if (!trigger_is_available(t))
+		return;
+	t->state = TRIGGER_OFF;
+}
+
+static inline void trigger_error(struct trigger *t)
+{
+	t->state = TRIGGER_ERROR;
+}
+
+static inline bool trigger_is_ready(struct trigger *t)
+{
+	return t->state == TRIGGER_READY;
+}
+
+static inline bool trigger_is_toggled(struct trigger *t)
+{
+	return t->state == TRIGGER_TOGGLED;
+}
+
+#define __TRIGGER_VAR(n) n##_state
+#define __DEF_TRIGGER_VOID_FUNC(n, op)	\
+static inline void n##_##op(void) {trigger_##op(&__TRIGGER_VAR(n)); }
+#define __DEF_TRIGGER_BOOL_FUNC(n, op)	\
+static inline bool n##_##op(void) {return trigger_##op(&__TRIGGER_VAR(n)); }
+
+#define DEFINE_TRIGGER(n)					\
+struct trigger n##_state = {.state = TRIGGER_OFF, .name = #n};	\
+__DEF_TRIGGER_VOID_FUNC(n, on)					\
+__DEF_TRIGGER_VOID_FUNC(n, ready)				\
+__DEF_TRIGGER_VOID_FUNC(n, toggle)				\
+__DEF_TRIGGER_VOID_FUNC(n, process)				\
+__DEF_TRIGGER_VOID_FUNC(n, off)					\
+__DEF_TRIGGER_VOID_FUNC(n, error)				\
+__DEF_TRIGGER_BOOL_FUNC(n, is_ready)				\
+__DEF_TRIGGER_BOOL_FUNC(n, is_toggled)				\
+__DEF_TRIGGER_BOOL_FUNC(n, is_error)
+
+#endif