]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Hash routine. |
2 | * Copyright (C) 1998 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published | |
8 | * by the Free Software Foundation; either version 2, or (at your | |
9 | * option) any later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
4db0cff1 | 22 | #include <math.h> |
718e3744 | 23 | |
24 | #include "hash.h" | |
25 | #include "memory.h" | |
4db0cff1 QY |
26 | #include "linklist.h" |
27 | #include "termtable.h" | |
28 | #include "vty.h" | |
29 | #include "command.h" | |
6f6f0010 | 30 | #include "libfrr.h" |
718e3744 | 31 | |
d62a17ae | 32 | DEFINE_MTYPE(LIB, HASH, "Hash") |
33 | DEFINE_MTYPE(LIB, HASH_BACKET, "Hash Bucket") | |
34 | DEFINE_MTYPE_STATIC(LIB, HASH_INDEX, "Hash Index") | |
4a1ab8e4 | 35 | |
6f6f0010 | 36 | pthread_mutex_t _hashes_mtx = PTHREAD_MUTEX_INITIALIZER; |
4db0cff1 QY |
37 | static struct list *_hashes; |
38 | ||
d62a17ae | 39 | struct hash *hash_create_size(unsigned int size, |
40 | unsigned int (*hash_key)(void *), | |
74df8d6d | 41 | bool (*hash_cmp)(const void *, const void *), |
d62a17ae | 42 | const char *name) |
718e3744 | 43 | { |
d62a17ae | 44 | struct hash *hash; |
45 | ||
46 | assert((size & (size - 1)) == 0); | |
47 | hash = XCALLOC(MTYPE_HASH, sizeof(struct hash)); | |
48 | hash->index = | |
49 | XCALLOC(MTYPE_HASH_INDEX, sizeof(struct hash_backet *) * size); | |
50 | hash->size = size; | |
d62a17ae | 51 | hash->hash_key = hash_key; |
52 | hash->hash_cmp = hash_cmp; | |
53 | hash->count = 0; | |
54 | hash->name = name ? XSTRDUP(MTYPE_HASH, name) : NULL; | |
55 | hash->stats.empty = hash->size; | |
56 | ||
57 | pthread_mutex_lock(&_hashes_mtx); | |
58 | { | |
59 | if (!_hashes) | |
60 | _hashes = list_new(); | |
61 | ||
62 | listnode_add(_hashes, hash); | |
63 | } | |
64 | pthread_mutex_unlock(&_hashes_mtx); | |
65 | ||
66 | return hash; | |
718e3744 | 67 | } |
68 | ||
d62a17ae | 69 | struct hash *hash_create(unsigned int (*hash_key)(void *), |
74df8d6d | 70 | bool (*hash_cmp)(const void *, const void *), |
d62a17ae | 71 | const char *name) |
718e3744 | 72 | { |
d62a17ae | 73 | return hash_create_size(HASH_INITIAL_SIZE, hash_key, hash_cmp, name); |
718e3744 | 74 | } |
75 | ||
d62a17ae | 76 | void *hash_alloc_intern(void *arg) |
718e3744 | 77 | { |
d62a17ae | 78 | return arg; |
718e3744 | 79 | } |
80 | ||
d62a17ae | 81 | #define hash_update_ssq(hz, old, new) \ |
82 | atomic_fetch_add_explicit(&hz->stats.ssq, (new + old) * (new - old), \ | |
83 | memory_order_relaxed); | |
6f6f0010 | 84 | |
97c84db0 | 85 | /* Expand hash if the chain length exceeds the threshold. */ |
d62a17ae | 86 | static void hash_expand(struct hash *hash) |
97c84db0 | 87 | { |
8b2a6222 | 88 | unsigned int i, new_size; |
d62a17ae | 89 | struct hash_backet *hb, *hbnext, **new_index; |
97c84db0 | 90 | |
d62a17ae | 91 | new_size = hash->size * 2; |
40520c36 DW |
92 | |
93 | if (hash->max_size && new_size > hash->max_size) | |
94 | return; | |
95 | ||
d62a17ae | 96 | new_index = XCALLOC(MTYPE_HASH_INDEX, |
97 | sizeof(struct hash_backet *) * new_size); | |
98 | if (new_index == NULL) | |
99 | return; | |
97c84db0 | 100 | |
d62a17ae | 101 | hash->stats.empty = new_size; |
6f6f0010 | 102 | |
d62a17ae | 103 | for (i = 0; i < hash->size; i++) |
104 | for (hb = hash->index[i]; hb; hb = hbnext) { | |
105 | unsigned int h = hb->key & (new_size - 1); | |
97c84db0 | 106 | |
d62a17ae | 107 | hbnext = hb->next; |
108 | hb->next = new_index[h]; | |
6f6f0010 | 109 | |
d62a17ae | 110 | int oldlen = hb->next ? hb->next->len : 0; |
111 | int newlen = oldlen + 1; | |
6f6f0010 | 112 | |
d62a17ae | 113 | if (newlen == 1) |
114 | hash->stats.empty--; | |
115 | else | |
116 | hb->next->len = 0; | |
6f6f0010 | 117 | |
d62a17ae | 118 | hb->len = newlen; |
6f6f0010 | 119 | |
d62a17ae | 120 | hash_update_ssq(hash, oldlen, newlen); |
6f6f0010 | 121 | |
d62a17ae | 122 | new_index[h] = hb; |
123 | } | |
97c84db0 | 124 | |
d62a17ae | 125 | /* Switch to new table */ |
126 | XFREE(MTYPE_HASH_INDEX, hash->index); | |
127 | hash->size = new_size; | |
128 | hash->index = new_index; | |
97c84db0 SH |
129 | } |
130 | ||
d62a17ae | 131 | void *hash_get(struct hash *hash, void *data, void *(*alloc_func)(void *)) |
718e3744 | 132 | { |
d62a17ae | 133 | unsigned int key; |
134 | unsigned int index; | |
135 | void *newdata; | |
d62a17ae | 136 | struct hash_backet *backet; |
137 | ||
efb149d9 DS |
138 | if (!alloc_func && !hash->count) |
139 | return NULL; | |
140 | ||
d62a17ae | 141 | key = (*hash->hash_key)(data); |
142 | index = key & (hash->size - 1); | |
d62a17ae | 143 | |
144 | for (backet = hash->index[index]; backet != NULL; | |
145 | backet = backet->next) { | |
146 | if (backet->key == key && (*hash->hash_cmp)(backet->data, data)) | |
147 | return backet->data; | |
97c84db0 SH |
148 | } |
149 | ||
d62a17ae | 150 | if (alloc_func) { |
151 | newdata = (*alloc_func)(data); | |
152 | if (newdata == NULL) | |
153 | return NULL; | |
6f6f0010 | 154 | |
bed7ad83 | 155 | if (HASH_THRESHOLD(hash->count + 1, hash->size)) { |
d62a17ae | 156 | hash_expand(hash); |
157 | index = key & (hash->size - 1); | |
158 | } | |
6f6f0010 | 159 | |
d62a17ae | 160 | backet = XCALLOC(MTYPE_HASH_BACKET, sizeof(struct hash_backet)); |
161 | backet->data = newdata; | |
162 | backet->key = key; | |
163 | backet->next = hash->index[index]; | |
164 | hash->index[index] = backet; | |
165 | hash->count++; | |
6f6f0010 | 166 | |
d62a17ae | 167 | int oldlen = backet->next ? backet->next->len : 0; |
168 | int newlen = oldlen + 1; | |
6f6f0010 | 169 | |
d62a17ae | 170 | if (newlen == 1) |
171 | hash->stats.empty--; | |
172 | else | |
173 | backet->next->len = 0; | |
6f6f0010 | 174 | |
d62a17ae | 175 | backet->len = newlen; |
176 | ||
177 | hash_update_ssq(hash, oldlen, newlen); | |
178 | ||
179 | return backet->data; | |
180 | } | |
181 | return NULL; | |
718e3744 | 182 | } |
183 | ||
d62a17ae | 184 | void *hash_lookup(struct hash *hash, void *data) |
718e3744 | 185 | { |
d62a17ae | 186 | return hash_get(hash, data, NULL); |
718e3744 | 187 | } |
188 | ||
d62a17ae | 189 | unsigned int string_hash_make(const char *str) |
6392aa83 | 190 | { |
d62a17ae | 191 | unsigned int hash = 0; |
6392aa83 | 192 | |
d62a17ae | 193 | while (*str) |
194 | hash = (hash * 33) ^ (unsigned int)*str++; | |
6392aa83 | 195 | |
d62a17ae | 196 | return hash; |
6392aa83 SH |
197 | } |
198 | ||
d62a17ae | 199 | void *hash_release(struct hash *hash, void *data) |
718e3744 | 200 | { |
d62a17ae | 201 | void *ret; |
202 | unsigned int key; | |
203 | unsigned int index; | |
204 | struct hash_backet *backet; | |
205 | struct hash_backet *pp; | |
206 | ||
207 | key = (*hash->hash_key)(data); | |
208 | index = key & (hash->size - 1); | |
209 | ||
210 | for (backet = pp = hash->index[index]; backet; backet = backet->next) { | |
211 | if (backet->key == key | |
212 | && (*hash->hash_cmp)(backet->data, data)) { | |
213 | int oldlen = hash->index[index]->len; | |
214 | int newlen = oldlen - 1; | |
215 | ||
216 | if (backet == pp) | |
217 | hash->index[index] = backet->next; | |
218 | else | |
219 | pp->next = backet->next; | |
220 | ||
221 | if (hash->index[index]) | |
222 | hash->index[index]->len = newlen; | |
223 | else | |
224 | hash->stats.empty++; | |
225 | ||
226 | hash_update_ssq(hash, oldlen, newlen); | |
227 | ||
228 | ret = backet->data; | |
229 | XFREE(MTYPE_HASH_BACKET, backet); | |
230 | hash->count--; | |
231 | return ret; | |
232 | } | |
233 | pp = backet; | |
718e3744 | 234 | } |
d62a17ae | 235 | return NULL; |
718e3744 | 236 | } |
237 | ||
d62a17ae | 238 | void hash_iterate(struct hash *hash, void (*func)(struct hash_backet *, void *), |
239 | void *arg) | |
718e3744 | 240 | { |
d62a17ae | 241 | unsigned int i; |
242 | struct hash_backet *hb; | |
243 | struct hash_backet *hbnext; | |
244 | ||
df66eb2e | 245 | for (i = 0; i < hash->size; i++) |
d62a17ae | 246 | for (hb = hash->index[i]; hb; hb = hbnext) { |
247 | /* get pointer to next hash backet here, in case (*func) | |
248 | * decides to delete hb by calling hash_release | |
249 | */ | |
250 | hbnext = hb->next; | |
251 | (*func)(hb, arg); | |
252 | } | |
718e3744 | 253 | } |
254 | ||
d62a17ae | 255 | void hash_walk(struct hash *hash, int (*func)(struct hash_backet *, void *), |
256 | void *arg) | |
3f9c7369 | 257 | { |
d62a17ae | 258 | unsigned int i; |
259 | struct hash_backet *hb; | |
260 | struct hash_backet *hbnext; | |
261 | int ret = HASHWALK_CONTINUE; | |
262 | ||
263 | for (i = 0; i < hash->size; i++) { | |
264 | for (hb = hash->index[i]; hb; hb = hbnext) { | |
265 | /* get pointer to next hash backet here, in case (*func) | |
266 | * decides to delete hb by calling hash_release | |
267 | */ | |
268 | hbnext = hb->next; | |
269 | ret = (*func)(hb, arg); | |
270 | if (ret == HASHWALK_ABORT) | |
271 | return; | |
272 | } | |
3f9c7369 | 273 | } |
3f9c7369 DS |
274 | } |
275 | ||
d62a17ae | 276 | void hash_clean(struct hash *hash, void (*free_func)(void *)) |
718e3744 | 277 | { |
d62a17ae | 278 | unsigned int i; |
279 | struct hash_backet *hb; | |
280 | struct hash_backet *next; | |
718e3744 | 281 | |
d62a17ae | 282 | for (i = 0; i < hash->size; i++) { |
283 | for (hb = hash->index[i]; hb; hb = next) { | |
284 | next = hb->next; | |
285 | ||
286 | if (free_func) | |
287 | (*free_func)(hb->data); | |
718e3744 | 288 | |
d62a17ae | 289 | XFREE(MTYPE_HASH_BACKET, hb); |
290 | hash->count--; | |
291 | } | |
292 | hash->index[i] = NULL; | |
718e3744 | 293 | } |
6f6f0010 | 294 | |
d62a17ae | 295 | hash->stats.ssq = 0; |
296 | hash->stats.empty = hash->size; | |
718e3744 | 297 | } |
298 | ||
91f10370 QY |
299 | static void hash_to_list_iter(struct hash_backet *hb, void *arg) |
300 | { | |
301 | struct list *list = arg; | |
302 | ||
303 | listnode_add(list, hb->data); | |
304 | } | |
305 | ||
306 | struct list *hash_to_list(struct hash *hash) | |
307 | { | |
308 | struct list *list = list_new(); | |
309 | ||
310 | hash_iterate(hash, hash_to_list_iter, list); | |
311 | return list; | |
312 | } | |
313 | ||
d62a17ae | 314 | void hash_free(struct hash *hash) |
718e3744 | 315 | { |
d62a17ae | 316 | pthread_mutex_lock(&_hashes_mtx); |
317 | { | |
318 | if (_hashes) { | |
319 | listnode_delete(_hashes, hash); | |
320 | if (_hashes->count == 0) { | |
6a154c88 | 321 | list_delete(&_hashes); |
d62a17ae | 322 | } |
323 | } | |
324 | } | |
325 | pthread_mutex_unlock(&_hashes_mtx); | |
326 | ||
327 | if (hash->name) | |
328 | XFREE(MTYPE_HASH, hash->name); | |
329 | ||
330 | XFREE(MTYPE_HASH_INDEX, hash->index); | |
331 | XFREE(MTYPE_HASH, hash); | |
4db0cff1 QY |
332 | } |
333 | ||
6f6f0010 QY |
334 | |
335 | /* CLI commands ------------------------------------------------------------ */ | |
4db0cff1 | 336 | |
40818cec DL |
337 | DEFUN_NOSH(show_hash_stats, |
338 | show_hash_stats_cmd, | |
339 | "show debugging hashtable [statistics]", | |
340 | SHOW_STR | |
341 | DEBUG_STR | |
342 | "Statistics about hash tables\n" | |
343 | "Statistics about hash tables\n") | |
4db0cff1 | 344 | { |
d62a17ae | 345 | struct hash *h; |
346 | struct listnode *ln; | |
347 | struct ttable *tt = ttable_new(&ttable_styles[TTSTYLE_BLANK]); | |
348 | ||
349 | ttable_add_row(tt, "Hash table|Buckets|Entries|Empty|LF|SD|FLF|SD"); | |
350 | tt->style.cell.lpad = 2; | |
351 | tt->style.cell.rpad = 1; | |
352 | tt->style.corner = '+'; | |
353 | ttable_restyle(tt); | |
354 | ttable_rowseps(tt, 0, BOTTOM, true, '-'); | |
355 | ||
356 | /* Summary statistics calculated are: | |
357 | * | |
c7d3895e CF |
358 | * - Load factor: This is the number of elements in the table divided |
359 | * by the number of buckets. Since this hash table implementation | |
360 | * uses chaining, this value can be greater than 1. | |
361 | * This number provides information on how 'full' the table is, but | |
362 | * does not provide information on how evenly distributed the | |
363 | * elements are. | |
364 | * Notably, a load factor >= 1 does not imply that every bucket has | |
365 | * an element; with a pathological hash function, all elements could | |
366 | * be in a single bucket. | |
d62a17ae | 367 | * |
368 | * - Full load factor: this is the number of elements in the table | |
c7d3895e | 369 | * divided by the number of buckets that have some elements in them. |
d62a17ae | 370 | * |
371 | * - Std. Dev.: This is the standard deviation calculated from the | |
c7d3895e CF |
372 | * relevant load factor. If the load factor is the mean of number of |
373 | * elements per bucket, the standard deviation measures how much any | |
374 | * particular bucket is likely to deviate from the mean. | |
375 | * As a rule of thumb this number should be less than 2, and ideally | |
376 | * <= 1 for optimal performance. A number larger than 3 generally | |
377 | * indicates a poor hash function. | |
d62a17ae | 378 | */ |
379 | ||
380 | double lf; // load factor | |
381 | double flf; // full load factor | |
382 | double var; // overall variance | |
383 | double fvar; // full variance | |
384 | double stdv; // overall stddev | |
385 | double fstdv; // full stddev | |
386 | ||
387 | long double x2; // h->count ^ 2 | |
388 | long double ldc; // (long double) h->count | |
389 | long double full; // h->size - h->stats.empty | |
390 | long double ssq; // ssq casted to long double | |
391 | ||
392 | pthread_mutex_lock(&_hashes_mtx); | |
393 | if (!_hashes) { | |
394 | pthread_mutex_unlock(&_hashes_mtx); | |
44f12f20 | 395 | ttable_del(tt); |
d62a17ae | 396 | vty_out(vty, "No hash tables in use.\n"); |
397 | return CMD_SUCCESS; | |
398 | } | |
399 | ||
400 | for (ALL_LIST_ELEMENTS_RO(_hashes, ln, h)) { | |
401 | if (!h->name) | |
402 | continue; | |
403 | ||
404 | ssq = (long double)h->stats.ssq; | |
61b9e9d6 | 405 | x2 = h->count * h->count; |
d62a17ae | 406 | ldc = (long double)h->count; |
407 | full = h->size - h->stats.empty; | |
408 | lf = h->count / (double)h->size; | |
409 | flf = full ? h->count / (double)(full) : 0; | |
410 | var = ldc ? (1.0 / ldc) * (ssq - x2 / ldc) : 0; | |
411 | fvar = full ? (1.0 / full) * (ssq - x2 / full) : 0; | |
412 | var = (var < .0001) ? 0 : var; | |
413 | fvar = (fvar < .0001) ? 0 : fvar; | |
414 | stdv = sqrt(var); | |
415 | fstdv = sqrt(fvar); | |
416 | ||
417 | ttable_add_row(tt, "%s|%d|%ld|%.0f%%|%.2lf|%.2lf|%.2lf|%.2lf", | |
418 | h->name, h->size, h->count, | |
419 | (h->stats.empty / (double)h->size) * 100, lf, | |
420 | stdv, flf, fstdv); | |
421 | } | |
422 | pthread_mutex_unlock(&_hashes_mtx); | |
423 | ||
424 | /* display header */ | |
425 | char header[] = "Showing hash table statistics for "; | |
426 | char underln[sizeof(header) + strlen(frr_protonameinst)]; | |
427 | memset(underln, '-', sizeof(underln)); | |
428 | underln[sizeof(underln) - 1] = '\0'; | |
429 | vty_out(vty, "%s%s\n", header, frr_protonameinst); | |
430 | vty_out(vty, "%s\n", underln); | |
431 | ||
432 | vty_out(vty, "# allocated: %d\n", _hashes->count); | |
433 | vty_out(vty, "# named: %d\n\n", tt->nrows - 1); | |
434 | ||
435 | if (tt->nrows > 1) { | |
436 | ttable_colseps(tt, 0, RIGHT, true, '|'); | |
437 | char *table = ttable_dump(tt, "\n"); | |
438 | vty_out(vty, "%s\n", table); | |
439 | XFREE(MTYPE_TMP, table); | |
440 | } else | |
441 | vty_out(vty, "No named hash tables to display.\n"); | |
442 | ||
443 | ttable_del(tt); | |
444 | ||
445 | return CMD_SUCCESS; | |
4db0cff1 QY |
446 | } |
447 | ||
d62a17ae | 448 | void hash_cmd_init() |
4db0cff1 | 449 | { |
d62a17ae | 450 | install_element(ENABLE_NODE, &show_hash_stats_cmd); |
4db0cff1 | 451 | } |