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 <sharpd@cumulusnetworks.com>
This commit is contained in:
Donald Sharp 2017-09-03 18:50:35 -04:00
parent f24fdd9921
commit bd74dc610a
5 changed files with 33 additions and 12 deletions

View File

@ -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");
}
/**

View File

@ -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);

View File

@ -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");
}
}

View File

@ -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);

View File

@ -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 */