2 * Copyright (C) 1998 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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.
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.
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
27 #include "termtable.h"
32 DEFINE_MTYPE(LIB
, HASH
, "Hash")
33 DEFINE_MTYPE(LIB
, HASH_BACKET
, "Hash Bucket")
34 DEFINE_MTYPE_STATIC(LIB
, HASH_INDEX
, "Hash Index")
36 static pthread_mutex_t _hashes_mtx
= PTHREAD_MUTEX_INITIALIZER
;
37 static struct list
*_hashes
;
39 struct hash
*hash_create_size(unsigned int size
,
40 unsigned int (*hash_key
)(void *),
41 bool (*hash_cmp
)(const void *, const void *),
46 assert((size
& (size
- 1)) == 0);
47 hash
= XCALLOC(MTYPE_HASH
, sizeof(struct hash
));
49 XCALLOC(MTYPE_HASH_INDEX
, sizeof(struct hash_bucket
*) * size
);
51 hash
->hash_key
= hash_key
;
52 hash
->hash_cmp
= hash_cmp
;
54 hash
->name
= name
? XSTRDUP(MTYPE_HASH
, name
) : NULL
;
55 hash
->stats
.empty
= hash
->size
;
57 pthread_mutex_lock(&_hashes_mtx
);
62 listnode_add(_hashes
, hash
);
64 pthread_mutex_unlock(&_hashes_mtx
);
69 struct hash
*hash_create(unsigned int (*hash_key
)(void *),
70 bool (*hash_cmp
)(const void *, const void *),
73 return hash_create_size(HASH_INITIAL_SIZE
, hash_key
, hash_cmp
, name
);
76 void *hash_alloc_intern(void *arg
)
81 #define hash_update_ssq(hz, old, new) \
82 atomic_fetch_add_explicit(&hz->stats.ssq, (new + old) * (new - old), \
83 memory_order_relaxed);
85 /* Expand hash if the chain length exceeds the threshold. */
86 static void hash_expand(struct hash
*hash
)
88 unsigned int i
, new_size
;
89 struct hash_bucket
*hb
, *hbnext
, **new_index
;
91 new_size
= hash
->size
* 2;
93 if (hash
->max_size
&& new_size
> hash
->max_size
)
96 new_index
= XCALLOC(MTYPE_HASH_INDEX
,
97 sizeof(struct hash_bucket
*) * new_size
);
99 hash
->stats
.empty
= new_size
;
101 for (i
= 0; i
< hash
->size
; i
++)
102 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
) {
103 unsigned int h
= hb
->key
& (new_size
- 1);
106 hb
->next
= new_index
[h
];
108 int oldlen
= hb
->next
? hb
->next
->len
: 0;
109 int newlen
= oldlen
+ 1;
118 hash_update_ssq(hash
, oldlen
, newlen
);
123 /* Switch to new table */
124 XFREE(MTYPE_HASH_INDEX
, hash
->index
);
125 hash
->size
= new_size
;
126 hash
->index
= new_index
;
129 void *hash_get(struct hash
*hash
, void *data
, void *(*alloc_func
)(void *))
134 struct hash_bucket
*bucket
;
136 if (!alloc_func
&& !hash
->count
)
139 key
= (*hash
->hash_key
)(data
);
140 index
= key
& (hash
->size
- 1);
142 for (bucket
= hash
->index
[index
]; bucket
!= NULL
;
143 bucket
= bucket
->next
) {
144 if (bucket
->key
== key
&& (*hash
->hash_cmp
)(bucket
->data
, data
))
149 newdata
= (*alloc_func
)(data
);
153 if (HASH_THRESHOLD(hash
->count
+ 1, hash
->size
)) {
155 index
= key
& (hash
->size
- 1);
158 bucket
= XCALLOC(MTYPE_HASH_BACKET
, sizeof(struct hash_bucket
));
159 bucket
->data
= newdata
;
161 bucket
->next
= hash
->index
[index
];
162 hash
->index
[index
] = bucket
;
165 int oldlen
= bucket
->next
? bucket
->next
->len
: 0;
166 int newlen
= oldlen
+ 1;
171 bucket
->next
->len
= 0;
173 bucket
->len
= newlen
;
175 hash_update_ssq(hash
, oldlen
, newlen
);
182 void *hash_lookup(struct hash
*hash
, void *data
)
184 return hash_get(hash
, data
, NULL
);
187 unsigned int string_hash_make(const char *str
)
189 unsigned int hash
= 0;
192 hash
= (hash
* 33) ^ (unsigned int)*str
++;
197 void *hash_release(struct hash
*hash
, void *data
)
202 struct hash_bucket
*bucket
;
203 struct hash_bucket
*pp
;
205 key
= (*hash
->hash_key
)(data
);
206 index
= key
& (hash
->size
- 1);
208 for (bucket
= pp
= hash
->index
[index
]; bucket
; bucket
= bucket
->next
) {
209 if (bucket
->key
== key
210 && (*hash
->hash_cmp
)(bucket
->data
, data
)) {
211 int oldlen
= hash
->index
[index
]->len
;
212 int newlen
= oldlen
- 1;
215 hash
->index
[index
] = bucket
->next
;
217 pp
->next
= bucket
->next
;
219 if (hash
->index
[index
])
220 hash
->index
[index
]->len
= newlen
;
224 hash_update_ssq(hash
, oldlen
, newlen
);
227 XFREE(MTYPE_HASH_BACKET
, bucket
);
236 void hash_iterate(struct hash
*hash
, void (*func
)(struct hash_bucket
*, void *),
240 struct hash_bucket
*hb
;
241 struct hash_bucket
*hbnext
;
243 for (i
= 0; i
< hash
->size
; i
++)
244 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
) {
245 /* get pointer to next hash bucket here, in case (*func)
246 * decides to delete hb by calling hash_release
253 void hash_walk(struct hash
*hash
, int (*func
)(struct hash_bucket
*, void *),
257 struct hash_bucket
*hb
;
258 struct hash_bucket
*hbnext
;
259 int ret
= HASHWALK_CONTINUE
;
261 for (i
= 0; i
< hash
->size
; i
++) {
262 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
) {
263 /* get pointer to next hash bucket here, in case (*func)
264 * decides to delete hb by calling hash_release
267 ret
= (*func
)(hb
, arg
);
268 if (ret
== HASHWALK_ABORT
)
274 void hash_clean(struct hash
*hash
, void (*free_func
)(void *))
277 struct hash_bucket
*hb
;
278 struct hash_bucket
*next
;
280 for (i
= 0; i
< hash
->size
; i
++) {
281 for (hb
= hash
->index
[i
]; hb
; hb
= next
) {
285 (*free_func
)(hb
->data
);
287 XFREE(MTYPE_HASH_BACKET
, hb
);
290 hash
->index
[i
] = NULL
;
294 hash
->stats
.empty
= hash
->size
;
297 static void hash_to_list_iter(struct hash_bucket
*hb
, void *arg
)
299 struct list
*list
= arg
;
301 listnode_add(list
, hb
->data
);
304 struct list
*hash_to_list(struct hash
*hash
)
306 struct list
*list
= list_new();
308 hash_iterate(hash
, hash_to_list_iter
, list
);
312 void hash_free(struct hash
*hash
)
314 pthread_mutex_lock(&_hashes_mtx
);
317 listnode_delete(_hashes
, hash
);
318 if (_hashes
->count
== 0) {
319 list_delete(&_hashes
);
323 pthread_mutex_unlock(&_hashes_mtx
);
325 XFREE(MTYPE_HASH
, hash
->name
);
327 XFREE(MTYPE_HASH_INDEX
, hash
->index
);
328 XFREE(MTYPE_HASH
, hash
);
332 /* CLI commands ------------------------------------------------------------ */
334 DEFUN_NOSH(show_hash_stats
,
336 "show debugging hashtable [statistics]",
339 "Statistics about hash tables\n"
340 "Statistics about hash tables\n")
344 struct ttable
*tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
346 ttable_add_row(tt
, "Hash table|Buckets|Entries|Empty|LF|SD|FLF|SD");
347 tt
->style
.cell
.lpad
= 2;
348 tt
->style
.cell
.rpad
= 1;
349 tt
->style
.corner
= '+';
351 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
353 /* Summary statistics calculated are:
355 * - Load factor: This is the number of elements in the table divided
356 * by the number of buckets. Since this hash table implementation
357 * uses chaining, this value can be greater than 1.
358 * This number provides information on how 'full' the table is, but
359 * does not provide information on how evenly distributed the
361 * Notably, a load factor >= 1 does not imply that every bucket has
362 * an element; with a pathological hash function, all elements could
363 * be in a single bucket.
365 * - Full load factor: this is the number of elements in the table
366 * divided by the number of buckets that have some elements in them.
368 * - Std. Dev.: This is the standard deviation calculated from the
369 * relevant load factor. If the load factor is the mean of number of
370 * elements per bucket, the standard deviation measures how much any
371 * particular bucket is likely to deviate from the mean.
372 * As a rule of thumb this number should be less than 2, and ideally
373 * <= 1 for optimal performance. A number larger than 3 generally
374 * indicates a poor hash function.
377 double lf
; // load factor
378 double flf
; // full load factor
379 double var
; // overall variance
380 double fvar
; // full variance
381 double stdv
; // overall stddev
382 double fstdv
; // full stddev
384 long double x2
; // h->count ^ 2
385 long double ldc
; // (long double) h->count
386 long double full
; // h->size - h->stats.empty
387 long double ssq
; // ssq casted to long double
389 pthread_mutex_lock(&_hashes_mtx
);
391 pthread_mutex_unlock(&_hashes_mtx
);
393 vty_out(vty
, "No hash tables in use.\n");
397 for (ALL_LIST_ELEMENTS_RO(_hashes
, ln
, h
)) {
401 ssq
= (long double)h
->stats
.ssq
;
402 x2
= h
->count
* h
->count
;
403 ldc
= (long double)h
->count
;
404 full
= h
->size
- h
->stats
.empty
;
405 lf
= h
->count
/ (double)h
->size
;
406 flf
= full
? h
->count
/ (double)(full
) : 0;
407 var
= ldc
? (1.0 / ldc
) * (ssq
- x2
/ ldc
) : 0;
408 fvar
= full
? (1.0 / full
) * (ssq
- x2
/ full
) : 0;
409 var
= (var
< .0001) ? 0 : var
;
410 fvar
= (fvar
< .0001) ? 0 : fvar
;
414 ttable_add_row(tt
, "%s|%d|%ld|%.0f%%|%.2lf|%.2lf|%.2lf|%.2lf",
415 h
->name
, h
->size
, h
->count
,
416 (h
->stats
.empty
/ (double)h
->size
) * 100, lf
,
419 pthread_mutex_unlock(&_hashes_mtx
);
422 char header
[] = "Showing hash table statistics for ";
423 char underln
[sizeof(header
) + strlen(frr_protonameinst
)];
424 memset(underln
, '-', sizeof(underln
));
425 underln
[sizeof(underln
) - 1] = '\0';
426 vty_out(vty
, "%s%s\n", header
, frr_protonameinst
);
427 vty_out(vty
, "%s\n", underln
);
429 vty_out(vty
, "# allocated: %d\n", _hashes
->count
);
430 vty_out(vty
, "# named: %d\n\n", tt
->nrows
- 1);
433 ttable_colseps(tt
, 0, RIGHT
, true, '|');
434 char *table
= ttable_dump(tt
, "\n");
435 vty_out(vty
, "%s\n", table
);
436 XFREE(MTYPE_TMP
, table
);
438 vty_out(vty
, "No named hash tables to display.\n");
445 void hash_cmd_init(void)
447 install_element(ENABLE_NODE
, &show_hash_stats_cmd
);