Skip to contentSkip to conversation

traceeval: Add way to extract the number of elements

PATCH Superseded Steven Rostedt 2 participants 2 messages Sep 28, 2023 – Oct 2, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 traceeval: Add way to extract the number of elements

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

Add traceeval_count() that will return the number of elements in the traceeval. This can be useful for allocating an array to store the elements. By getting the count, the allocation can be done first and then iterated to load the elements into the array.

---
 include/traceeval-hist.h |  2 ++
 src/histograms.c         | 11 +++++++++++
 2 files changed, 13 insertions(+)
include/traceeval-hist.h +2 -0
index eefa48d..44d071f 100644
@@ -204,6 +204,8 @@ int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *k
 void traceeval_results_release(struct traceeval *teval,
 			       const struct traceeval_data *results);
 
+size_t traceeval_count(struct traceeval *teval);
+
 #define traceeval_stat(teval, keys, type)				\
 	traceeval_stat_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), type)
 
src/histograms.c +11 -0
index 2456342..ac3f7f7 100644
@@ -1006,6 +1006,17 @@ int traceeval_remove_size(struct traceeval *teval,
 	return 1;
 }
 
+/**
+ * traceeval_count - Return the number of elements in the traceeval
+ * @teval: The traceeval handle to get the count from
+ *
+ * Returns the number of elements stored by unique keys in the @teval.
+ */
+size_t traceeval_count(struct traceeval *teval)
+{
+	return teval->update_counter;
+}
+
 /**
  * traceeval_iterator_put - release a given iterator
  * @iter: The iterartor to release
Signature · 1 line
2.40.1
#1 lore raw
Quoted 33 lines · Steven Rostedt

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

Add traceeval_count() that will return the number of elements in the traceeval. This can be useful for allocating an array to store the elements. By getting the count, the allocation can be done first and then iterated to load the elements into the array.

---
 include/traceeval-hist.h |  2 ++
 src/histograms.c         | 11 +++++++++++
 2 files changed, 13 insertions(+)
include/traceeval-hist.h +2 -0
index eefa48d..44d071f 100644
@@ -204,6 +204,8 @@ int traceeval_query_size(struct traceeval *teval, const struct traceeval_data *k
 void traceeval_results_release(struct traceeval *teval,
 			       const struct traceeval_data *results);
 
+size_t traceeval_count(struct traceeval *teval);
+
 #define traceeval_stat(teval, keys, type)				\
 	traceeval_stat_size(teval, keys, TRACEEVAL_ARRAY_SIZE(keys), type)
 
src/histograms.c +0 -0
index 2456342..ac3f7f7 100644
@@ -1006,6 +1006,17 @@ int traceeval_remove_size(struct traceeval *teval,
 	return 1;
 }

src/histograms.c int traceeval_remove_size(struct traceeval *teval, · line 1017

Quoted 10 lines
src/histograms.c +9 -0
 
+/**
+ * traceeval_count - Return the number of elements in the traceeval
+ * @teval: The traceeval handle to get the count from
+ *
+ * Returns the number of elements stored by unique keys in the @teval.
+ */
+size_t traceeval_count(struct traceeval *teval)
+{
+	return teval->update_counter;

I don't think this is going to do what you want. 'teval->update_counter' is a generation number, is monotonically increasing, and is incremented any time an element is added or removed from the hash.

I think we either need to actually keep an element count (increment in create_entry(), decrement in traceeval_remove_size() and traceeval_iterator_remove()), or else we're going to have to count the elements by walking the 'struct hash_table' buckets. Probably easier to just keep an explicit element count.

Quoted 8 lines
src/histograms.c +2 -0
+}
+
 /**
  * traceeval_iterator_put - release a given iterator
  * @iter: The iterartor to release
Signature · 1 line
2.40.1