Skip to contentSkip to conversation

libtracefs: Fixes for sqlhist

PATCH In review Steven Rostedt 2 participants 8 messages Jun 6, 2022 – Jun 7, 2022 Documentation

lore
Share & tools
lore
#0 lore raw

cover libtracefs: Fixes for sqlhist

From: "Steven Rostedt (Google)" <[email protected]>

At Kernel Recipes, I demonstrated sqlhist and how to use it. Along the way, I discovered several bugs. This series addresses those bugs that were found.

Steven Rostedt (Google) (6):
libtracefs: Fix make sqlhist when built again
libtracefs: Add libtracefs.a to dependency of sqlhist
libtracefs: Differentiate FROM and JOIN events if they are the same
event
libtracefs: Use unique names for sql field variables
libtracefs: Differentiate WHERE clause when FROM and TO events are the
same
libtracefs sqlhist: Report errors executing the commands

 Documentation/libtracefs-sql.txt |  18 ++++--
 include/tracefs-local.h          |   4 ++
 samples/Makefile                 |   4 +-
 src/sqlhist-parse.h              |   3 +-
 src/tracefs-hist.c               | 105 ++++++++++++++++++++++++++++---
 src/tracefs-sqlhist.c            |  44 +++++++++----
 6 files changed, 150 insertions(+), 28 deletions(-)
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/6 libtracefs: Fix make sqlhist when built again

From: "Steven Rostedt (Google)" <[email protected]>

"make sqlhist" currently only works after a "make clean". That is because the sqlhist.c is extracted from the man page and left there after the first build. On the second build, the sqlhist.c still exists and the target for "sqlhist" runs the default build as it has nothing to build with, which fails to build as the default does not work with it.

Add the sqlhist target to .PHONY to tell make that it is not a real target and it should not try to build it using the default methods.

---
 samples/Makefile | 2 ++
 1 file changed, 2 insertions(+)
samples/Makefile +2 -0
index 97a570d6c956..c1392fbbb12b 100644
@@ -67,3 +67,5 @@ $(bdir)/%.o: $(bdir)/%.c
 
 clean:
 	$(Q)$(call do_clean,$(sdir)/* $(bdir)/sqlhist.c $(bdir)/sqlhist.o)
+
+.PHONY: sqlhist
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/6 libtracefs: Add libtracefs.a to dependency of sqlhist

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

As sqlhist is statically linked to the libtracefs.a file, it should be rebuilt if the libtracefs.a is updated. Because sqlhist.c is extracted from the man pages, it does not get the automatic dependency updates like the rest of the source tree has.

---
 samples/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/Makefile +1 -1
index c1392fbbb12b..6628679e6227 100644
@@ -46,7 +46,7 @@ sqlhist: $(sdir)/sqlhist
 $(TARGETS): $(sdir)
 
 # sqlhist is unique and stands on its own
-$(sdir)/sqlhist: $(bdir)/sqlhist.c
+$(sdir)/sqlhist: $(bdir)/sqlhist.c $(LIBTRACEFS_STATIC)
 	$(call do_sample_build,$@,$<)
 
 $(sdir)/%: $(bdir)/%.o
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/6 libtracefs: Differentiate FROM and JOIN events if they are the same event

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

If both the FROM and the JOIN event are the same, the fields get confusing due to the way tracefs_sql() does not distinguish the events when transforming the variable names, and it may process the JOIN fields as the FROM fields, and the wrong field gets passed to the synthetic event.

Add a new FIELD_TYPE enum and set the fields to FROM or TO to know which event the field belongs to, and use that to avoid picking the field for the wrong event.

---
 src/tracefs-sqlhist.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
src/tracefs-sqlhist.c +16 -1
index caf927fd5e1c..d854f7b54344 100644
@@ -30,6 +30,12 @@ enum alias_type {
 	ALIAS_FIELD,
 };
 
+enum field_type {
+	FIELD_NONE,
+	FIELD_FROM,
+	FIELD_TO,
+};
+
 #define for_each_field(expr, field, table) \
 	for (expr = (table)->fields; expr; expr = (field)->next)
 
@@ -42,6 +48,7 @@ struct field {
 	const char		*label;
 	const char		*field;
 	const char		*type;
+	enum field_type		ftype;
 };
 
 struct filter {
@@ -583,6 +590,7 @@ static int update_vars(struct tep_handle *tep,
 {
 	struct sqlhist_bison *sb = table->sb;
 	struct field *event_field = &expr->field;
+	enum field_type ftype = FIELD_NONE;
 	struct tep_event *event;
 	struct field *field;
 	const char *label;
@@ -592,6 +600,11 @@ static int update_vars(struct tep_handle *tep,
 	const char *p;
 	int label_len = 0, event_len, system_len;
 
+	if (expr == table->to)
+		ftype = FIELD_TO;
+	else if (expr == table->from)
+		ftype = FIELD_FROM;
+
 	p = strchr(raw, '.');
 	if (p) {
 		char *str;
@@ -673,6 +686,7 @@ static int update_vars(struct tep_handle *tep,
 		field->event_name = event_name;
 		field->event = event;
 		field->field = raw + len + 1;
+		field->ftype = ftype;
 
 		if (!strcmp(field->field, "TIMESTAMP"))
 			field->field = store_str(sb, TRACEFS_TIMESTAMP);
@@ -1452,7 +1466,8 @@ static struct tracefs_synth *build_synth(struct tep_handle *tep,
 		if (expr->type == EXPR_FIELD) {
 			ret = -1;
 			field = &expr->field;
-			if (field->system == start_system &&
+			if (field->ftype != FIELD_TO &&
+			    field->system == start_system &&
 			    field->event_name == start_event) {
 				int type;
 				type = verify_field_type(tep, table->sb, expr);
Signature · 1 line
2.35.1
#4 lore raw

PATCH 4/6 libtracefs: Use unique names for sql field variables

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

When creating two different synthetic events using tracefs_sql(), if they share the same label, the second one will fail because tracefs_sql() will use that label as a variable name. In the kernel, each variable must be unique, and the second label will cause a conflict with the variable named after the first label.

Use a unique identifier when creating the variables based on the label, but still keep the label text in the variable name to let users still know what label the variable is for.

---
 include/tracefs-local.h |   4 ++
 src/sqlhist-parse.h     |   3 +-
 src/tracefs-hist.c      | 105 ++++++++++++++++++++++++++++++++++++----
 src/tracefs-sqlhist.c   |   2 +-
 4 files changed, 102 insertions(+), 12 deletions(-)
include/tracefs-local.h +4 -0
index d0ed2abfe5e7..678b9870d03e 100644
@@ -15,6 +15,8 @@
 #define BUILD_BUG_ON(cond)			\
 	do { if (!(1/!(cond))) { } } while (0)
 
+#define HASH_BITS 10
+
 struct tracefs_options_mask {
 	unsigned long long	mask;
 };
@@ -120,4 +122,6 @@ int trace_rescan_events(struct tep_handle *tep,
 struct tep_event *get_tep_event(struct tep_handle *tep,
 				const char *system, const char *name);
 
+unsigned int quick_hash(const char *str);
+
 #endif /* _TRACE_FS_LOCAL_H */
src/sqlhist-parse.h +2 -1
index d5b86cacd8ce..7bd2a6350ece 100644
@@ -4,8 +4,9 @@
 #include <stdarg.h>
 #include <tracefs.h>
 
+#include <tracefs-local.h>
+
 struct str_hash;
-#define HASH_BITS 10
 
 struct sql_table;
 
src/tracefs-hist.c +95 -10

211 lines not on this page · raw message

src/tracefs-sqlhist.c +1 -1
index d854f7b54344..22e4b652e48b 100644
@@ -174,7 +174,7 @@ static void parse_error(struct sqlhist_bison *sb, const char *text,
 	va_end(ap);
 }
 
-static inline unsigned int quick_hash(const char *str)
+__hidden unsigned int quick_hash(const char *str)
 {
 	unsigned int val = 0;
 	int len = strlen(str);
Signature · 1 line
2.35.1
#5 lore raw

PATCH 5/6 libtracefs: Differentiate WHERE clause when FROM and TO events are the same

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

With "FROM sched_switch as start JOIN sched_switch AS end" and then filtering with "WHERE start.prev_state = 2 && end.next_prio < 100" the parser gets confused and uses the "start.next_prio" instead of the "end.next_prio", making the filter only on the start event and not on the end event as expected.

Fix it by testing the field type (FIELD_TO/FIELD_FROM) as well as the system and event of the field when selecting which event the field is a part of.

---
 src/tracefs-sqlhist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
src/tracefs-sqlhist.c +15 -10
index 22e4b652e48b..5205356356a5 100644
@@ -904,20 +904,21 @@ static int verify_filter_error(struct sqlhist_bison *sb, struct expr *expr,
 }
 
 static int do_verify_filter(struct sqlhist_bison *sb, struct filter *filter,
-			    const char **system, const char **event)
+			    const char **system, const char **event,
+			    enum field_type *ftype)
 {
 	int ret;
 
 	if (filter->type == FILTER_OR ||
 	    filter->type == FILTER_AND) {
-		ret = do_verify_filter(sb, &filter->lval->filter, system, event);
+		ret = do_verify_filter(sb, &filter->lval->filter, system, event, ftype);
 		if (ret)
 			return ret;
-		return do_verify_filter(sb, &filter->rval->filter, system, event);
+		return do_verify_filter(sb, &filter->rval->filter, system, event, ftype);
 	}
 	if (filter->type == FILTER_GROUP ||
 	    filter->type == FILTER_NOT_GROUP) {
-		return do_verify_filter(sb, &filter->lval->filter, system, event);
+		return do_verify_filter(sb, &filter->lval->filter, system, event, ftype);
 	}
 
 	/*
@@ -927,6 +928,7 @@ static int do_verify_filter(struct sqlhist_bison *sb, struct filter *filter,
 	if (!*system && !*event) {
 		*system = filter->lval->field.system;
 		*event = filter->lval->field.event_name;
+		*ftype = filter->lval->field.ftype;
 		return 0;
 	}
 
@@ -938,7 +940,8 @@ static int do_verify_filter(struct sqlhist_bison *sb, struct filter *filter,
 }
 
 static int verify_filter(struct sqlhist_bison *sb, struct filter *filter,
-			 const char **system, const char **event)
+			 const char **system, const char **event,
+			 enum field_type *ftype)
 {
 	int ret;
 
@@ -949,17 +952,17 @@ static int verify_filter(struct sqlhist_bison *sb, struct filter *filter,
 	case FILTER_NOT_GROUP:
 		break;
 	default:
-		return do_verify_filter(sb, filter, system, event);
+		return do_verify_filter(sb, filter, system, event, ftype);
 	}
 
-	ret = do_verify_filter(sb, &filter->lval->filter, system, event);
+	ret = do_verify_filter(sb, &filter->lval->filter, system, event, ftype);
 	if (ret)
 		return ret;
 
 	switch (filter->type) {
 	case FILTER_OR:
 	case FILTER_AND:
-		return do_verify_filter(sb, &filter->rval->filter, system, event);
+		return do_verify_filter(sb, &filter->rval->filter, system, event, ftype);
 	default:
 		return 0;
 	}
@@ -1516,16 +1519,18 @@ static struct tracefs_synth *build_synth(struct tep_handle *tep,
 	for (expr = table->where; expr; expr = expr->next) {
 		const char *filter_system = NULL;
 		const char *filter_event = NULL;
+		enum field_type ftype = FIELD_NONE;
 		bool *started;
 		bool start;
 
 		ret = verify_filter(table->sb, &expr->filter, &filter_system,
-				    &filter_event);
+				    &filter_event, &ftype);
 		if (ret < 0)
 			goto free;
 
 		start = filter_system == start_system &&
-			filter_event == start_event;
+			filter_event == start_event &&
+			ftype != FIELD_TO;
 
 		if (start)
 			started = &started_start;
Signature · 1 line
2.35.1
#6 lore raw

PATCH 6/6 libtracefs sqlhist: Report errors executing the commands

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

If the -e option is set and the parsing succeeds but the executing does not, sqlhist exits with success. It should instead check the return of the creation of the histogram or synthetic event and return the error message on failure and not succeed.

---
 Documentation/libtracefs-sql.txt | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
Documentation/libtracefs-sql.txt +14 -4
index 5e48baa4da39..6d606dbcaf88 100644
@@ -407,8 +407,13 @@ static int do_sql(const char *instance_name,
 			}
 		}
 		tracefs_synth_echo_cmd(&seq, synth);
-		if (execute)
-			tracefs_synth_create(synth);
+		if (execute) {
+			ret = tracefs_synth_create(synth);
+			if (ret < 0) {
+				fprintf(stderr, "%s\n", tracefs_error_last(NULL));
+				exit(-1);
+			}
+		}
 	} else {
 		struct tracefs_instance *instance = NULL;
 		struct tracefs_hist *hist;
@@ -430,8 +435,13 @@ static int do_sql(const char *instance_name,
 			}
 		}
 		tracefs_hist_echo_cmd(&seq, instance, hist, 0);
-		if (execute)
-			tracefs_hist_start(instance, hist);
+		if (execute) {
+			ret = tracefs_hist_start(instance, hist);
+			if (ret < 0) {
+				fprintf(stderr, "%s\n", tracefs_error_last(instance));
+				exit(-1);
+			}
+		}
 	}
 
 	tracefs_synth_free(synth);
Signature · 1 line
2.35.1
Tested-by on the v1 series
#7 lore raw
In reply to Steven Rostedt

Hi,

Quoted 4 lines · Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

At Kernel Recipes, I demonstrated sqlhist and how to use it. Along the way, I discovered several bugs. This series addresses those bugs that were found.

For the whole series:

As a sanity check I accumulated offcpu time for a process:

	sqlhist -e -n 'offcpu' 'SELECT start.prev_comm AS comm, (end.TIMESTAMP_USECS - start.TIMESTAMP_USECS) as lat FROM sched_switch as start JOIN sched_switch as end ON start.prev_pid == end.next_pid WHERE start.prev_comm == "ping"'
	echo "hist:keys=comm:vals=lat:sort=lat" >/sys/kernel/tracing/events/synthetic/offcpu/trigger

And then verified the collected time is consistent by comparing with getrusage(2) values for the process (as returned by time(1)).

And indeed,

	wall_time == ru_utime + ru_stime + lat

So it is measuring exactly what I want.

Thanks again for getting this working!

Quoted 18 lines

Steven Rostedt (Google) (6):
libtracefs: Fix make sqlhist when built again
libtracefs: Add libtracefs.a to dependency of sqlhist
libtracefs: Differentiate FROM and JOIN events if they are the same
event
libtracefs: Use unique names for sql field variables
libtracefs: Differentiate WHERE clause when FROM and TO events are the
same
libtracefs sqlhist: Report errors executing the commands

 Documentation/libtracefs-sql.txt |  18 ++++--
 include/tracefs-local.h          |   4 ++
 samples/Makefile                 |   4 +-
 src/sqlhist-parse.h              |   3 +-
 src/tracefs-hist.c               | 105 ++++++++++++++++++++++++++++---
 src/tracefs-sqlhist.c            |  44 +++++++++----
 6 files changed, 150 insertions(+), 28 deletions(-)
Signature · 1 line
Harald