Skip to contentSkip to conversation

libtraceeval histogram: Updates

PATCH Superseded Steven Rostedt 2 participants 28 messages Aug 17, 2023 Kbuild

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 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 v2: https://lore.kernel.org/all/[email protected]/

- Fixed comments on traceeval_dyn_release_fn and traceeval_dyn_cmp_fn (Ross Zwisler)

- Change parameter ordering of compare_traceeval_data (Ross Zwisler)

- Added (size_t) to offset_of() macro (Ross Zwisler)

- Fixed iter->current_bucket off by one bug (Ross Zwisler)

- Removed HASH_MASK() from make_hash()

- Fixed goto fail_release in traceeval_init() (Ross Zwisler)

- Free sort and direction in traceeval_iterator_put() (Ross Zwisler)

- Use num_levels variable instead of (level + 1) (Ross Zwisler)

- Return 0 on success of update_entry (Ross Zwisler)

- Fixed the cleanup of old values in update_entry() (Ross Zwisler)

- Fixed resetting iter->next = 0 in traceeval_iterator_sort_custom() (Ross Zwisler)

- Added failure checks to task-eval.c sample for all traceeval_query() calls.

- Added traceeval_remove() to remove an item from traceveal (opposite of traceeval_insert())

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) (18):
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
libtraceeval: Add traceeval_remove()

 Makefile                 |   4 +-
 include/traceeval-hist.h | 117 +++--
 include/traceeval-test.h |  16 -
 samples/Makefile         |  29 ++
 samples/task-eval.c      | 966 +++++++++++++++++++++++++++++++++++++++
 src/Makefile             |   1 +
 src/eval-local.h         | 123 +++++
 src/hash.c               | 123 +++++
 src/histograms.c         | 923 +++++++++++++++++++++++++++----------
 9 files changed, 2012 insertions(+), 290 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 v3 1/18 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 v3 2/18 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.

This version uses the old API, and will be used to compare when converted over to the new API.

---
 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 v3 3/18 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 v3 4/18 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 | 50 +++++++++++++++++++++-------------------
 src/histograms.c         | 28 +++++++++++-----------
 2 files changed, 40 insertions(+), 38 deletions(-)
include/traceeval-hist.h +26 -24
index f6c4e8efb2be..4c42c82ea045 100644
@@ -64,14 +64,14 @@ union traceeval_data {
 
 struct traceeval_type;
 
-/* struct traceeval_dynamic release function signature */
-typedef void (*traceeval_dyn_release_fn)(struct traceeval_type *,
-					 struct traceeval_dynamic);
+/* release function callback on traceeval_data */
+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 *);
+/* compare function callback to compare traceeval_data */
+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..0c10edbc9acc 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 the release callback on the data.
  */
 static void data_release(size_t size, union traceeval_data **data,
 				struct traceeval_type *type)
Signature · 1 line
2.40.1
#5 lore raw

PATCH v3 5/18 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         | 31 ++++++++++++++++---------------
 2 files changed, 23 insertions(+), 21 deletions(-)
include/traceeval-hist.h +7 -6
index 4c42c82ea045..d7ffe42f3231 100644
@@ -63,16 +63,17 @@ union traceeval_data {
 };
 
 struct traceeval_type;
+struct traceeval;
 
 /* release function callback on traceeval_data */
-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);
 
 /* compare function callback to compare traceeval_data */
-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 +16 -15
index 0c10edbc9acc..1a7fa03e031c 100644
@@ -113,9 +113,10 @@ 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,
-				  const union traceeval_data *copy,
-				  struct traceeval_type *type)
+static int compare_traceeval_data(struct traceeval *teval,
+				  struct traceeval_type *type,
+				  union traceeval_data *orig,
+				  const union traceeval_data *copy)
 {
 	int i;
 
@@ -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,16 +168,17 @@ 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,
-				      const union traceeval_data *copy,
-				      struct traceeval_type *defs, size_t size)
+static int compare_traceeval_data_set(struct traceeval *teval,
+				      struct traceeval_type *defs,
+				      union traceeval_data *orig,
+				      const union traceeval_data *copy, size_t size)
 {
 	int check;
 	size_t i;
 
 	/* 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, defs + i, orig + i, copy + i)))
 			return check == -2 ? -1 : 0;
 	}
 
@@ -192,14 +194,14 @@ static int compare_entries(struct entry *orig, struct entry *copy,
 	int check;
 
 	/* compare keys */
-	check = compare_traceeval_data_set(orig->keys, copy->keys,
-			teval->key_types, teval->nr_key_types);
+	check = compare_traceeval_data_set(teval, teval->key_types,
+					   orig->keys, copy->keys, teval->nr_key_types);
 	if (check < 1)
 		return check;
 
 	/* compare values */
-	check = compare_traceeval_data_set(orig->vals, copy->vals,
-			teval->val_types, teval->nr_val_types);
+	check = compare_traceeval_data_set(teval, teval->val_types,
+					   orig->vals, copy->vals, 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, teval->key_types,
+						   entry->keys, keys, teval->nr_key_types);
 		if (!check)
 			continue;
 		break;
Signature · 1 line
2.40.1
#6 lore raw

PATCH v3 6/18 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
#7 lore raw

PATCH v3 7/18 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         | 166 +++++++++++++++++++++++++++++++++------
 2 files changed, 147 insertions(+), 26 deletions(-)
include/traceeval-hist.h +7 -0
index d7ffe42f3231..1edda56a712f 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 +140 -26

270 lines not on this page · raw message

Signature · 1 line
2.40.1
#8 lore raw

PATCH v3 8/18 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       | 123 +++++++++++++++++++++++++++++++++++++++++++++++
 src/histograms.c | 121 +++++++---------------------------------------
 4 files changed, 244 insertions(+), 104 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 +123 -0

123 lines not on this page · raw message

src/histograms.c +17 -104

195 lines not on this page · raw message

Signature · 1 line
2.40.1
#9 lore raw

PATCH v3 9/18 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 1edda56a712f..f80d039baa61 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
#10 lore raw

PATCH v3 10/18 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 f80d039baa61..164d34c0fa81 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 007fdb146b96..ed2e8abc9b63 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
#11 lore raw

PATCH v3 11/18 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/histograms.c         | 258 ++++++++++++++++++++++++++++++++++++++-
 3 files changed, 275 insertions(+), 1 deletion(-)
include/traceeval-hist.h +7 -0
index 164d34c0fa81..e8a7981ac2c0 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 ed2e8abc9b63..5c3918f17cc1 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/histograms.c +257 -1

267 lines not on this page · raw message

Signature · 1 line
2.40.1
#12 lore raw

PATCH v3 12/18 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 e8a7981ac2c0..8992ace81cdf 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 8cd1b288a7fc..fe13884ccff6 100644
@@ -511,6 +511,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
#13 lore raw

PATCH v3 13/18 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 fe13884ccff6..57b4b2166e3b 100644
@@ -595,12 +595,16 @@ static int copy_traceeval_data(struct traceeval_type *type,
 /*
  * Free @data with respect to @size and @type.
  *
- * Does not call the release callback on the data.
+ * Does not call the release() callback 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
#14 lore raw

PATCH v3 14/18 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 | 51 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 38 insertions(+), 13 deletions(-)
src/histograms.c +38 -13
index 57b4b2166e3b..3050bad4190e 100644
@@ -597,17 +597,23 @@ static int copy_traceeval_data(struct traceeval_type *type,
  *
  * Does not call the release() callback 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;
 }
@@ -641,7 +647,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;
 }
 
@@ -700,7 +706,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,
@@ -756,7 +762,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);
@@ -776,15 +782,34 @@ fail_entry:
 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 0;
 
-	clean_data_set(entry->vals, teval->val_types, teval->nr_val_types);
-	entry->vals = new_vals;
+	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;
 }
 
 struct traceeval_stat *traceeval_stat(struct traceeval *teval,
Signature · 1 line
2.40.1
#15 lore raw

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

src/histograms.c

Quoted 15 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 | 51 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 38 insertions(+), 13 deletions(-)
src/histograms.c +0 -0
index 57b4b2166e3b..3050bad4190e 100644

<>

Quoted 26 lines
src/histograms.c +14 -6
@@ -776,15 +782,34 @@ fail_entry:
 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 0;
 
-	clean_data_set(entry->vals, teval->val_types, teval->nr_val_types);
-	entry->vals = new_vals;
+	for (i = 0; i < size; i++) {
+		old[i] = copy[i];
+
+		if (copy_traceeval_data(types + i, stats + i,
+					vals + i, copy + i))
+			goto fail;

src/histograms.c fail_entry: · line 810

Quoted 10 lines
src/histograms.c +9 -0
+	}
+	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);

should be: old + i, copy + i);

Right now we're copying 'copy' into 'old', but we want to be doing it the other way around so we restore 'copy' back to its saved state in 'old'

Quoted 8 lines
src/histograms.c +2 -0
+	}
+	return -1;
 }
 
 struct traceeval_stat *traceeval_stat(struct traceeval *teval,
Signature · 1 line
2.40.1
#16 lore raw

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

Quoted 10 lines · On Thu, 17 Aug 2023 13:29:26 -0600 Ross Zwisler <[email protected]> wrote:

src/histograms.c fail_entry: · line 810

Quoted 7 lines

+ fail:

src/histograms.c +6 -0
+	/* 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);  

should be: old + i, copy + i);

Quoted 2 lines

Right now we're copying 'copy' into 'old', but we want to be doing it the other way around so we restore 'copy' back to its saved state in 'old'

I need to change the parameter names to "src" and "dst" as I can't think of "orig" and "copy" as which direction they go. I looked at that three times, and still am confused. And probably reverse them too to make it equivalent to memcpy():

From memcpy() man page;

SYNOPSIS

       #include <string.h>

       void *memcpy(void *restrict dest, const void *restrict src, size_t n);

DESCRIPTION
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.

As most standard libraries have it "dst, src" I'm pretty much thinking that way, and my "direction" is always dst = src --> dst, src. And that's exactly what I was thinking above :-p

-- Steve

#17 lore raw

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

Quoted 13 lines · Steven Rostedt
Quoted 10 lines · On Thu, 17 Aug 2023 13:29:26 -0600 Ross Zwisler <[email protected]> wrote:

src/histograms.c fail_entry: · line 810

Quoted 7 lines

+ fail:

src/histograms.c +6 -0
+	/* 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);  

should be: old + i, copy + i);

Quoted 5 lines
Quoted 2 lines

Right now we're copying 'copy' into 'old', but we want to be doing it the other way around so we restore 'copy' back to its saved state in 'old'

I need to change the parameter names to "src" and "dst" as I can't think of "orig" and "copy" as which direction they go.

I was thinking the same thing, 'src' and 'dst' would be better. :)

Quoted 22 lines

I looked at that three times,
and still am confused. And probably reverse them too to make it equivalent to memcpy():

From memcpy() man page;

SYNOPSIS

       #include <string.h>

       void *memcpy(void *restrict dest, const void *restrict src, size_t n);

DESCRIPTION
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.

As most standard libraries have it "dst, src" I'm pretty much thinking that way, and my "direction" is always dst = src --> dst, src. And that's exactly what I was thinking above :-p

-- Steve

#18 lore raw

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

Quoted 4 lines · On Thu, 17 Aug 2023 13:44:59 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 2 lines

I need to change the parameter names to "src" and "dst" as I can't think of "orig" and "copy" as which direction they go.

I was thinking the same thing, 'src' and 'dst' would be better. :)

and to make it even more confusing, I made made the compare callback function ordering that of "copy, orig" ("dst, src")

typedef int (*traceeval_data_copy_fn)(const struct traceeval_type *type,
union traceeval_data *copy, const union traceeval_data *origin);

I've just updated my next version to start out by reversing the order of the copy and to call it "dst" and "src", and I renamed copy_traceveal_data_set() to dup_traceeval_data_set(), as it's just duplicating the set, and not really "copying" it (think strdup()), as it needs to allocate it. That way, I can keep the order, as duplication variables should come at the end. And this will remove the ambiguity of the copy order.

-- Steve

#19 lore raw

PATCH v3 15/18 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         | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
include/traceeval-hist.h +9 -0
index 8992ace81cdf..fc1e288a43c6 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 +35 -0
index 3050bad4190e..c0f2248cce6f 100644
@@ -1160,6 +1160,41 @@ 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;
+	iter->next = 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
#20 lore raw

PATCH v3 16/18 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 fc1e288a43c6..37147d626fbb 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 c0f2248cce6f..c16b8866b84a 100644
@@ -670,7 +670,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;
@@ -682,8 +682,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;
 }
 
 /*
@@ -698,15 +698,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
#21 lore raw

PATCH v3 17/18 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 | 743 +++++++++++++++++++++++++-------------------
 2 files changed, 426 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 +425 -318

999 lines not on this page · raw message

Signature · 1 line
2.40.1
#22 lore raw

PATCH v3 18/18 libtraceeval: Add traceeval_remove()

In reply to Steven Rostedt

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

Add a way to remove an entry that's in the traceeval. Currently there's only ways to add entries, but there also needs to be a way to remove them too.

---
 include/traceeval-hist.h |  3 +++
 src/histograms.c         | 29 +++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
include/traceeval-hist.h +3 -0
index 37147d626fbb..8e5a6451f399 100644
@@ -160,6 +160,9 @@ int traceeval_insert(struct traceeval *teval,
 		     const union traceeval_data *keys,
 		     const union traceeval_data *vals);
 
+int traceeval_remove(struct traceeval *teval,
+		     const union traceeval_data *keys);
+
 int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
 		    const union traceeval_data **results);
 
src/histograms.c +29 -0
index c16b8866b84a..f35d1b2e583d 100644
@@ -934,6 +934,35 @@ int traceeval_insert(struct traceeval *teval,
 		return update_entry(teval, entry, vals);
 }
 
+/**
+ * traceeval_remove - remove an item from the traceeval descriptor
+ * @teval: The descriptor to insert into
+ * @keys: The list of keys that defines what is being removed
+ *
+ * This is the opposite of traceeval_insert(). Instead of inserting
+ * an item into the traceeval historgram, it removes it.
+ *
+ * Returns 1 if it found and removed an item,
+ *         0 if it did not find an time matching @keys
+ *        -1 if there was an error.
+ */
+int traceeval_remove(struct traceeval *teval,
+		     const union traceeval_data *keys)
+{
+	struct hash_table *hist = teval->hist;
+	struct entry *entry;
+	int check;
+
+	entry = NULL;
+	check = get_entry(teval, keys, &entry);
+
+	if (check < 1)
+		return check;
+
+	hash_remove(hist, &entry->hash);
+	return 1;
+}
+
 /**
  * traceeval_iterator_put - release a given iterator
  * @iter: The iterartor to release
Signature · 1 line
2.40.1
Reviewed-by on v3 18/18
#23 lore raw

Re: [PATCH v3 18/18] libtraceeval: Add traceeval_remove()

Quoted 31 lines · Steven Rostedt

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

Add a way to remove an entry that's in the traceeval. Currently there's only ways to add entries, but there also needs to be a way to remove them too.

---
 include/traceeval-hist.h |  3 +++
 src/histograms.c         | 29 +++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)
include/traceeval-hist.h +3 -0
index 37147d626fbb..8e5a6451f399 100644
@@ -160,6 +160,9 @@ int traceeval_insert(struct traceeval *teval,
 		     const union traceeval_data *keys,
 		     const union traceeval_data *vals);
 
+int traceeval_remove(struct traceeval *teval,
+		     const union traceeval_data *keys);
+
 int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
 		    const union traceeval_data **results);
 
src/histograms.c +0 -0
index c16b8866b84a..f35d1b2e583d 100644

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

Quoted 6 lines
src/histograms.c +3 -0
@@ -934,6 +934,35 @@ int traceeval_insert(struct traceeval *teval,
 		return update_entry(teval, entry, vals);
 }
 
+/**
+ * traceeval_remove - remove an item from the traceeval descriptor
+ * @teval: The descriptor to insert into

remove from

Aside from that one nit you can add:

Quoted 32 lines
src/histograms.c +26 -0
+ * @keys: The list of keys that defines what is being removed
+ *
+ * This is the opposite of traceeval_insert(). Instead of inserting
+ * an item into the traceeval historgram, it removes it.
+ *
+ * Returns 1 if it found and removed an item,
+ *         0 if it did not find an time matching @keys
+ *        -1 if there was an error.
+ */
+int traceeval_remove(struct traceeval *teval,
+		     const union traceeval_data *keys)
+{
+	struct hash_table *hist = teval->hist;
+	struct entry *entry;
+	int check;
+
+	entry = NULL;
+	check = get_entry(teval, keys, &entry);
+
+	if (check < 1)
+		return check;
+
+	hash_remove(hist, &entry->hash);
+	return 1;
+}
+
 /**
  * traceeval_iterator_put - release a given iterator
  * @iter: The iterartor to release
Signature · 1 line
2.40.1
#24 lore raw
In reply to Steven Rostedt
Quoted 1 line · On Wed, 16 Aug 2023 21:32:52 -0400 Steven Rostedt <[email protected]> wrote:

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

The diff between this version and the last version:

include/traceeval-hist.h +5 -2
index 315b66adb7ee..8e5a6451f399 100644
@@ -67,11 +67,11 @@ union traceeval_data {
 struct traceeval_type;
 struct traceeval;
 
-/* struct traceeval_dynamic release function signature */
+/* release function callback on traceeval_data */
 typedef void (*traceeval_data_release_fn)(const struct traceeval_type *type,
 					  union traceeval_data *data);
 
-/* struct traceeval_dynamic compare function signature */
+/* compare function callback to compare traceeval_data */
 typedef int (*traceeval_data_cmp_fn)(struct traceeval *teval,
 				     const struct traceeval_type *type,
 				     const union traceeval_data *A,
@@ -160,6 +160,9 @@ int traceeval_insert(struct traceeval *teval,
 		     const union traceeval_data *keys,
 		     const union traceeval_data *vals);
 
+int traceeval_remove(struct traceeval *teval,
+		     const union traceeval_data *keys);
+
 int traceeval_query(struct traceeval *teval, const union traceeval_data *keys,
 		    const union traceeval_data **results);
 
samples/task-eval.c +18 -4
index e0ef2d0bcb30..66d0c40dc0c8 100644
@@ -215,7 +215,9 @@ static void update_process(struct task_data *tdata, const char *comm,
 		return;
 	case STOP:
 		ret = traceeval_query(tdata->teval_processes.start, keys, &results);
-		if (ret <= 0)
+		if (ret < 0)
+			pdie("Could not query start process");
+		if (ret == 0)
 			return;
 		if (!results[0].number_64)
 			break;
@@ -264,7 +266,9 @@ get_process_data(struct task_data *tdata, const char *comm)
 	int ret;
 
 	ret = traceeval_query(tdata->teval_processes_data, keys, &results);
-	if (ret <= 0)
+	if (ret < 0)
+		pdie("Could not query process data");
+	if (ret == 0)
 		return NULL;
 
 	data = results[0].pointer;
@@ -289,6 +293,8 @@ void set_process_data(struct task_data *tdata, const char *comm, void *data)
 	ret = traceeval_query(tdata->teval_processes_data, keys, &results);
 	if (ret > 0)
 		goto out; /* It already exists ? */
+	if (ret < 0)
+		pdie("Could not query process data");
 
 	new_vals[0].pointer = data;
 	ret = traceeval_insert(tdata->teval_processes_data, keys, new_vals);
@@ -328,13 +334,17 @@ static void update_cpu(struct teval_pair *teval_pair, int cpu,
 			if (results[0].number_64)
 				break;
 		}
+		if (ret < 0)
+			pdie("Could not query cpu start data");
 		ret = traceeval_insert(teval_pair->start, keys, vals);
 		if (ret < 0)
 			pdie("Could not start CPU");
 		break;
 	case STOP:
 		ret = traceeval_query(teval_pair->start, keys, &results);
-		if (ret <= 0)
+		if (ret < 0)
+			pdie("Could not query cpu stop data");
+		if (ret == 0)
 			return;
 
 		if (!results[0].number_64)
@@ -400,7 +410,9 @@ static void update_thread(struct process_data *pdata, int tid,
 		return;
 	case STOP:
 		ret = traceeval_query(pdata->teval_threads.start, keys, &results);
-		if (ret <= 0)
+		if (ret < 0)
+			pdie("Could not query thread start");
+		if (ret == 0)
 			return;
 
 		new_vals[0].number_64 = ts - results[0].number_64;
@@ -825,6 +837,8 @@ static void display_processes(struct traceeval *teval,
 		const char *comm = keys[0].cstring;
 
 		ret = traceeval_query(teval_data, keys, &results);
+		if (ret < 0)
+			pdie("Could not query iterator");
 		if (ret < 1)
 			continue; /* ?? */
 
src/eval-local.h +1 -1
index c3ea920ed2e8..5c3918f17cc1 100644
@@ -6,7 +6,7 @@
 
 #define __hidden __attribute__((visibility ("hidden")))
 
-#define offset_of(type, field) (&(((type *)(NULL))->field))
+#define offset_of(type, field) ((size_t)(&(((type *)(NULL))->field)))
 #define container_of(ptr, type, field) \
 	(type *)((void *)(ptr) - (void *)offset_of(type, field))
 
src/hash.c +1 -1
index 221145efcbb9..82962fbba8d8 100644
@@ -78,7 +78,7 @@ __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))
 		return NULL;
 
 	item = iter->next_item;
src/histograms.c +60 -23

204 lines not on this page · raw message

-- Steve

Reviewed-by on the v3 series
#25 lore raw
In reply to Steven Rostedt
Quoted 56 lines · Steven Rostedt

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 v2: https://lore.kernel.org/all/[email protected]/

- Fixed comments on traceeval_dyn_release_fn and traceeval_dyn_cmp_fn (Ross Zwisler)

- Change parameter ordering of compare_traceeval_data (Ross Zwisler)

- Added (size_t) to offset_of() macro (Ross Zwisler)

- Fixed iter->current_bucket off by one bug (Ross Zwisler)

- Removed HASH_MASK() from make_hash()

- Fixed goto fail_release in traceeval_init() (Ross Zwisler)

- Free sort and direction in traceeval_iterator_put() (Ross Zwisler)

- Use num_levels variable instead of (level + 1) (Ross Zwisler)

- Return 0 on success of update_entry (Ross Zwisler)

Quoted 7 lines

- Fixed the cleanup of old values in update_entry() (Ross Zwisler)

- Fixed resetting iter->next = 0 in traceeval_iterator_sort_custom() (Ross Zwisler)

- Added failure checks to task-eval.c sample for all traceeval_query() calls.

- Added traceeval_remove() to remove an item from traceveal (opposite of traceeval_insert())

I think you've addressed all my review comments, you can add:

to the series. Thanks!

Quoted 60 lines

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) (18):
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
libtraceeval: Add traceeval_remove()

 Makefile                 |   4 +-
 include/traceeval-hist.h | 117 +++--
 include/traceeval-test.h |  16 -
 samples/Makefile         |  29 ++
 samples/task-eval.c      | 966 +++++++++++++++++++++++++++++++++++++++
 src/Makefile             |   1 +
 src/eval-local.h         | 123 +++++
 src/hash.c               | 123 +++++
 src/histograms.c         | 923 +++++++++++++++++++++++++++----------
 9 files changed, 2012 insertions(+), 290 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

--
2.40.1

#26 lore raw
Quoted 7 lines · On Thu, 17 Aug 2023 14:13:52 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 1 line

- Added traceeval_remove() to remove an item from traceveal (opposite of traceeval_insert())

I think you've addressed all my review comments, you can add:

to the series. Thanks!

Thanks for taking the time Ross. I'll be posting v4 soon, and hopefully I'll be able to add that.

Then I have yet another series on top of that!!

-- Steve

#27 lore raw
Quoted 7 lines · On Thu, 17 Aug 2023 16:31:19 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines · On Thu, 17 Aug 2023 14:13:52 -0600 Ross Zwisler <[email protected]> wrote:
Quoted 1 line

- Added traceeval_remove() to remove an item from traceveal (opposite of traceeval_insert())

I think you've addressed all my review comments, you can add:

Quoted 8 lines
Quoted 3 lines

to the series. Thanks!

Thanks for taking the time Ross. I'll be posting v4 soon, and hopefully I'll be able to add that.

Then I have yet another series on top of that!!

Because I did some changes to the parameters at the beginning, it caused conflicts through out. I'll post the v4 before adding your Reviewed-by tag, just so you can have one last look at it (I added two patches to do the rename and reorder of the parameters).

-- Steve