xref: /openbsd-src/sbin/unwind/libunbound/util/storage/slabhash.c (revision 096314fef8a8f610abc29edd0a9e7e61b7ff5976)
1ae8c6e27Sflorian /*
2ae8c6e27Sflorian  * util/storage/slabhash.c - hashtable consisting of several smaller tables.
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * This software is open source.
7ae8c6e27Sflorian  *
8ae8c6e27Sflorian  * Redistribution and use in source and binary forms, with or without
9ae8c6e27Sflorian  * modification, are permitted provided that the following conditions
10ae8c6e27Sflorian  * are met:
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Redistributions of source code must retain the above copyright notice,
13ae8c6e27Sflorian  * this list of conditions and the following disclaimer.
14ae8c6e27Sflorian  *
15ae8c6e27Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16ae8c6e27Sflorian  * this list of conditions and the following disclaimer in the documentation
17ae8c6e27Sflorian  * and/or other materials provided with the distribution.
18ae8c6e27Sflorian  *
19ae8c6e27Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20ae8c6e27Sflorian  * be used to endorse or promote products derived from this software without
21ae8c6e27Sflorian  * specific prior written permission.
22ae8c6e27Sflorian  *
23ae8c6e27Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24ae8c6e27Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25ae8c6e27Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26ae8c6e27Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27ae8c6e27Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28ae8c6e27Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29ae8c6e27Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30ae8c6e27Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31ae8c6e27Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32ae8c6e27Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33ae8c6e27Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ae8c6e27Sflorian  */
35ae8c6e27Sflorian 
36ae8c6e27Sflorian /**
37ae8c6e27Sflorian  * \file
38ae8c6e27Sflorian  *
39ae8c6e27Sflorian  * Implementation of hash table that consists of smaller hash tables.
40ae8c6e27Sflorian  * This results in a partitioned lruhash table.
41ae8c6e27Sflorian  * It cannot grow, but that gives it the ability to have multiple
42ae8c6e27Sflorian  * locks. Also this means there are multiple LRU lists.
43ae8c6e27Sflorian  */
44ae8c6e27Sflorian 
45ae8c6e27Sflorian #include "config.h"
46ae8c6e27Sflorian #include "util/storage/slabhash.h"
47ae8c6e27Sflorian 
slabhash_create(size_t numtables,size_t start_size,size_t maxmem,lruhash_sizefunc_type sizefunc,lruhash_compfunc_type compfunc,lruhash_delkeyfunc_type delkeyfunc,lruhash_deldatafunc_type deldatafunc,void * arg)48ae8c6e27Sflorian struct slabhash* slabhash_create(size_t numtables, size_t start_size,
49ae8c6e27Sflorian 	size_t maxmem, lruhash_sizefunc_type sizefunc,
50ae8c6e27Sflorian 	lruhash_compfunc_type compfunc, lruhash_delkeyfunc_type delkeyfunc,
51ae8c6e27Sflorian 	lruhash_deldatafunc_type deldatafunc, void* arg)
52ae8c6e27Sflorian {
53ae8c6e27Sflorian 	size_t i;
54ae8c6e27Sflorian 	struct slabhash* sl = (struct slabhash*)calloc(1,
55ae8c6e27Sflorian 		sizeof(struct slabhash));
56ae8c6e27Sflorian 	if(!sl) return NULL;
57ae8c6e27Sflorian 	sl->size = numtables;
58ae8c6e27Sflorian 	log_assert(sl->size > 0);
59ae8c6e27Sflorian 	sl->array = (struct lruhash**)calloc(sl->size, sizeof(struct lruhash*));
60ae8c6e27Sflorian 	if(!sl->array) {
61ae8c6e27Sflorian 		free(sl);
62ae8c6e27Sflorian 		return NULL;
63ae8c6e27Sflorian 	}
64ae8c6e27Sflorian 	sl->mask = (uint32_t)(sl->size - 1);
65ae8c6e27Sflorian 	if(sl->mask == 0) {
66ae8c6e27Sflorian 		sl->shift = 0;
67ae8c6e27Sflorian 	} else {
68ae8c6e27Sflorian 		log_assert( (sl->size & sl->mask) == 0
69ae8c6e27Sflorian 			/* size must be power of 2 */ );
70ae8c6e27Sflorian 		sl->shift = 0;
71ae8c6e27Sflorian 		while(!(sl->mask & 0x80000000)) {
72ae8c6e27Sflorian 			sl->mask <<= 1;
73ae8c6e27Sflorian 			sl->shift ++;
74ae8c6e27Sflorian 		}
75ae8c6e27Sflorian 	}
76ae8c6e27Sflorian 	for(i=0; i<sl->size; i++) {
77ae8c6e27Sflorian 		sl->array[i] = lruhash_create(start_size, maxmem / sl->size,
78ae8c6e27Sflorian 			sizefunc, compfunc, delkeyfunc, deldatafunc, arg);
79ae8c6e27Sflorian 		if(!sl->array[i]) {
80ae8c6e27Sflorian 			slabhash_delete(sl);
81ae8c6e27Sflorian 			return NULL;
82ae8c6e27Sflorian 		}
83ae8c6e27Sflorian 	}
84ae8c6e27Sflorian 	return sl;
85ae8c6e27Sflorian }
86ae8c6e27Sflorian 
slabhash_delete(struct slabhash * sl)87ae8c6e27Sflorian void slabhash_delete(struct slabhash* sl)
88ae8c6e27Sflorian {
89ae8c6e27Sflorian 	if(!sl)
90ae8c6e27Sflorian 		return;
91ae8c6e27Sflorian 	if(sl->array) {
92ae8c6e27Sflorian 		size_t i;
93ae8c6e27Sflorian 		for(i=0; i<sl->size; i++)
94ae8c6e27Sflorian 			lruhash_delete(sl->array[i]);
95ae8c6e27Sflorian 		free(sl->array);
96ae8c6e27Sflorian 	}
97ae8c6e27Sflorian 	free(sl);
98ae8c6e27Sflorian }
99ae8c6e27Sflorian 
slabhash_clear(struct slabhash * sl)100ae8c6e27Sflorian void slabhash_clear(struct slabhash* sl)
101ae8c6e27Sflorian {
102ae8c6e27Sflorian 	size_t i;
103ae8c6e27Sflorian 	if(!sl)
104ae8c6e27Sflorian 		return;
105ae8c6e27Sflorian 	for(i=0; i<sl->size; i++)
106ae8c6e27Sflorian 		lruhash_clear(sl->array[i]);
107ae8c6e27Sflorian }
108ae8c6e27Sflorian 
109ae8c6e27Sflorian /** helper routine to calculate the slabhash index */
110ae8c6e27Sflorian static unsigned int
slab_idx(struct slabhash * sl,hashvalue_type hash)111ae8c6e27Sflorian slab_idx(struct slabhash* sl, hashvalue_type hash)
112ae8c6e27Sflorian {
113ae8c6e27Sflorian 	return ((hash & sl->mask) >> sl->shift);
114ae8c6e27Sflorian }
115ae8c6e27Sflorian 
slabhash_insert(struct slabhash * sl,hashvalue_type hash,struct lruhash_entry * entry,void * data,void * arg)116ae8c6e27Sflorian void slabhash_insert(struct slabhash* sl, hashvalue_type hash,
117ae8c6e27Sflorian 	struct lruhash_entry* entry, void* data, void* arg)
118ae8c6e27Sflorian {
119ae8c6e27Sflorian 	lruhash_insert(sl->array[slab_idx(sl, hash)], hash, entry, data, arg);
120ae8c6e27Sflorian }
121ae8c6e27Sflorian 
slabhash_lookup(struct slabhash * sl,hashvalue_type hash,void * key,int wr)122ae8c6e27Sflorian struct lruhash_entry* slabhash_lookup(struct slabhash* sl,
123ae8c6e27Sflorian 	hashvalue_type hash, void* key, int wr)
124ae8c6e27Sflorian {
125ae8c6e27Sflorian 	return lruhash_lookup(sl->array[slab_idx(sl, hash)], hash, key, wr);
126ae8c6e27Sflorian }
127ae8c6e27Sflorian 
slabhash_remove(struct slabhash * sl,hashvalue_type hash,void * key)128ae8c6e27Sflorian void slabhash_remove(struct slabhash* sl, hashvalue_type hash, void* key)
129ae8c6e27Sflorian {
130ae8c6e27Sflorian 	lruhash_remove(sl->array[slab_idx(sl, hash)], hash, key);
131ae8c6e27Sflorian }
132ae8c6e27Sflorian 
slabhash_status(struct slabhash * sl,const char * id,int extended)133ae8c6e27Sflorian void slabhash_status(struct slabhash* sl, const char* id, int extended)
134ae8c6e27Sflorian {
135ae8c6e27Sflorian 	size_t i;
136ae8c6e27Sflorian 	char num[17];
137ae8c6e27Sflorian 	log_info("Slabhash %s: %u tables mask=%x shift=%d",
138ae8c6e27Sflorian 		id, (unsigned)sl->size, (unsigned)sl->mask, sl->shift);
139ae8c6e27Sflorian 	for(i=0; i<sl->size; i++) {
140ae8c6e27Sflorian 		snprintf(num, sizeof(num), "table %u", (unsigned)i);
141ae8c6e27Sflorian 		lruhash_status(sl->array[i], num, extended);
142ae8c6e27Sflorian 	}
143ae8c6e27Sflorian }
144ae8c6e27Sflorian 
slabhash_get_size(struct slabhash * sl)145ae8c6e27Sflorian size_t slabhash_get_size(struct slabhash* sl)
146ae8c6e27Sflorian {
147ae8c6e27Sflorian 	size_t i, total = 0;
148ae8c6e27Sflorian 	for(i=0; i<sl->size; i++) {
149ae8c6e27Sflorian 		lock_quick_lock(&sl->array[i]->lock);
150ae8c6e27Sflorian 		total += sl->array[i]->space_max;
151ae8c6e27Sflorian 		lock_quick_unlock(&sl->array[i]->lock);
152ae8c6e27Sflorian 	}
153ae8c6e27Sflorian 	return total;
154ae8c6e27Sflorian }
155ae8c6e27Sflorian 
slabhash_is_size(struct slabhash * sl,size_t size,size_t slabs)156ae8c6e27Sflorian int slabhash_is_size(struct slabhash* sl, size_t size, size_t slabs)
157ae8c6e27Sflorian {
158ae8c6e27Sflorian 	/* divide by slabs and then multiply by the number of slabs,
159ae8c6e27Sflorian 	 * because if the size is not an even multiple of slabs, the
160ae8c6e27Sflorian 	 * uneven amount needs to be removed for comparison */
161ae8c6e27Sflorian 	if(!sl) return 0;
162ae8c6e27Sflorian 	if(sl->size != slabs) return 0;
163ae8c6e27Sflorian 	if(slabs == 0) return 0;
164ae8c6e27Sflorian 	if( (size/slabs)*slabs == slabhash_get_size(sl))
165ae8c6e27Sflorian 		return 1;
166ae8c6e27Sflorian 	return 0;
167ae8c6e27Sflorian }
168ae8c6e27Sflorian 
slabhash_update_space_used(struct slabhash * sl,hashvalue_type hash,void * cb_arg,int diff_size)169*096314feSflorian void slabhash_update_space_used(struct slabhash* sl, hashvalue_type hash,
170*096314feSflorian 	void* cb_arg, int diff_size)
171*096314feSflorian {
172*096314feSflorian 	lruhash_update_space_used(sl->array[slab_idx(sl, hash)], cb_arg,
173*096314feSflorian 		diff_size);
174*096314feSflorian }
175*096314feSflorian 
slabhash_get_mem(struct slabhash * sl)176ae8c6e27Sflorian size_t slabhash_get_mem(struct slabhash* sl)
177ae8c6e27Sflorian {
178ae8c6e27Sflorian 	size_t i, total = sizeof(*sl);
179ae8c6e27Sflorian 	total += sizeof(struct lruhash*)*sl->size;
180ae8c6e27Sflorian 	for(i=0; i<sl->size; i++) {
181ae8c6e27Sflorian 		total += lruhash_get_mem(sl->array[i]);
182ae8c6e27Sflorian 	}
183ae8c6e27Sflorian 	return total;
184ae8c6e27Sflorian }
185ae8c6e27Sflorian 
slabhash_gettable(struct slabhash * sl,hashvalue_type hash)186ae8c6e27Sflorian struct lruhash* slabhash_gettable(struct slabhash* sl, hashvalue_type hash)
187ae8c6e27Sflorian {
188ae8c6e27Sflorian 	return sl->array[slab_idx(sl, hash)];
189ae8c6e27Sflorian }
190ae8c6e27Sflorian 
191ae8c6e27Sflorian /* test code, here to avoid linking problems with fptr_wlist */
192ae8c6e27Sflorian /** delete key */
delkey(struct slabhash_testkey * k)193ae8c6e27Sflorian static void delkey(struct slabhash_testkey* k) {
194ae8c6e27Sflorian 	lock_rw_destroy(&k->entry.lock); free(k);}
195ae8c6e27Sflorian /** delete data */
deldata(struct slabhash_testdata * d)196ae8c6e27Sflorian static void deldata(struct slabhash_testdata* d) {free(d);}
197ae8c6e27Sflorian 
test_slabhash_sizefunc(void * ATTR_UNUSED (key),void * ATTR_UNUSED (data))198ae8c6e27Sflorian size_t test_slabhash_sizefunc(void* ATTR_UNUSED(key), void* ATTR_UNUSED(data))
199ae8c6e27Sflorian {
200ae8c6e27Sflorian 	return sizeof(struct slabhash_testkey) +
201ae8c6e27Sflorian 		sizeof(struct slabhash_testdata);
202ae8c6e27Sflorian }
203ae8c6e27Sflorian 
test_slabhash_compfunc(void * key1,void * key2)204ae8c6e27Sflorian int test_slabhash_compfunc(void* key1, void* key2)
205ae8c6e27Sflorian {
206ae8c6e27Sflorian 	struct slabhash_testkey* k1 = (struct slabhash_testkey*)key1;
207ae8c6e27Sflorian 	struct slabhash_testkey* k2 = (struct slabhash_testkey*)key2;
208ae8c6e27Sflorian 	if(k1->id == k2->id)
209ae8c6e27Sflorian 		return 0;
210ae8c6e27Sflorian 	if(k1->id > k2->id)
211ae8c6e27Sflorian 		return 1;
212ae8c6e27Sflorian 	return -1;
213ae8c6e27Sflorian }
214ae8c6e27Sflorian 
test_slabhash_delkey(void * key,void * ATTR_UNUSED (arg))215ae8c6e27Sflorian void test_slabhash_delkey(void* key, void* ATTR_UNUSED(arg))
216ae8c6e27Sflorian {
217ae8c6e27Sflorian 	delkey((struct slabhash_testkey*)key);
218ae8c6e27Sflorian }
219ae8c6e27Sflorian 
test_slabhash_deldata(void * data,void * ATTR_UNUSED (arg))220ae8c6e27Sflorian void test_slabhash_deldata(void* data, void* ATTR_UNUSED(arg))
221ae8c6e27Sflorian {
222ae8c6e27Sflorian 	deldata((struct slabhash_testdata*)data);
223ae8c6e27Sflorian }
224ae8c6e27Sflorian 
slabhash_setmarkdel(struct slabhash * sl,lruhash_markdelfunc_type md)225ae8c6e27Sflorian void slabhash_setmarkdel(struct slabhash* sl, lruhash_markdelfunc_type md)
226ae8c6e27Sflorian {
227ae8c6e27Sflorian 	size_t i;
228ae8c6e27Sflorian 	for(i=0; i<sl->size; i++) {
229ae8c6e27Sflorian 		lruhash_setmarkdel(sl->array[i], md);
230ae8c6e27Sflorian 	}
231ae8c6e27Sflorian }
232ae8c6e27Sflorian 
slabhash_traverse(struct slabhash * sh,int wr,void (* func)(struct lruhash_entry *,void *),void * arg)233ae8c6e27Sflorian void slabhash_traverse(struct slabhash* sh, int wr,
234ae8c6e27Sflorian 	void (*func)(struct lruhash_entry*, void*), void* arg)
235ae8c6e27Sflorian {
236ae8c6e27Sflorian 	size_t i;
237ae8c6e27Sflorian 	for(i=0; i<sh->size; i++)
238ae8c6e27Sflorian 		lruhash_traverse(sh->array[i], wr, func, arg);
239ae8c6e27Sflorian }
240ae8c6e27Sflorian 
count_slabhash_entries(struct slabhash * sh)241ae8c6e27Sflorian size_t count_slabhash_entries(struct slabhash* sh)
242ae8c6e27Sflorian {
243ae8c6e27Sflorian 	size_t slab, cnt = 0;
244ae8c6e27Sflorian 
245ae8c6e27Sflorian 	for(slab=0; slab<sh->size; slab++) {
246ae8c6e27Sflorian 		lock_quick_lock(&sh->array[slab]->lock);
247ae8c6e27Sflorian 		cnt += sh->array[slab]->num;
248ae8c6e27Sflorian 		lock_quick_unlock(&sh->array[slab]->lock);
249ae8c6e27Sflorian 	}
250ae8c6e27Sflorian 	return cnt;
251ae8c6e27Sflorian }
252d500c338Sflorian 
get_slabhash_stats(struct slabhash * sh,long long * num,long long * collisions)253d500c338Sflorian void get_slabhash_stats(struct slabhash* sh, long long* num, long long* collisions)
254d500c338Sflorian {
255d500c338Sflorian 	size_t slab, cnt = 0, max_collisions = 0;
256d500c338Sflorian 
257d500c338Sflorian 	for(slab=0; slab<sh->size; slab++) {
258d500c338Sflorian 		lock_quick_lock(&sh->array[slab]->lock);
259d500c338Sflorian 		cnt += sh->array[slab]->num;
260d500c338Sflorian 		if (max_collisions < sh->array[slab]->max_collisions) {
261d500c338Sflorian 			max_collisions = sh->array[slab]->max_collisions;
262d500c338Sflorian 		}
263d500c338Sflorian 		lock_quick_unlock(&sh->array[slab]->lock);
264d500c338Sflorian 	}
265d500c338Sflorian 	if (num != NULL)
266d500c338Sflorian 		*num = cnt;
267d500c338Sflorian 	if (collisions != NULL)
268d500c338Sflorian 		*collisions = max_collisions;
269d500c338Sflorian }
270