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