[DTrace-devel] [PATCH v3 03/11] htab: add dt_htab_entries

Nick Alcock nick.alcock at oracle.com
Tue Nov 30 23:11:44 UTC 2021


On 30 Nov 2021, Kris Van Hees outgrape:

> Since you introduce a total number of entries for a hash, perhaps also change
> the dt_htab_stats() function since it no longer has to calculate that count
> anymore

Definitely, like so:

--------- >8 --------------
>From 532dc4b7a345a4c6dcb3a797558dbe6da93e80e5 Mon Sep 17 00:00:00 2001
From: Nick Alcock <nick.alcock at oracle.com>
Date: Tue, 12 Oct 2021 14:11:41 +0100
Subject: [PATCH] htab: add dt_htab_entries

This returns the number of entries added to the hashtable.

We also fix a bug in the per-bucket entry count (used only by
dt_htab_stats): it was being incremented on addition but never
decremented on removal, even though dt_htab_ops.del had better be an
inverse of dt_htab_ops.add, and nentries was being incremented on
addition -- and we don't have tombstones in the bucket chain list, so
the number of nentries in a bucket really does fall on removal.

We drop the code in dt_htab_stats which counts entries, since
the count is never actually used.

Signed-off-by: Nick Alcock <nick.alcock at oracle.com>
Reviewed-by: Eugene Loh <eugene.loh at oracle.com>
---
 libdtrace/dt_htab.c | 15 +++++++++++++--
 libdtrace/dt_htab.h |  1 +
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/libdtrace/dt_htab.c b/libdtrace/dt_htab.c
index 65296749bf74..9ac2901648d1 100644
--- a/libdtrace/dt_htab.c
+++ b/libdtrace/dt_htab.c
@@ -43,6 +43,7 @@ struct dt_htab {
 	int		size;
 	int		mask;
 	int		nbuckets;
+	size_t		nentries;
 	dt_htab_ops_t	*ops;
 };
 
@@ -59,6 +60,7 @@ dt_htab_t *dt_htab_create(dtrace_hdl_t *dtp, dt_htab_ops_t *ops)
 	htab->size = 1;
 	htab->mask = htab->size - 1;
 	htab->nbuckets = 0;
+	htab->nentries = 0;
 	htab->ops = ops;
 
 	htab->tab = dt_calloc(dtp, htab->size, sizeof(dt_hbucket_t *));
@@ -158,6 +160,7 @@ retry:
 add:
 	bucket->head = htab->ops->add(bucket->head, entry);
 	bucket->nentries++;
+	htab->nentries++;
 
 	return 0;
 }
@@ -199,6 +202,8 @@ int dt_htab_delete(dt_htab_t *htab, void *entry)
 		return -ENOENT;
 
 	head = htab->ops->del(bucket->head, entry);
+	bucket->nentries--;
+	htab->nentries--;
 	if (!head) {
 		dt_hbucket_t	*b = htab->tab[idx];
 
@@ -219,6 +224,14 @@ int dt_htab_delete(dt_htab_t *htab, void *entry)
 	return 0;
 }
 
+/*
+ * Return the number of entries in the hashtable.
+ */
+size_t dt_htab_entries(const dt_htab_t *htab)
+{
+	return htab->nentries;
+}
+
 /*
  * Report statistics on the given hashtable.
  */
@@ -227,7 +240,6 @@ void dt_htab_stats(const char *name, const dt_htab_t *htab)
 	int	i;
 	int	slotc = 0;
 	int	bckc = 0;
-	int	entc = 0;
 	int	maxbckinslot = 0;
 	int	maxentinbck = 0;
 	int	maxentinslot = 0;
@@ -262,7 +274,6 @@ void dt_htab_stats(const char *name, const dt_htab_t *htab)
 		entinslot += entryc;
 		entinbck += entryc;
 		bckc += bucketc;
-		entc += entryc;
 	}
 
 	if (!slotc) {
diff --git a/libdtrace/dt_htab.h b/libdtrace/dt_htab.h
index 3a716357362f..e12c96fc9006 100644
--- a/libdtrace/dt_htab.h
+++ b/libdtrace/dt_htab.h
@@ -88,6 +88,7 @@ extern dt_htab_t *dt_htab_create(struct dtrace_hdl *dtp, dt_htab_ops_t *ops);
 extern void dt_htab_destroy(struct dtrace_hdl *dtp, dt_htab_t *htab);
 extern int dt_htab_insert(dt_htab_t *htab, void *entry);
 extern void *dt_htab_lookup(const dt_htab_t *htab, const void *entry);
+extern size_t dt_htab_entries(const dt_htab_t *htab);
 extern int dt_htab_delete(dt_htab_t *htab, void *entry);
 extern void dt_htab_stats(const char *name, const dt_htab_t *htab);
 
--------- >8 --------------



More information about the DTrace-devel mailing list