[PATCH v2 08/10] trace-cmd: Introducing get_trace_cmd_type()

From: Vladislav Valtchev (VMware)
Date: Thu Nov 30 2017 - 08:22:05 EST


This patch aims to reduce the size of common_record_commads_code() by removing
a relatively long 'else if' sequence that was used to do the mapping between the
current trace command and the trace_type used by it.

Signed-off-by: Vladislav Valtchev (VMware) <vladislav.valtchev@xxxxxxxxx>
---
trace-record.c | 34 ++++++++++++++++++++++------------
1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/trace-record.c b/trace-record.c
index 7688565..ec0eaed 100644
--- a/trace-record.c
+++ b/trace-record.c
@@ -4746,6 +4746,27 @@ static void parse_record_options(int argc,
}
}

+static enum trace_type get_trace_cmd_type(enum trace_cmd cmd)
+{
+ const static struct {
+ enum trace_cmd cmd;
+ enum trace_type ttype;
+ } trace_type_per_command[] = {
+ {CMD_record, TRACE_TYPE_RECORD},
+ {CMD_stream, TRACE_TYPE_STREAM},
+ {CMD_extract, TRACE_TYPE_EXTRACT},
+ {CMD_profile, TRACE_TYPE_STREAM},
+ {CMD_start, TRACE_TYPE_START}
+ };
+
+ for (int i = 0; i < ARRAY_SIZE(trace_type_per_command); i++) {
+ if (trace_type_per_command[i].cmd == cmd)
+ return trace_type_per_command[i].ttype;
+ }
+
+ die("Trace type UNKNOWN for the given cmd_fun");
+}
+
/*
* This function contains common code for the following commands:
* record, start, extract, stream, profile.
@@ -4753,7 +4774,7 @@ static void parse_record_options(int argc,
static void record_trace(int argc, char **argv,
struct common_record_context *ctx)
{
- enum trace_type type = 0;
+ enum trace_type type = get_trace_cmd_type(ctx->curr_cmd);

/*
* If top_instance doesn't have any plugins or events, then
@@ -4820,17 +4841,6 @@ static void record_trace(int argc, char **argv,
set_buffer_size();
}

- if (IS_RECORD(ctx))
- type = TRACE_TYPE_RECORD;
- else if (IS_STREAM(ctx))
- type = TRACE_TYPE_STREAM;
- else if (IS_EXTRACT(ctx))
- type = TRACE_TYPE_EXTRACT;
- else if (IS_PROFILE(ctx))
- type = TRACE_TYPE_STREAM;
- else
- type = TRACE_TYPE_START;
-
update_plugins(type);

set_options();
--
2.14.1