Added enum with ftrace clock IDs and APIs to convert ftrace name to ID and vice versa, as part of libtracecmd. The clock items in the enum are organized as a bitmask, as it will be used by the timestamp synchronization protocol to declare supported ftrace clocks. Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@xxxxxxxxx> --- .../include/private/trace-cmd-private.h | 16 ++++++ lib/trace-cmd/trace-util.c | 52 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h index 08e98a50..0d323947 100644 --- a/lib/trace-cmd/include/private/trace-cmd-private.h +++ b/lib/trace-cmd/include/private/trace-cmd-private.h @@ -393,6 +393,22 @@ int tracecmd_msg_recv_time_sync(struct tracecmd_msg_handle *msg_handle, unsigned int *sync_msg_id, unsigned int *payload_size, char **payload); +enum tracecmd_clocks { + TRACECMD_CLOCK_UNKNOWN = 0, + TRACECMD_CLOCK_LOCAL = 1, + TRACECMD_CLOCK_GLOBAL = 1 << 1, + TRACECMD_CLOCK_COUNTER = 1 << 2, + TRACECMD_CLOCK_UPTIME = 1 << 3, + TRACECMD_CLOCK_PERF = 1 << 4, + TRACECMD_CLOCK_MONO = 1 << 5, + TRACECMD_CLOCK_MONO_RAW = 1 << 6, + TRACECMD_CLOCK_BOOT = 1 << 7, + TRACECMD_CLOCK_X86_TSC = 1 << 8 +}; + +enum tracecmd_clocks tracecmd_clock_str2id(const char *clock); +const char *tracecmd_clock_id2str(enum tracecmd_clocks clock); + /* --- Timestamp synchronization --- */ #define TRACECMD_TSYNC_PNAME_LENGTH 16 diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c index 8caec7c1..9661b266 100644 --- a/lib/trace-cmd/trace-util.c +++ b/lib/trace-cmd/trace-util.c @@ -33,6 +33,58 @@ static bool debug; static FILE *logfp; +const static struct { + const char *clock_str; + enum tracecmd_clocks clock_id; +} trace_clocks[] = { + {"local", TRACECMD_CLOCK_LOCAL}, + {"global", TRACECMD_CLOCK_GLOBAL}, + {"counter", TRACECMD_CLOCK_COUNTER}, + {"uptime", TRACECMD_CLOCK_UPTIME}, + {"perf", TRACECMD_CLOCK_PERF}, + {"mono", TRACECMD_CLOCK_MONO}, + {"mono_raw", TRACECMD_CLOCK_MONO_RAW}, + {"boot", TRACECMD_CLOCK_BOOT}, + {"x86-tsc", TRACECMD_CLOCK_X86_TSC}, + {NULL, -1} +}; + +/** + * tracecmd_clock_str2id - Convert ftrace clock name to clock ID + * @clock: Ftrace clock name + * Returns ID of the ftrace clock + */ +enum tracecmd_clocks tracecmd_clock_str2id(const char *clock) +{ + int i; + + if (!clock) + return TRACECMD_CLOCK_UNKNOWN; + + for (i = 0; trace_clocks[i].clock_str; i++) { + if (!strncmp(clock, trace_clocks[i].clock_str, + strlen(trace_clocks[i].clock_str))) + return trace_clocks[i].clock_id; + } + return TRACECMD_CLOCK_UNKNOWN; +} + +/** + * tracecmd_clock_id2str - Convert clock ID to ftare clock name + * @clock: Clock ID + * Returns name of a ftrace clock + */ +const char *tracecmd_clock_id2str(enum tracecmd_clocks clock) +{ + int i; + + for (i = 0; trace_clocks[i].clock_str; i++) { + if (trace_clocks[i].clock_id == clock) + return trace_clocks[i].clock_str; + } + return NULL; +} + /** * tracecmd_set_debug - Set debug mode of the tracecmd library * @set_debug: The new "debug" mode. If true, the tracecmd library is -- 2.29.2