Linux-Trace-Devel Archive mirror
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Linux Trace Devel <linux-trace-devel@vger.kernel.org>
Cc: Ross Zwisler <zwisler@chromium.org>
Subject: [PATCH v2] traceeval: Add way to extract the number of elements
Date: Tue, 3 Oct 2023 09:42:06 -0400	[thread overview]
Message-ID: <20231003094206.2e6a5fd6@gandalf.local.home> (raw)

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

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.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
Changes since v1: https://lore.kernel.org/linux-trace-devel/20230928102434.58e08f78@rorschach.local.home

 - Do not use "update_counter" for count, but create a new nr_elements
   to keep track of the number of elements in the traceeval (Ross Zwisler)

 include/traceeval-hist.h |  2 ++
 src/eval-local.h         |  1 +
 src/histograms.c         | 17 +++++++++++++++++
 3 files changed, 20 insertions(+)

diff --git a/include/traceeval-hist.h b/include/traceeval-hist.h
index 0c29977671a3..65a905034773 100644
--- a/include/traceeval-hist.h
+++ b/include/traceeval-hist.h
@@ -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)
 
diff --git a/src/eval-local.h b/src/eval-local.h
index f0917b6a45e9..805ec25ff1f4 100644
--- a/src/eval-local.h
+++ b/src/eval-local.h
@@ -69,6 +69,7 @@ struct traceeval {
 	size_t				nr_key_types;
 	size_t				nr_val_types;
 	size_t				update_counter;
+	size_t				nr_elements;
 	size_t				sizeof_type;
 	size_t				sizeof_data;
 };
diff --git a/src/histograms.c b/src/histograms.c
index bfeb162fffe4..483abaf802dc 100644
--- a/src/histograms.c
+++ b/src/histograms.c
@@ -786,6 +786,7 @@ static int create_entry(struct traceeval *teval,
 	entry->vals = new_vals;
 
 	teval->update_counter++;
+	teval->nr_elements++;
 
 	return 0;
 
@@ -1002,11 +1003,26 @@ int traceeval_remove_size(struct traceeval *teval,
 	hash_remove(hist, &entry->hash);
 	free_entry(teval, entry);
 
+	/* update_counter is used to know if there was an update. */
 	teval->update_counter++;
 
+	/* nr_elements keeps track of the number of stored elemnets */
+	teval->nr_elements--;
+
 	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->nr_elements;
+}
+
 /**
  * traceeval_iterator_put - release a given iterator
  * @iter: The iterartor to release
@@ -1457,6 +1473,7 @@ int traceeval_iterator_remove(struct traceeval_iterator *iter)
 	/* The entry no longer exists */
 	iter->entries[iter->next - 1] = NULL;
 	teval->update_counter++;
+	teval->nr_elements--;
 
 	return 1;
 }
-- 
2.40.1


             reply	other threads:[~2023-10-03 13:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-03 13:42 Steven Rostedt [this message]
2023-10-03 15:29 ` [PATCH v2] traceeval: Add way to extract the number of elements Ross Zwisler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231003094206.2e6a5fd6@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=zwisler@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).