Create a new 'class' named 'trigger' to model state of a trigger
and implement auxtrace_snapshot with it.

'trigger' defines 3 state transfering functions ('on', 'enable',
'disable') and 1 state query function ('is_enabled'). The state
'ON' and 'OFF' take higher priority than 'ENABLED' and 'DISABLED'.
A trigger must be 'on' before 'enable' and 'disable' take effect.

Signed-off-by: Wang Nan <wangn...@huawei.com>
Cc: Arnaldo Carvalho de Melo <a...@redhat.com>
Cc: Adrian Hunter <adrian.hun...@intel.com>
Cc: Jiri Olsa <jo...@kernel.org>
Cc: Masami Hiramatsu <mhira...@kernel.org>
Cc: Namhyung Kim <namhy...@kernel.org>
Cc: Zefan Li <lize...@huawei.com>
Cc: pi3or...@163.com
Cc: He Kuang <heku...@huawei.com>
---
 tools/perf/builtin-record.c | 37 +-------------------------------
 tools/perf/util/util.h      | 51 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 36 deletions(-)

diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 3239a6e..13fe9a6 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -127,42 +127,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 DEFINE_TRIGGER(auxtrace_snapshot, OFF);
 static volatile int auxtrace_snapshot_err;
 static volatile int auxtrace_record__snapshot_started;
 
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 3bf3de8..8121029 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -361,4 +361,55 @@ typedef void (*print_binary_t)(enum binary_printer_ops,
 void print_binary(unsigned char *data, size_t len,
                  size_t bytes_per_line, print_binary_t printer,
                  void *extra);
+
+struct trigger {
+       volatile enum {
+               TRIGGER_OFF = -1,
+               TRIGGER_DISABLED = 0,
+               TRIGGER_ENABLED = 1,
+       } state;
+};
+
+static inline void
+trigger_on(struct trigger *s)
+{
+       s->state = TRIGGER_DISABLED;
+}
+
+static inline void
+trigger_enable(struct trigger *s)
+{
+       if (s->state != TRIGGER_OFF)
+               s->state = TRIGGER_ENABLED;
+}
+
+static inline void
+trigger_disable(struct trigger *s)
+{
+       if (s->state != TRIGGER_OFF)
+               s->state = TRIGGER_DISABLED;
+}
+
+static inline bool
+trigger_is_enabled(struct trigger *s)
+{
+       if (s->state != TRIGGER_OFF)
+               return s->state == TRIGGER_ENABLED;
+       return false;
+}
+
+#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_FUNC(n, type, op)        \
+static inline type n##_##op(void) {return trigger_##op(&__TRIGGER_VAR(n)); }
+
+#define DEFINE_TRIGGER(n, def)                         \
+struct trigger n##_state = {.state = TRIGGER_##def};\
+__DEF_TRIGGER_VOID_FUNC(n, on)                         \
+__DEF_TRIGGER_VOID_FUNC(n, enable)                     \
+__DEF_TRIGGER_VOID_FUNC(n, disable)                    \
+__DEF_TRIGGER_FUNC(n, bool, is_enabled)
+
 #endif /* GIT_COMPAT_UTIL_H */
-- 
1.8.3.4

Reply via email to