xref: /freebsd/contrib/unbound/util/storage/lruhash.h (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 /*
2  * util/storage/lruhash.h - hashtable, hash function, LRU keeping.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains a hashtable with LRU keeping of entries.
40  *
41  * The hash table keeps a maximum memory size. Old entries are removed
42  * to make space for new entries.
43  *
44  * The locking strategy is as follows:
45  * 	o since (almost) every read also implies a LRU update, the
46  *	  hashtable lock is a spinlock, not rwlock.
47  *	o the idea is to move every thread through the hash lock quickly,
48  *	  so that the next thread can access the lookup table.
49  *	o User performs hash function.
50  *
51  * For read:
52  *	o lock hashtable.
53  *		o lookup hash bin.
54  *		o lock hash bin.
55  *			o find entry (if failed, unlock hash, unl bin, exit).
56  *			o swizzle pointers for LRU update.
57  *		o unlock hashtable.
58  *		o lock entry (rwlock).
59  *		o unlock hash bin.
60  *		o work on entry.
61  *	o unlock entry.
62  *
63  * To update an entry, gain writelock and change the entry.
64  * (the entry must keep the same hashvalue, so a data update.)
65  * (you cannot upgrade a readlock to a writelock, because the item may
66  *  be deleted, it would cause race conditions. So instead, unlock and
67  *  relookup it in the hashtable.)
68  *
69  * To delete an entry:
70  *	o unlock the entry if you hold the lock already.
71  *	o lock hashtable.
72  *		o lookup hash bin.
73  *		o lock hash bin.
74  *			o find entry (if failed, unlock hash, unl bin, exit).
75  *			o remove entry from hashtable bin overflow chain.
76  *		o unlock hashtable.
77  *		o lock entry (writelock).
78  *		o unlock hash bin.
79  *	o unlock entry (nobody else should be waiting for this lock,
80  *	  since you removed it from hashtable, and you got writelock while
81  *	  holding the hashbinlock so you are the only one.)
82  * 	  Note you are only allowed to obtain a lock while holding hashbinlock.
83  *	o delete entry.
84  *
85  * The above sequence is:
86  *	o race free, works with read, write and delete.
87  *	o but has a queue, imagine someone needing a writelock on an item.
88  *	  but there are still readlocks. The writelocker waits, but holds
89  *	  the hashbinlock. The next thread that comes in and needs the same
90  * 	  hashbin will wait for the lock while holding the hashtable lock.
91  *	  thus halting the entire system on hashtable.
92  *	  This is because of the delete protection.
93  *	  Readlocks will be easier on the rwlock on entries.
94  *	  While the writer is holding writelock, similar problems happen with
95  *	  a reader or writer needing the same item.
96  *	  the scenario requires more than three threads.
97  * 	o so the queue length is 3 threads in a bad situation. The fourth is
98  *	  unable to use the hashtable.
99  *
100  * If you need to acquire locks on multiple items from the hashtable.
101  *	o you MUST release all locks on items from the hashtable before
102  *	  doing the next lookup/insert/delete/whatever.
103  *	o To acquire multiple items you should use a special routine that
104  *	  obtains the locks on those multiple items in one go.
105  */
106 
107 #ifndef UTIL_STORAGE_LRUHASH_H
108 #define UTIL_STORAGE_LRUHASH_H
109 #include "util/locks.h"
110 struct lruhash_bin;
111 struct lruhash_entry;
112 
113 /** default start size for hash arrays */
114 #define HASH_DEFAULT_STARTARRAY		1024 /* entries in array */
115 /** default max memory for hash arrays */
116 #define HASH_DEFAULT_MAXMEM		4*1024*1024 /* bytes */
117 
118 /** the type of a hash value */
119 typedef uint32_t hashvalue_type;
120 
121 /**
122  * Type of function that calculates the size of an entry.
123  * Result must include the size of struct lruhash_entry.
124  * Keys that are identical must also calculate to the same size.
125  * size = func(key, data).
126  */
127 typedef size_t (*lruhash_sizefunc_type)(void*, void*);
128 
129 /** type of function that compares two keys. return 0 if equal. */
130 typedef int (*lruhash_compfunc_type)(void*, void*);
131 
132 /** old keys are deleted.
133  * The RRset type has to revoke its ID number, markdel() is used first.
134  * This function is called: func(key, userarg) */
135 typedef void (*lruhash_delkeyfunc_type)(void*, void*);
136 
137 /** old data is deleted. This function is called: func(data, userarg). */
138 typedef void (*lruhash_deldatafunc_type)(void*, void*);
139 
140 /** mark a key as pending to be deleted (and not to be used by anyone).
141  * called: func(key) */
142 typedef void (*lruhash_markdelfunc_type)(void*);
143 
144 /**
145  * Hash table that keeps LRU list of entries.
146  */
147 struct lruhash {
148 	/** lock for exclusive access, to the lookup array */
149 	lock_quick_type lock;
150 	/** the size function for entries in this table */
151 	lruhash_sizefunc_type sizefunc;
152 	/** the compare function for entries in this table. */
153 	lruhash_compfunc_type compfunc;
154 	/** how to delete keys. */
155 	lruhash_delkeyfunc_type delkeyfunc;
156 	/** how to delete data. */
157 	lruhash_deldatafunc_type deldatafunc;
158 	/** how to mark a key pending deletion */
159 	lruhash_markdelfunc_type markdelfunc;
160 	/** user argument for user functions */
161 	void* cb_arg;
162 
163 	/** the size of the lookup array */
164 	size_t size;
165 	/** size bitmask - since size is a power of 2 */
166 	int size_mask;
167 	/** lookup array of bins */
168 	struct lruhash_bin* array;
169 
170 	/** the lru list, start and end, noncyclical double linked list. */
171 	struct lruhash_entry* lru_start;
172 	/** lru list end item (least recently used) */
173 	struct lruhash_entry* lru_end;
174 
175 	/** the number of entries in the hash table. */
176 	size_t num;
177 	/** the amount of space used, roughly the number of bytes in use. */
178 	size_t space_used;
179 	/** the amount of space the hash table is maximally allowed to use. */
180 	size_t space_max;
181 };
182 
183 /**
184  * A single bin with a linked list of entries in it.
185  */
186 struct lruhash_bin {
187 	/**
188 	 * Lock for exclusive access to the linked list
189 	 * This lock makes deletion of items safe in this overflow list.
190 	 */
191 	lock_quick_type lock;
192 	/** linked list of overflow entries */
193 	struct lruhash_entry* overflow_list;
194 };
195 
196 /**
197  * An entry into the hash table.
198  * To change overflow_next you need to hold the bin lock.
199  * To change the lru items you need to hold the hashtable lock.
200  * This structure is designed as part of key struct. And key pointer helps
201  * to get the surrounding structure. Data should be allocated on its own.
202  */
203 struct lruhash_entry {
204 	/**
205 	 * rwlock for access to the contents of the entry
206 	 * Note that it does _not_ cover the lru_ and overflow_ ptrs.
207 	 * Even with a writelock, you cannot change hash and key.
208 	 * You need to delete it to change hash or key.
209 	 */
210 	lock_rw_type lock;
211 	/** next entry in overflow chain. Covered by hashlock and binlock. */
212 	struct lruhash_entry* overflow_next;
213 	/** next entry in lru chain. covered by hashlock. */
214 	struct lruhash_entry* lru_next;
215 	/** prev entry in lru chain. covered by hashlock. */
216 	struct lruhash_entry* lru_prev;
217 	/** hash value of the key. It may not change, until entry deleted. */
218 	hashvalue_type hash;
219 	/** key */
220 	void* key;
221 	/** data */
222 	void* data;
223 };
224 
225 /**
226  * Create new hash table.
227  * @param start_size: size of hashtable array at start, must be power of 2.
228  * @param maxmem: maximum amount of memory this table is allowed to use.
229  * @param sizefunc: calculates memory usage of entries.
230  * @param compfunc: compares entries, 0 on equality.
231  * @param delkeyfunc: deletes key.
232  *   Calling both delkey and deldata will also free the struct lruhash_entry.
233  *   Make it part of the key structure and delete it in delkeyfunc.
234  * @param deldatafunc: deletes data.
235  * @param arg: user argument that is passed to user function calls.
236  * @return: new hash table or NULL on malloc failure.
237  */
238 struct lruhash* lruhash_create(size_t start_size, size_t maxmem,
239 	lruhash_sizefunc_type sizefunc, lruhash_compfunc_type compfunc,
240 	lruhash_delkeyfunc_type delkeyfunc,
241 	lruhash_deldatafunc_type deldatafunc, void* arg);
242 
243 /**
244  * Delete hash table. Entries are all deleted.
245  * @param table: to delete.
246  */
247 void lruhash_delete(struct lruhash* table);
248 
249 /**
250  * Clear hash table. Entries are all deleted, while locking them before
251  * doing so. At end the table is empty.
252  * @param table: to make empty.
253  */
254 void lruhash_clear(struct lruhash* table);
255 
256 /**
257  * Insert a new element into the hashtable.
258  * If key is already present data pointer in that entry is updated.
259  * The space calculation function is called with the key, data.
260  * If necessary the least recently used entries are deleted to make space.
261  * If necessary the hash array is grown up.
262  *
263  * @param table: hash table.
264  * @param hash: hash value. User calculates the hash.
265  * @param entry: identifies the entry.
266  * 	If key already present, this entry->key is deleted immediately.
267  *	But entry->data is set to NULL before deletion, and put into
268  * 	the existing entry. The data is then freed.
269  * @param data: the data.
270  * @param cb_override: if not null overrides the cb_arg for the deletefunc.
271  */
272 void lruhash_insert(struct lruhash* table, hashvalue_type hash,
273 	struct lruhash_entry* entry, void* data, void* cb_override);
274 
275 /**
276  * Lookup an entry in the hashtable.
277  * At the end of the function you hold a (read/write)lock on the entry.
278  * The LRU is updated for the entry (if found).
279  * @param table: hash table.
280  * @param hash: hash of key.
281  * @param key: what to look for, compared against entries in overflow chain.
282  *    the hash value must be set, and must work with compare function.
283  * @param wr: set to true if you desire a writelock on the entry.
284  *    with a writelock you can update the data part.
285  * @return: pointer to the entry or NULL. The entry is locked.
286  *    The user must unlock the entry when done.
287  */
288 struct lruhash_entry* lruhash_lookup(struct lruhash* table,
289 	hashvalue_type hash, void* key, int wr);
290 
291 /**
292  * Touch entry, so it becomes the most recently used in the LRU list.
293  * Caller must hold hash table lock. The entry must be inserted already.
294  * @param table: hash table.
295  * @param entry: entry to make first in LRU.
296  */
297 void lru_touch(struct lruhash* table, struct lruhash_entry* entry);
298 
299 /**
300  * Set the markdelfunction (or NULL)
301  */
302 void lruhash_setmarkdel(struct lruhash* table, lruhash_markdelfunc_type md);
303 
304 /************************* getdns functions ************************/
305 /*** these are used by getdns only and not by unbound. ***/
306 
307 /**
308  * Demote entry, so it becomes the least recently used in the LRU list.
309  * Caller must hold hash table lock. The entry must be inserted already.
310  * @param table: hash table.
311  * @param entry: entry to make last in LRU.
312  */
313 void lru_demote(struct lruhash* table, struct lruhash_entry* entry);
314 
315 /**
316  * Insert a new element into the hashtable, or retrieve the corresponding
317  * element of it exits.
318  *
319  * If key is already present data pointer in that entry is kept.
320  * If it is not present, a new entry is created. In that case,
321  * the space calculation function is called with the key, data.
322  * If necessary the least recently used entries are deleted to make space.
323  * If necessary the hash array is grown up.
324  *
325  * @param table: hash table.
326  * @param hash: hash value. User calculates the hash.
327  * @param entry: identifies the entry.
328  * @param data: the data.
329  * @param cb_arg: if not null overrides the cb_arg for the deletefunc.
330  * @return: pointer to the existing entry if the key was already present,
331  *     or to the entry argument if it was not.
332  */
333 struct lruhash_entry* lruhash_insert_or_retrieve(struct lruhash* table, hashvalue_type hash,
334         struct lruhash_entry* entry, void* data, void* cb_arg);
335 
336 /************************* Internal functions ************************/
337 /*** these are only exposed for unit tests. ***/
338 
339 /**
340  * Remove entry from hashtable. Does nothing if not found in hashtable.
341  * Delfunc is called for the entry.
342  * @param table: hash table.
343  * @param hash: hash of key.
344  * @param key: what to look for.
345  */
346 void lruhash_remove(struct lruhash* table, hashvalue_type hash, void* key);
347 
348 /** init the hash bins for the table */
349 void bin_init(struct lruhash_bin* array, size_t size);
350 
351 /** delete the hash bin and entries inside it */
352 void bin_delete(struct lruhash* table, struct lruhash_bin* bin);
353 
354 /**
355  * Find entry in hash bin. You must have locked the bin.
356  * @param table: hash table with function pointers.
357  * @param bin: hash bin to look into.
358  * @param hash: hash value to look for.
359  * @param key: key to look for.
360  * @return: the entry or NULL if not found.
361  */
362 struct lruhash_entry* bin_find_entry(struct lruhash* table,
363 	struct lruhash_bin* bin, hashvalue_type hash, void* key);
364 
365 /**
366  * Remove entry from bin overflow chain.
367  * You must have locked the bin.
368  * @param bin: hash bin to look into.
369  * @param entry: entry ptr that needs removal.
370  */
371 void bin_overflow_remove(struct lruhash_bin* bin,
372 	struct lruhash_entry* entry);
373 
374 /**
375  * Split hash bin into two new ones. Based on increased size_mask.
376  * Caller must hold hash table lock.
377  * At the end the routine acquires all hashbin locks (in the old array).
378  * This makes it wait for other threads to finish with the bins.
379  * So the bins are ready to be deleted after this function.
380  * @param table: hash table with function pointers.
381  * @param newa: new increased array.
382  * @param newmask: new lookup mask.
383  */
384 void bin_split(struct lruhash* table, struct lruhash_bin* newa,
385 	int newmask);
386 
387 /**
388  * Try to make space available by deleting old entries.
389  * Assumes that the lock on the hashtable is being held by caller.
390  * Caller must not hold bin locks.
391  * @param table: hash table.
392  * @param list: list of entries that are to be deleted later.
393  *	Entries have been removed from the hash table and writelock is held.
394  */
395 void reclaim_space(struct lruhash* table, struct lruhash_entry** list);
396 
397 /**
398  * Grow the table lookup array. Becomes twice as large.
399  * Caller must hold the hash table lock. Must not hold any bin locks.
400  * Tries to grow, on malloc failure, nothing happened.
401  * @param table: hash table.
402  */
403 void table_grow(struct lruhash* table);
404 
405 /**
406  * Put entry at front of lru. entry must be unlinked from lru.
407  * Caller must hold hash table lock.
408  * @param table: hash table with lru head and tail.
409  * @param entry: entry to make most recently used.
410  */
411 void lru_front(struct lruhash* table, struct lruhash_entry* entry);
412 
413 /**
414  * Remove entry from lru list.
415  * Caller must hold hash table lock.
416  * @param table: hash table with lru head and tail.
417  * @param entry: entry to remove from lru.
418  */
419 void lru_remove(struct lruhash* table, struct lruhash_entry* entry);
420 
421 /**
422  * Output debug info to the log as to state of the hash table.
423  * @param table: hash table.
424  * @param id: string printed with table to identify the hash table.
425  * @param extended: set to true to print statistics on overflow bin lengths.
426  */
427 void lruhash_status(struct lruhash* table, const char* id, int extended);
428 
429 /**
430  * Get memory in use now by the lruhash table.
431  * @param table: hash table. Will be locked before use. And unlocked after.
432  * @return size in bytes.
433  */
434 size_t lruhash_get_mem(struct lruhash* table);
435 
436 /**
437  * Traverse a lruhash. Call back for every element in the table.
438  * @param h: hash table.  Locked before use.
439  * @param wr: if true writelock is obtained on element, otherwise readlock.
440  * @param func: function for every element. Do not lock or unlock elements.
441  * @param arg: user argument to func.
442  */
443 void lruhash_traverse(struct lruhash* h, int wr,
444         void (*func)(struct lruhash_entry*, void*), void* arg);
445 
446 #endif /* UTIL_STORAGE_LRUHASH_H */
447