Skip to contentSkip to conversation

trace-cmd: SQL-like syntax for ftrace histograms configuration

PATCH New Tzvetomir Stoyanov (VMware) 1 participant 6 messages Feb 26, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 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.

Few trace-cmd library APIs are introduced in this patch set, used in the sql parser logic:
in libtracefs: tracefs_instance_file_append()
in libtraceevent: new flag to suppress parsing warnings

The patch set is based on Steven Rostedt's PoC implementation:
https://github.com/rostedt/sqlhist

[
v3 changes:
- Do not allocate tep handler in sqlparser library, pass it as input parameter to sqlhist_parse() API. The tep handler, initialized with all required ftarce events must, be supplied by the API caller.
- Add tep handler to common_record_context of trace-cmd record and similar commands. Add logic to initialize and destroy it.
- Implemented new traceevent API to suppress parsing error while reading ftrace events.
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) (5):
trace-cmd: Add new libtracefs API tracefs_instance_file_append()
trace-cmd: Unit test for tracefs_instance_file_append() API
trace-cmd: Add new libtraceevent flag to suppress parsing warnings
trace-cmd: Suppress parsing warnings in tracefs_local_events() API
trace-cmd: Add "--sql" option to trace-cmd start and record sub
commands

 Documentation/trace-cmd-record.1.txt |   37 +
 Makefile                             |    1 +
 include/traceevent/event-parse.h     |    2 +
 include/tracefs/tracefs.h            |    2 +
 lib/traceevent/event-parse.c         |    6 +-
 lib/tracefs/tracefs-events.c         |    2 +-
 lib/tracefs/tracefs-instance.c       |   53 +-
 tracecmd/Makefile                    |   12 +-
 tracecmd/include/trace-local.h       |    4 +
 tracecmd/sqlparser/Makefile          |   52 +
 tracecmd/sqlparser/sqlhist-api.h     |   29 +
 tracecmd/sqlparser/sqlhist-parse.c   | 1526 ++++++++++++++++++++++++++
 tracecmd/sqlparser/sqlhist.h         |   34 +
 tracecmd/sqlparser/sqlhist.l         |   59 +
 tracecmd/sqlparser/sqlhist.y         |  175 +++
 tracecmd/trace-record.c              |  142 ++-
 tracecmd/trace-usage.c               |    2 +
 utest/tracefs-utest.c                |  106 +-
 18 files changed, 2202 insertions(+), 42 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
#1 lore raw

PATCH v3 1/5 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 | 53 +++++++++++++++++++++++++---------
 2 files changed, 41 insertions(+), 14 deletions(-)
include/tracefs/tracefs.h +2 -0
index bc8bebcb..22f147a3 100644
@@ -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 +39 -14

80 lines not on this page · raw message

Signature · 1 line
2.24.1
#2 lore raw

PATCH v3 2/5 trace-cmd: Unit test for tracefs_instance_file_append() API

In reply to Tzvetomir Stoyanov (VMware)

A unit test for tracefs_instance_file_append() API is added.

---
 utest/tracefs-utest.c | 106 +++++++++++++++++++++++++++++++++---------
 1 file changed, 84 insertions(+), 22 deletions(-)
utest/tracefs-utest.c +84 -22
index 3f57ecad..1dc15e1b 100644
@@ -176,6 +176,86 @@ static void test_instance_file_read(struct tracefs_instance *inst, char *fname)
 #define ALL_TRACERS	"available_tracers"
 #define CUR_TRACER	"current_tracer"
 #define PER_CPU		"per_cpu"
+#define SYNTH_EVENTS	"synthetic_events"
+static void test_trace_file_read(struct tracefs_instance *instance)
+{
+	test_instance_file_read(NULL, ALL_TRACERS);
+	test_instance_file_read(instance, ALL_TRACERS);
+}
+
+static void test_trace_file_write(struct tracefs_instance *instance)
+{
+	char *tracer;
+	char *file1;
+	char *file2;
+	int size;
+	int ret;
+
+	file1 = tracefs_instance_file_read(instance, ALL_TRACERS, NULL);
+	CU_TEST(file1 != NULL);
+	tracer = strtok(file1, " ");
+	CU_TEST(tracer != NULL);
+	ret = tracefs_instance_file_write(instance, CUR_TRACER, tracer);
+	CU_TEST(ret == strlen(tracer));
+	file2 = tracefs_instance_file_read(instance, CUR_TRACER, &size);
+	CU_TEST(file2 != NULL);
+	CU_TEST(size >= strlen(tracer));
+	CU_TEST(strncmp(file2, tracer, strlen(tracer)) == 0);
+	free(file1);
+	free(file2);
+}
+
+static bool check_file_line(struct tracefs_instance *instance,
+			    char *fname, char *line, bool last)
+{
+	bool found = false;
+	char *buf = NULL;
+	char *l;
+	int n;
+
+	buf = tracefs_instance_file_read(instance, fname, &n);
+	if (!buf)
+		return 0;
+	l = strtok(buf, "\n");
+	while (l) {
+		if (strncmp(l, line, strlen(line)) == 0) {
+			if (last) {
+				if (strtok(NULL, "\n") == NULL)
+					found = true;
+			} else
+				found = true;
+			break;
+		}
+		l = strtok(NULL, "\n");
+	}
+	free(buf);
+	return found;
+}
+
+static void test_trace_file_append(struct tracefs_instance *instance)
+{
+	char *sevent1 = "first	u64 start; u64 end; pid_t pid; u64 delta";
+	char *sevent2 = "second	u64 start; u64 end; pid_t pid; u64 delta";
+	char buf[256];
+	int ret;
+
+	if (!tracefs_file_exist(NULL, SYNTH_EVENTS))
+		return;
+
+	CU_TEST(!check_file_line(NULL, SYNTH_EVENTS, sevent1, false));
+	ret = tracefs_instance_file_append(NULL, SYNTH_EVENTS, sevent1);
+	CU_TEST(ret == strlen(sevent1));
+	CU_TEST(check_file_line(NULL, SYNTH_EVENTS, sevent1, true));
+	ret = tracefs_instance_file_append(NULL, SYNTH_EVENTS, sevent2);
+	CU_TEST(ret == strlen(sevent2));
+	CU_TEST(check_file_line(NULL, SYNTH_EVENTS, sevent1, false));
+	CU_TEST(check_file_line(NULL, SYNTH_EVENTS, sevent2, true));
+	snprintf(buf, 256, "!%s", sevent1);
+	tracefs_instance_file_write(NULL, SYNTH_EVENTS, buf);
+	snprintf(buf, 256, "!%s", sevent2);
+	tracefs_instance_file_write(NULL, SYNTH_EVENTS, buf);
+}
+
 static void test_instance_file(void)
 {
 	struct tracefs_instance *instance = NULL;
@@ -186,11 +266,6 @@ static void test_instance_file(void)
 	char *inst_dir;
 	struct stat st;
 	char *fname;
-	char *file1;
-	char *file2;
-	char *tracer;
-	int size;
-	int ret;
 
 	tdir  = tracefs_get_tracing_dir();
 	CU_TEST(tdir != NULL);
@@ -232,26 +307,13 @@ static void test_instance_file(void)
 	inst_file = tracefs_instance_get_file(instance, ALL_TRACERS);
 	CU_TEST(inst_file != NULL);
 	CU_TEST(strcmp(fname, inst_file) == 0);
-
-	test_instance_file_read(NULL, ALL_TRACERS);
-	test_instance_file_read(instance, ALL_TRACERS);
-
-	file1 = tracefs_instance_file_read(instance, ALL_TRACERS, NULL);
-	CU_TEST(file1 != NULL);
-	tracer = strtok(file1, " ");
-	CU_TEST(tracer != NULL);
-	ret = tracefs_instance_file_write(instance, CUR_TRACER, tracer);
-	CU_TEST(ret == strlen(tracer));
-	file2 = tracefs_instance_file_read(instance, CUR_TRACER, &size);
-	CU_TEST(file2 != NULL);
-	CU_TEST(size >= strlen(tracer));
-	CU_TEST(strncmp(file2, tracer, strlen(tracer)) == 0);
-	free(file1);
-	free(file2);
-
 	tracefs_put_tracing_file(inst_file);
 	free(fname);
 
+	test_trace_file_read(instance);
+	test_trace_file_write(instance);
+	test_trace_file_append(instance);
+
 	CU_TEST(tracefs_file_exist(NULL, (char *)name) == false);
 	CU_TEST(tracefs_dir_exist(NULL, (char *)name) == false);
 	CU_TEST(tracefs_file_exist(instance, (char *)name) == false);
Signature · 1 line
2.24.1
#3 lore raw

PATCH v3 3/5 trace-cmd: Add new libtraceevent flag to suppress parsing warnings

In reply to Tzvetomir Stoyanov (VMware)

When the event's format strings are parsed by tep_parse_format() API and there is a parsing error, a warning is printed. There are use cases when these errors are not important and should be suppressed. A new flag is added to
enum tep_flag {
...
TEP_NO_PARSING_WARNINGS
...
}
for such use cases. It can be set / cleared / checked using the existing libtraceevent APIs:
void tep_set_flag(struct tep_handle *tep, int flag);
void tep_clear_flag(struct tep_handle *tep, enum tep_flag flag); bool tep_test_flag(struct tep_handle *tep, enum tep_flag flag);

---
 include/traceevent/event-parse.h | 2 ++
 lib/traceevent/event-parse.c     | 6 ++++--
 2 files changed, 6 insertions(+), 2 deletions(-)
include/traceevent/event-parse.h +2 -0
index 52bafa54..49e3d287 100644
@@ -321,6 +321,8 @@ enum tep_flag {
 	TEP_NSEC_OUTPUT		= 1,	/* output in NSECS */
 	TEP_DISABLE_SYS_PLUGINS	= 1 << 1,
 	TEP_DISABLE_PLUGINS	= 1 << 2,
+	TEP_NO_PARSING_WARNINGS	= 1 << 3, /* Disable warnings while parsing
+					     event's format strings */
 };
 
 #define TEP_ERRORS 							      \
lib/traceevent/event-parse.c +4 -2
index 064c100d..61cd7ea1 100644
@@ -6222,6 +6222,7 @@ enum tep_errno __tep_parse_format(struct tep_event **eventp,
 				  struct tep_handle *tep, const char *buf,
 				  unsigned long size, const char *sys)
 {
+	int show_warning_state = show_warning;
 	struct tep_event *event;
 	int ret;
 
@@ -6274,11 +6275,12 @@ enum tep_errno __tep_parse_format(struct tep_event **eventp,
 	 * If the event has an override, don't print warnings if the event
 	 * print format fails to parse.
 	 */
-	if (tep && find_event_handle(tep, event))
+	if (tep && (tep_test_flag(tep, TEP_NO_PARSING_WARNINGS) ||
+		    find_event_handle(tep, event)))
 		show_warning = 0;
 
 	ret = event_read_print(event);
-	show_warning = 1;
+	show_warning = show_warning_state;
 
 	if (ret < 0) {
 		ret = TEP_ERRNO__READ_PRINT_FAILED;
Signature · 1 line
2.24.1
#4 lore raw

PATCH v3 4/5 trace-cmd: Suppress parsing warnings in tracefs_local_events() API

In reply to Tzvetomir Stoyanov (VMware)

The tracefs_local_events() API allocates new tep handler and initializes it with the events from the local system. However, at that moment no traceevent plugins are loaded. Part of events loading is parsing the event's format file, but successful parsing of print format string from this file depends of traceevent plugins. That's why a lot of parsing could be printed.
To suppress the warnings, the TEP_NO_PARSING_WARNINGS flag is set on the newly allocated tep handler.

---
 lib/tracefs/tracefs-events.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tracefs/tracefs-events.c +1 -1
index 8e825f50..0875e665 100644
@@ -590,7 +590,7 @@ struct tep_handle *tracefs_local_events_system(const char *tracing_dir,
 	tep = tep_alloc();
 	if (!tep)
 		return NULL;
-
+	tep_set_flag(tep, TEP_NO_PARSING_WARNINGS);
 	if (fill_local_events_system(tracing_dir, tep, sys_names, NULL)) {
 		tep_free(tep);
 		tep = NULL;
Signature · 1 line
2.24.1
#5 lore raw

PATCH v3 5/5 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   | 1526 ++++++++++++++++++++++++++
 tracecmd/sqlparser/sqlhist.h         |   34 +
 tracecmd/sqlparser/sqlhist.l         |   59 +
 tracecmd/sqlparser/sqlhist.y         |  175 +++
 tracecmd/trace-record.c              |  142 ++-
 tracecmd/trace-usage.c               |    2 +
 12 files changed, 2070 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
index 0d75e43d..fd482e1c 100644
@@ -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
index a3facaa9..1928f1cf 100644
@@ -224,6 +224,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
index 0976341c..678dab31 100644
@@ -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
index 7a0804e2..108b9a0a 100644
@@ -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
new file mode 100644
index 00000000..bb47990c
@@ -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(struct tep_handle *tep, 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 +1526 -0

1,526 lines not on this page · raw message

tracecmd/sqlparser/sqlhist.h new file +34 -0
new file mode 100644
index 00000000..68ff196e
@@ -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 +140 -2

238 lines not on this page · raw message

tracecmd/trace-usage.c +2 -0
index 502a4102..ba196eca 100644
@@ -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