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

Nick Alcock nick.alcock at oracle.com
Mon Nov 1 14:29:39 UTC 2021


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.

Signed-off-by: Nick Alcock <nick.alcock at oracle.com>
---
 libdtrace/dt_htab.c | 13 +++++++++++++
 libdtrace/dt_htab.h |  1 +
 2 files changed, 14 insertions(+)

diff --git a/libdtrace/dt_htab.c b/libdtrace/dt_htab.c
index 65296749bf74..e4876fc93042 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.
  */
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);
 
-- 
2.33.1.257.g9e0974a4e8




More information about the DTrace-devel mailing list