Skip to contentSkip to conversation

libtracefs: Fix trace_sql() to handle buckets cast

PATCH New Steven Rostedt 1 participant 1 message Apr 10, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libtracefs: Fix trace_sql() to handle buckets cast

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

The "CAST(start.delta as buckets=50)" doesn't work as the sqlhist.y doesn't parse the =50 properly, and even if it did, the code in the histogram logic fails to process the count.

Update the sqlhist.y (and run make sqlhist_remake) and fix the histogram logic to process the buckets count.

Fixes faa0ba7f ("libtracefs: Add buckets field to tracefs_sql() function")
---
 src/sqlhist-parse.h   |   1 +
 src/sqlhist.tab.c     | 351 ++++++++++++++++++++++--------------------
 src/sqlhist.y         |   4 +
 src/tracefs-hist.c    |  32 +++-
 src/tracefs-sqlhist.c |  17 ++
 5 files changed, 231 insertions(+), 174 deletions(-)
src/sqlhist-parse.h +1 -0
index 5e8d1c8..69aa266 100644
@@ -67,6 +67,7 @@ int tfs_add_selection(struct sqlhist_bison *sb, void *item, const char *label);
 int tfs_add_from(struct sqlhist_bison *sb, void *item);
 int tfs_add_to(struct sqlhist_bison *sb, void *item);
 void *tfs_add_cast(struct sqlhist_bison *sb, void *field, const char *type);
+void *tfs_add_cast_buckets(struct sqlhist_bison *sb, void *field, const char *type, int num);
 
 void *tfs_add_string(struct sqlhist_bison *sb, const char *str);
 void *tfs_add_number(struct sqlhist_bison *sb, long val);
src/sqlhist.tab.c +180 -171

538 lines not on this page · raw message

src/sqlhist.y +4 -0
index c35a1a3..96609bb 100644
@@ -108,6 +108,10 @@ selection_expr :
  | '(' field ')'		{  $$ = $2; }
  | selection_addition
  | '(' selection_addition ')'	{  $$ = $2; }
+ | CAST '(' field AS FIELD '=' NUMBER ')' {
+					 $$ = tfs_add_cast_buckets(sb, $3, $5, $7);
+					 CHECK_RETURN_PTR($$);
+				}
  | CAST '(' field AS FIELD ')'	{
 					 $$ = tfs_add_cast(sb, $3, $5);
 					 CHECK_RETURN_PTR($$);
src/tracefs-hist.c +29 -3
index 0ea8312..bfbcd65 100644
@@ -250,6 +250,17 @@ tracefs_hist_alloc(struct tep_handle *tep,
 	return tracefs_hist_alloc_nd(tep, system, event_name, axis);
 }
 
+static struct tracefs_hist *
+hist_alloc_cnt(struct tep_handle *tep,
+	       const char *system, const char *event_name,
+	       const char *key, enum tracefs_hist_key_type type,
+	       int cnt)
+{
+	struct tracefs_hist_axis_cnt axis[] = {{key, type, cnt}, {NULL, 0}};
+
+	return tracefs_hist_alloc_nd_cnt(tep, system, event_name, axis);
+}
+
 /**
  * tracefs_hist_alloc_2d - Initialize two-dimensional histogram
  * @tep: The tep handle that has the @system and @event.
@@ -323,6 +334,7 @@ hist_alloc_nd(struct tep_handle *tep,
 	tracefs_hist_free(hist);
 	return NULL;
 }
+
 /**
  * tracefs_hist_alloc_nd - Initialize N-dimensional histogram
  * @tep: The tep handle that has the @system and @event.
@@ -778,6 +790,7 @@ struct tracefs_synth {
 	unsigned int		end_parens;
 	unsigned int		end_state;
 	int			*start_type;
+	int			*start_cnts;
 	char			arg_name[16];
 	int			arg_cnt;
 	bool			new_format;
@@ -847,6 +860,7 @@ void tracefs_synth_free(struct tracefs_synth *synth)
 	free(synth->start_filter);
 	free(synth->end_filter);
 	free(synth->start_type);
+	free(synth->start_cnts);
 
 	tep_unref(synth->tep);
 
@@ -1473,6 +1487,7 @@ __hidden int tfs_synth_add_start_field(struct tracefs_synth *synth,
 	char *start_arg;
 	char **tmp;
 	int *types;
+	int *cnts;
 	int len;
 	int ret;
 
@@ -1526,6 +1541,14 @@ __hidden int tfs_synth_add_start_field(struct tracefs_synth *synth,
 	synth->start_type = types;
 	synth->start_type[len - 1] = type;
 
+	cnts = realloc(synth->start_cnts, sizeof(*cnts) * len);
+	if (!cnts) {
+		ret = -1;
+		goto out_free;
+	}
+	synth->start_cnts = cnts;
+	synth->start_cnts[len - 1] = count;
+
  out_free:
 	free(start_arg);
 	return ret;
@@ -2182,6 +2205,7 @@ tracefs_synth_get_start_hist(struct tracefs_synth *synth)
 	const char *key;
 	char **keys;
 	int *types;
+	int *cnts;
 	int ret;
 	int i;
 
@@ -2194,6 +2218,7 @@ tracefs_synth_get_start_hist(struct tracefs_synth *synth)
 	event = synth->start_event->name;
 	types = synth->start_type;
 	keys = synth->start_keys;
+	cnts = synth->start_cnts;
 	tep = synth->tep;
 
 	if (!keys)
@@ -2204,6 +2229,7 @@ tracefs_synth_get_start_hist(struct tracefs_synth *synth)
 
 	for (i = 0; keys[i]; i++) {
 		int type = types ? types[i] : 0;
+		int cnt = cnts ? cnts[i] : 0;
 
 		if (type == HIST_COUNTER_TYPE)
 			continue;
@@ -2211,14 +2237,14 @@ tracefs_synth_get_start_hist(struct tracefs_synth *synth)
 		key = keys[i];
 
 		if (i) {
-			ret = tracefs_hist_add_key(hist, key, type);
+			ret = tracefs_hist_add_key_cnt(hist, key, type, cnt);
 			if (ret < 0) {
 				tracefs_hist_free(hist);
 				return NULL;
 			}
 		} else {
-			hist = tracefs_hist_alloc(tep, system, event,
-						  key, type);
+			hist = hist_alloc_cnt(tep, system, event,
+					      key, type, cnt);
 			if (!hist)
 				return NULL;
 		}
src/tracefs-sqlhist.c +17 -0
index 3cad2ff..67be678 100644
@@ -260,6 +260,23 @@ __hidden void *tfs_add_cast(struct sqlhist_bison *sb, void *data, const char *ty
 	return expr;
 }
 
+__hidden void *tfs_add_cast_buckets(struct sqlhist_bison *sb, void *data,
+				    const char *type, int num)
+{
+	struct expr *expr = data;
+	struct field *field = &expr->field;
+	char *str;
+
+	if (asprintf(&str, "%s=%u", type, num) < 0)
+		return NULL;
+
+	field->type = tfs_store_str(sb, str);
+	free(str);
+	if (!field->type)
+		return NULL;
+	return expr;
+}
+
 __hidden int tfs_add_selection(struct sqlhist_bison *sb, void *select, const char *name)
 {
 	struct sql_table *table = sb->table;
Signature · 1 line
2.47.2