From bd74dc610a2069f8549a26668940ef655f51598d Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Sun, 3 Sep 2017 18:50:35 -0400 Subject: [PATCH] lib: Hash creation cleanup 1) Some hash key functions where converting pointers directly to a 32 bit value via downcasting. Pointers are 64 bit on a majority of our platforms. 2) Some hashes were being created with 256 entries, downsize the hash creation size to more appropriate values. 3) Add hash names to hash creation so we can watch the hash via 'show debugging hashtable' Signed-off-by: Donald Sharp --- lib/command.c | 9 +++++++-- lib/if_rmap.c | 5 ++++- lib/qobj.c | 5 ++++- lib/routemap.c | 15 ++++++++++----- lib/thread.c | 11 ++++++++--- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/lib/command.c b/lib/command.c index a30378137..d1b086737 100644 --- a/lib/command.c +++ b/lib/command.c @@ -43,6 +43,7 @@ #include "qobj.h" #include "defaults.h" #include "libfrr.h" +#include "jhash.h" DEFINE_MTYPE(LIB, HOST, "Host config") DEFINE_MTYPE(LIB, STRVEC, "String vector") @@ -278,7 +279,9 @@ int argv_find(struct cmd_token **argv, int argc, const char *text, int *index) static unsigned int cmd_hash_key(void *p) { - return (uintptr_t)p; + int size = sizeof(p); + + return jhash(p, size, 0); } static int cmd_hash_cmp(const void *a, const void *b) @@ -298,7 +301,9 @@ void install_node(struct cmd_node *node, int (*func)(struct vty *)) cmd_token_new(START_TKN, CMD_ATTR_NORMAL, NULL, NULL); graph_new_node(node->cmdgraph, token, (void (*)(void *)) & cmd_token_del); - node->cmd_hash = hash_create(cmd_hash_key, cmd_hash_cmp, NULL); + node->cmd_hash = hash_create_size(16, cmd_hash_key, + cmd_hash_cmp, + "Command Hash"); } /** diff --git a/lib/if_rmap.c b/lib/if_rmap.c index 968c087c3..53c2824a9 100644 --- a/lib/if_rmap.c +++ b/lib/if_rmap.c @@ -294,7 +294,10 @@ void if_rmap_reset() void if_rmap_init(int node) { - ifrmaphash = hash_create(if_rmap_hash_make, if_rmap_hash_cmp, NULL); + ifrmaphash = hash_create_size(4, + if_rmap_hash_make, + if_rmap_hash_cmp, + "Interface Route-Map Hash"); if (node == RIPNG_NODE) { } else if (node == RIP_NODE) { install_element(RIP_NODE, &if_rmap_cmd); diff --git a/lib/qobj.c b/lib/qobj.c index 5f450ca0d..c75002052 100644 --- a/lib/qobj.c +++ b/lib/qobj.c @@ -25,6 +25,7 @@ #include "hash.h" #include "log.h" #include "qobj.h" +#include "jhash.h" static pthread_rwlock_t nodes_lock; static struct hash *nodes = NULL; @@ -97,7 +98,9 @@ void qobj_init(void) { if (!nodes) { pthread_rwlock_init(&nodes_lock, NULL); - nodes = hash_create(qobj_key, qobj_cmp, NULL); + nodes = hash_create_size(16, qobj_key, + qobj_cmp, + "QOBJ Hash"); } } diff --git a/lib/routemap.c b/lib/routemap.c index 409c9c378..31afc33f5 100644 --- a/lib/routemap.c +++ b/lib/routemap.c @@ -1581,8 +1581,10 @@ static void *route_map_dep_hash_alloc(void *p) dep_entry = XCALLOC(MTYPE_ROUTE_MAP_DEP, sizeof(struct route_map_dep)); dep_entry->dep_name = XSTRDUP(MTYPE_ROUTE_MAP_NAME, dep_name); - dep_entry->dep_rmap_hash = hash_create(route_map_dep_hash_make_key, - route_map_rmap_hash_cmp, NULL); + dep_entry->dep_rmap_hash = hash_create_size(8, + route_map_dep_hash_make_key, + route_map_rmap_hash_cmp, + "Route Map Dep Hash"); dep_entry->this_hash = NULL; return ((void *)dep_entry); @@ -2784,12 +2786,15 @@ void route_map_init(void) route_match_vec = vector_init(1); route_set_vec = vector_init(1); route_map_master_hash = - hash_create(route_map_hash_key_make, route_map_hash_cmp, NULL); + hash_create_size(8, route_map_hash_key_make, + route_map_hash_cmp, + "Route Map Master Hash"); for (i = 1; i < ROUTE_MAP_DEP_MAX; i++) route_map_dep_hash[i] = - hash_create(route_map_dep_hash_make_key, - route_map_dep_hash_cmp, NULL); + hash_create_size(8, route_map_dep_hash_make_key, + route_map_dep_hash_cmp, + "Route Map Dep Hash"); cmd_variable_handler_register(rmap_var_handlers); diff --git a/lib/thread.c b/lib/thread.c index b39f2d55c..0ce38dd34 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -31,6 +31,7 @@ #include "command.h" #include "sigevent.h" #include "network.h" +#include "jhash.h" DEFINE_MTYPE_STATIC(LIB, THREAD, "Thread") DEFINE_MTYPE_STATIC(LIB, THREAD_MASTER, "Thread master") @@ -58,7 +59,9 @@ static struct list *masters; /* CLI start ---------------------------------------------------------------- */ static unsigned int cpu_record_hash_key(struct cpu_thread_history *a) { - return (uintptr_t)a->func; + int size = sizeof (&a->func); + + return jhash(&a->func, size, 0); } static int cpu_record_hash_cmp(const struct cpu_thread_history *a, @@ -376,9 +379,11 @@ struct thread_master *thread_master_create(const char *name) return NULL; } - rv->cpu_record = hash_create( + rv->cpu_record = hash_create_size( + 8, (unsigned int (*)(void *))cpu_record_hash_key, - (int (*)(const void *, const void *))cpu_record_hash_cmp, NULL); + (int (*)(const void *, const void *))cpu_record_hash_cmp, + "Thread Hash"); /* Initialize the timer queues */ -- 2.39.2