Skip to contentSkip to conversation

libtraceeval: Even more updates!

PATCH In review Steven Rostedt 2 participants 22 messages Sep 27, 2023 – Oct 3, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover libtraceeval: Even more updates!

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

More updates to libtraceeval

- As the results and stats are often needed in the iterator loops, add helper functions to quickly retrieve them. The iterator has a handle on the current entry, no need to do a key lookup to find it.

- The iterator can be used to search for items to remove or update. Add a helper function to remove the current iterator item. This will also update the iterator so that other functions called are still safe, even if the removed entry is the current one.

- Convert traceeval_data into a proper structure with a "type" field and a union data portion. This will allow for checking the type of data being passed into the functions to make sure they match their corresponding types.

- Add checks to make sure that the data matches the types.

- Make traceeval_insert() and traceeval_query() into macros and add a traceeval_query_size() and traceeval_insert_size() that take the size of the list of data being passed into it. The macros work when the arrays are defined.

- Add checks to make sure the array sizes passed in match the sizes internally.

- Add back the STATS flag, and only do STATS on values that are numeric and have this set. This should speed things up, and also allow for optimizations in the future.

Changes since v1: https://lore.kernel.org/linux-trace-devel/[email protected]/

- Use of ARRAY_SIZE() macro to make some other macro logic simpler.

- Fix TRACEEVAL_FL_STAT (was a duplicate of 3 not 4) (Ross Zwisler)

- Add traceeval_remove_size() to check traceeval_remove()

- Add traceeval_query_size() to check traceeval_query()

- Add traceeval_stat_size() to check traceeval_stat()

- Add traceeval_iterator_results_release()

- Use TRACEEVAL_SET_NUMBER() on keys in task-eval.c (Ross Zwisler)

- Use is_stat_type() in copy_traceeval_data() (Ross Zwisler)

- Add type test to update_entry() (Ross Zwisler)

- Folded checking patch into size check patches (Ross Zwisler)

- Free entry on traceeval_remove() and trace_iterator_remove (Ross Zwisler)

- Fix added tab of return -1 (Ross Zwisler)

- Fix comment of traceeval_iterator_query() (Ross Zwisler)

- Fix comment of traceeval_iterator_remove() (Ross Zwisler)

Steven Rostedt (Google) (11):
libtraceeval: Add check for updates to know to recreate iter array
libtraceeval: Add traceeval_iterator_query()
libtraceeval: Add traceeval_iterator_results_release()
libtraceeval: Add traceeval_iterator_stat()
libtraceeval: Add traceeval_iterator_remove() libtraceeval histogram: Add type to traceeval_data and make it a
structure
libtraceveal: Add type checks to traceeval_data vals and keys
libtraceeval: Add size checks to insert and query functions
libtraceeval: Make traceeval_remove() check size of keys array
libtraceeval: Make traceeval_stat() check size of keys array
libtraceeval: Only do stats on values marked with the STAT flag

 include/traceeval-hist.h | 118 +++++++++----
 samples/task-eval.c      | 148 +++++++----------
 src/eval-local.h         |   6 +-
 src/histograms.c         | 349 +++++++++++++++++++++++++++++++--------
 4 files changed, 429 insertions(+), 192 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH v2 1/11 libtraceeval: Add check for updates to know to recreate iter array

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

When an iterator is created, it creates an array of pointers to point to all the elements in the traceeval. This is used to index through the entities in an nice order. But if an event is added or removed from the traceeval, the size and count of this array will be off in the iterator.

Add an "update_counter" that gets incremented every time an item is added or removed (doesn't need to keep track of updates to existing entries). If the counter is different from the last time the iterator created the sort array, it will need to delete and recreate the list again before it can do a sort.

Note: It is safe to use the iterator to remove items, so a removal (or even insert) should not affect the traceeval_iterator_next(). But it should be explained in the man pages (soon to be written) that doing so must be done with care. And maybe a helper function should be used instead!

---
 src/eval-local.h |  2 ++
 src/histograms.c | 91 ++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 74 insertions(+), 19 deletions(-)
src/eval-local.h +2 -0
index 5c3918f17cc1..73f52efdf0da 100644
@@ -68,6 +68,7 @@ struct traceeval {
 	struct hash_table		*hist;
 	size_t				nr_key_types;
 	size_t				nr_val_types;
+	size_t				update_counter;
 };
 
 struct traceeval_iterator {
@@ -75,6 +76,7 @@ struct traceeval_iterator {
 	struct entry			**entries;
 	struct traceeval_type		**sort;
 	bool				*direction;
+	size_t				update_counter;
 	size_t				nr_entries;
 	size_t				nr_sort;
 	size_t				next;
src/histograms.c +72 -19

141 lines not on this page · raw message

Signature · 1 line
2.40.1
#2 lore raw

PATCH v2 2/11 libtraceeval: Add traceeval_iterator_query()

In reply to Steven Rostedt

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

Add a query to the iterator that will return the values of the last entry that matches the keys returned by the last call of traceeval_iterator_next().

---
 include/traceeval-hist.h |  2 ++
 samples/task-eval.c      |  2 +-
 src/histograms.c         | 30 ++++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)
include/traceeval-hist.h +2 -0
index 0cee9bcbeb83..837a74f61a66 100644
@@ -186,5 +186,7 @@ int traceeval_iterator_sort_custom(struct traceeval_iterator *iter,
 				   traceeval_cmp_fn sort_fn, void *data);
 int traceeval_iterator_next(struct traceeval_iterator *iter,
 			    const union traceeval_data **keys);
+int traceeval_iterator_query(struct traceeval_iterator *iter,
+			     const union traceeval_data **results);
 
 #endif /* __LIBTRACEEVAL_HIST_H__ */
samples/task-eval.c +1 -1
index 66d0c40dc0c8..18e07f44e11e 100644
@@ -836,7 +836,7 @@ static void display_processes(struct traceeval *teval,
 		struct process_data *pdata = NULL;
 		const char *comm = keys[0].cstring;
 
-		ret = traceeval_query(teval_data, keys, &results);
+		ret = traceeval_iterator_query(iter, &results);
 		if (ret < 0)
 			pdie("Could not query iterator");
 		if (ret < 1)
src/histograms.c +30 -0
index 9bf20e4d6d26..06613a8933ec 100644
@@ -1306,3 +1306,33 @@ int traceeval_iterator_next(struct traceeval_iterator *iter,
 	*keys = entry->keys;
 	return 1;
 }
+
+/**
+ * traceeval_iterator_query - return the results from the current entry in the iterator
+ * @iter: The iterator to retrieve the entry results from
+ * @results: The returned results of the last entry (if exists)
+ *
+ * This returns the @results of the values from the last instance of
+ * traceeval_iterator_next(). It is equivalent of calling:
+ *
+ * traceeval_query() with the keys returned by traceeval_iterator_next().
+ *
+ * Except that it will always quickly return the current entry, whereas the
+ * traceeval_query() will reset the cached next_entry and do a full
+ * lookup again.
+ *
+ * Returns 1 if another entry is returned, or 0 if not (or negative on error)
+ */
+int traceeval_iterator_query(struct traceeval_iterator *iter,
+			     const union traceeval_data **results)
+{
+	struct entry *entry;
+
+	if (iter->next < 1 || iter->next > iter->nr_entries)
+		return 0;
+
+	entry = iter->entries[iter->next - 1];
+	*results = entry->vals;
+
+	return 1;
+}
Signature · 1 line
2.40.1
#3 lore raw

PATCH v2 3/11 libtraceeval: Add traceeval_iterator_results_release()

In reply to Steven Rostedt

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

Add the function that can release the results from the traceeval_iterator_qeury() function.

Currently, the user must use traceeval_results_release() for the results returned by traceeval_iterator_query(), but that requires the caller to also have access to the traceeval where the iterator came from. There could be many iterators and many traceevals, the user should not need to worry about which one goes with which.

This new traceeval_iterator_results_release() will take care of that for them.

---
 include/traceeval-hist.h |  2 ++
 src/histograms.c         | 23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+)
include/traceeval-hist.h +2 -0
index 837a74f61a66..80450d7ae7f9 100644
@@ -188,5 +188,7 @@ int traceeval_iterator_next(struct traceeval_iterator *iter,
 			    const union traceeval_data **keys);
 int traceeval_iterator_query(struct traceeval_iterator *iter,
 			     const union traceeval_data **results);
+void traceeval_iterator_results_release(struct traceeval_iterator *iter,
+					const union traceeval_data *results);
 
 #endif /* __LIBTRACEEVAL_HIST_H__ */
src/histograms.c +23 -0
index 06613a8933ec..50dc8b689637 100644
@@ -1336,3 +1336,26 @@ int traceeval_iterator_query(struct traceeval_iterator *iter,
 
 	return 1;
 }
+
+/*
+ * traceeval_iterator_results_release - release the results return by traceeval_iterator_query()
+ * @iter: The iterator descriptor used in traceeval_iterator_query()
+ * @results: The results returned by traceeval_iterator_query()
+ *
+ * The @results returned by traceeval_iterator_query() is owned by @teval,
+ * that is attached to the iterator and how it manages it is implementation
+ * specific. The caller should not worry about it. When the caller of
+ * traceeval_iterator_query() is done with the @results, it must call
+ * traceeval_iterator_results_release() (or traceeval_results_release() if it
+ * has the handle of the teval used to get the iterator) on it to allow traceeval
+ * to clean up its references.
+ */
+void traceeval_iterator_results_release(struct traceeval_iterator *iter,
+					const union traceeval_data *results)
+{
+	if (!iter || !results) {
+		if (!iter)
+			print_err("Results to be freed without accompanied iterator!");
+		return;
+	}
+}
Signature · 1 line
2.40.1
Reviewed-by on v2 3/11
#4 lore raw

Re: [PATCH v2 03/11] libtraceeval: Add traceeval_iterator_results_release()

Quoted 11 lines · Steven Rostedt

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

Add the function that can release the results from the traceeval_iterator_qeury() function.

Currently, the user must use traceeval_results_release() for the results returned by traceeval_iterator_query(), but that requires the caller to also have access to the traceeval where the iterator came from. There could be many iterators and many traceevals, the user should not need to worry about which one goes with which.

Quoted 4 lines

This new traceeval_iterator_results_release() will take care of that for them.

#5 lore raw

PATCH v2 4/11 libtraceeval: Add traceeval_iterator_stat()

In reply to Steven Rostedt

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

Add traceeval_iterator_stat() that will return a stat of a value of the current entry from a traceeval_iterator_next() loop.

---
 include/traceeval-hist.h |  2 ++
 samples/task-eval.c      |  6 ++---
 src/histograms.c         | 47 ++++++++++++++++++++++++++++++++--------
 3 files changed, 43 insertions(+), 12 deletions(-)
include/traceeval-hist.h +2 -0
index 80450d7ae7f9..d6c7097a66c8 100644
@@ -190,5 +190,7 @@ int traceeval_iterator_query(struct traceeval_iterator *iter,
 			     const union traceeval_data **results);
 void traceeval_iterator_results_release(struct traceeval_iterator *iter,
 					const union traceeval_data *results);
+struct traceeval_stat *traceeval_iterator_stat(struct traceeval_iterator *iter,
+					       struct traceeval_type *type);
 
 #endif /* __LIBTRACEEVAL_HIST_H__ */
samples/task-eval.c +3 -3
index 18e07f44e11e..bde167d1219b 100644
@@ -706,7 +706,7 @@ static void display_cpus(struct traceeval *teval)
 		int state = keys[1].number;
 		int cpu = keys[0].number;
 
-		stat = traceeval_stat(teval, keys, &delta_vals[0]);
+		stat = traceeval_iterator_stat(iter, &delta_vals[0]);
 		if (!stat)
 			continue; // die?
 
@@ -773,7 +773,7 @@ static void display_threads(struct traceeval *teval)
 		int state = keys[1].number;
 		int tid = keys[0].number;
 
-		stat = traceeval_stat(teval, keys, &delta_vals[0]);
+		stat = traceeval_iterator_stat(iter, &delta_vals[0]);
 		if (!stat)
 			continue; // die?
 
@@ -875,7 +875,7 @@ static void display(struct task_data *tdata)
 	while (traceeval_iterator_next(iter, &keys) > 0) {
 		int state = keys[1].number;
 
-		stat = traceeval_stat(teval, keys, &delta_vals[0]);
+		stat = traceeval_iterator_stat(iter, &delta_vals[0]);
 		if (!stat)
 			continue;
 
src/histograms.c +38 -9

67 lines not on this page · raw message

Signature · 1 line
2.40.1
#6 lore raw

PATCH v2 5/11 libtraceeval: Add traceeval_iterator_remove()

In reply to Steven Rostedt

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

Add an API traceeval_iterator_remove() that is safe to call in the traceeval_iterator_next() loop. Currently, traceeval_remove() can also be called "safely", but that may change in the future.

The main difference between traceeval_remove() and traceeval_iterator_remove() is that that traceeval_iterator_remove() will NULL out the entry in the sort array, and use this in the other iterator functions. If the entry is NULL, it will not be returned.

---
 include/traceeval-hist.h |  1 +
 src/histograms.c         | 49 ++++++++++++++++++++++++++++++++++++----
 2 files changed, 46 insertions(+), 4 deletions(-)
include/traceeval-hist.h +1 -0
index d6c7097a66c8..e619d52ea0d0 100644
@@ -192,5 +192,6 @@ void traceeval_iterator_results_release(struct traceeval_iterator *iter,
 					const union traceeval_data *results);
 struct traceeval_stat *traceeval_iterator_stat(struct traceeval_iterator *iter,
 					       struct traceeval_type *type);
+int traceeval_iterator_remove(struct traceeval_iterator *iter);
 
 #endif /* __LIBTRACEEVAL_HIST_H__ */
src/histograms.c +45 -4

72 lines not on this page · raw message

Signature · 1 line
2.40.1
Reviewed-by on v2 5/11
#7 lore raw

Re: [PATCH v2 05/11] libtraceeval: Add traceeval_iterator_remove()

Quoted 6 lines · Steven Rostedt

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

Add an API traceeval_iterator_remove() that is safe to call in the traceeval_iterator_next() loop. Currently, traceeval_remove() can also be called "safely", but that may change in the future.

Quoted 6 lines

The main difference between traceeval_remove() and traceeval_iterator_remove() is that that traceeval_iterator_remove() will NULL out the entry in the sort array, and use this in the other iterator functions. If the entry is NULL, it will not be returned.

#8 lore raw

PATCH v2 6/11 libtraceeval histogram: Add type to traceeval_data and make it a structure

In reply to Steven Rostedt

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

Having a straight union for passing in the data that must match the types is dangerous and prone for buggy code. If some data doesn't match its type, there's nothing to catch it.

Instead of having a union traceeval_data of each type, have it be a structure with a "type" field follow by a union, where the type defines what kind of data it is.

Add helper macros to make it easy to define the data when using it.

---
 include/traceeval-hist.h |  90 ++++++++++++++++---------
 samples/task-eval.c      | 139 ++++++++++++++++-----------------------
 src/eval-local.h         |   4 +-
 src/histograms.c         |  70 ++++++++++----------
 4 files changed, 153 insertions(+), 150 deletions(-)
include/traceeval-hist.h +60 -30
index e619d52ea0d0..78cfac5982ee 100644
@@ -52,45 +52,75 @@ struct traceeval_dynamic {
  * Trace data entry for a traceeval histogram
  * Constitutes keys and values.
  */
-union traceeval_data {
-	struct traceeval_dynamic	dyn_data;
-	char				*string;
-	const char			*cstring;
-	void				*pointer;
-	unsigned long			number;
-	unsigned long long		number_64;
-	unsigned int			number_32;
-	unsigned short			number_16;
-	unsigned char			number_8;
+struct traceeval_data {
+	enum traceeval_data_type		type;
+	union {
+		struct traceeval_dynamic	dyn_data;
+		char				*string;
+		const char			*cstring;
+		void				*pointer;
+		unsigned long			number;
+		unsigned long long		number_64;
+		unsigned int			number_32;
+		unsigned short			number_16;
+		unsigned char			number_8;
+	};
 };
 
+#define __TRACEEVAL_DATA(data_type, member, data)			\
+	{  .type = TRACEEVAL_TYPE_##data_type, .member = (data) }
+
+#define DEFINE_TRACEEVAL_NUMBER(data)	   __TRACEEVAL_DATA(NUMBER, number, data)
+#define DEFINE_TRACEEVAL_NUMBER_8(data)	   __TRACEEVAL_DATA(NUMBER_8, number_8, data)
+#define DEFINE_TRACEEVAL_NUMBER_16(data)   __TRACEEVAL_DATA(NUMBER_16, number_16, data)
+#define DEFINE_TRACEEVAL_NUMBER_32(data)   __TRACEEVAL_DATA(NUMBER_32, number_32, data)
+#define DEFINE_TRACEEVAL_NUMBER_64(data)   __TRACEEVAL_DATA(NUMBER_64, number_64, data)
+#define DEFINE_TRACEEVAL_STRING(data)	   __TRACEEVAL_DATA(STRING, string, data)
+#define DEFINE_TRACEEVAL_CSTRING(data)	   __TRACEEVAL_DATA(STRING, cstring, data)
+#define DEFINE_TRACEEVAL_POINTER(data)	   __TRACEEVAL_DATA(POINTER, pointer, data)
+
+#define __TRACEEVAL_SET(data, data_type, member, val)		\
+	do {							\
+		(data).type = TRACEEVAL_TYPE_##data_type;	\
+		(data).member = (val);				\
+	} while (0)
+
+#define TRACEEVAL_SET_NUMBER(data, val)	     __TRACEEVAL_SET(data, NUMBER, number, val)
+#define TRACEEVAL_SET_NUMBER_8(data, val)    __TRACEEVAL_SET(data, NUMBER_8, number_8, val)
+#define TRACEEVAL_SET_NUMBER_16(data, val)   __TRACEEVAL_SET(data, NUMBER_16, number_16, val)
+#define TRACEEVAL_SET_NUMBER_32(data, val)   __TRACEEVAL_SET(data, NUMBER_32, number_32, val)
+#define TRACEEVAL_SET_NUMBER_64(data, val)   __TRACEEVAL_SET(data, NUMBER_64, number_64, val)
+#define TRACEEVAL_SET_STRING(data, val)	     __TRACEEVAL_SET(data, STRING, string, val)
+#define TRACEEVAL_SET_CSTRING(data, val)     __TRACEEVAL_SET(data, STRING, cstring, val)
+#define TRACEEVAL_SET_POINTER(data, val)     __TRACEEVAL_SET(data, POINTER, pointer, val)
+
 struct traceeval_type;
 struct traceeval;
 
 /* release function callback on traceeval_data */
 typedef void (*traceeval_data_release_fn)(const struct traceeval_type *type,
-					  union traceeval_data *data);
+					  struct traceeval_data *data);
 
 /* compare function callback to compare traceeval_data */
 typedef int (*traceeval_data_cmp_fn)(struct traceeval *teval,
 				     const struct traceeval_type *type,
-				     const union traceeval_data *A,
-				     const union traceeval_data *B);
+				     const struct traceeval_data *A,
+				     const struct traceeval_data *B);
 
 /* make a unique value */
 typedef int (*traceeval_data_hash_fn)(struct traceeval *teval,
 				      const struct traceeval_type *type,
-				      const union traceeval_data *data);
+				      const struct traceeval_data *data);
 
 typedef int (*traceeval_data_copy_fn)(const struct traceeval_type *type,
-				      union traceeval_data *dst,
-				      const union traceeval_data *src);
+				      struct traceeval_data *dst,
+				      const struct traceeval_data *src);
 
 typedef int (*traceeval_cmp_fn)(struct traceeval *teval,
-				const union traceeval_data *Akeys,
-				const union traceeval_data *Avals,
-				const union traceeval_data *Bkeys,
-				const union traceeval_data *Bvals,
+				const struct traceeval_data *Akeys,
+				const struct traceeval_data *Avals,
+				const struct traceeval_data *Bkeys,
+				const struct traceeval_data *Bvals,
 				void *data);
 
 /*
@@ -157,20 +187,20 @@ struct traceeval *traceeval_init(struct traceeval_type *keys,
 void traceeval_release(struct traceeval *teval);
 
 int traceeval_insert(struct traceeval *teval,
-		     const union traceeval_data *keys,
-		     const union traceeval_data *vals);
+		     const struct traceeval_data *keys,
+		     const struct traceeval_data *vals);
 
 int traceeval_remove(struct traceeval *teval,
-		     const union traceeval_data *keys);
+		     const struct traceeval_data *keys);
 
-int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
-		    const union traceeval_data **results);
+int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
+		    const struct traceeval_data **results);
 
 void traceeval_results_release(struct traceeval *teval,
-			       const union traceeval_data *results);
+			       const struct traceeval_data *results);
 
 struct traceeval_stat *traceeval_stat(struct traceeval *teval,
-				      const union traceeval_data *keys,
+				      const struct traceeval_data *keys,
 				      struct traceeval_type *type);
 
 unsigned long long traceeval_stat_max(struct traceeval_stat *stat);
@@ -185,11 +215,11 @@ int traceeval_iterator_sort(struct traceeval_iterator *iter, const char *sort_fi
 int traceeval_iterator_sort_custom(struct traceeval_iterator *iter,
 				   traceeval_cmp_fn sort_fn, void *data);
 int traceeval_iterator_next(struct traceeval_iterator *iter,
-			    const union traceeval_data **keys);
+			    const struct traceeval_data **keys);
 int traceeval_iterator_query(struct traceeval_iterator *iter,
-			     const union traceeval_data **results);
+			     const struct traceeval_data **results);
 void traceeval_iterator_results_release(struct traceeval_iterator *iter,
-					const union traceeval_data *results);
+					const struct traceeval_data *results);
 struct traceeval_stat *traceeval_iterator_stat(struct traceeval_iterator *iter,
 					       struct traceeval_type *type);
 int traceeval_iterator_remove(struct traceeval_iterator *iter);
samples/task-eval.c +56 -83

260 lines not on this page · raw message

src/eval-local.h +2 -2
index 73f52efdf0da..26b3c9b29929 100644
@@ -56,8 +56,8 @@ struct traceeval_stat {
 /* A key-value pair */
 struct entry {
 	struct hash_item	hash;
-	union traceeval_data	*keys;
-	union traceeval_data	*vals;
+	struct traceeval_data	*keys;
+	struct traceeval_data	*vals;
 	struct traceeval_stat	*val_stats;
 };
 
src/histograms.c +35 -35

211 lines not on this page · raw message

Signature · 1 line
2.40.1
#9 lore raw

Re: [PATCH v2 06/11] libtraceeval histogram: Add type to traceeval_data and make it a structure

Quoted 405 lines · Steven Rostedt

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

Having a straight union for passing in the data that must match the types is dangerous and prone for buggy code. If some data doesn't match its type, there's nothing to catch it.

Instead of having a union traceeval_data of each type, have it be a structure with a "type" field follow by a union, where the type defines what kind of data it is.

Add helper macros to make it easy to define the data when using it.

---
 include/traceeval-hist.h |  90 ++++++++++++++++---------
 samples/task-eval.c      | 139 ++++++++++++++++-----------------------
 src/eval-local.h         |   4 +-
 src/histograms.c         |  70 ++++++++++----------
 4 files changed, 153 insertions(+), 150 deletions(-)
include/traceeval-hist.h +60 -30
index e619d52ea0d0..78cfac5982ee 100644
@@ -52,45 +52,75 @@ struct traceeval_dynamic {
  * Trace data entry for a traceeval histogram
  * Constitutes keys and values.
  */
-union traceeval_data {
-	struct traceeval_dynamic	dyn_data;
-	char				*string;
-	const char			*cstring;
-	void				*pointer;
-	unsigned long			number;
-	unsigned long long		number_64;
-	unsigned int			number_32;
-	unsigned short			number_16;
-	unsigned char			number_8;
+struct traceeval_data {
+	enum traceeval_data_type		type;
+	union {
+		struct traceeval_dynamic	dyn_data;
+		char				*string;
+		const char			*cstring;
+		void				*pointer;
+		unsigned long			number;
+		unsigned long long		number_64;
+		unsigned int			number_32;
+		unsigned short			number_16;
+		unsigned char			number_8;
+	};
 };
 
+#define __TRACEEVAL_DATA(data_type, member, data)			\
+	{  .type = TRACEEVAL_TYPE_##data_type, .member = (data) }
+
+#define DEFINE_TRACEEVAL_NUMBER(data)	   __TRACEEVAL_DATA(NUMBER, number, data)
+#define DEFINE_TRACEEVAL_NUMBER_8(data)	   __TRACEEVAL_DATA(NUMBER_8, number_8, data)
+#define DEFINE_TRACEEVAL_NUMBER_16(data)   __TRACEEVAL_DATA(NUMBER_16, number_16, data)
+#define DEFINE_TRACEEVAL_NUMBER_32(data)   __TRACEEVAL_DATA(NUMBER_32, number_32, data)
+#define DEFINE_TRACEEVAL_NUMBER_64(data)   __TRACEEVAL_DATA(NUMBER_64, number_64, data)
+#define DEFINE_TRACEEVAL_STRING(data)	   __TRACEEVAL_DATA(STRING, string, data)
+#define DEFINE_TRACEEVAL_CSTRING(data)	   __TRACEEVAL_DATA(STRING, cstring, data)
+#define DEFINE_TRACEEVAL_POINTER(data)	   __TRACEEVAL_DATA(POINTER, pointer, data)
+
+#define __TRACEEVAL_SET(data, data_type, member, val)		\
+	do {							\
+		(data).type = TRACEEVAL_TYPE_##data_type;	\
+		(data).member = (val);				\
+	} while (0)
+
+#define TRACEEVAL_SET_NUMBER(data, val)	     __TRACEEVAL_SET(data, NUMBER, number, val)
+#define TRACEEVAL_SET_NUMBER_8(data, val)    __TRACEEVAL_SET(data, NUMBER_8, number_8, val)
+#define TRACEEVAL_SET_NUMBER_16(data, val)   __TRACEEVAL_SET(data, NUMBER_16, number_16, val)
+#define TRACEEVAL_SET_NUMBER_32(data, val)   __TRACEEVAL_SET(data, NUMBER_32, number_32, val)
+#define TRACEEVAL_SET_NUMBER_64(data, val)   __TRACEEVAL_SET(data, NUMBER_64, number_64, val)
+#define TRACEEVAL_SET_STRING(data, val)	     __TRACEEVAL_SET(data, STRING, string, val)
+#define TRACEEVAL_SET_CSTRING(data, val)     __TRACEEVAL_SET(data, STRING, cstring, val)
+#define TRACEEVAL_SET_POINTER(data, val)     __TRACEEVAL_SET(data, POINTER, pointer, val)
+
 struct traceeval_type;
 struct traceeval;
 
 /* release function callback on traceeval_data */
 typedef void (*traceeval_data_release_fn)(const struct traceeval_type *type,
-					  union traceeval_data *data);
+					  struct traceeval_data *data);
 
 /* compare function callback to compare traceeval_data */
 typedef int (*traceeval_data_cmp_fn)(struct traceeval *teval,
 				     const struct traceeval_type *type,
-				     const union traceeval_data *A,
-				     const union traceeval_data *B);
+				     const struct traceeval_data *A,
+				     const struct traceeval_data *B);
 
 /* make a unique value */
 typedef int (*traceeval_data_hash_fn)(struct traceeval *teval,
 				      const struct traceeval_type *type,
-				      const union traceeval_data *data);
+				      const struct traceeval_data *data);
 
 typedef int (*traceeval_data_copy_fn)(const struct traceeval_type *type,
-				      union traceeval_data *dst,
-				      const union traceeval_data *src);
+				      struct traceeval_data *dst,
+				      const struct traceeval_data *src);
 
 typedef int (*traceeval_cmp_fn)(struct traceeval *teval,
-				const union traceeval_data *Akeys,
-				const union traceeval_data *Avals,
-				const union traceeval_data *Bkeys,
-				const union traceeval_data *Bvals,
+				const struct traceeval_data *Akeys,
+				const struct traceeval_data *Avals,
+				const struct traceeval_data *Bkeys,
+				const struct traceeval_data *Bvals,
 				void *data);
 
 /*
@@ -157,20 +187,20 @@ struct traceeval *traceeval_init(struct traceeval_type *keys,
 void traceeval_release(struct traceeval *teval);
 
 int traceeval_insert(struct traceeval *teval,
-		     const union traceeval_data *keys,
-		     const union traceeval_data *vals);
+		     const struct traceeval_data *keys,
+		     const struct traceeval_data *vals);
 
 int traceeval_remove(struct traceeval *teval,
-		     const union traceeval_data *keys);
+		     const struct traceeval_data *keys);
 
-int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
-		    const union traceeval_data **results);
+int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
+		    const struct traceeval_data **results);
 
 void traceeval_results_release(struct traceeval *teval,
-			       const union traceeval_data *results);
+			       const struct traceeval_data *results);
 
 struct traceeval_stat *traceeval_stat(struct traceeval *teval,
-				      const union traceeval_data *keys,
+				      const struct traceeval_data *keys,
 				      struct traceeval_type *type);
 
 unsigned long long traceeval_stat_max(struct traceeval_stat *stat);
@@ -185,11 +215,11 @@ int traceeval_iterator_sort(struct traceeval_iterator *iter, const char *sort_fi
 int traceeval_iterator_sort_custom(struct traceeval_iterator *iter,
 				   traceeval_cmp_fn sort_fn, void *data);
 int traceeval_iterator_next(struct traceeval_iterator *iter,
-			    const union traceeval_data **keys);
+			    const struct traceeval_data **keys);
 int traceeval_iterator_query(struct traceeval_iterator *iter,
-			     const union traceeval_data **results);
+			     const struct traceeval_data **results);
 void traceeval_iterator_results_release(struct traceeval_iterator *iter,
-					const union traceeval_data *results);
+					const struct traceeval_data *results);
 struct traceeval_stat *traceeval_iterator_stat(struct traceeval_iterator *iter,
 					       struct traceeval_type *type);
 int traceeval_iterator_remove(struct traceeval_iterator *iter);
samples/task-eval.c +49 -77
index bde167d1219b..e62bfddafdd3 100644
@@ -190,21 +190,15 @@ static void update_process(struct task_data *tdata, const char *comm,
 			   enum sched_state state, enum command cmd,
 			   unsigned long long ts)
 {
-	union traceeval_data keys[] = {
-		{
-			.cstring	= comm,
-		},
-		{
-			.number		= state,
-		},
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_CSTRING(	comm	),
+		DEFINE_TRACEEVAL_NUMBER(	state	),
 	};
-	union traceeval_data vals[] = {
-		{
-			.number_64	= ts,
-		},
+	struct traceeval_data vals[] = {
+		DEFINE_TRACEEVAL_NUMBER_64(	ts	),
 	};
-	union traceeval_data new_vals[1] = { };
-	const union traceeval_data *results;
+	struct traceeval_data new_vals[1] = { };
+	const struct traceeval_data *results;
 	int ret;
 
 	switch (cmd) {
@@ -222,14 +216,15 @@ static void update_process(struct task_data *tdata, const char *comm,
 		if (!results[0].number_64)
 			break;
 
-		new_vals[0].number_64 = ts - results[0].number_64;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], ts - results[0].number_64);
 
 		ret = traceeval_insert(tdata->teval_processes.stop, keys, new_vals);
 		if (ret < 0)
 			pdie("Could not stop process");
 
 		/* Reset the start */
-		new_vals[0].number_64 = 0;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], 0);
+
 		ret = traceeval_insert(tdata->teval_processes.start, keys, new_vals);
 		if (ret < 0)
 			pdie("Could not start CPU");
@@ -253,15 +248,11 @@ static void stop_process(struct task_data *tdata, const char *comm,
 static struct process_data *
 get_process_data(struct task_data *tdata, const char *comm)
 {
-	union traceeval_data keys[] = {
-		{
-			.cstring = comm,
-		},
-		{
-			.number = RUNNING,
-		}
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_CSTRING(	comm	),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING	),
 	};
-	const union traceeval_data *results;
+	const struct traceeval_data *results;
 	void *data;
 	int ret;
 
@@ -278,16 +269,12 @@ get_process_data(struct task_data *tdata, const char *comm)
 
 void set_process_data(struct task_data *tdata, const char *comm, void *data)
 {
-	union traceeval_data keys[] = {
-		{
-			.cstring = comm,
-		},
-		{
-			.number = RUNNING,
-		}
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_CSTRING(	comm	),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING	),
 	};
-	union traceeval_data new_vals[1] = { };
-	const union traceeval_data *results;
+	struct traceeval_data new_vals[1] = { };
+	const struct traceeval_data *results;
 	int ret;
 
 	ret = traceeval_query(tdata->teval_processes_data, keys, &results);
@@ -296,7 +283,7 @@ void set_process_data(struct task_data *tdata, const char *comm, void *data)
 	if (ret < 0)
 		pdie("Could not query process data");
 
-	new_vals[0].pointer = data;
+	TRACEEVAL_SET_POINTER(new_vals[0], data);
 	ret = traceeval_insert(tdata->teval_processes_data, keys, new_vals);
 	if (ret < 0)
 		pdie("Failed to set process data");
@@ -309,21 +296,15 @@ static void update_cpu(struct teval_pair *teval_pair, int cpu,
 		       enum sched_state state, enum command cmd,
 		       unsigned long long ts)
 {
-	const union traceeval_data *results;
-	union traceeval_data keys[] = {
-		{
-			.number = cpu,
-		},
-		{
-			.number = state,
-		}
+	const struct traceeval_data *results;
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_NUMBER(	cpu	),
+		DEFINE_TRACEEVAL_NUMBER(	state	),
 	};
-	union traceeval_data vals[] = {
-		{
-			.number_64	= ts,
-		},
+	struct traceeval_data vals[] = {
+		DEFINE_TRACEEVAL_NUMBER_64(	ts	),
 	};
-	union traceeval_data new_vals[1] = { };
+	struct traceeval_data new_vals[1] = { };
 	int ret;
 
 	switch (cmd) {
@@ -350,14 +331,14 @@ static void update_cpu(struct teval_pair *teval_pair, int cpu,
 		if (!results[0].number_64)
 			break;
 
-		new_vals[0].number_64 = ts - results[0].number_64;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], ts - results[0].number_64);
 
 		ret = traceeval_insert(teval_pair->stop, keys, new_vals);
 		if (ret < 0)
 			pdie("Could not stop CPU");
 
 		/* Reset the start */
-		new_vals[0].number_64 = 0;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], 0);
 		ret = traceeval_insert(teval_pair->start, keys, new_vals);
 		if (ret < 0)
 			pdie("Could not start CPU");
@@ -385,21 +366,15 @@ static void update_thread(struct process_data *pdata, int tid,
 			  enum sched_state state, enum command cmd,
 			  unsigned long long ts)
 {
-	const union traceeval_data *results;
-	union traceeval_data keys[] = {
-		{
-			.number = tid,
-		},
-		{
-			.number = state,
-		}
+	const struct traceeval_data *results;
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_NUMBER(	tid	),
+		DEFINE_TRACEEVAL_NUMBER(	state	),
 	};
-	union traceeval_data vals[] = {
-		{
-			.number_64	= ts,
-		},
+	struct traceeval_data vals[] = {
+		DEFINE_TRACEEVAL_NUMBER_64(	ts	),
 	};
-	union traceeval_data new_vals[1] = { };
+	struct traceeval_data new_vals[1] = { };
 	int ret;
 
 	switch (cmd) {
@@ -415,7 +390,7 @@ static void update_thread(struct process_data *pdata, int tid,
 		if (ret == 0)
 			return;
 
-		new_vals[0].number_64 = ts - results[0].number_64;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], ts - results[0].number_64);
 
 		ret = traceeval_insert(pdata->teval_threads.stop, keys, new_vals);
 		traceeval_results_release(pdata->teval_threads.start, results);
@@ -646,17 +621,19 @@ static void print_microseconds(int idx, unsigned long long nsecs)
  *  If RUNNING is equal, then sort by COMM.
  */
 static int compare_pdata(struct traceeval *teval_data,
-				const union traceeval_data *Akeys,
-				const union traceeval_data *Avals,
-				const union traceeval_data *Bkeys,
-				const union traceeval_data *Bvals,
+				const struct traceeval_data *Akeys,
+				const struct traceeval_data *Avals,
+				const struct traceeval_data *Bkeys,
+				const struct traceeval_data *Bvals,
 				void *data)
 {
 	struct traceeval *teval = data; /* The deltas are here */
-	union traceeval_data keysA[] = {
-		{ .cstring = Akeys[0].cstring }, { .number = RUNNING } };
-	union traceeval_data keysB[] = {
-		{ .cstring = Bkeys[0].cstring }, { .number = RUNNING } };
+	struct traceeval_data keysA[] = {
+		DEFINE_TRACEEVAL_CSTRING(	Akeys[0].cstring	),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING			), };
+	struct traceeval_data keysB[] = {
+		DEFINE_TRACEEVAL_CSTRING(	Bkeys[0].cstring	),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING			), };
 	struct traceeval_stat *statA;
 	struct traceeval_stat *statB;
 	unsigned long long totalA = -1;
@@ -690,7 +667,7 @@ static int compare_pdata(struct traceeval *teval_data,
 static void display_cpus(struct traceeval *teval)
 {
 	struct traceeval_iterator *iter = traceeval_iterator_get(teval);
-	const union traceeval_data *keys;
+	const struct traceeval_data *keys;
 	struct traceeval_stat *stat;
 	int last_cpu = -1;
 
@@ -762,7 +739,7 @@ static void display_state_times(int state, unsigned long long total)
 static void display_threads(struct traceeval *teval)
 {
 	struct traceeval_iterator *iter = traceeval_iterator_get(teval);
-	const union traceeval_data *keys;
+	const struct traceeval_data *keys;
 	struct traceeval_stat *stat;
 	int last_tid = -1;
 
@@ -802,17 +779,13 @@ static void display_process_stats(struct traceeval *teval,
 {
 	struct traceeval_stat *stat;
 	unsigned long long delta;
-	union traceeval_data keys[] = {
-		{
-			.cstring = comm,
-		},
-		{

samples/task-eval.c static void display_process_stats(struct traceeval *teval, · line 788

Quoted 10 lines
samples/task-eval.c +4 -3
-			.number = RUNNING,
-		}
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_CSTRING(	comm		),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING		),
 	};
 
 	for (int i = 0; i < OTHER; i++) {
-		keys[1].number = i;
+		TRACEEVAL_SET_NUMBER_64(keys[1], i);

I think this should be
+ TRACEEVAL_SET_NUMBER(keys[1], i);

to match the
+ DEFINE_TRACEEVAL_NUMBER( RUNNING ),

a little up in this function.

#10 lore raw

Re: [PATCH v2 06/11] libtraceeval histogram: Add type to traceeval_data and make it a structure

Quoted 423 lines · Ross Zwisler
Quoted 405 lines · Steven Rostedt

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

Having a straight union for passing in the data that must match the types is dangerous and prone for buggy code. If some data doesn't match its type, there's nothing to catch it.

Instead of having a union traceeval_data of each type, have it be a structure with a "type" field follow by a union, where the type defines what kind of data it is.

Add helper macros to make it easy to define the data when using it.

---
 include/traceeval-hist.h |  90 ++++++++++++++++---------
 samples/task-eval.c      | 139 ++++++++++++++++-----------------------
 src/eval-local.h         |   4 +-
 src/histograms.c         |  70 ++++++++++----------
 4 files changed, 153 insertions(+), 150 deletions(-)
include/traceeval-hist.h +60 -30
index e619d52ea0d0..78cfac5982ee 100644
@@ -52,45 +52,75 @@ struct traceeval_dynamic {
  * Trace data entry for a traceeval histogram
  * Constitutes keys and values.
  */
-union traceeval_data {
-	struct traceeval_dynamic	dyn_data;
-	char				*string;
-	const char			*cstring;
-	void				*pointer;
-	unsigned long			number;
-	unsigned long long		number_64;
-	unsigned int			number_32;
-	unsigned short			number_16;
-	unsigned char			number_8;
+struct traceeval_data {
+	enum traceeval_data_type		type;
+	union {
+		struct traceeval_dynamic	dyn_data;
+		char				*string;
+		const char			*cstring;
+		void				*pointer;
+		unsigned long			number;
+		unsigned long long		number_64;
+		unsigned int			number_32;
+		unsigned short			number_16;
+		unsigned char			number_8;
+	};
 };
 
+#define __TRACEEVAL_DATA(data_type, member, data)			\
+	{  .type = TRACEEVAL_TYPE_##data_type, .member = (data) }
+
+#define DEFINE_TRACEEVAL_NUMBER(data)	   __TRACEEVAL_DATA(NUMBER, number, data)
+#define DEFINE_TRACEEVAL_NUMBER_8(data)	   __TRACEEVAL_DATA(NUMBER_8, number_8, data)
+#define DEFINE_TRACEEVAL_NUMBER_16(data)   __TRACEEVAL_DATA(NUMBER_16, number_16, data)
+#define DEFINE_TRACEEVAL_NUMBER_32(data)   __TRACEEVAL_DATA(NUMBER_32, number_32, data)
+#define DEFINE_TRACEEVAL_NUMBER_64(data)   __TRACEEVAL_DATA(NUMBER_64, number_64, data)
+#define DEFINE_TRACEEVAL_STRING(data)	   __TRACEEVAL_DATA(STRING, string, data)
+#define DEFINE_TRACEEVAL_CSTRING(data)	   __TRACEEVAL_DATA(STRING, cstring, data)
+#define DEFINE_TRACEEVAL_POINTER(data)	   __TRACEEVAL_DATA(POINTER, pointer, data)
+
+#define __TRACEEVAL_SET(data, data_type, member, val)		\
+	do {							\
+		(data).type = TRACEEVAL_TYPE_##data_type;	\
+		(data).member = (val);				\
+	} while (0)
+
+#define TRACEEVAL_SET_NUMBER(data, val)	     __TRACEEVAL_SET(data, NUMBER, number, val)
+#define TRACEEVAL_SET_NUMBER_8(data, val)    __TRACEEVAL_SET(data, NUMBER_8, number_8, val)
+#define TRACEEVAL_SET_NUMBER_16(data, val)   __TRACEEVAL_SET(data, NUMBER_16, number_16, val)
+#define TRACEEVAL_SET_NUMBER_32(data, val)   __TRACEEVAL_SET(data, NUMBER_32, number_32, val)
+#define TRACEEVAL_SET_NUMBER_64(data, val)   __TRACEEVAL_SET(data, NUMBER_64, number_64, val)
+#define TRACEEVAL_SET_STRING(data, val)	     __TRACEEVAL_SET(data, STRING, string, val)
+#define TRACEEVAL_SET_CSTRING(data, val)     __TRACEEVAL_SET(data, STRING, cstring, val)
+#define TRACEEVAL_SET_POINTER(data, val)     __TRACEEVAL_SET(data, POINTER, pointer, val)
+
 struct traceeval_type;
 struct traceeval;
 
 /* release function callback on traceeval_data */
 typedef void (*traceeval_data_release_fn)(const struct traceeval_type *type,
-					  union traceeval_data *data);
+					  struct traceeval_data *data);
 
 /* compare function callback to compare traceeval_data */
 typedef int (*traceeval_data_cmp_fn)(struct traceeval *teval,
 				     const struct traceeval_type *type,
-				     const union traceeval_data *A,
-				     const union traceeval_data *B);
+				     const struct traceeval_data *A,
+				     const struct traceeval_data *B);
 
 /* make a unique value */
 typedef int (*traceeval_data_hash_fn)(struct traceeval *teval,
 				      const struct traceeval_type *type,
-				      const union traceeval_data *data);
+				      const struct traceeval_data *data);
 
 typedef int (*traceeval_data_copy_fn)(const struct traceeval_type *type,
-				      union traceeval_data *dst,
-				      const union traceeval_data *src);
+				      struct traceeval_data *dst,
+				      const struct traceeval_data *src);
 
 typedef int (*traceeval_cmp_fn)(struct traceeval *teval,
-				const union traceeval_data *Akeys,
-				const union traceeval_data *Avals,
-				const union traceeval_data *Bkeys,
-				const union traceeval_data *Bvals,
+				const struct traceeval_data *Akeys,
+				const struct traceeval_data *Avals,
+				const struct traceeval_data *Bkeys,
+				const struct traceeval_data *Bvals,
 				void *data);
 
 /*
@@ -157,20 +187,20 @@ struct traceeval *traceeval_init(struct traceeval_type *keys,
 void traceeval_release(struct traceeval *teval);
 
 int traceeval_insert(struct traceeval *teval,
-		     const union traceeval_data *keys,
-		     const union traceeval_data *vals);
+		     const struct traceeval_data *keys,
+		     const struct traceeval_data *vals);
 
 int traceeval_remove(struct traceeval *teval,
-		     const union traceeval_data *keys);
+		     const struct traceeval_data *keys);
 
-int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
-		    const union traceeval_data **results);
+int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
+		    const struct traceeval_data **results);
 
 void traceeval_results_release(struct traceeval *teval,
-			       const union traceeval_data *results);
+			       const struct traceeval_data *results);
 
 struct traceeval_stat *traceeval_stat(struct traceeval *teval,
-				      const union traceeval_data *keys,
+				      const struct traceeval_data *keys,
 				      struct traceeval_type *type);
 
 unsigned long long traceeval_stat_max(struct traceeval_stat *stat);
@@ -185,11 +215,11 @@ int traceeval_iterator_sort(struct traceeval_iterator *iter, const char *sort_fi
 int traceeval_iterator_sort_custom(struct traceeval_iterator *iter,
 				   traceeval_cmp_fn sort_fn, void *data);
 int traceeval_iterator_next(struct traceeval_iterator *iter,
-			    const union traceeval_data **keys);
+			    const struct traceeval_data **keys);
 int traceeval_iterator_query(struct traceeval_iterator *iter,
-			     const union traceeval_data **results);
+			     const struct traceeval_data **results);
 void traceeval_iterator_results_release(struct traceeval_iterator *iter,
-					const union traceeval_data *results);
+					const struct traceeval_data *results);
 struct traceeval_stat *traceeval_iterator_stat(struct traceeval_iterator *iter,
 					       struct traceeval_type *type);
 int traceeval_iterator_remove(struct traceeval_iterator *iter);
samples/task-eval.c +49 -77
index bde167d1219b..e62bfddafdd3 100644
@@ -190,21 +190,15 @@ static void update_process(struct task_data *tdata, const char *comm,
 			   enum sched_state state, enum command cmd,
 			   unsigned long long ts)
 {
-	union traceeval_data keys[] = {
-		{
-			.cstring	= comm,
-		},
-		{
-			.number		= state,
-		},
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_CSTRING(	comm	),
+		DEFINE_TRACEEVAL_NUMBER(	state	),
 	};
-	union traceeval_data vals[] = {
-		{
-			.number_64	= ts,
-		},
+	struct traceeval_data vals[] = {
+		DEFINE_TRACEEVAL_NUMBER_64(	ts	),
 	};
-	union traceeval_data new_vals[1] = { };
-	const union traceeval_data *results;
+	struct traceeval_data new_vals[1] = { };
+	const struct traceeval_data *results;
 	int ret;
 
 	switch (cmd) {
@@ -222,14 +216,15 @@ static void update_process(struct task_data *tdata, const char *comm,
 		if (!results[0].number_64)
 			break;
 
-		new_vals[0].number_64 = ts - results[0].number_64;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], ts - results[0].number_64);
 
 		ret = traceeval_insert(tdata->teval_processes.stop, keys, new_vals);
 		if (ret < 0)
 			pdie("Could not stop process");
 
 		/* Reset the start */
-		new_vals[0].number_64 = 0;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], 0);
+
 		ret = traceeval_insert(tdata->teval_processes.start, keys, new_vals);
 		if (ret < 0)
 			pdie("Could not start CPU");
@@ -253,15 +248,11 @@ static void stop_process(struct task_data *tdata, const char *comm,
 static struct process_data *
 get_process_data(struct task_data *tdata, const char *comm)
 {
-	union traceeval_data keys[] = {
-		{
-			.cstring = comm,
-		},
-		{
-			.number = RUNNING,
-		}
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_CSTRING(	comm	),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING	),
 	};
-	const union traceeval_data *results;
+	const struct traceeval_data *results;
 	void *data;
 	int ret;
 
@@ -278,16 +269,12 @@ get_process_data(struct task_data *tdata, const char *comm)
 
 void set_process_data(struct task_data *tdata, const char *comm, void *data)
 {
-	union traceeval_data keys[] = {
-		{
-			.cstring = comm,
-		},
-		{
-			.number = RUNNING,
-		}
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_CSTRING(	comm	),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING	),
 	};
-	union traceeval_data new_vals[1] = { };
-	const union traceeval_data *results;
+	struct traceeval_data new_vals[1] = { };
+	const struct traceeval_data *results;
 	int ret;
 
 	ret = traceeval_query(tdata->teval_processes_data, keys, &results);
@@ -296,7 +283,7 @@ void set_process_data(struct task_data *tdata, const char *comm, void *data)
 	if (ret < 0)
 		pdie("Could not query process data");
 
-	new_vals[0].pointer = data;
+	TRACEEVAL_SET_POINTER(new_vals[0], data);
 	ret = traceeval_insert(tdata->teval_processes_data, keys, new_vals);
 	if (ret < 0)
 		pdie("Failed to set process data");
@@ -309,21 +296,15 @@ static void update_cpu(struct teval_pair *teval_pair, int cpu,
 		       enum sched_state state, enum command cmd,
 		       unsigned long long ts)
 {
-	const union traceeval_data *results;
-	union traceeval_data keys[] = {
-		{
-			.number = cpu,
-		},
-		{
-			.number = state,
-		}
+	const struct traceeval_data *results;
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_NUMBER(	cpu	),
+		DEFINE_TRACEEVAL_NUMBER(	state	),
 	};
-	union traceeval_data vals[] = {
-		{
-			.number_64	= ts,
-		},
+	struct traceeval_data vals[] = {
+		DEFINE_TRACEEVAL_NUMBER_64(	ts	),
 	};
-	union traceeval_data new_vals[1] = { };
+	struct traceeval_data new_vals[1] = { };
 	int ret;
 
 	switch (cmd) {
@@ -350,14 +331,14 @@ static void update_cpu(struct teval_pair *teval_pair, int cpu,
 		if (!results[0].number_64)
 			break;
 
-		new_vals[0].number_64 = ts - results[0].number_64;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], ts - results[0].number_64);
 
 		ret = traceeval_insert(teval_pair->stop, keys, new_vals);
 		if (ret < 0)
 			pdie("Could not stop CPU");
 
 		/* Reset the start */
-		new_vals[0].number_64 = 0;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], 0);
 		ret = traceeval_insert(teval_pair->start, keys, new_vals);
 		if (ret < 0)
 			pdie("Could not start CPU");
@@ -385,21 +366,15 @@ static void update_thread(struct process_data *pdata, int tid,
 			  enum sched_state state, enum command cmd,
 			  unsigned long long ts)
 {
-	const union traceeval_data *results;
-	union traceeval_data keys[] = {
-		{
-			.number = tid,
-		},
-		{
-			.number = state,
-		}
+	const struct traceeval_data *results;
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_NUMBER(	tid	),
+		DEFINE_TRACEEVAL_NUMBER(	state	),
 	};
-	union traceeval_data vals[] = {
-		{
-			.number_64	= ts,
-		},
+	struct traceeval_data vals[] = {
+		DEFINE_TRACEEVAL_NUMBER_64(	ts	),
 	};
-	union traceeval_data new_vals[1] = { };
+	struct traceeval_data new_vals[1] = { };
 	int ret;
 
 	switch (cmd) {
@@ -415,7 +390,7 @@ static void update_thread(struct process_data *pdata, int tid,
 		if (ret == 0)
 			return;
 
-		new_vals[0].number_64 = ts - results[0].number_64;
+		TRACEEVAL_SET_NUMBER_64(new_vals[0], ts - results[0].number_64);
 
 		ret = traceeval_insert(pdata->teval_threads.stop, keys, new_vals);
 		traceeval_results_release(pdata->teval_threads.start, results);
@@ -646,17 +621,19 @@ static void print_microseconds(int idx, unsigned long long nsecs)
  *  If RUNNING is equal, then sort by COMM.
  */
 static int compare_pdata(struct traceeval *teval_data,
-				const union traceeval_data *Akeys,
-				const union traceeval_data *Avals,
-				const union traceeval_data *Bkeys,
-				const union traceeval_data *Bvals,
+				const struct traceeval_data *Akeys,
+				const struct traceeval_data *Avals,
+				const struct traceeval_data *Bkeys,
+				const struct traceeval_data *Bvals,
 				void *data)
 {
 	struct traceeval *teval = data; /* The deltas are here */
-	union traceeval_data keysA[] = {
-		{ .cstring = Akeys[0].cstring }, { .number = RUNNING } };
-	union traceeval_data keysB[] = {
-		{ .cstring = Bkeys[0].cstring }, { .number = RUNNING } };
+	struct traceeval_data keysA[] = {
+		DEFINE_TRACEEVAL_CSTRING(	Akeys[0].cstring	),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING			), };
+	struct traceeval_data keysB[] = {
+		DEFINE_TRACEEVAL_CSTRING(	Bkeys[0].cstring	),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING			), };
 	struct traceeval_stat *statA;
 	struct traceeval_stat *statB;
 	unsigned long long totalA = -1;
@@ -690,7 +667,7 @@ static int compare_pdata(struct traceeval *teval_data,
 static void display_cpus(struct traceeval *teval)
 {
 	struct traceeval_iterator *iter = traceeval_iterator_get(teval);
-	const union traceeval_data *keys;
+	const struct traceeval_data *keys;
 	struct traceeval_stat *stat;
 	int last_cpu = -1;
 
@@ -762,7 +739,7 @@ static void display_state_times(int state, unsigned long long total)
 static void display_threads(struct traceeval *teval)
 {
 	struct traceeval_iterator *iter = traceeval_iterator_get(teval);
-	const union traceeval_data *keys;
+	const struct traceeval_data *keys;
 	struct traceeval_stat *stat;
 	int last_tid = -1;
 
@@ -802,17 +779,13 @@ static void display_process_stats(struct traceeval *teval,
 {
 	struct traceeval_stat *stat;
 	unsigned long long delta;
-	union traceeval_data keys[] = {
-		{
-			.cstring = comm,
-		},
-		{

samples/task-eval.c static void display_process_stats(struct traceeval *teval, · line 788

Quoted 10 lines
samples/task-eval.c +4 -3
-			.number = RUNNING,
-		}
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_CSTRING(	comm		),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING		),
 	};
 
 	for (int i = 0; i < OTHER; i++) {
-		keys[1].number = i;
+		TRACEEVAL_SET_NUMBER_64(keys[1], i);

I think this should be

(quoted diff) +1 -0
+		TRACEEVAL_SET_NUMBER(keys[1], i);

to match the

(quoted diff) +1 -0
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING		),
Quoted 1 line

a little up in this function.

Ah, it looks like you fix this in the next patch.

#11 lore raw

Re: [PATCH v2 06/11] libtraceeval histogram: Add type to traceeval_data and make it a structure

Quoted 6 lines · On Mon, 2 Oct 2023 13:54:48 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 4 lines

to match the

(quoted diff) +1 -0
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING		),

a little up in this function.

Ah, it looks like you fix this in the next patch.

Now I remember. Yeah, the next patch catches this bug! :-)

-- Steve

#12 lore raw

Re: [PATCH v2 06/11] libtraceeval histogram: Add type to traceeval_data and make it a structure

In reply to Ross Zwisler
Quoted 16 lines · On Mon, 2 Oct 2023 13:53:04 -0600 Ross Zwisler <[email protected]> wrote:

samples/task-eval.c static void display_process_stats(struct traceeval *teval, · line 788

Quoted 9 lines
samples/task-eval.c +4 -2
-		}
+	struct traceeval_data keys[] = {
+		DEFINE_TRACEEVAL_CSTRING(	comm		),
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING		),
 	};
 
 	for (int i = 0; i < OTHER; i++) {
-		keys[1].number = i;
+		TRACEEVAL_SET_NUMBER_64(keys[1], i);  

I think this should be

(quoted diff) +1 -0
+		TRACEEVAL_SET_NUMBER(keys[1], i);

to match the

(quoted diff) +1 -0
+		DEFINE_TRACEEVAL_NUMBER(	RUNNING		),
Quoted 1 line

a little up in this function.

You're correct, and I already fixed it. Just haven't posted the fix yet.

-- Steve

#13 lore raw

PATCH v2 7/11 libtraceveal: Add type checks to traceeval_data vals and keys

In reply to Steven Rostedt

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

Now that the traceeval_data has a type, add checks to traceeval_insert() and traceveal_query() as well as traceeval_stat() to make sure the keys and vals match the types that were for the traceeval.

Which also caught a bug in task_eval.c display_process_stats().

---
 samples/task-eval.c |  2 +-
 src/histograms.c    | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
samples/task-eval.c +1 -1
index e62bfddafdd3..475df190acd2 100644
@@ -785,7 +785,7 @@ static void display_process_stats(struct traceeval *teval,
 	};
 
 	for (int i = 0; i < OTHER; i++) {
-		TRACEEVAL_SET_NUMBER_64(keys[1], i);
+		TRACEEVAL_SET_NUMBER(keys[1], i);
 
 		delta = 0;
 		stat = traceeval_stat(teval, keys, &delta_vals[0]);
src/histograms.c +17 -0
index fc29558d75fe..954042da28e4 100644
@@ -479,10 +479,16 @@ static int get_entry(struct traceeval *teval, const struct traceeval_data *keys,
 	struct hash_item *item;
 	unsigned key;
 	int check = 0;
+	int i;
 
 	if (!teval || !keys)
 		return -1;
 
+	for (i = 0; i < teval->nr_key_types; i++) {
+		if (keys[i].type != teval->key_types[i].type)
+			return -1;
+	}
+
 	key = make_hash(teval, keys, hist->bits);
 
 	for (iter = hash_iter_bucket(hist, key); (item = hash_iter_bucket_next(iter)); ) {
@@ -792,6 +798,11 @@ static int update_entry(struct traceeval *teval, struct entry *entry,
 	if (!size)
 		return 0;
 
+	for (i = 0; i < teval->nr_val_types; i++) {
+		if (vals[i].type != teval->val_types[i].type)
+			return -1;
+	}
+
 	for (i = 0; i < size; i++) {
 		old[i] = copy[i];
 
@@ -928,10 +939,16 @@ int traceeval_insert(struct traceeval *teval,
 {
 	struct entry *entry;
 	int check;
+	int i;
 
 	entry = NULL;
 	check = get_entry(teval, keys, &entry);
 
+	for (i = 0; i < teval->nr_val_types; i++) {
+		if (vals[i].type != teval->val_types[i].type)
+			return -1;
+	}
+
 	if (check == -1)
 		return check;
 
Signature · 1 line
2.40.1
Reviewed-by on v2 7/11
#14 lore raw

Re: [PATCH v2 07/11] libtraceveal: Add type checks to traceeval_data vals and keys

Quoted 9 lines · Steven Rostedt

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

Now that the traceeval_data has a type, add checks to traceeval_insert() and traceveal_query() as well as traceeval_stat() to make sure the keys and vals match the types that were for the traceeval.

Which also caught a bug in task_eval.c display_process_stats().

#15 lore raw

PATCH v2 8/11 libtraceeval: Add size checks to insert and query functions

In reply to Steven Rostedt

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

Currently, there's nothing that checks the size of the keys/values being passed into traceeval_query() and traceeval_insert(). Make functions that take the size of those arrays:

traceeval_query_size()
traceeval_insert_size()

and convert the traceeval_query() and traceeval_insert() into macros that calculate the size of the fields to pass in:

  #define TRACEEVAL_ARRAY_SIZE(data)  \
     	(sizeof(data) / sizeof((data)[0]))

  #define traceeval_query(teval, keys, results) \
     traceeval_query_size(teval, TRACEEVAL_ARRAY_SIZE(keys), results)

and this will allow the code to check to make sure the size matches what is expected (note, that currently is not done, but can be added).

If the keys or vals is not an array, but instead a pointer, if one of the macros is used, the compiler will complain with:

warning: division sizeof (const struct traceeval_data *) / sizeof (const struct traceeval_data)
does not compute the number of array elements [-Wsizeof-pointer-div]
20 | #define TRACEEVAL_ARRAY_SIZE(data) (sizeof(data) / sizeof((data)[0]))
| ^
include/traceeval-hist.h:206:43: note: in expansion of macro TRACEEVAL_ARRAY_SIZE
206 | traceeval_query_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), results)

      |                                           ^~~~~~~~~~~~~~~~~~~~

Then the user would need to call the "_size()" function directly (as is done in the sample code, where the keys received by an iterator is
used for a query)

Now that there are size functions for traceeval_insert() and traceveal_query(), add checks to make sure that the size being passed in is actually the size expected.

---
 include/traceeval-hist.h | 19 ++++++++++++++-----
 src/histograms.c         | 16 +++++++++++-----
 2 files changed, 25 insertions(+), 10 deletions(-)
include/traceeval-hist.h +14 -5
index 78cfac5982ee..7f48bb92cc96 100644
@@ -17,6 +17,8 @@
 /* Field name/descriptor for number of hits */
 #define TRACEEVAL_VAL_HITS ((const char *)(-1UL))
 
+#define TRACEEVAL_ARRAY_SIZE(data)	(sizeof(data) / sizeof((data)[0]))
+
 /* Data type distinguishers */
 enum traceeval_data_type {
 	TRACEEVAL_TYPE_NONE,
@@ -186,15 +188,22 @@ struct traceeval *traceeval_init(struct traceeval_type *keys,
 
 void traceeval_release(struct traceeval *teval);
 
-int traceeval_insert(struct traceeval *teval,
-		     const struct traceeval_data *keys,
-		     const struct traceeval_data *vals);
+int traceeval_insert_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys,
+			  const struct traceeval_data *vals, size_t nr_vals);
+
+#define traceeval_insert(teval, keys, vals)				\
+	traceeval_insert_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), \
+			      vals, TRACEEVAL_ARRAY_SIZE(vals))
 
 int traceeval_remove(struct traceeval *teval,
 		     const struct traceeval_data *keys);
 
-int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
-		    const struct traceeval_data **results);
+int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *keys,
+			 size_t nr_keys, const struct traceeval_data **results);
+
+#define traceeval_query(teval, keys, results)				\
+	traceeval_query_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), results)
 
 void traceeval_results_release(struct traceeval *teval,
 			       const struct traceeval_data *results);
src/histograms.c +11 -5
index 954042da28e4..ab8a560fe14d 100644
@@ -675,8 +675,8 @@ fail:
  *
  * Returns 1 if found, 0 if not found, and -1 on error.
  */
-int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
-		    const struct traceeval_data **results)
+int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *keys,
+			 size_t nr_keys, const struct traceeval_data **results)
 {
 	struct entry *entry;
 	int check;
@@ -684,6 +684,9 @@ int traceeval_query(struct traceeval *teval, const struct traceeval_data *keys,
 	if (!teval || !keys || !results)
 		return -1;
 
+	if (nr_keys != teval->nr_key_types)
+		return -1;
+
 	/* find key and copy its corresponding value pair */
 	if ((check = get_entry(teval, keys, &entry)) < 1)
 		return check;
@@ -933,14 +936,17 @@ unsigned long long traceeval_stat_count(struct traceeval_stat *stat)
  *
  * Returns 0 on success, and -1 on error.
  */
-int traceeval_insert(struct traceeval *teval,
-		     const struct traceeval_data *keys,
-		     const struct traceeval_data *vals)
+int traceeval_insert_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys,
+			  const struct traceeval_data *vals, size_t nr_vals)
 {
 	struct entry *entry;
 	int check;
 	int i;
 
+	if (nr_keys != teval->nr_key_types || nr_vals != teval->nr_val_types)
+		return -1;
+
 	entry = NULL;
 	check = get_entry(teval, keys, &entry);
 
Signature · 1 line
2.40.1
#16 lore raw

PATCH v2 9/11 libtraceeval: Make traceeval_remove() check size of keys array

In reply to Steven Rostedt

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

Make traceeval_remove() into a macro to pass in the size of the keys array, and rename the function to traceeval_remove_size() that now takes the size of the keys array.

---
 include/traceeval-hist.h | 7 +++++--
 src/histograms.c         | 7 +++++--
 2 files changed, 10 insertions(+), 4 deletions(-)
include/traceeval-hist.h +5 -2
index 7f48bb92cc96..804a0aaa631d 100644
@@ -196,8 +196,11 @@ int traceeval_insert_size(struct traceeval *teval,
 	traceeval_insert_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), \
 			      vals, TRACEEVAL_ARRAY_SIZE(vals))
 
-int traceeval_remove(struct traceeval *teval,
-		     const struct traceeval_data *keys);
+int traceeval_remove_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys);
+
+#define traceeval_remove(teval, keys)					\
+	traceeval_remove_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys))
 
 int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *keys,
 			 size_t nr_keys, const struct traceeval_data **results);
src/histograms.c +5 -2
index ab8a560fe14d..28cf0d4ed225 100644
@@ -977,13 +977,16 @@ int traceeval_insert_size(struct traceeval *teval,
  *         0 if it did not find an time matching @keys
  *        -1 if there was an error.
  */
-int traceeval_remove(struct traceeval *teval,
-		     const struct traceeval_data *keys)
+int traceeval_remove_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys)
 {
 	struct hash_table *hist = teval->hist;
 	struct entry *entry;
 	int check;
 
+	if (teval->nr_key_types != nr_keys)
+		return -1;
+
 	entry = NULL;
 	check = get_entry(teval, keys, &entry);
 
Signature · 1 line
2.40.1
Reviewed-by on v2 9/11
#17 lore raw

Re: [PATCH v2 09/11] libtraceeval: Make traceeval_remove() check size of keys array

src/histograms.c int traceeval_insert_size(struct traceeval *teval,

Quoted 35 lines · Steven Rostedt

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

Make traceeval_remove() into a macro to pass in the size of the keys array, and rename the function to traceeval_remove_size() that now takes the size of the keys array.

---
 include/traceeval-hist.h | 7 +++++--
 src/histograms.c         | 7 +++++--
 2 files changed, 10 insertions(+), 4 deletions(-)
include/traceeval-hist.h +5 -2
index 7f48bb92cc96..804a0aaa631d 100644
@@ -196,8 +196,11 @@ int traceeval_insert_size(struct traceeval *teval,
 	traceeval_insert_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), \
 			      vals, TRACEEVAL_ARRAY_SIZE(vals))
 
-int traceeval_remove(struct traceeval *teval,
-		     const struct traceeval_data *keys);
+int traceeval_remove_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys);
+
+#define traceeval_remove(teval, keys)					\
+	traceeval_remove_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys))
 
 int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *keys,
 			 size_t nr_keys, const struct traceeval_data **results);
src/histograms.c +0 -0
index ab8a560fe14d..28cf0d4ed225 100644
@@ -977,13 +977,16 @@ int traceeval_insert_size(struct traceeval *teval,

I think we need to update the comments above this function to account for the new name and the additional 'nr_keys' param, but other than that you can add:

Quoted 21 lines
src/histograms.c +5 -2
  *         0 if it did not find an time matching @keys
  *        -1 if there was an error.
  */
-int traceeval_remove(struct traceeval *teval,
-		     const struct traceeval_data *keys)
+int traceeval_remove_size(struct traceeval *teval,
+			  const struct traceeval_data *keys, size_t nr_keys)
 {
 	struct hash_table *hist = teval->hist;
 	struct entry *entry;
 	int check;
 
+	if (teval->nr_key_types != nr_keys)
+		return -1;
+
 	entry = NULL;
 	check = get_entry(teval, keys, &entry);
 
Signature · 1 line
2.40.1
#18 lore raw

PATCH v2 10/11 libtraceeval: Make traceeval_stat() check size of keys array

In reply to Steven Rostedt

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

Make traceeval_stat() into a macro to pass in the size of the keys array, and rename the function to traceeval_stat_size() that now takes the size of the keys array.

---
 include/traceeval-hist.h | 10 +++++++---
 src/histograms.c         | 10 +++++++---
 2 files changed, 14 insertions(+), 6 deletions(-)
include/traceeval-hist.h +7 -3
index 804a0aaa631d..a1b498108fb4 100644
@@ -211,9 +211,13 @@ int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *k
 void traceeval_results_release(struct traceeval *teval,
 			       const struct traceeval_data *results);
 
-struct traceeval_stat *traceeval_stat(struct traceeval *teval,
-				      const struct traceeval_data *keys,
-				      struct traceeval_type *type);
+#define traceeval_stat(teval, keys, type)				\
+	traceeval_stat_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), type)
+
+struct traceeval_stat *traceeval_stat_size(struct traceeval *teval,
+					   const struct traceeval_data *keys,
+					   size_t nr_keys,
+					   struct traceeval_type *type);
 
 unsigned long long traceeval_stat_max(struct traceeval_stat *stat);
 unsigned long long traceeval_stat_min(struct traceeval_stat *stat);
src/histograms.c +7 -3
index 28cf0d4ed225..4d534d127cff 100644
@@ -844,13 +844,17 @@ static bool is_stat_type(struct traceeval_type *type)
 	}
 }
 
-struct traceeval_stat *traceeval_stat(struct traceeval *teval,
-				      const struct traceeval_data *keys,
-				      struct traceeval_type *type)
+struct traceeval_stat *traceeval_stat_size(struct traceeval *teval,
+					   const struct traceeval_data *keys,
+					   size_t nr_keys,
+					   struct traceeval_type *type)
 {
 	struct entry *entry;
 	int ret;
 
+	if (teval->nr_key_types != nr_keys)
+		return NULL;
+
 	if (!is_stat_type(type))
 		return NULL;
 
Signature · 1 line
2.40.1
Reviewed-by on v2 10/11
#19 lore raw

Re: [PATCH v2 10/11] libtraceeval: Make traceeval_stat() check size of keys array

Quoted 7 lines · Steven Rostedt

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

Make traceeval_stat() into a macro to pass in the size of the keys array, and rename the function to traceeval_stat_size() that now takes the size of the keys array.

#20 lore raw

PATCH v2 11/11 libtraceeval: Only do stats on values marked with the STAT flag

In reply to Steven Rostedt

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

Add TRACEEVAL_FL_STAT to perform stats on the value, otherwise ignore it.

---
 include/traceeval-hist.h |  1 +
 samples/task-eval.c      |  1 +
 src/histograms.c         | 39 ++++++++++++++++++++-------------------
 3 files changed, 22 insertions(+), 19 deletions(-)
include/traceeval-hist.h +1 -0
index a1b498108fb4..618e67593dc0 100644
@@ -38,6 +38,7 @@ enum traceeval_flags {
 	TRACEEVAL_FL_VALUE		= (1 << 1),
 	TRACEEVAL_FL_SIGNED		= (1 << 2),
 	TRACEEVAL_FL_TIMESTAMP		= (1 << 3),
+	TRACEEVAL_FL_STAT		= (1 << 4),
 };
 
 /*
samples/task-eval.c +1 -0
index 475df190acd2..6b01b8d076f2 100644
@@ -147,6 +147,7 @@ static struct traceeval_type delta_vals[] = {
 	{
 		.type	= TRACEEVAL_TYPE_NUMBER_64,
 		.name	= "delta",
+		.flags = TRACEEVAL_FL_STAT,
 	},
 	{
 		.type	= TRACEEVAL_TYPE_NONE,
src/histograms.c +20 -19
index 4d534d127cff..0a3ab84d288e 100644
@@ -505,6 +505,25 @@ static int get_entry(struct traceeval *teval, const struct traceeval_data *keys,
 	return check;
 }
 
+static bool is_stat_type(struct traceeval_type *type)
+{
+	/* Only value numbers have stats */
+	if (!(type->flags & TRACEEVAL_FL_VALUE) ||
+	    !(type->flags & TRACEEVAL_FL_STAT))
+		return false;
+
+	switch (type->type) {
+	case TRACEEVAL_TYPE_NUMBER:
+	case TRACEEVAL_TYPE_NUMBER_64:
+	case TRACEEVAL_TYPE_NUMBER_32:
+	case TRACEEVAL_TYPE_NUMBER_16:
+	case TRACEEVAL_TYPE_NUMBER_8:
+		return true;
+	default:
+		return false;
+	}
+}
+
 /*
  * Copy @src to @dst with respect to @type.
  *
@@ -571,7 +590,7 @@ static int copy_traceeval_data(struct traceeval_type *type,
 		return 0;
 	}
 
-	if (!stat)
+	if (!stat || !is_stat_type(type))
 		return 0;
 
 	if (!stat->count++) {
@@ -826,24 +845,6 @@ static int update_entry(struct traceeval *teval, struct entry *entry,
 	return -1;
 }
 
-static bool is_stat_type(struct traceeval_type *type)
-{
-	/* Only value numbers have stats */
-	if (!(type->flags & TRACEEVAL_FL_VALUE))
-		return false;
-
-	switch (type->type) {
-	case TRACEEVAL_TYPE_NUMBER:
-	case TRACEEVAL_TYPE_NUMBER_64:
-	case TRACEEVAL_TYPE_NUMBER_32:
-	case TRACEEVAL_TYPE_NUMBER_16:
-	case TRACEEVAL_TYPE_NUMBER_8:
-		return true;
-	default:
-		return false;
-	}
-}
-
 struct traceeval_stat *traceeval_stat_size(struct traceeval *teval,
 					   const struct traceeval_data *keys,
 					   size_t nr_keys,
Signature · 1 line
2.40.1