trace-cmd: SQL-like syntax for ftrace histograms configuration
Subsystems (2)
v2 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.
[
v2 changes:
- Removed patches for trace-cmd reset sub command from this patch set, as they are not directly related to the SQL-like syntax implementation.
- Fixed the use case with more than one "--sql" argument. Added logic to clean up the parser state before parsing a new string.
- Implemented API for freeing all resources, allocated by the parser.
- Fixed the use case with ftrace instances.
]
Tzvetomir Stoyanov (VMware) (2):
trace-cmd: Add new libtracefs API tracefs_instance_file_append()
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 | 1539 ++++++++++++++++++++++++++ tracecmd/sqlparser/sqlhist.h | 34 + tracecmd/sqlparser/sqlhist.l | 59 + tracecmd/sqlparser/sqlhist.y | 175 +++ tracecmd/trace-record.c | 121 +- tracecmd/trace-usage.c | 2 + 14 files changed, 2100 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 v2 1/2 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
PATCH v2 2/2 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 | 1539 ++++++++++++++++++++++++++ tracecmd/sqlparser/sqlhist.h | 34 + tracecmd/sqlparser/sqlhist.l | 59 + tracecmd/sqlparser/sqlhist.y | 175 +++ tracecmd/trace-record.c | 121 +- tracecmd/trace-usage.c | 2 + 12 files changed, 2062 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 +1539 -0
1,539 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 +119 -2
199 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