Skip to contentSkip to conversation

libtraceeval histogram: Updates

PATCH Superseded Steven Rostedt 2 participants 39 messages Aug 11, 2023 – Aug 17, 2023 Kbuild

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover libtraceeval histogram: Updates

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

This patch set is based on top of:

https://lore.kernel.org/all/[email protected]/

I added a sample program task-eval which is one of the tools that will be using this library. The first patch adds task-eval but that is still using the old API (defined in trace-analysis.c).

The next patches modify the new API to fit with the use case of task-eval. One is to use "pointer" as I'm not sure exactly the usecase of the dynamic structure.

The cmp and release callbacks are changed to be more generic, and they get called if they simply exist for a given type. I can imagine wanting a release function for event the most mundane types (like number_32).

The cmp was also updated to pass in the traceeval descriptor, as I found that I needed access to it while doing a compare (although, I rewrote the code a bit where that use case isn't in the tool anymore).

Some fixes were made to the query.

I also did a bit of code restructuring and add the hash and iterator logic.

The last patch updates the sample code task-eval.c and has it give pretty much the same logic as the original.

That sample could be updated to implement the code consolidation that Ross suggested. I may do that later.

Happy programming!

Changes since v1: https://lore.kernel.org/all/[email protected]/

- Lots!

- Converted to using a hash table

- Removed the unused compare code. With the other updates, it was taking too much time to keep updating them.

- Added checks and labels to the types to have them know what type they are, and index they are at.

- Added stat logic

- Added iterator logic

- Have a working sample with the new code!

Steven Rostedt (Google) (17):
libtraceeval histograms: Fix traceeval_results_release() error message
libtraceeval: Add sample task-eval program
libtraceeval hist: Add pointer and const string types libtraceeval histogram: Have cmp and release functions be generic libtraceeval histograms: Add traceeval struct to compare function libtraceeval histogram: Remove comparing of traceeval and types
libtraceeval: Convert hist array into a hash table libtraceeval histograms: Move hash functions into their own file libtraceeval histogram: Label and check keys and values libtraceeval histogram: Add updating of stats
libtraceeval histogram: Add iterator APIs
libtraceeval histogram: Add data copy callback libtraceeval histogram: Do the release on updates libtraceeval histogram: Use stack for old copy in update libtraceeval histogram: Add traceeval_iterator_sort_custom() libtraceeval histogram: Have traceeval_query() just give the pointer
to results
libtraceeval samples: Update task-eval to use the histogram logic

 Makefile                 |   4 +-
 include/traceeval-hist.h | 110 +++--
 include/traceeval-test.h |  16 -
 samples/Makefile         |  29 ++
 samples/task-eval.c      | 952 +++++++++++++++++++++++++++++++++++++++
 src/Makefile             |   1 +
 src/eval-local.h         | 123 +++++
 src/hash.c               | 123 +++++
 src/histograms.c         | 878 ++++++++++++++++++++++++++----------
 9 files changed, 1952 insertions(+), 284 deletions(-)
 delete mode 100644 include/traceeval-test.h
 create mode 100644 samples/Makefile
 create mode 100644 samples/task-eval.c
 create mode 100644 src/eval-local.h
 create mode 100644 src/hash.c
Signature · 1 line
2.40.1
#1 lore raw

PATCH v2 1/17 libtraceeval histograms: Fix traceeval_results_release() error message

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

The function traceeval_results_release() must have a teval if passed in to free the accompanying results, but currently the error message is only displayed if the results is NULL and teval exists. It should be the other way around.

---
 src/histograms.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/histograms.c +1 -1
index a159892e509b..d22d15238616 100644
@@ -681,7 +681,7 @@ void traceeval_results_release(struct traceeval *teval,
 			       union traceeval_data *results)
 {
 	if (!teval || !results) {
-		if (!results)
+		if (!teval)
 			print_err("Results to be freed without accompanied traceeval instance!");
 		return;
 	}
Signature · 1 line
2.40.1
#2 lore raw

PATCH v2 2/17 libtraceeval: Add sample task-eval program

In reply to Steven Rostedt

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

Add a sample task eval program to test the traceeval logic.

This relies on having libtracecmd installed.

---
 Makefile            |   3 +
 samples/Makefile    |  29 ++
 samples/task-eval.c | 859 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 891 insertions(+)
 create mode 100644 samples/Makefile
 create mode 100644 samples/task-eval.c
Makefile +3 -0
index 3ea051cbebf5..62ca9285d7a0 100644
@@ -348,6 +348,9 @@ $(LIBRARY_STATIC): force
 $(LIBRARY_SHARED): force
 	$(Q)$(call descend,$(src)/src,$(LIBRARY_SO))
 
+samples: $(LIBRARY_STATIC) force
+	$(Q)$(call descend,$(src)/samples,all)
+
 #	$(Q)$(call descend_clean,utest)
 clean:
 	$(Q)$(call descend_clean,src)
samples/Makefile new file +29 -0
new file mode 100644
index 000000000000..012301ec5542
@@ -0,0 +1,29 @@
+# SPDX-License-Identifier: LGPL-2.1
+
+include $(src)/scripts/utils.mk
+
+TARGETS :=
+TARGETS += task-eval
+
+sdir := $(obj)/bin
+
+CFLAGS += `pkg-config --cflags libtracecmd`
+LIBRARY_LIBS += `pkg-config --libs libtracecmd`
+
+TARGETS := $(patsubst %,$(sdir)/%,$(TARGETS))
+
+all: $(TARGETS)
+
+$(sdir):
+	@mkdir -p $(sdir)
+
+$(TARGETS): $(sdir) $(LIBTRACEEVAL_STATIC)
+
+$(sdir)/%: $(bdir)/%.o
+	$(call do_sample_build,$@,$<)
+
+$(bdir)/%.o: $(bdir)/%.c
+	$(Q)$(CC) -o $@ -c $< $(CFLAGS) $(INCLUDES)
+
+clean:
+	$(Q)$(call do_clean,$(sdir)/*)
samples/task-eval.c new file +859 -0

859 lines not on this page · raw message

Signature · 1 line
2.40.1
#3 lore raw

PATCH v2 3/17 libtraceeval hist: Add pointer and const string types

In reply to Steven Rostedt

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

Add a pointer type to traceeval_data, as this can be used as a generic pointer type. This may even obsolete the dynamic type.

Also, add a const char * "cstring" type. There's times where the key and value data needs to be assigned to a const char *string, and without having an option of that type, the compiler complains about losing the const.

---
 include/traceeval-hist.h | 3 +++
 1 file changed, 3 insertions(+)
include/traceeval-hist.h +3 -0
index 08e0696f2d83..f6c4e8efb2be 100644
@@ -25,6 +25,7 @@ enum traceeval_data_type {
 	TRACEEVAL_TYPE_NUMBER_32,
 	TRACEEVAL_TYPE_NUMBER_64,
 	TRACEEVAL_TYPE_NUMBER,
+	TRACEEVAL_TYPE_POINTER,
 	TRACEEVAL_TYPE_STRING,
 	TRACEEVAL_TYPE_DYNAMIC
 };
@@ -52,6 +53,8 @@ struct traceeval_dynamic {
 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;
Signature · 1 line
2.40.1
#4 lore raw

PATCH v2 4/17 libtraceeval histogram: Have cmp and release functions be generic

In reply to Steven Rostedt

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

Having the ability to override the compare function for a given type can be very advantageous. There's also no reason that any type could ask for a release callback to be called when the type is being released. It could be used for information as well as for freeing.

Rename traceeval_dyn_cmp_fn to traceeval_data_cmp_fn and
traceeval_dyn_release_fn to traceeval_data_release_fn and have
them take the union traceeval_type instead of struct traceeval_dynamic.

Also this changes the compare to pass a pointer to union traceeval_data instead of passing in the structure of the dyn_data type.

In the structure, rename dyn_cmp to just cmp, and dyn_release to just release.

---
 include/traceeval-hist.h | 46 +++++++++++++++++++++-------------------
 src/histograms.c         | 28 ++++++++++++------------
 2 files changed, 38 insertions(+), 36 deletions(-)
include/traceeval-hist.h +24 -22
index f6c4e8efb2be..22e9029133d5 100644
@@ -65,13 +65,13 @@ union traceeval_data {
 struct traceeval_type;
 
 /* struct traceeval_dynamic release function signature */
-typedef void (*traceeval_dyn_release_fn)(struct traceeval_type *,
-					 struct traceeval_dynamic);
+typedef void (*traceeval_data_release_fn)(struct traceeval_type *,
+					  union traceeval_data *);
 
 /* struct traceeval_dynamic compare function signature */
-typedef int (*traceeval_dyn_cmp_fn)(struct traceeval_dynamic,
-				    struct traceeval_dynamic,
-				    struct traceeval_type *);
+typedef int (*traceeval_data_cmp_fn)(const union traceeval_data *,
+				     const union traceeval_data *,
+				     struct traceeval_type *);
 
 /*
  * struct traceeval_type - Describes the type of a traceevent_data instance
@@ -79,8 +79,8 @@ typedef int (*traceeval_dyn_cmp_fn)(struct traceeval_dynamic,
  * @name: The string name of the traceeval_data
  * @flags: flags to describe the traceeval_data
  * @id: User specified identifier
- * @dyn_release: For dynamic types called on release (ignored for other types)
- * @dyn_cmp: A way to compare dynamic types (ignored for other types)
+ * @release: An optional callback for when the data is being released
+ * @cmp: An optional callback to specify a way to compare the type
  *
  * The traceeval_type structure defines expectations for a corresponding
  * traceeval_data instance for a traceeval histogram instance. Used to
@@ -91,29 +91,31 @@ typedef int (*traceeval_dyn_cmp_fn)(struct traceeval_dynamic,
  * which each relate to distinct user defined struct traceeval_dynamic
  * 'sub-types'.
  *
- * For flexibility, @dyn_cmp() and @dyn_release() take a struct
- * traceeval_type instance. This allows the user to distinguish between
- * different sub-types of struct traceeval_dynamic within a single
- * callback function by examining the @id field. This is not a required
- * approach, merely one that is accommodated.
+ * For flexibility, @cmp() and @release() take a struct traceeval_type
+ * instance. This allows the user to handle dyn_data and pointer types.
+ * It may also be used for other types if the default cmp() or release()
+ * need to be overridden. Note for string types, even if the release()
+ * is called, the string freeing is still taken care of by the traceeval
+ * infrastructure.
  *
- * @dyn_cmp() is used to compare two struct traceeval_dynamic instances when a
- * corresponding struct traceeval_type is reached with its type field set to
- * TRACEEVAL_TYPE_DYNAMIC. It should return 0 on equality, 1 if the first
- * argument is greater than the second, -1 for the other way around, and -2 on
- * error.
+ * The @id field is a user specified field that may allow the same callback
+ * to be used by multiple types and not needing to do a strcmp() against the
+ * name (could be used for switch statements).
  *
- * dyn_release() is used during traceeval_release() to release a union
- * traceeval_data's struct traceeval_dynamic field when the corresponding
- * traceeval_type type is set to TRACEEVAL_TYPE_DYNAMIC.
+ * @cmp() is used to override the default compare of a type. This is
+ * required to compare dyn_data and pointer types. It should return 0
+ * on equality, 1 if the first argument is greater than the second,
+ * -1 for the other way around, and -2 on error.
+ *
+ * @release() is called when a data element is being released (or freed).
  */
 struct traceeval_type {
 	char				*name;
 	enum traceeval_data_type	type;
 	size_t				flags;
 	size_t				id;
-	traceeval_dyn_release_fn	dyn_release;
-	traceeval_dyn_cmp_fn		dyn_cmp;
+	traceeval_data_release_fn	release;
+	traceeval_data_cmp_fn		cmp;
 };
 
 /* Statistics about a given entry element */
src/histograms.c +14 -14
index d22d15238616..09cdf57a8f6a 100644
@@ -90,9 +90,9 @@ static int compare_traceeval_type(struct traceeval_type *orig,
 			return 0;
 		if (orig[i].id != copy[i].id)
 			return 0;
-		if (orig[i].dyn_release != copy[i].dyn_release)
+		if (orig[i].release != copy[i].release)
 			return 0;
-		if (orig[i].dyn_cmp != copy[i].dyn_cmp)
+		if (orig[i].cmp != copy[i].cmp)
 			return 0;
 
 		// make sure both names are same type
@@ -128,6 +128,9 @@ static int compare_traceeval_data(union traceeval_data *orig,
 	if (!copy)
 		return 1;
 
+	if (type->cmp)
+		return type->cmp(orig, copy, type);
+
 	switch (type->type) {
 	case TRACEEVAL_TYPE_STRING:
 		i = strcmp(orig->string, copy->string);
@@ -149,8 +152,7 @@ static int compare_traceeval_data(union traceeval_data *orig,
 		compare_numbers_return(orig->number_8, copy->number_8);
 
 	case TRACEEVAL_TYPE_DYNAMIC:
-		if (type->dyn_cmp)
-			return type->dyn_cmp(orig->dyn_data, copy->dyn_data, type);
+		/* If it didn't specify a cmp function, then punt */
 		return 0;
 
 	default:
@@ -236,8 +238,8 @@ static int compare_hist(struct traceeval *orig, struct traceeval *copy)
  *
  * This compares the values of the key definitions, value definitions, and
  * inserted data between @orig and @copy in order. It does not compare
- * by memory address, except for struct traceeval_type's dyn_release() and
- * dyn_cmp() fields.
+ * by memory address, except for struct traceeval_type's release() and
+ * cmp() fields.
  *
  * Returns 1 if @orig and @copy are the same, 0 if not, and -1 on error.
  */
@@ -438,14 +440,13 @@ fail:
  */
 static void clean_data(union traceeval_data *data, struct traceeval_type *type)
 {
+		if (type->release)
+			type->release(type, data);
+
 		switch (type->type) {
 		case TRACEEVAL_TYPE_STRING:
 			free(data->string);
 			break;
-		case TRACEEVAL_TYPE_DYNAMIC:
-			if (type->dyn_release)
-				type->dyn_release(type, data->dyn_data);
-			break;
 		default:
 			break;
 		}
@@ -465,9 +466,8 @@ static void clean_data_set(union traceeval_data *data, struct traceeval_type *de
 		return;
 	}
 
-	for (i = 0; i < size; i++) {
+	for (i = 0; i < size; i++)
 		clean_data(data + i, defs + i);
-	}
 
 	free(data);
 }
@@ -512,7 +512,7 @@ static void hist_table_release(struct traceeval *teval)
  * it must call traceeval_release().
  *
  * This frees all internally allocated data of @teval and will call the
- * corresponding dyn_release() functions registered for keys and values of
+ * corresponding release() functions registered for keys and values of
  * type TRACEEVAL_TYPE_DYNAMIC.
  */
 void traceeval_release(struct traceeval *teval)
@@ -588,7 +588,7 @@ static int copy_traceeval_data(struct traceeval_type *type,
 /*
  * Free @data with respect to @size and @type.
  *
- * Does not call dyn_release on type TRACEEVAL_TYPE_DYNAMIC.
+ * Does not call release on type TRACEEVAL_TYPE_DYNAMIC.
  */
 static void data_release(size_t size, union traceeval_data **data,
 				struct traceeval_type *type)
Signature · 1 line
2.40.1
Reviewed-by on v2 4/17
#5 lore raw

Re: [PATCH v2 04/17] libtraceeval histogram: Have cmp and release functions be generic

Quoted 28 lines · Steven Rostedt

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

Having the ability to override the compare function for a given type can be very advantageous. There's also no reason that any type could ask for a release callback to be called when the type is being released. It could be used for information as well as for freeing.

Rename traceeval_dyn_cmp_fn to traceeval_data_cmp_fn and
traceeval_dyn_release_fn to traceeval_data_release_fn and have
them take the union traceeval_type instead of struct traceeval_dynamic.

Also this changes the compare to pass a pointer to union traceeval_data instead of passing in the structure of the dyn_data type.

In the structure, rename dyn_cmp to just cmp, and dyn_release to just release.

---
 include/traceeval-hist.h | 46 +++++++++++++++++++++-------------------
 src/histograms.c         | 28 ++++++++++++------------
 2 files changed, 38 insertions(+), 36 deletions(-)
include/traceeval-hist.h +0 -0
index f6c4e8efb2be..22e9029133d5 100644

include/traceeval-hist.h union traceeval_data { · line 67

Quoted 3 lines
include/traceeval-hist.h +0 -0
@@ -65,13 +65,13 @@ union traceeval_data {
 struct traceeval_type;
 
 /* struct traceeval_dynamic release function signature */

You may want to update the comment here to make it clear that this is now for both dynamic and pointer types. Ditto for the compare function signature.

Quoted 5 lines
include/traceeval-hist.h +2 -2
-typedef void (*traceeval_dyn_release_fn)(struct traceeval_type *,
-					 struct traceeval_dynamic);
+typedef void (*traceeval_data_release_fn)(struct traceeval_type *,
+					  union traceeval_data *);
 

include/traceeval-hist.h union traceeval_data { · line 77

Quoted 10 lines
include/traceeval-hist.h +3 -3
 /* struct traceeval_dynamic compare function signature */
-typedef int (*traceeval_dyn_cmp_fn)(struct traceeval_dynamic,
-				    struct traceeval_dynamic,
-				    struct traceeval_type *);
+typedef int (*traceeval_data_cmp_fn)(const union traceeval_data *,
+				     const union traceeval_data *,
+				     struct traceeval_type *);
 
 /*
  * struct traceeval_type - Describes the type of a traceevent_data instance

<>

include/traceeval-hist.h static int copy_traceeval_data(struct traceeval_type *type, · line 591

Quoted 6 lines
include/traceeval-hist.h +1 -1
@@ -588,7 +588,7 @@ static int copy_traceeval_data(struct traceeval_type *type,
 /*
  * Free @data with respect to @size and @type.
  *
- * Does not call dyn_release on type TRACEEVAL_TYPE_DYNAMIC.
+ * Does not call release on type TRACEEVAL_TYPE_DYNAMIC.

or for TRACEEVAL_TYPE_POINTER.

Quoted 5 lines
include/traceeval-hist.h +0 -0
  */
 static void data_release(size_t size, union traceeval_data **data,
 				struct traceeval_type *type)
Signature · 1 line
2.40.1

Aside from these two comment nits, you can add:

#6 lore raw

Re: [PATCH v2 04/17] libtraceeval histogram: Have cmp and release functions be generic

Quoted 11 lines · On Tue, 15 Aug 2023 10:50:28 -0600 Ross Zwisler <[email protected]> wrote:

include/traceeval-hist.h union traceeval_data { · line 67

Quoted 8 lines
include/traceeval-hist.h +0 -0
index f6c4e8efb2be..22e9029133d5 100644
@@ -65,13 +65,13 @@ union traceeval_data {
 struct traceeval_type;
 
 /* struct traceeval_dynamic release function signature */  

You may want to update the comment here to make it clear that this is now for both dynamic and pointer types. Ditto for the compare function signature.

Agreed, (I was tired and new I was going to miss some spots).

Quoted 17 lines
Quoted 6 lines
include/traceeval-hist.h +2 -2
-typedef void (*traceeval_dyn_release_fn)(struct traceeval_type *,
-					 struct traceeval_dynamic);
+typedef void (*traceeval_data_release_fn)(struct traceeval_type *,
+					  union traceeval_data *);
 

include/traceeval-hist.h union traceeval_data { · line 77

Quoted 10 lines
include/traceeval-hist.h +3 -3
 /* struct traceeval_dynamic compare function signature */
-typedef int (*traceeval_dyn_cmp_fn)(struct traceeval_dynamic,
-				    struct traceeval_dynamic,
-				    struct traceeval_type *);
+typedef int (*traceeval_data_cmp_fn)(const union traceeval_data *,
+				     const union traceeval_data *,
+				     struct traceeval_type *);
 
 /*
  * struct traceeval_type - Describes the type of a traceevent_data instance  

<>

Quoted 8 lines

include/traceeval-hist.h static int copy_traceeval_data(struct traceeval_type *type, · line 591

Quoted 6 lines
include/traceeval-hist.h +1 -1
@@ -588,7 +588,7 @@ static int copy_traceeval_data(struct traceeval_type *type,
 /*
  * Free @data with respect to @size and @type.
  *
- * Does not call dyn_release on type TRACEEVAL_TYPE_DYNAMIC.
+ * Does not call release on type TRACEEVAL_TYPE_DYNAMIC.  

or for TRACEEVAL_TYPE_POINTER.

+1

Quoted 10 lines
Quoted 6 lines
include/traceeval-hist.h +0 -0
  */
 static void data_release(size_t size, union traceeval_data **data,
 				struct traceeval_type *type)
Signature · 1 line
2.40.1  

Aside from these two comment nits, you can add:

Thanks Ross!

-- Steve

#7 lore raw

PATCH v2 5/17 libtraceeval histograms: Add traceeval struct to compare function

In reply to Steven Rostedt

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

When looking at how this code would be implemented, I found that I needed access to the traceeval structure within the compare callbacks. Pass that in too.

Also, rearrange the parameters a little. The traceeval and type should go first as they describe the "object", and the data should go last as they are the values of the function.

---
 include/traceeval-hist.h | 13 +++++++------
 src/histograms.c         | 19 ++++++++++---------
 2 files changed, 17 insertions(+), 15 deletions(-)
include/traceeval-hist.h +7 -6
index 22e9029133d5..412efdbe8681 100644
@@ -63,16 +63,17 @@ union traceeval_data {
 };
 
 struct traceeval_type;
+struct traceeval;
 
 /* struct traceeval_dynamic release function signature */
-typedef void (*traceeval_data_release_fn)(struct traceeval_type *,
-					  union traceeval_data *);
+typedef void (*traceeval_data_release_fn)(const struct traceeval_type *type,
+					  union traceeval_data *data);
 
 /* struct traceeval_dynamic compare function signature */
-typedef int (*traceeval_data_cmp_fn)(const union traceeval_data *,
-				     const union traceeval_data *,
-				     struct traceeval_type *);
-
+typedef int (*traceeval_data_cmp_fn)(struct traceeval *teval,
+				     const struct traceeval_type *type,
+				     const union traceeval_data *A,
+				     const union traceeval_data *B);
 /*
  * struct traceeval_type - Describes the type of a traceevent_data instance
  * @type: The enum type that describes the traceeval_data
src/histograms.c +10 -9
index 09cdf57a8f6a..ece1395ac300 100644
@@ -113,7 +113,8 @@ static int compare_traceeval_type(struct traceeval_type *orig,
  * Return 0 if @orig and @copy are the same, 1 if @orig is greater than @copy,
  * -1 for the other way around, and -2 on error.
  */
-static int compare_traceeval_data(union traceeval_data *orig,
+static int compare_traceeval_data(struct traceeval *teval,
+				  union traceeval_data *orig,
 				  const union traceeval_data *copy,
 				  struct traceeval_type *type)
 {
@@ -129,7 +130,7 @@ static int compare_traceeval_data(union traceeval_data *orig,
 		return 1;
 
 	if (type->cmp)
-		return type->cmp(orig, copy, type);
+		return type->cmp(teval, type, orig, copy);
 
 	switch (type->type) {
 	case TRACEEVAL_TYPE_STRING:
@@ -167,7 +168,8 @@ static int compare_traceeval_data(union traceeval_data *orig,
  *
  * Return 1 if @orig and @copy are the same, 0 if not, and -1 on error.
  */
-static int compare_traceeval_data_set(union traceeval_data *orig,
+static int compare_traceeval_data_set(struct traceeval *teval,
+				      union traceeval_data *orig,
 				      const union traceeval_data *copy,
 				      struct traceeval_type *defs, size_t size)
 {
@@ -176,7 +178,7 @@ static int compare_traceeval_data_set(union traceeval_data *orig,
 
 	/* compare data arrays */
 	for (i = 0; i < size; i++) {
-		if ((check = compare_traceeval_data(orig + i, copy + i, defs + i)))
+		if ((check = compare_traceeval_data(teval, orig + i, copy + i, defs + i)))
 			return check == -2 ? -1 : 0;
 	}
 
@@ -192,13 +194,13 @@ static int compare_entries(struct entry *orig, struct entry *copy,
 	int check;
 
 	/* compare keys */
-	check = compare_traceeval_data_set(orig->keys, copy->keys,
+	check = compare_traceeval_data_set(teval, orig->keys, copy->keys,
 			teval->key_types, teval->nr_key_types);
 	if (check < 1)
 		return check;
 
 	/* compare values */
-	check = compare_traceeval_data_set(orig->vals, copy->vals,
+	check = compare_traceeval_data_set(teval, orig->vals, copy->vals,
 			teval->val_types, teval->nr_val_types);
 	return check;
 }
@@ -546,9 +548,8 @@ static int get_entry(struct traceeval *teval, const union traceeval_data *keys,
 
 	hist = teval->hist;
 	for (i = 0, entry = hist->map; i < hist->nr_entries; entry = &hist->map[++i]) {
-		check = compare_traceeval_data_set(entry->keys, keys,
-				teval->key_types, teval->nr_key_types);
-
+		check = compare_traceeval_data_set(teval, entry->keys, keys,
+						   teval->key_types, teval->nr_key_types);
 		if (!check)
 			continue;
 		break;
Signature · 1 line
2.40.1
Reviewed-by on v2 5/17
#8 lore raw

Re: [PATCH v2 05/17] libtraceeval histograms: Add traceeval struct to compare function

Quoted 49 lines · Steven Rostedt

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

When looking at how this code would be implemented, I found that I needed access to the traceeval structure within the compare callbacks. Pass that in too.

Also, rearrange the parameters a little. The traceeval and type should go first as they describe the "object", and the data should go last as they are the values of the function.

---
 include/traceeval-hist.h | 13 +++++++------
 src/histograms.c         | 19 ++++++++++---------
 2 files changed, 17 insertions(+), 15 deletions(-)
include/traceeval-hist.h +7 -6
index 22e9029133d5..412efdbe8681 100644
@@ -63,16 +63,17 @@ union traceeval_data {
 };
 
 struct traceeval_type;
+struct traceeval;
 
 /* struct traceeval_dynamic release function signature */
-typedef void (*traceeval_data_release_fn)(struct traceeval_type *,
-					  union traceeval_data *);
+typedef void (*traceeval_data_release_fn)(const struct traceeval_type *type,
+					  union traceeval_data *data);
 
 /* struct traceeval_dynamic compare function signature */
-typedef int (*traceeval_data_cmp_fn)(const union traceeval_data *,
-				     const union traceeval_data *,
-				     struct traceeval_type *);
-
+typedef int (*traceeval_data_cmp_fn)(struct traceeval *teval,
+				     const struct traceeval_type *type,
+				     const union traceeval_data *A,
+				     const union traceeval_data *B);
 /*
  * struct traceeval_type - Describes the type of a traceevent_data instance
  * @type: The enum type that describes the traceeval_data
src/histograms.c +0 -0
index 09cdf57a8f6a..ece1395ac300 100644

src/histograms.c static int compare_traceeval_type(struct traceeval_type *orig, · line 119

Quoted 8 lines
src/histograms.c +2 -1
@@ -113,7 +113,8 @@ static int compare_traceeval_type(struct traceeval_type *orig,
  * Return 0 if @orig and @copy are the same, 1 if @orig is greater than @copy,
  * -1 for the other way around, and -2 on error.
  */
-static int compare_traceeval_data(union traceeval_data *orig,
+static int compare_traceeval_data(struct traceeval *teval,
+				  union traceeval_data *orig,
 				  const union traceeval_data *copy,
 				  struct traceeval_type *type)

Nit: it would be nice if these had the same ordering as the args to traceeval_data_cmp_fn.

Other than that you can add:

#9 lore raw

Re: [PATCH v2 05/17] libtraceeval histograms: Add traceeval struct to compare function

Quoted 6 lines · On Tue, 15 Aug 2023 10:55:54 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 6 lines
src/histograms.c +0 -0
@@ -113,7 +113,8 @@ static int compare_traceeval_type(struct traceeval_type *orig,
  * Return 0 if @orig and @copy are the same, 1 if @orig is greater than @copy,
  * -1 for the other way around, and -2 on error.
  */
Quoted 9 lines

src/histograms.c static int compare_traceeval_type(struct traceeval_type *orig, · line 119

Quoted 5 lines
src/histograms.c +2 -1
-static int compare_traceeval_data(union traceeval_data *orig,
+static int compare_traceeval_data(struct traceeval *teval,
+				  union traceeval_data *orig,
 				  const union traceeval_data *copy,
 				  struct traceeval_type *type)  

Nit: it would be nice if these had the same ordering as the args to traceeval_data_cmp_fn.

Yeah, I noticed that to, but figured I can change it later, as it's not API.

I'll update it.

Quoted 3 lines

Other than that you can add:

Thanks Ross,

-- Steve

#10 lore raw

PATCH v2 6/17 libtraceeval histogram: Remove comparing of traceeval and types

In reply to Steven Rostedt

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

The compare functions for traceeval and types were created for testing. But because the tests have not yet been pulled in, remove these functions as they are currently unused and cause an unnecessary burden of making sure they are updated when other code they use is updated.

---
 Makefile                 |   1 -
 include/traceeval-test.h |  16 -----
 src/histograms.c         | 127 ---------------------------------------
 3 files changed, 144 deletions(-)
 delete mode 100644 include/traceeval-test.h
Makefile +0 -1
index 62ca9285d7a0..d8c57ef4e356 100644
@@ -172,7 +172,6 @@ libs: $(LIBRARY_A) $(LIBRARY_SO)
 
 VALGRIND = $(shell which valgrind)
 UTEST_DIR = utest
-UTEST_BINARY = eval-utest
 
 test: force $(LIBRARY_STATIC)
 ifneq ($(CUNIT_INSTALLED),1)
include/traceeval-test.h deleted +0 -16
deleted file mode 100644
index bb8092ac2e50..000000000000
@@ -1,16 +0,0 @@
-/* SPDX-License-Identifier: MIT */
-/*
- * libtraceeval interface for unit testing.
- *
- * Copyright (C) 2023 Google Inc, Steven Rostedt <[email protected]>
- * Copyright (C) 2023 Google Inc, Stevie Alvarez <[email protected]>
- */
-
-#ifndef __LIBTRACEEVAL_TEST_H__
-#define __LIBTRACEEVAL_TEST_H__
-
-#include <traceeval-hist.h>
-
-int traceeval_compare(struct traceeval *orig, struct traceeval *copy);
-
-#endif /* __LIBTRACEEVAL_TEST_H__ */
src/histograms.c +0 -127

145 lines not on this page · raw message

Signature · 1 line
2.40.1
#11 lore raw

PATCH v2 7/17 libtraceeval: Convert hist array into a hash table

In reply to Steven Rostedt

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

The lookups need to be extremely fast. Instead of doing a linear search across all entries (which could be thousands), do a hash lookup instead.

---
 include/traceeval-hist.h |   7 ++
 src/histograms.c         | 172 +++++++++++++++++++++++++++++++++------
 2 files changed, 153 insertions(+), 26 deletions(-)
include/traceeval-hist.h +7 -0
index 412efdbe8681..4baed4237787 100644
@@ -74,6 +74,12 @@ typedef int (*traceeval_data_cmp_fn)(struct traceeval *teval,
 				     const struct traceeval_type *type,
 				     const union traceeval_data *A,
 				     const union 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);
+
 /*
  * struct traceeval_type - Describes the type of a traceevent_data instance
  * @type: The enum type that describes the traceeval_data
@@ -117,6 +123,7 @@ struct traceeval_type {
 	size_t				id;
 	traceeval_data_release_fn	release;
 	traceeval_data_cmp_fn		cmp;
+	traceeval_data_hash_fn		hash;
 };
 
 /* Statistics about a given entry element */
src/histograms.c +146 -26

276 lines not on this page · raw message

Signature · 1 line
2.40.1
#12 lore raw

Re: [PATCH v2 07/17] libtraceeval: Convert hist array into a hash table

include/traceeval-hist.h

Quoted 15 lines · Steven Rostedt

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

The lookups need to be extremely fast. Instead of doing a linear search across all entries (which could be thousands), do a hash lookup instead.

---
 include/traceeval-hist.h |   7 ++
 src/histograms.c         | 172 +++++++++++++++++++++++++++++++++------
 2 files changed, 153 insertions(+), 26 deletions(-)
include/traceeval-hist.h +0 -0
index 412efdbe8681..4baed4237787 100644

<>

include/traceeval-hist.h line 16

Quoted 5 lines
include/traceeval-hist.h +1 -0
@@ -12,6 +13,14 @@
 
 #include <traceeval-hist.h>
 
+#define offset_of(type, field) (&(((type *)(NULL))->field))

This currently returns a pointer type, but the kernel implementation(s) have this cast back to a size_t, which makes sense I think.

https://elixir.bootlin.com/linux/latest/source/tools/include/nolibc/types.h#L220

include/traceeval-hist.h line 26

Quoted 10 lines
include/traceeval-hist.h +7 -0
+#define container_of(ptr, type, field) \
+	(type *)((void *)(ptr) - (void *)offset_of(type, field))
+
+#define HASH_BITS 10	/* Start with 1K of buckets */
+#define HASH_SIZE(bits)	(1 << (bits))
+#define HASH_MASK(bits)	(HASH_SIZE(bits) - 1)
+
 /*
  * Compare two integers of variable length.
  *

<>

include/traceeval-hist.h static void clean_entry(struct entry *entry, struct traceeval *teval) · line 407

Quoted 3 lines
include/traceeval-hist.h +0 -0
@@ -365,16 +406,19 @@ static void clean_entry(struct entry *entry, struct traceeval *teval)
  */
 static void hist_table_release(struct traceeval *teval)

This probably wants to be 'hash_table_release()' now since 'hist_table' is gone.

Quoted 21 lines
include/traceeval-hist.h +7 -4
 {
-	struct hist_table *hist = teval->hist;
+	struct hash_table *hist = teval->hist;
 
 	if (!hist)
 		return;
 
-	for (size_t i = 0; i < hist->nr_entries; i++) {
-		clean_entry(hist->map + i, teval);
+	for (size_t i = 0; i < HASH_SIZE(hist->bits); i++) {
+		if (!hist->hash[i])
+			continue;
+
+		free_entries(teval, hist->hash[i]);
 	}
 
-	free(hist->map);
+	free(hist->hash);
 	free(hist);
 	teval->hist = NULL;
 }
#13 lore raw

Re: [PATCH v2 07/17] libtraceeval: Convert hist array into a hash table

Quoted 19 lines · On Tue, 15 Aug 2023 12:44:21 -0600 Ross Zwisler <[email protected]> wrote:

include/traceeval-hist.h

Quoted 15 lines · Steven Rostedt

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

The lookups need to be extremely fast. Instead of doing a linear search across all entries (which could be thousands), do a hash lookup instead.

---
 include/traceeval-hist.h |   7 ++
 src/histograms.c         | 172 +++++++++++++++++++++++++++++++++------
 2 files changed, 153 insertions(+), 26 deletions(-)
include/traceeval-hist.h +0 -0
index 412efdbe8681..4baed4237787 100644

<>

Quoted 2 lines
include/traceeval-hist.h +0 -0
@@ -12,6 +13,14 @@
 
Quoted 8 lines

include/traceeval-hist.h line 16

Quoted 3 lines
include/traceeval-hist.h +1 -0
 #include <traceeval-hist.h>
 
+#define offset_of(type, field) (&(((type *)(NULL))->field))  

This currently returns a pointer type, but the kernel implementation(s) have this cast back to a size_t, which makes sense I think.

https://elixir.bootlin.com/linux/latest/source/tools/include/nolibc/types.h#L220

As this is under an MIT license, I created this without looking at the GPL version.

It's really too small and common to be licensed, but still. I'll make the update.

Quoted 12 lines

include/traceeval-hist.h line 26

Quoted 11 lines
include/traceeval-hist.h +7 -0
+#define container_of(ptr, type, field) \
+	(type *)((void *)(ptr) - (void *)offset_of(type, field))
+
+#define HASH_BITS 10	/* Start with 1K of buckets */
+#define HASH_SIZE(bits)	(1 << (bits))
+#define HASH_MASK(bits)	(HASH_SIZE(bits) - 1)
+
 /*
  * Compare two integers of variable length.
  *  

<>

Quoted 6 lines

include/traceeval-hist.h static void clean_entry(struct entry *entry, struct traceeval *teval) · line 407

Quoted 3 lines
include/traceeval-hist.h +0 -0
@@ -365,16 +406,19 @@ static void clean_entry(struct entry *entry, struct traceeval *teval)
  */
 static void hist_table_release(struct traceeval *teval)  

This probably wants to be 'hash_table_release()' now since 'hist_table' is gone.

I actually purposely kept the "hist" name (teval->hist) for this local file.

Just because that's what Stevie called it ;-)

-- Steve

Quoted 22 lines
Quoted 22 lines
include/traceeval-hist.h +7 -4
 {
-	struct hist_table *hist = teval->hist;
+	struct hash_table *hist = teval->hist;
 
 	if (!hist)
 		return;
 
-	for (size_t i = 0; i < hist->nr_entries; i++) {
-		clean_entry(hist->map + i, teval);
+	for (size_t i = 0; i < HASH_SIZE(hist->bits); i++) {
+		if (!hist->hash[i])
+			continue;
+
+		free_entries(teval, hist->hash[i]);
 	}
 
-	free(hist->map);
+	free(hist->hash);
 	free(hist);
 	teval->hist = NULL;
 }  
#14 lore raw

PATCH v2 8/17 libtraceeval histograms: Move hash functions into their own file

In reply to Steven Rostedt

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

Move the hash functions into their own file so that it does not clutter the histogram.c file. Functionality should be the same, but some code restructuring was done.

To keep the hash abstract, helper functions were introduced:

hash_iter_start() - to start iterating over all items in the hash hash_iter_next() - to get the next item in the hash

hash_iter_bucket() - to start iterating over a single bucket in the hash hash_iter_bucket_next() - to get the next item in the bucket

hash_nr_items() - to get the number of items in the hash

Also implemented hash_remove() to remove an item from the hash.

Also created a eval-local.h header file to store the prototypes of the local functions as well as moved the structures there too.

---
 src/Makefile     |   1 +
 src/eval-local.h | 103 ++++++++++++++++++++++++++++++++++++++
 src/hash.c       | 119 ++++++++++++++++++++++++++++++++++++++++++++
 src/histograms.c | 126 +++++++----------------------------------------
 4 files changed, 240 insertions(+), 109 deletions(-)
 create mode 100644 src/eval-local.h
 create mode 100644 src/hash.c
src/Makefile +1 -0
index b32a3891333d..4b7b3051a8b2 100644
@@ -5,6 +5,7 @@ include $(src)/scripts/utils.mk
 OBJS =
 OBJS += trace-analysis.o
 OBJS += histograms.o
+OBJS += hash.o
 
 OBJS := $(OBJS:%.o=$(bdir)/%.o)
 
src/eval-local.h new file +103 -0

103 lines not on this page · raw message

src/hash.c new file +119 -0

119 lines not on this page · raw message

src/histograms.c +17 -109

198 lines not on this page · raw message

Signature · 1 line
2.40.1
#15 lore raw

Re: [PATCH v2 08/17] libtraceeval histograms: Move hash functions into their own file

Quoted 22 lines · Steven Rostedt

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

Move the hash functions into their own file so that it does not clutter the histogram.c file. Functionality should be the same, but some code restructuring was done.

To keep the hash abstract, helper functions were introduced:

hash_iter_start() - to start iterating over all items in the hash hash_iter_next() - to get the next item in the hash

hash_iter_bucket() - to start iterating over a single bucket in the hash hash_iter_bucket_next() - to get the next item in the bucket

hash_nr_items() - to get the number of items in the hash

Also implemented hash_remove() to remove an item from the hash.

Also created a eval-local.h header file to store the prototypes of the local functions as well as moved the structures there too.

Quoted 8 lines
---
 src/Makefile     |   1 +
 src/eval-local.h | 103 ++++++++++++++++++++++++++++++++++++++
 src/hash.c       | 119 ++++++++++++++++++++++++++++++++++++++++++++
 src/histograms.c | 126 +++++++----------------------------------------
 4 files changed, 240 insertions(+), 109 deletions(-)
 create mode 100644 src/eval-local.h
 create mode 100644 src/hash.c

<>

Quoted 34 lines
src/hash.c new file +28 -0
new file mode 100644
index 000000000000..e4f2a983d39c
@@ -0,0 +1,119 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * libtraceeval hashtable interface implementation.
+ *
+ * Copyright (C) 2023 Google Inc, Steven Rostedt <[email protected]>
+ */
+
+#include <traceeval-hist.h>
+
+#include "eval-local.h"
+
+__hidden struct hash_table *hash_alloc(void)
+{
+	struct hash_table *hash;
+
+	hash = calloc(1, sizeof(*hash));
+	if (!hash)
+		return NULL;
+
+	hash->bits = HASH_BITS;
+	hash->hash = calloc(HASH_SIZE(hash->bits), sizeof(*hash->hash));
+	if (!hash->hash) {
+		free(hash);
+		hash = NULL;
+	}
+
+	return hash;
+}

src/hash.c line 38

Quoted 10 lines
src/hash.c new file +10 -0
+
+__hidden void hash_free(struct hash_table *hash)
+{
+	free(hash->hash);
+	free(hash);
+}
+
+__hidden void hash_add(struct hash_table *hash, struct hash_item *item, unsigned key)
+{
+	key &= HASH_MASK(hash->bits);

key should already be masked to HASH_MASK(hash->bits) via make_hash(). If those bits are set, we have a bug somewhere.

I think it's better to check to see if those bits are set and bail out loudly with an error.

Quoted 21 lines
src/hash.c new file +21 -0
+
+	item->next = hash->hash[key];
+	hash->hash[key] = item;
+	item->key = key;
+
+	hash->nr_items++;
+}
+
+__hidden int hash_remove(struct hash_table *hash, struct hash_item *item)
+{
+	struct hash_item **parent;
+
+	for (parent = &hash->hash[item->key]; *parent; parent = &(*parent)->next) {
+		if (*parent == item) {
+			*parent = item->next;
+			hash->nr_items--;
+			return 1;
+		}
+	}
+	return 0;
+}

src/hash.c line 69

Quoted 10 lines
src/hash.c new file +10 -0
+
+__hidden struct hash_iter *hash_iter_start(struct hash_table *hash)
+{
+	struct hash_iter *iter = &hash->iter;
+	size_t i;
+
+	for (i = 0; i < HASH_SIZE(hash->bits); i++) {
+		if (!hash->hash[i])
+			continue;
+		iter->next_item = hash->hash[i];

I think we need to break here after we've found a populated bucket and set iter->next_item. Right now this works only if we have a single populated bucket, because we'll set iter->next_item once and then keep iterating until i == HASH_SIZE(hash->bits).

This will mean that iter->current_bucket will always == HASH_SIZE(hash->bits), but we have a bug in hash_iter_next() that meant we weren't returning early with NULL when we hit this condition.

src/hash.c line 80

Quoted 11 lines
src/hash.c new file +11 -0
+	}
+	iter->current_bucket = i;
+	return iter;
+}
+
+__hidden struct hash_item *hash_iter_next(struct hash_iter *iter)
+{
+	struct hash_table *hash = container_of(iter, struct hash_table, iter);
+	struct hash_item *item;
+
+	if (iter->current_bucket > HASH_SIZE(hash->bits))
	if (iter->current_bucket >= HASH_SIZE(hash->bits))

Right now we're missing the exit case where iter->current_bucket == HASH_SIZE(hash->bits), which means we've run out of buckets with entries.

src/hash.c line 92

Quoted 12 lines
src/hash.c new file +12 -0
+		return NULL;
+
+	item = iter->next_item;
+
+	iter->next_item = item->next;
+	if (!iter->next_item) {
+		size_t i;
+
+		for (i = iter->current_bucket + 1; i < HASH_SIZE(hash->bits); i++) {
+			if (!hash->hash[i])
+				continue;
+			iter->next_item = hash->hash[i];

As in hash_iter_start(), we need to break when we find the next non-empty bucket and set iter->next_item. This will let us set iter->current_bucket correctly as well.

src/hash.c line 103

Quoted 11 lines
src/hash.c new file +11 -0
+		}
+		iter->current_bucket = i;
+	}
+	return item;
+}
+
+__hidden struct hash_iter *hash_iter_bucket(struct hash_table *hash, unsigned key)
+{
+	struct hash_iter *iter = &hash->iter;
+
+	key &= HASH_MASK(hash->bits);

As with hash_add(), 'key' should already be masked and I think it'd be better to error out loudly if upper bits in 'key' are unexpectedly set.

Quoted 7 lines
src/hash.c new file +7 -0
+
+	iter->current_bucket = key;
+	iter->next_item = hash->hash[key];
+
+	return iter;
+}
+
#16 lore raw

Re: [PATCH v2 08/17] libtraceeval histograms: Move hash functions into their own file

Quoted 49 lines · On Tue, 15 Aug 2023 13:31:56 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 35 lines
src/hash.c new file +28 -0
new file mode 100644
index 000000000000..e4f2a983d39c
@@ -0,0 +1,119 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * libtraceeval hashtable interface implementation.
+ *
+ * Copyright (C) 2023 Google Inc, Steven Rostedt <[email protected]>
+ */
+
+#include <traceeval-hist.h>
+
+#include "eval-local.h"
+
+__hidden struct hash_table *hash_alloc(void)
+{
+	struct hash_table *hash;
+
+	hash = calloc(1, sizeof(*hash));
+	if (!hash)
+		return NULL;
+
+	hash->bits = HASH_BITS;
+	hash->hash = calloc(HASH_SIZE(hash->bits), sizeof(*hash->hash));
+	if (!hash->hash) {
+		free(hash);
+		hash = NULL;
+	}
+
+	return hash;
+}

src/hash.c line 38

Quoted 10 lines
src/hash.c new file +10 -0
+
+__hidden void hash_free(struct hash_table *hash)
+{
+	free(hash->hash);
+	free(hash);
+}
+
+__hidden void hash_add(struct hash_table *hash, struct hash_item *item, unsigned key)
+{
+	key &= HASH_MASK(hash->bits);  

key should already be masked to HASH_MASK(hash->bits) via make_hash(). If those bits are set, we have a bug somewhere.

Quoted 2 lines

I think it's better to check to see if those bits are set and bail out loudly with an error.

I debated a bit about where to do the mask. I didn't want to put a dependency that the bits were already masked, so I just did it twice. It's a very fast operation.

I also don't want to make the dependency that key can only come from mask_hash(). As it's critical that key is masked here (or we have a array overflow), I just kept it in both places.

I can comment that here.

Quoted 38 lines
Quoted 22 lines
src/hash.c new file +21 -0
+
+	item->next = hash->hash[key];
+	hash->hash[key] = item;
+	item->key = key;
+
+	hash->nr_items++;
+}
+
+__hidden int hash_remove(struct hash_table *hash, struct hash_item *item)
+{
+	struct hash_item **parent;
+
+	for (parent = &hash->hash[item->key]; *parent; parent = &(*parent)->next) {
+		if (*parent == item) {
+			*parent = item->next;
+			hash->nr_items--;
+			return 1;
+		}
+	}
+	return 0;
+}

src/hash.c line 69

Quoted 10 lines
src/hash.c new file +10 -0
+
+__hidden struct hash_iter *hash_iter_start(struct hash_table *hash)
+{
+	struct hash_iter *iter = &hash->iter;
+	size_t i;
+
+	for (i = 0; i < HASH_SIZE(hash->bits); i++) {
+		if (!hash->hash[i])
+			continue;
+		iter->next_item = hash->hash[i];  

I think we need to break here after we've found a populated bucket and set iter->next_item. Right now this works only if we have a single populated bucket, because we'll set iter->next_item once and then keep iterating until i == HASH_SIZE(hash->bits).

Quoted 3 lines

This will mean that iter->current_bucket will always == HASH_SIZE(hash->bits), but we have a bug in hash_iter_next() that meant we weren't returning early with NULL when we hit this condition.

Nice catch, but I found this bug during my tests but fixed it in patch 11. :-p

I'll move that change here, as that's the proper place it belongs.

Quoted 16 lines

src/hash.c line 80

Quoted 13 lines
src/hash.c new file +8 -0
+	}
+	iter->current_bucket = i;
+	return iter;
+}
+
+__hidden struct hash_item *hash_iter_next(struct hash_iter *iter)
+{
+	struct hash_table *hash = container_of(iter, struct

hash_table, iter);

src/hash.c new file +3 -0
+	struct hash_item *item;
+
+	if (iter->current_bucket > HASH_SIZE(hash->bits))  
	if (iter->current_bucket >= HASH_SIZE(hash->bits))
Quoted 3 lines

Right now we're missing the exit case where iter->current_bucket == HASH_SIZE(hash->bits), which means we've run out of buckets with entries.

You are correct that it should be fixed, but it just happens that the rest of the logic will still end up returning NULL when this happens. But the above was suppose to be a "shortcut" that never gets hit (or why I fixed it in patch 11)!

Quoted 4 lines

src/hash.c line 83

Quoted 4 lines
src/hash.c new file +3 -0
+		return NULL;
+
+	item = iter->next_item;

In patch 11, I added:

	if (!item)
		return NULL;

That should be here too (along with the above off by one fix).

Quoted 6 lines
Quoted 6 lines
src/hash.c new file +6 -0
+
+	iter->next_item = item->next;
+	if (!iter->next_item) {
+		size_t i;
+
+		for (i = iter->current_bucket + 1; i <
Quoted 8 lines

src/hash.c line 69

Quoted 4 lines

HASH_SIZE(hash->bits); i++) {

src/hash.c new file +3 -0
+			if (!hash->hash[i])
+				continue;
+			iter->next_item = hash->hash[i];  

As in hash_iter_start(), we need to break when we find the next non-empty bucket and set iter->next_item. This will let us set iter->current_bucket correctly as well.

And this too was fixed in patch 11.

Quoted 15 lines

src/hash.c line 103

Quoted 12 lines
src/hash.c new file +7 -0
+		}
+		iter->current_bucket = i;
+	}
+	return item;
+}
+
+__hidden struct hash_iter *hash_iter_bucket(struct hash_table *hash,

unsigned key) +{

src/hash.c new file +3 -0
+	struct hash_iter *iter = &hash->iter;
+
+	key &= HASH_MASK(hash->bits);  

As with hash_add(), 'key' should already be masked and I think it'd be better to error out loudly if upper bits in 'key' are unexpectedly set.

Again, I don't want to add a dependency of where key was created. It may be used for other purposes (and the upper bits may hold info later). I think it's just more robust to keep the extra masks. Again, they are very fast operations.

Thanks Ross,

-- Steve

Quoted 8 lines
Quoted 8 lines
src/hash.c new file +7 -0
+
+	iter->current_bucket = key;
+	iter->next_item = hash->hash[key];
+
+	return iter;
+}
+  
#17 lore raw

Re: [PATCH v2 08/17] libtraceeval histograms: Move hash functions into their own file

Quoted 14 lines · On Tue, 15 Aug 2023 16:23:33 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 9 lines

src/hash.c line 38

Quoted 3 lines
src/hash.c new file +3 -0
+__hidden void hash_add(struct hash_table *hash, struct hash_item *item, unsigned key)
+{
+	key &= HASH_MASK(hash->bits);    

key should already be masked to HASH_MASK(hash->bits) via make_hash(). If those bits are set, we have a bug somewhere.

I think it's better to check to see if those bits are set and bail out loudly with an error.

I debated a bit about where to do the mask. I didn't want to put a dependency that the bits were already masked, so I just did it twice. It's a very fast operation.

Quoted 5 lines

I also don't want to make the dependency that key can only come from mask_hash(). As it's critical that key is masked here (or we have a array overflow), I just kept it in both places.

I can comment that here.

Thinking about this more, I'm not even sure I want HASH_MASK() in make_hash(). In fact, I think I'm going to remove it. The hash size is really internal to the hash code, and to maintain a proper abstraction, other code should not assume how big the bucket array is.

-- Steve

#18 lore raw

PATCH v2 9/17 libtraceeval histogram: Label and check keys and values

In reply to Steven Rostedt

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

When initializing the traceeval descriptor, mark each key and value as their type via the flags (keys get TRACEEVAL_FL_KEY and values get TRACEEVAL_FL_VALUE) as well as adding the index of that key/value type of the key/value data it represents. This will be used by the iterators for sorting. The iterator will point to the key/value type and use that information to know which key/value data to sort with.

The keys and values passed in will also be updated to have their flags match the proper key/value type as well as the index. This will be useful for traceeval_stat() that will take the type as a parameter, and use it to figure out fast where the data is that is to be looked up.

All pointer and dynamic types in keys must have both a cmp and hash function defined, otherwise they cannot be indexed or hashed. Add a check to make sure all key types of pointer and dynamic have those functions.

---
 include/traceeval-hist.h | 11 +++++---
 src/histograms.c         | 60 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 64 insertions(+), 7 deletions(-)
include/traceeval-hist.h +7 -4
index 4baed4237787..1c02f3039809 100644
@@ -32,8 +32,10 @@ enum traceeval_data_type {
 
 /* Statistics specification flags */
 enum traceeval_flags {
-	TRACEEVAL_FL_SIGNED		= (1 << 0),
-	TRACEEVAL_FL_TIMESTAMP		= (1 << 1),
+	TRACEEVAL_FL_KEY		= (1 << 0),
+	TRACEEVAL_FL_VALUE		= (1 << 1),
+	TRACEEVAL_FL_SIGNED		= (1 << 2),
+	TRACEEVAL_FL_TIMESTAMP		= (1 << 3),
 };
 
 /*
@@ -120,6 +122,7 @@ struct traceeval_type {
 	char				*name;
 	enum traceeval_data_type	type;
 	size_t				flags;
+	size_t				index;
 	size_t				id;
 	traceeval_data_release_fn	release;
 	traceeval_data_cmp_fn		cmp;
@@ -142,8 +145,8 @@ struct traceeval;
 
 /* Histogram interfaces */
 
-struct traceeval *traceeval_init(const struct traceeval_type *keys,
-				 const struct traceeval_type *vals);
+struct traceeval *traceeval_init(struct traceeval_type *keys,
+				 struct traceeval_type *vals);
 
 void traceeval_release(struct traceeval *teval);
 
src/histograms.c +57 -3

87 lines not on this page · raw message

Signature · 1 line
2.40.1
#19 lore raw

Re: [PATCH v2 09/17] libtraceeval histogram: Label and check keys and values

include/traceeval-hist.h

Quoted 28 lines · Steven Rostedt

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

When initializing the traceeval descriptor, mark each key and value as their type via the flags (keys get TRACEEVAL_FL_KEY and values get TRACEEVAL_FL_VALUE) as well as adding the index of that key/value type of the key/value data it represents. This will be used by the iterators for sorting. The iterator will point to the key/value type and use that information to know which key/value data to sort with.

The keys and values passed in will also be updated to have their flags match the proper key/value type as well as the index. This will be useful for traceeval_stat() that will take the type as a parameter, and use it to figure out fast where the data is that is to be looked up.

All pointer and dynamic types in keys must have both a cmp and hash function defined, otherwise they cannot be indexed or hashed. Add a check to make sure all key types of pointer and dynamic have those functions.

---
 include/traceeval-hist.h | 11 +++++---
 src/histograms.c         | 60 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 64 insertions(+), 7 deletions(-)
include/traceeval-hist.h +0 -0
index 4baed4237787..1c02f3039809 100644

<>

include/traceeval-hist.h struct traceeval *traceeval_init(const struct traceeval_type *keys, · line 302

Quoted 12 lines
include/traceeval-hist.h +8 -0
@@ -248,6 +292,16 @@ struct traceeval *traceeval_init(const struct traceeval_type *keys,
 		goto fail;
 	}
 
+	ret = check_keys(keys);
+	if (ret < 0)
+		goto fail;
+
+	if (vals) {
+		ret = check_vals(vals);
+		if (ret < 0)
+			goto fail;

These two goto statements should both be to fail_release so we clean up 'teval', else we could do this check above the alloc.

Quoted 8 lines
include/traceeval-hist.h +2 -0
+	}
+
 	/* alloc key types */
 	teval->nr_key_types = type_alloc(keys, &teval->key_types);
 	if (teval->nr_key_types <= 0) {
Signature · 1 line
2.40.1
#20 lore raw

Re: [PATCH v2 09/17] libtraceeval histogram: Label and check keys and values

Quoted 34 lines · On Tue, 15 Aug 2023 13:48:09 -0600 Ross Zwisler <[email protected]> wrote:

include/traceeval-hist.h

Quoted 28 lines · Steven Rostedt

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

When initializing the traceeval descriptor, mark each key and value as their type via the flags (keys get TRACEEVAL_FL_KEY and values get TRACEEVAL_FL_VALUE) as well as adding the index of that key/value type of the key/value data it represents. This will be used by the iterators for sorting. The iterator will point to the key/value type and use that information to know which key/value data to sort with.

The keys and values passed in will also be updated to have their flags match the proper key/value type as well as the index. This will be useful for traceeval_stat() that will take the type as a parameter, and use it to figure out fast where the data is that is to be looked up.

All pointer and dynamic types in keys must have both a cmp and hash function defined, otherwise they cannot be indexed or hashed. Add a check to make sure all key types of pointer and dynamic have those functions.

---
 include/traceeval-hist.h | 11 +++++---
 src/histograms.c         | 60 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 64 insertions(+), 7 deletions(-)
include/traceeval-hist.h +0 -0
index 4baed4237787..1c02f3039809 100644

<>

Quoted 4 lines
include/traceeval-hist.h +0 -0
@@ -248,6 +292,16 @@ struct traceeval *traceeval_init(const struct traceeval_type *keys,
 		goto fail;
 	}
 
Quoted 11 lines

include/traceeval-hist.h struct traceeval *traceeval_init(const struct traceeval_type *keys, · line 302

Quoted 8 lines
include/traceeval-hist.h +8 -0
+	ret = check_keys(keys);
+	if (ret < 0)
+		goto fail;
+
+	if (vals) {
+		ret = check_vals(vals);
+		if (ret < 0)
+			goto fail;  

These two goto statements should both be to fail_release so we clean up 'teval', else we could do this check above the alloc.

Nice catch. Will fix.

-- Steve

Quoted 9 lines
Quoted 9 lines
include/traceeval-hist.h +2 -0
+	}
+
 	/* alloc key types */
 	teval->nr_key_types = type_alloc(keys, &teval->key_types);
 	if (teval->nr_key_types <= 0) {
Signature · 1 line
2.40.1
#21 lore raw

PATCH v2 10/17 libtraceeval histogram: Add updating of stats

In reply to Steven Rostedt

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

Whenever an entry is added that already exists (overwriting the values) keep track of the stats for the number values (max, min, total, count).

Also move the stat structure out of the public view. We may want to modify this structure in the future, and so it should not become an API.

Add accessor functions to get to the stat values.

Add traceeval_stat() to acquire a stat handle from a specific key for a specific value.

---
 include/traceeval-hist.h |  17 ++--
 src/eval-local.h         |   9 ++
 src/histograms.c         | 182 +++++++++++++++++++++++++++++++++++----
 3 files changed, 183 insertions(+), 25 deletions(-)
include/traceeval-hist.h +10 -7
index 1c02f3039809..d061a4532b06 100644
@@ -130,13 +130,7 @@ struct traceeval_type {
 };
 
 /* Statistics about a given entry element */
-struct traceeval_stat {
-	unsigned long long	max;
-	unsigned long long	min;
-	unsigned long long	total;
-	unsigned long long	avg;
-	unsigned long long	std;
-};
+struct traceeval_stat;
 
 /* Iterator over aggregated data */
 struct traceeval_iterator;
@@ -160,4 +154,13 @@ int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
 void traceeval_results_release(struct traceeval *teval,
 			       union traceeval_data *results);
 
+struct traceeval_stat *traceeval_stat(struct traceeval *teval,
+				      const union traceeval_data *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);
+unsigned long long traceeval_stat_total(struct traceeval_stat *stat);
+unsigned long long traceeval_stat_count(struct traceeval_stat *stat);
+
 #endif /* __LIBTRACEEVAL_HIST_H__ */
src/eval-local.h +9 -0
index 820d7ad096e8..190b19db14d2 100644
@@ -45,11 +45,20 @@ struct hash_table {
 	struct hash_iter	iter;
 };
 
+struct traceeval_stat {
+	unsigned long long	max;
+	unsigned long long	min;
+	unsigned long long	total;
+	unsigned long long	std;
+	size_t			count;
+};
+
 /* A key-value pair */
 struct entry {
 	struct hash_item	hash;
 	union traceeval_data	*keys;
 	union traceeval_data	*vals;
+	struct traceeval_stat	*val_stats;
 };
 
 /* Histogram */
src/histograms.c +164 -18

246 lines not on this page · raw message

Signature · 1 line
2.40.1
#22 lore raw

Re: [PATCH v2 10/17] libtraceeval histogram: Add updating of stats

Quoted 65 lines · Steven Rostedt

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

Whenever an entry is added that already exists (overwriting the values) keep track of the stats for the number values (max, min, total, count).

Also move the stat structure out of the public view. We may want to modify this structure in the future, and so it should not become an API.

Add accessor functions to get to the stat values.

Add traceeval_stat() to acquire a stat handle from a specific key for a specific value.

---
 include/traceeval-hist.h |  17 ++--
 src/eval-local.h         |   9 ++
 src/histograms.c         | 182 +++++++++++++++++++++++++++++++++++----
 3 files changed, 183 insertions(+), 25 deletions(-)
include/traceeval-hist.h +10 -7
index 1c02f3039809..d061a4532b06 100644
@@ -130,13 +130,7 @@ struct traceeval_type {
 };
 
 /* Statistics about a given entry element */
-struct traceeval_stat {
-	unsigned long long	max;
-	unsigned long long	min;
-	unsigned long long	total;
-	unsigned long long	avg;
-	unsigned long long	std;
-};
+struct traceeval_stat;
 
 /* Iterator over aggregated data */
 struct traceeval_iterator;
@@ -160,4 +154,13 @@ int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
 void traceeval_results_release(struct traceeval *teval,
 			       union traceeval_data *results);
 
+struct traceeval_stat *traceeval_stat(struct traceeval *teval,
+				      const union traceeval_data *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);
+unsigned long long traceeval_stat_total(struct traceeval_stat *stat);
+unsigned long long traceeval_stat_count(struct traceeval_stat *stat);
+
 #endif /* __LIBTRACEEVAL_HIST_H__ */
src/eval-local.h +4 -0
index 820d7ad096e8..190b19db14d2 100644
@@ -45,11 +45,20 @@ struct hash_table {
 	struct hash_iter	iter;
 };
 
+struct traceeval_stat {
+	unsigned long long	max;
+	unsigned long long	min;
+	unsigned long long	total;

src/eval-local.h struct hash_table { · line 61

Quoted 10 lines
src/eval-local.h +5 -0
+	unsigned long long	std;
+	size_t			count;
+};
+
 /* A key-value pair */
 struct entry {
 	struct hash_item	hash;
 	union traceeval_data	*keys;
 	union traceeval_data	*vals;
+	struct traceeval_stat	*val_stats;

I'm confused about why 'val_stats' is in 'struct entry', instead of in 'struct traceeval'?

I would think that we'd want to keep our stats on a per-histogram basis, where we have an array of 'struct traceeval_stat' structs with the same size as our 'val_types' array, so that for a given compound value, say

{ TRACEVAL_TYPE_NUMBER_64, TRACEEVAL_TYPE_NUMBER_16, TRACEEVAL_NUMBER_8}

we'd have stats for each of these entries, like min, max, average, etc.?

With the stats associated with each entry, I don't see how we keep all the stats for all the entries up to date as we do insertions & removals.

#23 lore raw

Re: [PATCH v2 10/17] libtraceeval histogram: Add updating of stats

Quoted 84 lines · On Tue, 15 Aug 2023 14:25:54 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 65 lines · Steven Rostedt

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

Whenever an entry is added that already exists (overwriting the values) keep track of the stats for the number values (max, min, total, count).

Also move the stat structure out of the public view. We may want to modify this structure in the future, and so it should not become an API.

Add accessor functions to get to the stat values.

Add traceeval_stat() to acquire a stat handle from a specific key for a specific value.

---
 include/traceeval-hist.h |  17 ++--
 src/eval-local.h         |   9 ++
 src/histograms.c         | 182 +++++++++++++++++++++++++++++++++++----
 3 files changed, 183 insertions(+), 25 deletions(-)
include/traceeval-hist.h +10 -7
index 1c02f3039809..d061a4532b06 100644
@@ -130,13 +130,7 @@ struct traceeval_type {
 };
 
 /* Statistics about a given entry element */
-struct traceeval_stat {
-	unsigned long long	max;
-	unsigned long long	min;
-	unsigned long long	total;
-	unsigned long long	avg;
-	unsigned long long	std;
-};
+struct traceeval_stat;
 
 /* Iterator over aggregated data */
 struct traceeval_iterator;
@@ -160,4 +154,13 @@ int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
 void traceeval_results_release(struct traceeval *teval,
 			       union traceeval_data *results);
 
+struct traceeval_stat *traceeval_stat(struct traceeval *teval,
+				      const union traceeval_data *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);
+unsigned long long traceeval_stat_total(struct traceeval_stat *stat);
+unsigned long long traceeval_stat_count(struct traceeval_stat *stat);
+
 #endif /* __LIBTRACEEVAL_HIST_H__ */
src/eval-local.h +4 -0
index 820d7ad096e8..190b19db14d2 100644
@@ -45,11 +45,20 @@ struct hash_table {
 	struct hash_iter	iter;
 };
 
+struct traceeval_stat {
+	unsigned long long	max;
+	unsigned long long	min;
+	unsigned long long	total;

src/eval-local.h struct hash_table { · line 61

Quoted 10 lines
src/eval-local.h +5 -0
+	unsigned long long	std;
+	size_t			count;
+};
+
 /* A key-value pair */
 struct entry {
 	struct hash_item	hash;
 	union traceeval_data	*keys;
 	union traceeval_data	*vals;
+	struct traceeval_stat	*val_stats;  

I'm confused about why 'val_stats' is in 'struct entry', instead of in 'struct traceeval'?

I would think that we'd want to keep our stats on a per-histogram basis, where we have an array of 'struct traceeval_stat' structs with the same size as our 'val_types' array, so that for a given compound value, say

Quoted 6 lines

{ TRACEVAL_TYPE_NUMBER_64, TRACEEVAL_TYPE_NUMBER_16, TRACEEVAL_NUMBER_8}

we'd have stats for each of these entries, like min, max, average, etc.?

With the stats associated with each entry, I don't see how we keep all the stats for all the entries up to date as we do insertions & removals.

Because the stats are for each key, not the total of the traceeval. If you look at the code in task-eval, I want to know the max,min,total,count,avg of the sleep time for a task when it is blocked, when it is sleeping, when it is running, etc:

Task: migrate

      Total run time (us):       4645364
      Total blocked time (us):         0
      Total preempt time (us):     98540
      Total sleep time (us):      115559

    thread id: 1884
      Total run time (us):           808

    thread id: 1885
      Total run time (us):       2920763
      Total preempt time (us):    245570
      Total sleep time (us):   224289277

    thread id: 1886
      Total run time (us):       3601375
      Total preempt time (us):   1308185
      Total sleep time (us):    12692630

    thread id: 1887
      Total run time (us):       3740300
      Total preempt time (us):   2620102
      Total sleep time (us):     4806722

That "Task: migrate" is just one entry in the traceeval, the threads are entries in the nested traceeval that is saved per task.

Actually, task-eval is only looking at the total time, but I could easily add the max/min/avg times too. If I were to write a latency program, and was tracing the wake up latency of a task, you probably want these stats for each key, so I can see the max latency per task, and not just the total for all tasks.

-- Steve

#24 lore raw

PATCH v2 11/17 libtraceeval histogram: Add iterator APIs

In reply to Steven Rostedt

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

Provide an interface for the application to iterate over all the entries in the traceeval histogram.

traceeval_iterator_get() - acquire an iterator for the given traceeval traceveal_iterator_put() - release the iterator traceeval_iterator_sort() - sort the iterator for a given key/value traceeval_iterator_next() - return the keys of the next entry in the
traceeval

---
 include/traceeval-hist.h |   7 ++
 src/eval-local.h         |  11 ++
 src/hash.c               |   4 +
 src/histograms.c         | 255 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 276 insertions(+), 1 deletion(-)
include/traceeval-hist.h +7 -0
index d061a4532b06..2fc81a64e324 100644
@@ -163,4 +163,11 @@ unsigned long long traceeval_stat_min(struct traceeval_stat *stat);
 unsigned long long traceeval_stat_total(struct traceeval_stat *stat);
 unsigned long long traceeval_stat_count(struct traceeval_stat *stat);
 
+struct traceeval_iterator *traceeval_iterator_get(struct traceeval *teval);
+void traceeval_iterator_put(struct traceeval_iterator *iter);
+int traceeval_iterator_sort(struct traceeval_iterator *iter, const char *sort_field,
+			    int level, bool ascending);
+int traceeval_iterator_next(struct traceeval_iterator *iter,
+			    const union traceeval_data **keys);
+
 #endif /* __LIBTRACEEVAL_HIST_H__ */
src/eval-local.h +11 -0
index 190b19db14d2..c3ea920ed2e8 100644
@@ -70,6 +70,17 @@ struct traceeval {
 	size_t				nr_val_types;
 };
 
+struct traceeval_iterator {
+	struct traceeval		*teval;
+	struct entry			**entries;
+	struct traceeval_type		**sort;
+	bool				*direction;
+	size_t				nr_entries;
+	size_t				nr_sort;
+	size_t				next;
+	bool				needs_sort;
+};
+
 extern struct hash_table *hash_alloc(void);
 extern void hash_free(struct hash_table *hash);
 extern void hash_add(struct hash_table *hash, struct hash_item *item, unsigned key);
src/hash.c +4 -0
index e4f2a983d39c..221145efcbb9 100644
@@ -67,6 +67,7 @@ __hidden struct hash_iter *hash_iter_start(struct hash_table *hash)
 		if (!hash->hash[i])
 			continue;
 		iter->next_item = hash->hash[i];
+		break;
 	}
 	iter->current_bucket = i;
 	return iter;
@@ -81,6 +82,8 @@ __hidden struct hash_item *hash_iter_next(struct hash_iter *iter)
 		return NULL;
 
 	item = iter->next_item;
+	if (!item)
+		return NULL;
 
 	iter->next_item = item->next;
 	if (!iter->next_item) {
@@ -90,6 +93,7 @@ __hidden struct hash_item *hash_iter_next(struct hash_iter *iter)
 			if (!hash->hash[i])
 				continue;
 			iter->next_item = hash->hash[i];
+			break;
 		}
 		iter->current_bucket = i;
 	}
src/histograms.c +254 -1

264 lines not on this page · raw message

Signature · 1 line
2.40.1
#25 lore raw

Re: [PATCH v2 11/17] libtraceeval histogram: Add iterator APIs

Quoted 11 lines · Steven Rostedt

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

Provide an interface for the application to iterate over all the entries in the traceeval histogram.

traceeval_iterator_get() - acquire an iterator for the given traceeval traceveal_iterator_put() - release the iterator traceeval_iterator_sort() - sort the iterator for a given key/value traceeval_iterator_next() - return the keys of the next entry in the
traceeval

Quoted 8 lines
---
 include/traceeval-hist.h |   7 ++
 src/eval-local.h         |  11 ++
 src/hash.c               |   4 +
 src/histograms.c         | 255 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 276 insertions(+), 1 deletion(-)

<>

Quoted 21 lines
src/histograms.c +5 -1
index 9a8ec4d85301..2b823ad5c26d 100644
@@ -37,7 +37,7 @@ static void print_err(const char *fmt, ...)
  * -1 for the other way around, and -2 on error.
  */
 static int compare_traceeval_data(struct traceeval *teval,
-				  union traceeval_data *orig,
+				  const union traceeval_data *orig,
 				  const union traceeval_data *copy,
 				  struct traceeval_type *type)
 {
@@ -904,3 +904,256 @@ int traceeval_insert(struct traceeval *teval,
 	else
 		return update_entry(teval, entry, vals);
 }
+
+/**
+ * traceeval_iterator_put - release a given iterator
+ * @iter: The iterartor to release

src/histograms.c int traceeval_insert(struct traceeval *teval, · line 920

Quoted 10 lines
src/histograms.c +10 -0
+ *
+ * Frees the resources of an @iter that was created by
+ * traceeval_iterator_get().
+ */
+void traceeval_iterator_put(struct traceeval_iterator *iter)
+{
+	if (!iter)
+		return;
+
+	free(iter->entries);

I think we also need to free **sort and *direction, which are both allocated
in traceeval_iterator_sort(). Probably need to update the function comment
as well to include allocs from that function.

src/histograms.c int traceeval_insert(struct traceeval *teval, · line 922

Quoted 2 lines
src/histograms.c +2 -0
+	free(iter);
+}

<>

Quoted 46 lines
src/histograms.c +46 -0
+/**
+ * traceeval_iterator_sort - sort the entries that an iterator will return
+ * @iter: The iterator to specify the sort order of the entries
+ * @sort_field: The name of the key or value to sort with.
+ * @level: The level of sorting (0 for first order, 1 for second, ...)
+ * @ascending: If the sort should go forward or backward.
+ *
+ * The iterator has a list of entries to produce with traceeval_iterator_next().
+ * This function specifies what the order of the output of that function will
+ * be. Note, whenever this function is called, it resets the @iter so that
+ * the traceveal_iterator_next() will start from the beginning again.
+ *
+ * In other words, be very careful to ever call this function in a middle
+ * of a loop that is using traceeval_iterator_next(), otherwise you may end
+ * up in an infinite loop!
+ *
+ * The @level specifies the level of sorting. That is, for @level = 0,
+ * it will decide the main sorting of the @iter. For @level = 1, it will
+ * be the tie breaker for two entries that are equal for the @level = 0
+ * sort. @level = 2, will be the tie breaker for @level = 1, and so on.
+ *
+ * Note, if traceeval_iterator_next() is called, and there's a missing @level,
+ * it will fail. That is, if this function is called once with @level = 0 and
+ * againg with @level = 2, but never with @level = 1, the call to
+ * traceeval_iterator_next() will fail.
+ *
+ * If this function is called multiple times with the same @level, then the
+ * last call will define the what that @level will do.
+ *
+ * The @ascending will determine if "smaller" items go first if true, and
+ * "larger" items go first if false.
+ *
+ * Return 0 on success and -1 on failure.
+ */
+int traceeval_iterator_sort(struct traceeval_iterator *iter, const char *sort_field,
+			    int level, bool ascending)
+{
+	bool *direction = iter->direction;
+	struct traceeval_type **sort = iter->sort;
+	struct traceeval_type *type;
+
+	type = find_sort_type(iter->teval, sort_field);
+	if (!type)
+		return -1;
+
+	/* pointer and dynamic types must have a cmp function */

src/histograms.c int traceeval_insert(struct traceeval *teval, · line 1051

Quoted 10 lines
src/histograms.c +10 -0
+	switch (type->type) {
+	case TRACEEVAL_TYPE_POINTER:
+	case TRACEEVAL_TYPE_DYNAMIC:
+		if (!type->cmp)
+			return -1;
+		break;
+	default:
+		break;
+	}
+

nit: It'd probably help with readability to have:

  int num_levels = level + 1;

and use that instead of (level + 1).

Quoted 15 lines
src/histograms.c +15 -0
+	if ((level + 1) > iter->nr_sort) {
+		sort = realloc(sort, sizeof(*sort) * (level + 1));
+		if (!sort)
+			return -1;
+
+		iter->sort = sort;
+
+		direction = realloc(direction, sizeof(*direction) * (level + 1));
+		if (!direction)
+			return -1;
+
+		iter->direction = direction;
+
+		/* Make sure the newly allocated contain NULL */
+		for (int i = iter->nr_sort; i < (level + 1); i++)

src/histograms.c int traceeval_insert(struct traceeval *teval, · line 1076

Quoted 10 lines
src/histograms.c +10 -0
+			sort[i] = NULL;
+
+		iter->nr_sort = level + 1;
+	}
+
+	sort[level] = type;
+	direction[level] = ascending;
+	iter->needs_sort = true;
+	return 0;
+}

<>

Quoted 31 lines
src/histograms.c +31 -0
+static int sort_iter(struct traceeval_iterator *iter)
+{
+	int i;
+
+	/* Make sure all levels are filled */
+	for (i = 0; i < iter->nr_sort; i++) {
+		if (!iter->sort[i])
+			return -1;
+	}
+
+	qsort_r(iter->entries, iter->nr_entries, sizeof(*iter->entries),
+		iter_cmp, iter);
+
+	iter->needs_sort = false;
+	iter->next = 0;
+
+	return 0;
+}
+
+/**
+ * traceeval_iterator_next - retrieve the next entry from an iterator
+ * @iter: The iterator to retrieve the next entry from
+ * @keys: The returned keys of the next entry (if exists)
+ *
+ * This returns the keys for the next entry in the traceeval being
+ * iterated over by @iter. If there are no more entries, 0 is returned
+ * and @keys are untouched.
+ *
+ * Returns 1 if another entry is returned, or 0 if not (or negative on error)
+ */
+int traceeval_iterator_next(struct traceeval_iterator *iter,

src/histograms.c int traceeval_insert(struct traceeval *teval, · line 1150

Quoted 10 lines
src/histograms.c +10 -0
+			    const union traceeval_data **keys)
+{
+	struct entry *entry;
+	int ret;
+
+	if (iter->needs_sort) {
+		ret = sort_iter(iter);
+		if (ret < 0)
+			return ret;
+		iter->next = 0;

This is already done in sort_iter().

Quoted 12 lines
src/histograms.c +9 -0
+	}
+
+	if (iter->next >= iter->nr_entries)
+		return 0;
+
+	entry = iter->entries[iter->next++];
+	*keys = entry->keys;
+	return 1;
+}
Signature · 1 line
2.40.1
#26 lore raw

Re: [PATCH v2 11/17] libtraceeval histogram: Add iterator APIs

Quoted 10 lines · On Wed, 16 Aug 2023 15:34:49 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 10 lines
src/histograms.c +10 -0
+
+/**
+ * traceeval_iterator_put - release a given iterator
+ * @iter: The iterartor to release
+ *
+ * Frees the resources of an @iter that was created by
+ * traceeval_iterator_get().
+ */
+void traceeval_iterator_put(struct traceeval_iterator *iter)
+{
Quoted 8 lines

src/histograms.c int traceeval_insert(struct traceeval *teval, · line 920

Quoted 4 lines
src/histograms.c +4 -0
+	if (!iter)
+		return;
+
+	free(iter->entries);  

I think we also need to free **sort and *direction, which are both allocated
in traceeval_iterator_sort(). Probably need to update the function comment
as well to include allocs from that function.

Yes, thanks.

Quoted 63 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+	free(iter);
+}  

<>

Quoted 46 lines
src/histograms.c +46 -0
+/**
+ * traceeval_iterator_sort - sort the entries that an iterator will return
+ * @iter: The iterator to specify the sort order of the entries
+ * @sort_field: The name of the key or value to sort with.
+ * @level: The level of sorting (0 for first order, 1 for second, ...)
+ * @ascending: If the sort should go forward or backward.
+ *
+ * The iterator has a list of entries to produce with traceeval_iterator_next().
+ * This function specifies what the order of the output of that function will
+ * be. Note, whenever this function is called, it resets the @iter so that
+ * the traceveal_iterator_next() will start from the beginning again.
+ *
+ * In other words, be very careful to ever call this function in a middle
+ * of a loop that is using traceeval_iterator_next(), otherwise you may end
+ * up in an infinite loop!
+ *
+ * The @level specifies the level of sorting. That is, for @level = 0,
+ * it will decide the main sorting of the @iter. For @level = 1, it will
+ * be the tie breaker for two entries that are equal for the @level = 0
+ * sort. @level = 2, will be the tie breaker for @level = 1, and so on.
+ *
+ * Note, if traceeval_iterator_next() is called, and there's a missing @level,
+ * it will fail. That is, if this function is called once with @level = 0 and
+ * againg with @level = 2, but never with @level = 1, the call to
+ * traceeval_iterator_next() will fail.
+ *
+ * If this function is called multiple times with the same @level, then the
+ * last call will define the what that @level will do.
+ *
+ * The @ascending will determine if "smaller" items go first if true, and
+ * "larger" items go first if false.
+ *
+ * Return 0 on success and -1 on failure.
+ */
+int traceeval_iterator_sort(struct traceeval_iterator *iter, const char *sort_field,
+			    int level, bool ascending)
+{
+	bool *direction = iter->direction;
+	struct traceeval_type **sort = iter->sort;
+	struct traceeval_type *type;
+
+	type = find_sort_type(iter->teval, sort_field);
+	if (!type)
+		return -1;
+
+	/* pointer and dynamic types must have a cmp function */

src/histograms.c int traceeval_insert(struct traceeval *teval, · line 1051

Quoted 10 lines
src/histograms.c +10 -0
+	switch (type->type) {
+	case TRACEEVAL_TYPE_POINTER:
+	case TRACEEVAL_TYPE_DYNAMIC:
+		if (!type->cmp)
+			return -1;
+		break;
+	default:
+		break;
+	}
+  

nit: It'd probably help with readability to have:

Quoted 3 lines
  int num_levels = level + 1;

and use that instead of (level + 1).

Sure, I'm just so use to the (level + 1), that it comes natural for me now. But I do agree, from a readability standpoint, having a "num_levels" would be better.

Quoted 62 lines
Quoted 16 lines
src/histograms.c +15 -0
+	if ((level + 1) > iter->nr_sort) {
+		sort = realloc(sort, sizeof(*sort) * (level + 1));
+		if (!sort)
+			return -1;
+
+		iter->sort = sort;
+
+		direction = realloc(direction, sizeof(*direction) * (level + 1));
+		if (!direction)
+			return -1;
+
+		iter->direction = direction;
+
+		/* Make sure the newly allocated contain NULL */
+		for (int i = iter->nr_sort; i < (level + 1); i++)

src/histograms.c int traceeval_insert(struct traceeval *teval, · line 1076

Quoted 10 lines
src/histograms.c +10 -0
+			sort[i] = NULL;
+
+		iter->nr_sort = level + 1;
+	}
+
+	sort[level] = type;
+	direction[level] = ascending;
+	iter->needs_sort = true;
+	return 0;
+}  

<>

Quoted 35 lines
src/histograms.c +35 -0
+static int sort_iter(struct traceeval_iterator *iter)
+{
+	int i;
+
+	/* Make sure all levels are filled */
+	for (i = 0; i < iter->nr_sort; i++) {
+		if (!iter->sort[i])
+			return -1;
+	}
+
+	qsort_r(iter->entries, iter->nr_entries, sizeof(*iter->entries),
+		iter_cmp, iter);
+
+	iter->needs_sort = false;
+	iter->next = 0;
+
+	return 0;
+}
+
+/**
+ * traceeval_iterator_next - retrieve the next entry from an iterator
+ * @iter: The iterator to retrieve the next entry from
+ * @keys: The returned keys of the next entry (if exists)
+ *
+ * This returns the keys for the next entry in the traceeval being
+ * iterated over by @iter. If there are no more entries, 0 is returned
+ * and @keys are untouched.
+ *
+ * Returns 1 if another entry is returned, or 0 if not (or negative on error)
+ */
+int traceeval_iterator_next(struct traceeval_iterator *iter,
+			    const union traceeval_data **keys)
+{
+	struct entry *entry;
+	int ret;
Quoted 8 lines

src/histograms.c int traceeval_insert(struct traceeval *teval, · line 1150

Quoted 6 lines
src/histograms.c +6 -0
+
+	if (iter->needs_sort) {
+		ret = sort_iter(iter);
+		if (ret < 0)
+			return ret;
+		iter->next = 0;  

This is already done in sort_iter().

Yep, I know. But I love redundancy ;-)

I actually added it to both at the same time because I didn't know which one was better to have it. I compromised, and just said "OK do it in both!"

-- Steve

Quoted 13 lines
Quoted 13 lines
src/histograms.c +9 -0
+	}
+
+	if (iter->next >= iter->nr_entries)
+		return 0;
+
+	entry = iter->entries[iter->next++];
+	*keys = entry->keys;
+	return 1;
+}
Signature · 1 line
2.40.1
#27 lore raw

PATCH v2 12/17 libtraceeval histogram: Add data copy callback

In reply to Steven Rostedt

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

Add a callback "copy" to the type that gets called when a copy is done. This is can be used to copy dynamic types. Otherwise the old one is just going to be released.

---
 include/traceeval-hist.h | 5 +++++
 src/histograms.c         | 3 +++
 2 files changed, 8 insertions(+)
include/traceeval-hist.h +5 -0
index 2fc81a64e324..1a24d6117b93 100644
@@ -82,6 +82,10 @@ typedef int (*traceeval_data_hash_fn)(struct traceeval *teval,
 				      const struct traceeval_type *type,
 				      const union traceeval_data *data);
 
+typedef int (*traceeval_data_copy_fn)(const struct traceeval_type *type,
+				      union traceeval_data *copy,
+				      const union traceeval_data *origin);
+
 /*
  * struct traceeval_type - Describes the type of a traceevent_data instance
  * @type: The enum type that describes the traceeval_data
@@ -126,6 +130,7 @@ struct traceeval_type {
 	size_t				id;
 	traceeval_data_release_fn	release;
 	traceeval_data_cmp_fn		cmp;
+	traceeval_data_copy_fn		copy;
 	traceeval_data_hash_fn		hash;
 };
 
src/histograms.c +3 -0
index 2b823ad5c26d..f7f72dd3a50b 100644
@@ -512,6 +512,9 @@ static int copy_traceeval_data(struct traceeval_type *type,
 {
 	unsigned long long val;
 
+	if (type->copy)
+		return type->copy(type, copy, orig);
+
 	*copy = *orig;
 
 	switch(type->type) {
Signature · 1 line
2.40.1
#28 lore raw

PATCH v2 13/17 libtraceeval histogram: Do the release on updates

In reply to Steven Rostedt

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

If no copy() function exists for a type but a release() function does, then do the release on the old copy.

---
 src/histograms.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
src/histograms.c +5 -1
index f7f72dd3a50b..8c73d8cd9f45 100644
@@ -596,12 +596,16 @@ static int copy_traceeval_data(struct traceeval_type *type,
 /*
  * Free @data with respect to @size and @type.
  *
- * Does not call release on type TRACEEVAL_TYPE_DYNAMIC.
+ * Does not call release() if a copy() exists.
  */
 static void data_release(size_t size, union traceeval_data **data,
 				struct traceeval_type *type)
 {
 	for (size_t i = 0; i < size; i++) {
+		/* A copy should handle releases */
+		if (type[i].release && !type[i].copy)
+			type[i].release(&type[i], &(*data)[i]);
+
 		if (type[i].type == TRACEEVAL_TYPE_STRING)
 			free((*data)[i].string);
 	}
Signature · 1 line
2.40.1
#29 lore raw

PATCH v2 14/17 libtraceeval histogram: Use stack for old copy in update

In reply to Steven Rostedt

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

In the update, instead of using the heap, use the stack to save the old values. This should save time where it does not need to allocate and then free the value list to do an update.

---
 src/histograms.c | 48 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 14 deletions(-)
src/histograms.c +34 -14
index 8c73d8cd9f45..643a550422f6 100644
@@ -598,17 +598,23 @@ static int copy_traceeval_data(struct traceeval_type *type,
  *
  * Does not call release() if a copy() exists.
  */
-static void data_release(size_t size, union traceeval_data **data,
-				struct traceeval_type *type)
+static void data_release(size_t size, union traceeval_data *data,
+			 struct traceeval_type *type)
 {
 	for (size_t i = 0; i < size; i++) {
 		/* A copy should handle releases */
 		if (type[i].release && !type[i].copy)
-			type[i].release(&type[i], &(*data)[i]);
+			type[i].release(&type[i], &data[i]);
 
 		if (type[i].type == TRACEEVAL_TYPE_STRING)
-			free((*data)[i].string);
+			free(data[i].string);
 	}
+}
+
+static void data_release_and_free(size_t size, union traceeval_data **data,
+				struct traceeval_type *type)
+{
+	data_release(size, *data, type);
 	free(*data);
 	*data = NULL;
 }
@@ -642,7 +648,7 @@ static int copy_traceeval_data_set(size_t size, struct traceeval_type *type,
 	return 1;
 
 fail:
-	data_release(i, copy, type);
+	data_release_and_free(i, copy, type);
 	return -1;
 }
 
@@ -701,7 +707,7 @@ void traceeval_results_release(struct traceeval *teval,
 		return;
 	}
 
-	data_release(teval->nr_val_types, &results, teval->val_types);
+	data_release_and_free(teval->nr_val_types, &results, teval->val_types);
 }
 
 static struct entry *create_hist_entry(struct traceeval *teval,
@@ -757,7 +763,7 @@ static int create_entry(struct traceeval *teval,
 	return 0;
 
 fail:
-	data_release(teval->nr_key_types, &new_keys, teval->key_types);
+	data_release_and_free(teval->nr_key_types, &new_keys, teval->key_types);
 
 fail_stats:
 	free(entry->val_stats);
@@ -772,20 +778,34 @@ fail_entry:
  *
  * Frees the old vals field of @entry, unless an error occurs.
  *
- * Return 0 on success, -1 on error.
+ * Return 1 on success, -1 on error.
  */
 static int update_entry(struct traceeval *teval, struct entry *entry,
 			const union traceeval_data *vals)
 {
-	union traceeval_data *new_vals;
+	struct traceeval_stat *stats = entry->val_stats;
+	struct traceeval_type *types = teval->val_types;
+	union traceeval_data *copy = entry->vals;
+	union traceeval_data old[teval->nr_val_types];
+	size_t size = teval->nr_val_types;
+	size_t i;
 
-	if (copy_traceeval_data_set(teval->nr_val_types, teval->val_types,
-				vals, entry->val_stats, &new_vals) == -1)
-		return -1;
+	if (!size)
+		return 1;
+
+	for (i = 0; i < size; i++) {
+		old[i] = copy[i];
+
+		if (copy_traceeval_data(types + i, stats + i,
+					vals + i, copy + i))
+			goto fail;
+	}
 
-	clean_data_set(entry->vals, teval->val_types, teval->nr_val_types);
-	entry->vals = new_vals;
 	return 0;
+
+fail:
+	data_release(i, old, types);
+	return -1;
 }
 
 struct traceeval_stat *traceeval_stat(struct traceeval *teval,
Signature · 1 line
2.40.1
#30 lore raw

Re: [PATCH v2 14/17] libtraceeval histogram: Use stack for old copy in update

Quoted 11 lines · Steven Rostedt

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

In the update, instead of using the heap, use the stack to save the old values. This should save time where it does not need to allocate and then free the value list to do an update.

---
 src/histograms.c | 48 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 14 deletions(-)

<>

src/histograms.c fail_entry: · line 781

Quoted 6 lines
src/histograms.c +1 -1
@@ -772,20 +778,34 @@ fail_entry:
  *
  * Frees the old vals field of @entry, unless an error occurs.
  *
- * Return 0 on success, -1 on error.
+ * Return 1 on success, -1 on error.

The code is now returning 1, 0 and -1 in different cases, and all three of these return values are percolating up to be returned by traceeval_insert(), which is only supposed to return 0 or 1.

Quoted 7 lines
src/histograms.c +2 -1
  */
 static int update_entry(struct traceeval *teval, struct entry *entry,
 			const union traceeval_data *vals)
 {
-	union traceeval_data *new_vals;
+	struct traceeval_stat *stats = entry->val_stats;
+	struct traceeval_type *types = teval->val_types;

src/histograms.c fail_entry: · line 794

Quoted 10 lines
src/histograms.c +6 -3
+	union traceeval_data *copy = entry->vals;
+	union traceeval_data old[teval->nr_val_types];
+	size_t size = teval->nr_val_types;
+	size_t i;
 
-	if (copy_traceeval_data_set(teval->nr_val_types, teval->val_types,
-				vals, entry->val_stats, &new_vals) == -1)
-		return -1;
+	if (!size)
+		return 1;

If we try and copy a 0 length val, is it okay to just return 0 here and call it success?

src/histograms.c fail_entry: · line 802

Quoted 8 lines
src/histograms.c +8 -0
+
+	for (i = 0; i < size; i++) {
+		old[i] = copy[i];
+
+		if (copy_traceeval_data(types + i, stats + i,
+					vals + i, copy + i))
+			goto fail;
+	}

I think we still need to rip through old[] and free strings, and also call .release on types that define it, probably via data_release().

I don't understand why data_release() only calls .release if a .copy function isn't defined? Are we saying that .copy() must also release the old data? If so that needs to be explicit, but I think we still need to free strings at a minimum.

Quoted 14 lines
src/histograms.c +4 -2
-	clean_data_set(entry->vals, teval->val_types, teval->nr_val_types);
-	entry->vals = new_vals;
 	return 0;
+
+fail:
+	data_release(i, old, types);
+	return -1;
 }
 
 struct traceeval_stat *traceeval_stat(struct traceeval *teval,
Signature · 1 line
2.40.1
#31 lore raw

Re: [PATCH v2 14/17] libtraceeval histogram: Use stack for old copy in update

Quoted 13 lines · On Wed, 16 Aug 2023 16:37:54 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 11 lines · Steven Rostedt

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

In the update, instead of using the heap, use the stack to save the old values. This should save time where it does not need to allocate and then free the value list to do an update.

---
 src/histograms.c | 48 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 14 deletions(-)

<>

Quoted 11 lines

src/histograms.c fail_entry: · line 781

Quoted 6 lines
src/histograms.c +1 -1
@@ -772,20 +778,34 @@ fail_entry:
  *
  * Frees the old vals field of @entry, unless an error occurs.
  *
- * Return 0 on success, -1 on error.
+ * Return 1 on success, -1 on error.  

The code is now returning 1, 0 and -1 in different cases, and all three of these return values are percolating up to be returned by traceeval_insert(), which is only supposed to return 0 or 1.

I didn't update the comments well. The idea is, return 1 if the element already existed, 0 if it did not, and -1 on error. I need to fix this.

Quoted 21 lines
Quoted 7 lines
src/histograms.c +2 -1
  */
 static int update_entry(struct traceeval *teval, struct entry *entry,
 			const union traceeval_data *vals)
 {
-	union traceeval_data *new_vals;
+	struct traceeval_stat *stats = entry->val_stats;
+	struct traceeval_type *types = teval->val_types;

src/histograms.c fail_entry: · line 794

Quoted 10 lines
src/histograms.c +6 -3
+	union traceeval_data *copy = entry->vals;
+	union traceeval_data old[teval->nr_val_types];
+	size_t size = teval->nr_val_types;
+	size_t i;
 
-	if (copy_traceeval_data_set(teval->nr_val_types, teval->val_types,
-				vals, entry->val_stats, &new_vals) == -1)
-		return -1;
+	if (!size)
+		return 1;  

If we try and copy a 0 length val, is it okay to just return 0 here and call it success?

Quoted 10 lines

src/histograms.c fail_entry: · line 802

Quoted 8 lines
src/histograms.c +8 -0
+
+	for (i = 0; i < size; i++) {
+		old[i] = copy[i];
+
+		if (copy_traceeval_data(types + i, stats + i,
+					vals + i, copy + i))
+			goto fail;
+	}  

I think we still need to rip through old[] and free strings, and also call

Yes, I forgot to add that :-p

Quoted 6 lines

.release on types that define it, probably via data_release().

I don't understand why data_release() only calls .release if a .copy function isn't defined? Are we saying that .copy() must also release the old data? If so that needs to be explicit, but I think we still need to free strings at a minimum.

So the idea is that the release function is called to release (free) the old data. But the copy may not need to allocate or free, it could use the existing data. This will be explicitly stated in the man pages (when I get around to writing them).

-- Steve

Quoted 15 lines
Quoted 15 lines
src/histograms.c +4 -2
-	clean_data_set(entry->vals, teval->val_types, teval->nr_val_types);
-	entry->vals = new_vals;
 	return 0;
+
+fail:
+	data_release(i, old, types);
+	return -1;
 }
 
 struct traceeval_stat *traceeval_stat(struct traceeval *teval,
Signature · 1 line
2.40.1
#32 lore raw

Re: [PATCH v2 14/17] libtraceeval histogram: Use stack for old copy in update

Quoted 28 lines · On Wed, 16 Aug 2023 19:12:25 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 13 lines · On Wed, 16 Aug 2023 16:37:54 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 11 lines · Steven Rostedt

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

In the update, instead of using the heap, use the stack to save the old values. This should save time where it does not need to allocate and then free the value list to do an update.

---
 src/histograms.c | 48 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 14 deletions(-)

<>

Quoted 11 lines

src/histograms.c fail_entry: · line 781

Quoted 6 lines
src/histograms.c +1 -1
@@ -772,20 +778,34 @@ fail_entry:
  *
  * Frees the old vals field of @entry, unless an error occurs.
  *
- * Return 0 on success, -1 on error.
+ * Return 1 on success, -1 on error.    

The code is now returning 1, 0 and -1 in different cases, and all three of these return values are percolating up to be returned by traceeval_insert(), which is only supposed to return 0 or 1.

Quoted 2 lines

I didn't update the comments well. The idea is, return 1 if the element already existed, 0 if it did not, and -1 on error. I need to fix this.

Anyway, it this does not belong in this patch. The return value of traceeval_insert() change should be a separate patch. One that I will add in another series after this one.

-- Steve

#33 lore raw

Re: [PATCH v2 14/17] libtraceeval histogram: Use stack for old copy in update

In reply to Steven Rostedt
Quoted 11 lines · On Wed, 16 Aug 2023 19:12:25 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 10 lines

src/histograms.c fail_entry: · line 802

Quoted 8 lines
src/histograms.c +8 -0
+
+	for (i = 0; i < size; i++) {
+		old[i] = copy[i];
+
+		if (copy_traceeval_data(types + i, stats + i,
+					vals + i, copy + i))
+			goto fail;
+	}    

I think we still need to rip through old[] and free strings, and also call

Quoted 4 lines

Yes, I forgot to add that :-p

Quoted 2 lines

.release on types that define it, probably via data_release().

I realized I actually had this kind of backwards. On failure, we want to put back old and remove the data added to vals (aka copy).

Here's what I'm doing:

	for (i = 0; i < size; i++) {
		old[i] = copy[i];

		if (copy_traceeval_data(types + i, stats + i,
					vals + i, copy + i))
			goto fail;
	}
	data_release(size, old, types);
	return 0;
 fail:
	/* Free the new values that were added */
	data_release(i, copy, types);
	/* Put back the old values */
	for (i--; i >= 0; i--) {
		copy_traceeval_data(types + i, NULL,
				    copy + i, old + i);
	}
	return -1;

-- Steve

#34 lore raw

PATCH v2 15/17 libtraceeval histogram: Add traceeval_iterator_sort_custom()

In reply to Steven Rostedt

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

Add an iterator where the application can supply the sort algorithm where it gets the teval descriptor along with the keys and values of both of the entries to compare against. Also, allow it to submit its own data to the compare function:

int traceeval_iterator_sort_custom(struct traceeval_iterator *iter,

				   traceeval_cmp_fn sort_fn, void *data);

with

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, void *data);

---
 include/traceeval-hist.h |  9 +++++++++
 src/histograms.c         | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
include/traceeval-hist.h +9 -0
index 1a24d6117b93..839f63630897 100644
@@ -86,6 +86,13 @@ typedef int (*traceeval_data_copy_fn)(const struct traceeval_type *type,
 				      union traceeval_data *copy,
 				      const union traceeval_data *origin);
 
+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,
+				void *data);
+
 /*
  * struct traceeval_type - Describes the type of a traceevent_data instance
  * @type: The enum type that describes the traceeval_data
@@ -172,6 +179,8 @@ struct traceeval_iterator *traceeval_iterator_get(struct traceeval *teval);
 void traceeval_iterator_put(struct traceeval_iterator *iter);
 int traceeval_iterator_sort(struct traceeval_iterator *iter, const char *sort_field,
 			    int level, bool ascending);
+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);
 
src/histograms.c +34 -0
index 643a550422f6..33c87644d468 100644
@@ -1153,6 +1153,40 @@ static int sort_iter(struct traceeval_iterator *iter)
 	return 0;
 }
 
+struct iter_custom_data {
+	struct traceeval_iterator *iter;
+	traceeval_cmp_fn sort_fn;
+	void *data;
+};
+
+static int iter_custom_cmp(const void *A, const void *B, void *data)
+{
+	struct iter_custom_data *cust_data = data;
+	struct traceeval_iterator *iter = cust_data->iter;
+	struct traceeval *teval = iter->teval;
+	const struct entry *a = *((const struct entry **)A);
+	const struct entry *b = *((const struct entry **)B);
+
+	return cust_data->sort_fn(teval, a->keys, a->vals, b->keys, b->vals,
+				  cust_data->data);
+}
+
+int traceeval_iterator_sort_custom(struct traceeval_iterator *iter,
+				   traceeval_cmp_fn sort_fn, void *data)
+{
+	struct iter_custom_data cust_data = {
+		.iter = iter,
+		.sort_fn = sort_fn,
+		.data = data
+	};
+
+	qsort_r(iter->entries, iter->nr_entries, sizeof(*iter->entries),
+		iter_custom_cmp, &cust_data);
+
+	iter->needs_sort = false;
+	return 0;
+}
+
 /**
  * traceeval_iterator_next - retrieve the next entry from an iterator
  * @iter: The iterator to retrieve the next entry from
Signature · 1 line
2.40.1
#35 lore raw

Re: [PATCH v2 15/17] libtraceeval histogram: Add traceeval_iterator_sort_custom()

Quoted 79 lines · Steven Rostedt

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

Add an iterator where the application can supply the sort algorithm where it gets the teval descriptor along with the keys and values of both of the entries to compare against. Also, allow it to submit its own data to the compare function:

int traceeval_iterator_sort_custom(struct traceeval_iterator *iter,

				   traceeval_cmp_fn sort_fn, void *data);

with

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, void *data);

---
 include/traceeval-hist.h |  9 +++++++++
 src/histograms.c         | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
include/traceeval-hist.h +9 -0
index 1a24d6117b93..839f63630897 100644
@@ -86,6 +86,13 @@ typedef int (*traceeval_data_copy_fn)(const struct traceeval_type *type,
 				      union traceeval_data *copy,
 				      const union traceeval_data *origin);
 
+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,
+				void *data);
+
 /*
  * struct traceeval_type - Describes the type of a traceevent_data instance
  * @type: The enum type that describes the traceeval_data
@@ -172,6 +179,8 @@ struct traceeval_iterator *traceeval_iterator_get(struct traceeval *teval);
 void traceeval_iterator_put(struct traceeval_iterator *iter);
 int traceeval_iterator_sort(struct traceeval_iterator *iter, const char *sort_field,
 			    int level, bool ascending);
+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);
 
src/histograms.c +19 -0
index 643a550422f6..33c87644d468 100644
@@ -1153,6 +1153,40 @@ static int sort_iter(struct traceeval_iterator *iter)
 	return 0;
 }
 
+struct iter_custom_data {
+	struct traceeval_iterator *iter;
+	traceeval_cmp_fn sort_fn;
+	void *data;
+};
+
+static int iter_custom_cmp(const void *A, const void *B, void *data)
+{
+	struct iter_custom_data *cust_data = data;
+	struct traceeval_iterator *iter = cust_data->iter;
+	struct traceeval *teval = iter->teval;
+	const struct entry *a = *((const struct entry **)A);
+	const struct entry *b = *((const struct entry **)B);
+
+	return cust_data->sort_fn(teval, a->keys, a->vals, b->keys, b->vals,
+				  cust_data->data);
+}
+
+int traceeval_iterator_sort_custom(struct traceeval_iterator *iter,

src/histograms.c static int sort_iter(struct traceeval_iterator *iter) · line 1184

Quoted 10 lines
src/histograms.c +10 -0
+				   traceeval_cmp_fn sort_fn, void *data)
+{
+	struct iter_custom_data cust_data = {
+		.iter = iter,
+		.sort_fn = sort_fn,
+		.data = data
+	};
+
+	qsort_r(iter->entries, iter->nr_entries, sizeof(*iter->entries),
+		iter_custom_cmp, &cust_data);

I guess I don't yet see what this gives us over the existing sorting and iterators? Does this do the same thing, we just pass in the sort function instead of calling traceeval_iterator_sort() one or more times?

src/histograms.c static int sort_iter(struct traceeval_iterator *iter) · line 1186

Quoted 2 lines
src/histograms.c +2 -0
+
+	iter->needs_sort = false;

Also probably need to set
iter->next = 0;

Quoted 9 lines
src/histograms.c +3 -0
+	return 0;
+}
+
 /**
  * traceeval_iterator_next - retrieve the next entry from an iterator
  * @iter: The iterator to retrieve the next entry from
Signature · 1 line
2.40.1
#36 lore raw

Re: [PATCH v2 15/17] libtraceeval histogram: Add traceeval_iterator_sort_custom()

Quoted 19 lines · On Wed, 16 Aug 2023 16:57:16 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 19 lines
src/histograms.c +19 -0
+static int iter_custom_cmp(const void *A, const void *B, void *data)
+{
+	struct iter_custom_data *cust_data = data;
+	struct traceeval_iterator *iter = cust_data->iter;
+	struct traceeval *teval = iter->teval;
+	const struct entry *a = *((const struct entry **)A);
+	const struct entry *b = *((const struct entry **)B);
+
+	return cust_data->sort_fn(teval, a->keys, a->vals, b->keys, b->vals,
+				  cust_data->data);
+}
+
+int traceeval_iterator_sort_custom(struct traceeval_iterator *iter,
+				   traceeval_cmp_fn sort_fn, void *data)
+{
+	struct iter_custom_data cust_data = {
+		.iter = iter,
+		.sort_fn = sort_fn,
+		.data = data
Quoted 8 lines

src/histograms.c static int sort_iter(struct traceeval_iterator *iter) · line 1184

Quoted 4 lines
src/histograms.c +4 -0
+	};
+
+	qsort_r(iter->entries, iter->nr_entries, sizeof(*iter->entries),
+		iter_custom_cmp, &cust_data);  

I guess I don't yet see what this gives us over the existing sorting and iterators? Does this do the same thing, we just pass in the sort function instead of calling traceeval_iterator_sort() one or more times?

This should be used in replace of the other sorting. It allows the user to make a complex sort. If you look at the last patch, the sample uses this:

+/*
+ * Sort all the processes by the RUNNING state.
+ * If A and B have the same COMM, then sort by state.
+ * else
+ * Find the RUNNNIG state for A and B
+ * If the RUNNING state does not exist, it's considered -1
+ * 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,
+ 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_stat *statA;
+ struct traceeval_stat *statB;
+ unsigned long long totalA = -1;
+ unsigned long long totalB = -1;
+
+ /* First check if we are on the same task */
+ if (strcmp(Akeys[0].cstring, Bkeys[0].cstring) == 0) {
+ /* Sort decending */
+ if (Bkeys[1].number > Akeys[1].number)
+ return -1;
+ return Bkeys[1].number != Akeys[1].number;
+ }
+
+ /* Get the RUNNING values for both processes */
+ statA = traceeval_stat(teval, keysA, &delta_vals[0]);
+ if (statA)
+ totalA = traceeval_stat_total(statA);
+
+ statB = traceeval_stat(teval, keysB, &delta_vals[0]);
+ if (statB)
+ totalB = traceeval_stat_total(statB);
+
+ if (totalB < totalA)
+ return -1;
+ if (totalB > totalA)
+ return 1;
+
+ return strcmp(Bkeys[0].cstring, Akeys[0].cstring);
+}

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+
+	iter->needs_sort = false;  

Also probably need to set
iter->next = 0;

So much for redundancy!

Thanks,

-- Steve

Quoted 4 lines
Quoted 4 lines
src/histograms.c +3 -0
+	return 0;
+}
+
#37 lore raw

PATCH v2 16/17 libtraceeval histogram: Have traceeval_query() just give the pointer to results

In reply to Steven Rostedt

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

Instead of wasting allocation to return the results for queries, especially since the query is likely not to modify the results, just give a direct pointer to the entry->vals.

Mark it as const so (hopefully) nobody modifies it.

We still require the traceeval_results_release() to be called on it, as we may in the future add a ref count to it and perhaps do a copy-on-write if an update happens while something still has a hold on it.

---
 include/traceeval-hist.h |  4 ++--
 src/histograms.c         | 10 ++++------
 2 files changed, 6 insertions(+), 8 deletions(-)
include/traceeval-hist.h +2 -2
index 839f63630897..315b66adb7ee 100644
@@ -161,10 +161,10 @@ int traceeval_insert(struct traceeval *teval,
 		     const union traceeval_data *vals);
 
 int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
-		    union traceeval_data **results);
+		    const union traceeval_data **results);
 
 void traceeval_results_release(struct traceeval *teval,
-			       union traceeval_data *results);
+			       const union traceeval_data *results);
 
 struct traceeval_stat *traceeval_stat(struct traceeval *teval,
 				      const union traceeval_data *keys,
src/histograms.c +4 -6
index 33c87644d468..cd68d4c834af 100644
@@ -671,7 +671,7 @@ fail:
  * Returns 1 if found, 0 if not found, and -1 on error.
  */
 int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
-		    union traceeval_data **results)
+		    const union traceeval_data **results)
 {
 	struct entry *entry;
 	int check;
@@ -683,8 +683,8 @@ int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
 	if ((check = get_entry(teval, keys, &entry)) < 1)
 		return check;
 
-	return copy_traceeval_data_set(teval->nr_val_types, teval->val_types,
-			entry->vals, NULL, results);
+	*results = entry->vals;
+	return 1;
 }
 
 /*
@@ -699,15 +699,13 @@ int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
  * allow traceeval to clean up its references.
  */
 void traceeval_results_release(struct traceeval *teval,
-			       union traceeval_data *results)
+			       const union traceeval_data *results)
 {
 	if (!teval || !results) {
 		if (!teval)
 			print_err("Results to be freed without accompanied traceeval instance!");
 		return;
 	}
-
-	data_release_and_free(teval->nr_val_types, &results, teval->val_types);
 }
 
 static struct entry *create_hist_entry(struct traceeval *teval,
Signature · 1 line
2.40.1
#38 lore raw

PATCH v2 17/17 libtraceeval samples: Update task-eval to use the histogram logic

In reply to Steven Rostedt

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

Convert task-eval over to the new API.

---
 samples/Makefile    |   2 +-
 samples/task-eval.c | 729 +++++++++++++++++++++++++-------------------
 2 files changed, 412 insertions(+), 319 deletions(-)
samples/Makefile +1 -1
index 012301ec5542..eb14411189f6 100644
@@ -19,7 +19,7 @@ $(sdir):
 
 $(TARGETS): $(sdir) $(LIBTRACEEVAL_STATIC)
 
-$(sdir)/%: $(bdir)/%.o
+$(sdir)/%: $(LIBTRACEEVAL_STATIC) $(bdir)/%.o
 	$(call do_sample_build,$@,$<)
 
 $(bdir)/%.o: $(bdir)/%.c
samples/task-eval.c +411 -318

985 lines not on this page · raw message

Signature · 1 line
2.40.1