trace-cmd: SQL-like syntax for ftrace histograms configuration
Subsystems (2)
cover trace-cmd: SQL-like syntax for ftrace histograms configuration
Configuration of complex ftrace histograms could be hard, using the existing ftrace interface. The "--sql" option is implemented to simplify this task. It uses a sql-like syntax to define the histograms and generates necessary ftrace syntetic events and triggers. Each ftrace event represents a sql tible, while the fields of this event are rows in the table.
The patch set is based on Steven Rostedt's PoC implementation:
https://github.com/rostedt/sqlhist
Tzvetomir Stoyanov (VMware) (4):
trace-cmd: Add new libtracefs API tracefs_instance_file_append()
trace-cmd: Clear synthetic events on reset subcommand
trace-cmd: Clear error log on reset subcommand
trace-cmd: Add "--sql" option to trace-cmd start and record sub
commands
Documentation/trace-cmd-record.1.txt | 37 + Makefile | 1 + include/tracefs/tracefs.h | 2 + lib/tracefs/tracefs-instance.c | 40 +- tracecmd/Makefile | 12 +- tracecmd/include/trace-local.h | 4 + tracecmd/sqlparser/Makefile | 52 + tracecmd/sqlparser/sqlhist-api.h | 29 + tracecmd/sqlparser/sqlhist-parse.c | 1454 ++++++++++++++++++++++++++ tracecmd/sqlparser/sqlhist.h | 34 + tracecmd/sqlparser/sqlhist.l | 59 ++ tracecmd/sqlparser/sqlhist.y | 175 ++++ tracecmd/trace-record.c | 172 ++- tracecmd/trace-usage.c | 2 + 14 files changed, 2066 insertions(+), 7 deletions(-) create mode 100644 tracecmd/sqlparser/Makefile create mode 100644 tracecmd/sqlparser/sqlhist-api.h create mode 100644 tracecmd/sqlparser/sqlhist-parse.c create mode 100644 tracecmd/sqlparser/sqlhist.h create mode 100644 tracecmd/sqlparser/sqlhist.l create mode 100644 tracecmd/sqlparser/sqlhist.y
Signature · 1 line
2.24.1
PATCH 1/4 trace-cmd: Add new libtracefs API tracefs_instance_file_append()
The existing tracefs_instance_file_write() API truncates the file before writing to it. The are use cases where the file must not be truncated. The new
tracefs_instance_file_append()
API appends to the end of the file.
--- include/tracefs/tracefs.h | 2 ++ lib/tracefs/tracefs-instance.c | 40 ++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-)
include/tracefs/tracefs.h +2 -0
@@ -30,6 +30,8 @@ tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);char *tracefs_instance_get_dir (struct tracefs_instance *instance);int tracefs_instance_file_write (struct tracefs_instance *instance,const char *file,const char *str); +int tracefs_instance_file_append (struct tracefs_instance *instance, +const char *file,const char *str);char *tracefs_instance_file_read (struct tracefs_instance *instance,char *file,int *psize);
lib/tracefs/tracefs-instance.c +36 -4
@@ -176,12 +176,16 @@ char *tracefs_instance_get_name(struct tracefs_instance *instance)return NULL ; } -static int write_file (const char *file,const char *str) +static int write_file (const char *file,const char *str,bool appned) {int ret;int fd; - fd =open (file,O_WRONLY |O_TRUNC ); +if (appned) + fd =open (file,O_WRONLY |O_APPEND ); +else + fd =open (file,O_WRONLY |O_TRUNC ); +if (fd <0 ) {warning ("Failed to open '%s'" , file);return -1 ;
@@ -198,7 +202,9 @@ static int write_file(const char *file, const char *str)* @file: name of the file * @str: nul terminated string, that will be written in the file. * -* Returns the number of written bytes, or -1 in case of an error +* Returns the number of written bytes, or -1 in case of an error. +* The content of the file is replaced with the new one. +* */ int tracefs_instance_file_write (struct tracefs_instance *instance,const char *file,const char *str)
@@ -212,7 +218,33 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,return -1 ; ret =stat (path, &st);if (ret ==0 ) - ret =write_file (path, str); + ret =write_file (path, str, false); +tracefs_put_tracing_file (path); + +return ret; +} + +/** +* tracefs_instance_file_append - Append to a trace file of specific instance. +* @instance: ftrace instance, can be NULL for the top instance +* @file: name of the file +* @str: nul terminated string, that will be written in the file. +* +* Returns the number of written bytes, or -1 in case of an error. +*/ +int tracefs_instance_file_append (struct tracefs_instance *instance, +const char *file,const char *str) +{ +struct stat st; +char *path; +int ret; + + path =tracefs_instance_get_file (instance, file); +if (!path) +return -1 ; + ret =stat (path, &st); +if (ret ==0 ) + ret =write_file (path, str, true);tracefs_put_tracing_file (path);return ret;
Signature · 1 line
2.24.1
Re: [PATCH 1/4] trace-cmd: Add new libtracefs API tracefs_instance_file_append()
Quoted 29 lines · On Mon, 17 Feb 2020 19:39:56 +0200 "Tzvetomir Stoyanov (VMware)"…
The existing
tracefs_instance_file_write()API truncates the file before writing to it. The are use cases where the file must not be truncated. The new
tracefs_instance_file_append()
API appends to the end of the file.Signed-off-by Tzvetomir Stoyanov (VMware) <[email protected]>--- include/tracefs/tracefs.h | 2 ++ lib/tracefs/tracefs-instance.c | 40 ++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-)include/tracefs/tracefs.h +2
-0@@ -30,6 +30,8 @@ tracefs_instance_get_file(struct tracefs_instance *instance, const char *file);char *tracefs_instance_get_dir (struct tracefs_instance *instance);int tracefs_instance_file_write (struct tracefs_instance *instance,const char *file,const char *str); +int tracefs_instance_file_append (struct tracefs_instance *instance, +const char *file,const char *str);char *tracefs_instance_file_read (struct tracefs_instance *instance,char *file,int *psize);lib/tracefs/tracefs-instance.c +0
-0
lib/tracefs/tracefs-instance.c char *tracefs_instance_get_name(struct tracefs_instance *instance) · line 179
Quoted 5 lines
lib/tracefs/tracefs-instance.c +1
-1@@ -176,12 +176,16 @@ char *tracefs_instance_get_name(struct tracefs_instance *instance)return NULL ; } -static int write_file (const char *file,const char *str) +static int write_file (const char *file,const char *str,bool appned)
Typo, s/appned/append/
Quoted 49 lines
lib/tracefs/tracefs-instance.c +28
-3{int ret;int fd; - fd =open (file,O_WRONLY |O_TRUNC ); +if (appned) + fd =open (file,O_WRONLY |O_APPEND ); +else + fd =open (file,O_WRONLY |O_TRUNC ); +if (fd <0 ) {warning ("Failed to open '%s'" , file);return -1 ;@@ -198,7 +202,9 @@ static int write_file(const char *file, const char *str)* @file: name of the file * @str: nul terminated string, that will be written in the file. * -* Returns the number of written bytes, or -1 in case of an error +* Returns the number of written bytes, or -1 in case of an error. +* The content of the file is replaced with the new one. +* */ int tracefs_instance_file_write (struct tracefs_instance *instance,const char *file,const char *str)@@ -212,7 +218,33 @@ int tracefs_instance_file_write(struct tracefs_instance *instance,return -1 ; ret =stat (path, &st);if (ret ==0 ) - ret =write_file (path, str); + ret =write_file (path, str, false); +tracefs_put_tracing_file (path); + +return ret; +} + +/** +* tracefs_instance_file_append - Append to a trace file of specific instance. +* @instance: ftrace instance, can be NULL for the top instance +* @file: name of the file +* @str: nul terminated string, that will be written in the file. +* +* Returns the number of written bytes, or -1 in case of an error. +*/ +int tracefs_instance_file_append (struct tracefs_instance *instance, +const char *file,const char *str) +{ +struct stat st; +char *path; +int ret;
lib/tracefs/tracefs-instance.c int tracefs_instance_file_write(struct tracefs_instance *instance, · line 250
Quoted 10 lines
lib/tracefs/tracefs-instance.c +7
-0+ + path =tracefs_instance_get_file (instance, file); +if (!path) +return -1 ; + ret =stat (path, &st); +if (ret ==0 ) + ret =write_file (path, str, true);tracefs_put_tracing_file (path);return ret;
I would actually make one more static helper as the above two functions are identical except for the write file part.
static int instance_file_write(struct tracefs_instance *instance,
const char *file, const char *str, bool append)
{
struct stat st;
char *path;
int ret;
path = tracefs_instance_get_file(instance, file);
if (!path)
return -1;
ret = stat(path, &st);
if (ret == 0)
ret = write_file(path, str, append);
tracefs_put_tracing_file(path);
return ret;
}int tracefs_instance_file_write(struct tracefs_instance *instance,
const char *file, const char *str)
{
return instance_file_write(instance, file, str, false);
}int tracefs_instance_file_append(struct tracefs_instance *instance,
const char *file, const char *str)
{
return instance_file_write(instance, file, str, true);
}-- Steve
PATCH 2/4 trace-cmd: Clear synthetic events on reset subcommand
In reply to Tzvetomir Stoyanov (VMware)The "trace-cmd reset" command is supposed to set the ftrace state to default. However, the synthetic events are not reseted.
A logic is added to delete all entries from "synthetic_events" files.
--- tracecmd/trace-record.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
tracecmd/trace-record.c +38 -0
@@ -4638,6 +4638,43 @@ static void clear_triggers(void)clear_instance_triggers (instance); } +static void clear_all_synth_events (void ) +{ +char sevent[BUFSIZ ]; +char *save =NULL ; +char *line; +char *file; +char *buf; +int len; + + file =tracefs_instance_get_file (NULL ,"synthetic_events" ); +if (!file) +return ; + + buf =read_file (file); +if (!buf) { +perror (file); + goto out; + } + + sevent[0 ] ='!' ; + +for (line =strtok_r (buf,"\n" , &save); line; line =strtok_r (NULL ,"\n" , &save)) { + len =strlen (line); +if (len >BUFSIZ -2 ) + len =BUFSIZ -2 ; +strncpy (sevent +1 , line, len); + sevent[len +1 ] ='\0' ; +write_file (file, sevent); + } +out: +free (buf); +tracefs_put_tracing_file (file); + +} + + +static void clear_func_filters (void ) {struct buffer_instance *instance;
@@ -5344,6 +5381,7 @@ void trace_reset(int argc, char **argv)set_buffer_size ();clear_filters ();clear_triggers (); +clear_all_synth_events ();/* set clock to "local" */ reset_clock ();reset_event_pid ();
Signature · 1 line
2.24.1
PATCH 3/4 trace-cmd: Clear error log on reset subcommand
In reply to Tzvetomir Stoyanov (VMware)The "trace-cmd reset" command is supposed to set the ftrace state to default. However, the ftrace error logs are not reseted.
A logic is added to delete the content of "error_log" file when "trace-cmd reset" is executed.
--- tracecmd/trace-record.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
tracecmd/trace-record.c +20 -0
@@ -4638,6 +4638,25 @@ static void clear_triggers(void)clear_instance_triggers (instance); } +static void clear_instance_error_log (struct buffer_instance *instance) +{ +char *file; + + file =tracefs_instance_get_file (instance->tracefs,"error_log" ); +if (!file) +return ; +write_file (file," " ); +tracefs_put_tracing_file (file); +} + +static void clear_error_log (void ) +{ +struct buffer_instance *instance; + +for_all_instances (instance) +clear_instance_error_log (instance); +} +static void clear_all_synth_events (void ) {char sevent[BUFSIZ ];
@@ -5382,6 +5401,7 @@ void trace_reset(int argc, char **argv)clear_filters ();clear_triggers ();clear_all_synth_events (); +clear_error_log ();/* set clock to "local" */ reset_clock ();reset_event_pid ();
Signature · 1 line
2.24.1
PATCH 4/4 trace-cmd: Add "--sql" option to trace-cmd start and record sub commands
In reply to Tzvetomir Stoyanov (VMware)Configuration of complex ftrace histograms could be hard, using the existing ftrace interface. The "--sql" option is implemented to simplify this task. It uses a sql-like syntax to define the histograms and generates necessary ftrace syntetic events and triggers. Each ftrace event represents a sql tible, while the fields of this event are rows in the table. Additional option "-V" is added to trace-cmd start and report sub commands, which can be used to dump the auto generated ftrace synthetic events and triggers configuration.
--- Documentation/trace-cmd-record.1.txt | 37 + Makefile | 1 + tracecmd/Makefile | 12 +- tracecmd/include/trace-local.h | 4 + tracecmd/sqlparser/Makefile | 52 + tracecmd/sqlparser/sqlhist-api.h | 29 + tracecmd/sqlparser/sqlhist-parse.c | 1454 ++++++++++++++++++++++++++ tracecmd/sqlparser/sqlhist.h | 34 + tracecmd/sqlparser/sqlhist.l | 59 ++ tracecmd/sqlparser/sqlhist.y | 175 ++++ tracecmd/trace-record.c | 114 +- tracecmd/trace-usage.c | 2 + 12 files changed, 1970 insertions(+), 3 deletions(-) create mode 100644 tracecmd/sqlparser/Makefile create mode 100644 tracecmd/sqlparser/sqlhist-api.h create mode 100644 tracecmd/sqlparser/sqlhist-parse.c create mode 100644 tracecmd/sqlparser/sqlhist.h create mode 100644 tracecmd/sqlparser/sqlhist.l create mode 100644 tracecmd/sqlparser/sqlhist.y
Documentation/trace-cmd-record.1.txt +37 -0
@@ -343,6 +343,43 @@ OPTIONS executed will not be changed. This is useful if you want to monitor the output of the command being executed, but not see the output from trace-cmd. +*--sql*:: + Generate necessary ftrace syntetic events and triggers, based on the given + sql query. A simplified sql syntax can be used to describe ftrace histograms. + Each ftrace event represents a sql tible, while the fields of this event are + rows in the table. A sql aliases can be used to define synthetic events and + simplify the sql statement. + + Example 1: + --sql 'select pid as key1, common_pid from sched_waking' + Configures a histogram, where the key is the pid from sched/sched_waking + event and the value is the common_pid filed from the same event. + The auto generated ftrace config, written in sched/sched_waking/trigger file + is: + hist:keys=pid:values=common_pid + + Example 2: + --sql '(select start.common_timestamp as start_time, end.common_timestamp + as end_time, start.pid, (end_time - start_time) as delta + from sched_waking as start join sched_switch as end on + start.pid = end.next_pid) as first' + A more complex histogram example, using a synthetic event. The synthetic + event "first" is configured in synthetic_events file: + first u64 start_time u64 end_time pid_t pid u64 delta + After that, a histogram trigger is configured in sched/sched_waking/trigger + file: + hist:keys=pid:start_time=common_timestamp,__arg0__=pid + A second histogram trigger is configured in sched/sched_switch/trigger file: + hist:keys=next_pid:__arg1__=$start_time,__arg2__=$__arg0__, + delta=common_timestamp-$__arg1__: + onmatch(sched.sched_waking).trace(first,$__arg1__,common_timestamp,$__arg2__,$delta) + The "onmatch" histogram action is used to trigger the synthetic event "first" + with interested parameters, when there is a match between next_pid and pid + fields from both events. + +*-V*:: + Dump ftrace configuration, auto generated by the *--sql* option. + EXAMPLES --------
Makefile +1 -0
@@ -225,6 +225,7 @@ INCLUDES += -I$(src)/lib/traceevent/include INCLUDES += -I$(src)/lib/trace-cmd/include INCLUDES += -I$(src)/lib/tracefs/include INCLUDES += -I$(src)/tracecmd/include +INCLUDES += -I$(src)/tracecmd/sqlparser INCLUDES += -I$(obj)/tracecmd/include include $(src)/features.mk
tracecmd/Makefile +11 -1
@@ -5,6 +5,9 @@ EXTRAVERSION := $(TC_EXTRAVERSION) bdir:=$(obj)/tracecmd +sqldir:=$(bdir)/sqlparser +LIBSQLPARSE = $(sqldir)/libsqlparse.a + TC_VERSION := $(bdir)/include/tc_version.h TARGETS = $(bdir)/trace-cmd $(TC_VERSION)
@@ -42,6 +45,8 @@ ALL_OBJS := $(TRACE_CMD_OBJS:%.o=$(bdir)/%.o) all_objs := $(sort $(ALL_OBJS)) all_deps := $(all_objs:$(bdir)/%.o=$(bdir)/.%.d) +CFLAGS += -I$(sqldir) + CONFIG_INCLUDES = CONFIG_LIBS = -lrt CONFIG_FLAGS =
@@ -63,7 +68,8 @@ $(all_objs): | $(bdir) $(bdir)/trace-cmd: $(ALL_OBJS) $(Q)$(do_app_build) -$(bdir)/trace-cmd: $(LIBTRACECMD_STATIC) $(LIBTRACEEVENT_STATIC) $(LIBTRACEFS_STATIC) +$(bdir)/trace-cmd: $(LIBTRACECMD_STATIC) $(LIBTRACEEVENT_STATIC)\ + $(LIBTRACEFS_STATIC) $(LIBSQLPARSE) $(bdir)/%.o: %.c $(Q)$(call do_compile)
@@ -81,8 +87,12 @@ ifneq ($(dep_includes),) include $(dep_includes) endif +$(LIBSQLPARSE): force + $(Q)$(MAKE) -C $(sqldir) $@ + clean: $(RM) $(bdir)/*.a $(bdir)/*.so $(bdir)/*.o $(bdir)/.*.d $(TARGETS) + $(Q)$(MAKE) -C $(sqldir) $@ force: .PHONY: clean
tracecmd/include/trace-local.h +4 -0
@@ -11,6 +11,7 @@#include "trace-cmd.h" #include "event-utils.h" +#include "sqlhist-api.h" #define TRACE_AGENT_DEFAULT_PORT 823
@@ -213,6 +214,9 @@ struct buffer_instance {char *max_graph_depth; +struct sqlhist_trigger *auto_triggers; +struct sqlhist_synth_event *synth_events; +int flags;int tracing_on_init_val;int tracing_on_fd;
tracecmd/sqlparser/Makefile new file +52 -0
53 lines not on this page · raw message
tracecmd/sqlparser/sqlhist-api.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: LGPL-2.0 */ +/* +* Copyright (C) 2020 VMware Inc, Steven Rostedt <[email protected]> +* +*/ +#ifndef __SQLHIST_API_H +#define __SQLHIST_API_H + +struct sqlhist_trigger { +struct sqlhist_trigger *next; +char *system; +char *event; +char *config; +}; + +struct sqlhist_synth_event { +struct sqlhist_synth_event *next; +char *name; +char *config; +}; + +int sqlhist_parse (char *buff,size_t buff_size, +struct sqlhist_trigger **triggers, +struct sqlhist_synth_event **synt_events); + +void sqlhist_trigger_free (struct sqlhist_trigger *list); +void sqlhist_synth_event_free (struct sqlhist_synth_event *list); + +#endif /* __SQLHIST_API_H */
tracecmd/sqlparser/sqlhist-parse.c new file +1454 -0
1,454 lines not on this page · raw message
tracecmd/sqlparser/sqlhist.h new file +34 -0
@@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.0 */ +/* +* Copyright (C) 2020 VMware Inc, Steven Rostedt <[email protected]> +* +*/ +#ifndef __SQLHIST_H +#define __SQLHIST_H + +#include <tracefs.h> +#include "sqlhist.tab.h" + +char *store_str (const char *str); +char *store_printf (const char *fmt, ...); +void add_label (const char *label,const char *val); +void add_match (const char *A ,const char *B ); +void table_start (void ); +void table_end (const char *name); +void from_table_end (const char *name); +void simple_table_end (void ); + +const char *show_expr (void *expr); +void *add_plus (void *A ,void *B ); +void *add_minus (void *A ,void *B ); +void *add_mult (void *A ,void *B ); +void *add_divid (void *A ,void *B ); +void *add_field (const char *field,const char *label); + +void add_expr (const char *name,void *expr); + +void add_selection (void *item); +void add_from (void *item); +void add_to (void *item); + +#endif /* __SQLHIST_H */
tracecmd/sqlparser/sqlhist.l new file +59 -0
59 lines not on this page · raw message
tracecmd/sqlparser/sqlhist.y new file +175 -0
175 lines not on this page · raw message
tracecmd/trace-record.c +112 -2
192 lines not on this page · raw message
tracecmd/trace-usage.c +2 -0
@@ -60,6 +60,8 @@ static struct usage_help usage_help[] = {" --no-filter include trace-cmd threads in the trace\n" " --proc-map save the traced processes address map into the trace.dat file\n" " --user execute the specified [command ...] as given user\n" +" --sql generate necessary ftrace syntetic events and triggers, based on the given sql query\n" +" -V dump ftrace configuration, auto generated by the --sql option" }, { "start",
Signature · 1 line
2.24.1