Skip to content

Commit

Permalink
selinux: print sum of chain lengths^2 for hash tables
Browse files Browse the repository at this point in the history
Print the sum of chain lengths squared as a metric for hash tables to
provide more insights, similar to avtabs.

While on it add a comma in the avtab message to improve readability of
the output.

Signed-off-by: Christian Göttsche <[email protected]>
Reviewed-by: Stephen Smalley <[email protected]>
Signed-off-by: Paul Moore <[email protected]>
  • Loading branch information
cgzones authored and pcmoore committed Sep 13, 2023
1 parent 34df255 commit fb8142f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion security/selinux/ss/avtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void avtab_hash_eval(struct avtab *h, const char *tag)
}

pr_debug("SELinux: %s: %d entries and %d/%d buckets used, "
"longest chain length %d sum of chain length^2 %llu\n",
"longest chain length %d, sum of chain length^2 %llu\n",
tag, h->nel, slots_used, h->nslot, max_chain_len,
chain2_len_sum);
}
Expand Down
5 changes: 5 additions & 0 deletions security/selinux/ss/hashtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ int hashtab_map(struct hashtab *h,
void hashtab_stat(struct hashtab *h, struct hashtab_info *info)
{
u32 i, chain_len, slots_used, max_chain_len;
u64 chain2_len_sum;
struct hashtab_node *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) {
Expand All @@ -123,11 +125,14 @@ void hashtab_stat(struct hashtab *h, struct hashtab_info *info)

if (chain_len > max_chain_len)
max_chain_len = chain_len;

chain2_len_sum += (u64)chain_len * chain_len;
}
}

info->slots_used = slots_used;
info->max_chain_len = max_chain_len;
info->chain2_len_sum = chain2_len_sum;
}
#endif /* CONFIG_SECURITY_SELINUX_DEBUG */

Expand Down
1 change: 1 addition & 0 deletions security/selinux/ss/hashtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct hashtab {
struct hashtab_info {
u32 slots_used;
u32 max_chain_len;
u64 chain2_len_sum;
};

/*
Expand Down
4 changes: 2 additions & 2 deletions security/selinux/ss/policydb.c
Original file line number Diff line number Diff line change
Expand Up @@ -684,9 +684,9 @@ static void hash_eval(struct hashtab *h, const char *hash_name)
struct hashtab_info info;

hashtab_stat(h, &info);
pr_debug("SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d\n",
pr_debug("SELinux: %s: %d entries and %d/%d buckets used, longest chain length %d, sum of chain length^2 %llu\n",
hash_name, h->nel, info.slots_used, h->size,
info.max_chain_len);
info.max_chain_len, info.chain2_len_sum);
}

static void symtab_hash_eval(struct symtab *s)
Expand Down

0 comments on commit fb8142f

Please sign in to comment.