diff mbox series

[1/5] libsepol: include length squared in hashtab_hash_eval()

Message ID 20230816123845.80171-1-cgzones@googlemail.com (mailing list archive)
State Accepted
Commit 26cec7caa7bd
Delegated to: Petr Lautrbach
Headers show
Series [1/5] libsepol: include length squared in hashtab_hash_eval() | expand

Commit Message

Christian Göttsche Aug. 16, 2023, 12:38 p.m. UTC
Include the chain length squared sum as metric in the debug function
hashtab_hash_eval(), adopted from the kernel avtab.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libsepol/src/hashtab.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/libsepol/src/hashtab.c b/libsepol/src/hashtab.c
index b1a9bdc2..2af3a9bf 100644
--- a/libsepol/src/hashtab.c
+++ b/libsepol/src/hashtab.c
@@ -243,11 +243,12 @@  int hashtab_map(hashtab_t h,
 void hashtab_hash_eval(hashtab_t h, const char *tag)
 {
 	unsigned int i;
-	size_t chain_len, slots_used, max_chain_len;
+	size_t chain_len, slots_used, max_chain_len, chain2_len_sum;
 	hashtab_ptr_t cur;
 
 	slots_used = 0;
 	max_chain_len = 0;
+	chain2_len_sum = 0;
 	for (i = 0; i < h->size; i++) {
 		cur = h->htable[i];
 		if (cur) {
@@ -260,10 +261,12 @@  void hashtab_hash_eval(hashtab_t h, const char *tag)
 
 			if (chain_len > max_chain_len)
 				max_chain_len = chain_len;
+			chain2_len_sum += chain_len * chain_len;
 		}
 	}
 
 	printf
-	    ("%s:  %d entries and %zu/%d buckets used, longest chain length %zu\n",
-	     tag, h->nel, slots_used, h->size, max_chain_len);
+	    ("%s:  %d entries and %zu/%d buckets used, longest chain length %zu, chain length^2 %zu, normalized chain length^2 %.2f\n",
+	     tag, h->nel, slots_used, h->size, max_chain_len, chain2_len_sum,
+	     chain2_len_sum ? (float)chain2_len_sum / slots_used : 0);
 }