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