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 pthread_mutex_t _hashes_mtx
= PTHREAD_MUTEX_INITIALIZER
;
37 static struct list
*_hashes
;
39 /* Allocate a new hash. */
40 struct hash
*hash_create_size(unsigned int size
,
41 unsigned int (*hash_key
)(void *),
42 int (*hash_cmp
)(const void *, const void *),
47 assert((size
& (size
- 1)) == 0);
48 hash
= XCALLOC(MTYPE_HASH
, sizeof(struct hash
));
50 XCALLOC(MTYPE_HASH_INDEX
, sizeof(struct hash_backet
*) * size
);
52 hash
->hash_key
= hash_key
;
53 hash
->hash_cmp
= hash_cmp
;
55 hash
->name
= name
? XSTRDUP(MTYPE_HASH
, name
) : NULL
;
56 hash
->stats
.empty
= hash
->size
;
58 pthread_mutex_lock(&_hashes_mtx
);
63 listnode_add(_hashes
, hash
);
65 pthread_mutex_unlock(&_hashes_mtx
);
70 /* Allocate a new hash with default hash size. */
71 struct hash
*hash_create(unsigned int (*hash_key
)(void *),
72 int (*hash_cmp
)(const void *, const void *),
75 return hash_create_size(HASH_INITIAL_SIZE
, hash_key
, hash_cmp
, name
);
78 /* Utility function for hash_get(). When this function is specified
79 as alloc_func, return arugment as it is. This function is used for
80 intern already allocated value. */
81 void *hash_alloc_intern(void *arg
)
86 #define hash_update_ssq(hz, old, new) \
87 atomic_fetch_add_explicit(&hz->stats.ssq, (new + old) * (new - old), \
88 memory_order_relaxed);
90 /* Expand hash if the chain length exceeds the threshold. */
91 static void hash_expand(struct hash
*hash
)
93 unsigned int i
, new_size
;
94 struct hash_backet
*hb
, *hbnext
, **new_index
;
96 new_size
= hash
->size
* 2;
98 if (hash
->max_size
&& new_size
> hash
->max_size
)
101 new_index
= XCALLOC(MTYPE_HASH_INDEX
,
102 sizeof(struct hash_backet
*) * new_size
);
103 if (new_index
== NULL
)
106 hash
->stats
.empty
= new_size
;
108 for (i
= 0; i
< hash
->size
; i
++)
109 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
) {
110 unsigned int h
= hb
->key
& (new_size
- 1);
113 hb
->next
= new_index
[h
];
115 int oldlen
= hb
->next
? hb
->next
->len
: 0;
116 int newlen
= oldlen
+ 1;
125 hash_update_ssq(hash
, oldlen
, newlen
);
130 /* Switch to new table */
131 XFREE(MTYPE_HASH_INDEX
, hash
->index
);
132 hash
->size
= new_size
;
133 hash
->index
= new_index
;
136 /* Lookup and return hash backet in hash. If there is no
137 corresponding hash backet and alloc_func is specified, create new
139 void *hash_get(struct hash
*hash
, void *data
, void *(*alloc_func
)(void *))
145 struct hash_backet
*backet
;
147 key
= (*hash
->hash_key
)(data
);
148 index
= key
& (hash
->size
- 1);
151 for (backet
= hash
->index
[index
]; backet
!= NULL
;
152 backet
= backet
->next
) {
153 if (backet
->key
== key
&& (*hash
->hash_cmp
)(backet
->data
, data
))
159 newdata
= (*alloc_func
)(data
);
163 if (len
> HASH_THRESHOLD
) {
165 index
= key
& (hash
->size
- 1);
168 backet
= XCALLOC(MTYPE_HASH_BACKET
, sizeof(struct hash_backet
));
169 backet
->data
= newdata
;
171 backet
->next
= hash
->index
[index
];
172 hash
->index
[index
] = backet
;
175 int oldlen
= backet
->next
? backet
->next
->len
: 0;
176 int newlen
= oldlen
+ 1;
181 backet
->next
->len
= 0;
183 backet
->len
= newlen
;
185 hash_update_ssq(hash
, oldlen
, newlen
);
193 void *hash_lookup(struct hash
*hash
, void *data
)
195 return hash_get(hash
, data
, NULL
);
198 /* Simple Bernstein hash which is simple and fast for common case */
199 unsigned int string_hash_make(const char *str
)
201 unsigned int hash
= 0;
204 hash
= (hash
* 33) ^ (unsigned int)*str
++;
209 /* This function release registered value from specified hash. When
210 release is successfully finished, return the data pointer in the
212 void *hash_release(struct hash
*hash
, void *data
)
217 struct hash_backet
*backet
;
218 struct hash_backet
*pp
;
220 key
= (*hash
->hash_key
)(data
);
221 index
= key
& (hash
->size
- 1);
223 for (backet
= pp
= hash
->index
[index
]; backet
; backet
= backet
->next
) {
224 if (backet
->key
== key
225 && (*hash
->hash_cmp
)(backet
->data
, data
)) {
226 int oldlen
= hash
->index
[index
]->len
;
227 int newlen
= oldlen
- 1;
230 hash
->index
[index
] = backet
->next
;
232 pp
->next
= backet
->next
;
234 if (hash
->index
[index
])
235 hash
->index
[index
]->len
= newlen
;
239 hash_update_ssq(hash
, oldlen
, newlen
);
242 XFREE(MTYPE_HASH_BACKET
, backet
);
251 /* Iterator function for hash. */
252 void hash_iterate(struct hash
*hash
, void (*func
)(struct hash_backet
*, void *),
256 struct hash_backet
*hb
;
257 struct hash_backet
*hbnext
;
259 for (i
= 0; i
< hash
->size
; i
++)
260 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
) {
261 /* get pointer to next hash backet here, in case (*func)
262 * decides to delete hb by calling hash_release
269 /* Iterator function for hash. */
270 void hash_walk(struct hash
*hash
, int (*func
)(struct hash_backet
*, void *),
274 struct hash_backet
*hb
;
275 struct hash_backet
*hbnext
;
276 int ret
= HASHWALK_CONTINUE
;
278 for (i
= 0; i
< hash
->size
; i
++) {
279 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
) {
280 /* get pointer to next hash backet here, in case (*func)
281 * decides to delete hb by calling hash_release
284 ret
= (*func
)(hb
, arg
);
285 if (ret
== HASHWALK_ABORT
)
292 void hash_clean(struct hash
*hash
, void (*free_func
)(void *))
295 struct hash_backet
*hb
;
296 struct hash_backet
*next
;
298 for (i
= 0; i
< hash
->size
; i
++) {
299 for (hb
= hash
->index
[i
]; hb
; hb
= next
) {
303 (*free_func
)(hb
->data
);
305 XFREE(MTYPE_HASH_BACKET
, hb
);
308 hash
->index
[i
] = NULL
;
312 hash
->stats
.empty
= hash
->size
;
315 /* Free hash memory. You may call hash_clean before call this
317 void hash_free(struct hash
*hash
)
319 pthread_mutex_lock(&_hashes_mtx
);
322 listnode_delete(_hashes
, hash
);
323 if (_hashes
->count
== 0) {
324 list_delete(_hashes
);
329 pthread_mutex_unlock(&_hashes_mtx
);
332 XFREE(MTYPE_HASH
, hash
->name
);
334 XFREE(MTYPE_HASH_INDEX
, hash
->index
);
335 XFREE(MTYPE_HASH
, hash
);
339 /* CLI commands ------------------------------------------------------------ */
341 DEFUN(show_hash_stats
,
343 "show hashtable [statistics]",
345 "Statistics about hash tables\n"
346 "Statistics about hash tables\n")
350 struct ttable
*tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
352 ttable_add_row(tt
, "Hash table|Buckets|Entries|Empty|LF|SD|FLF|SD");
353 tt
->style
.cell
.lpad
= 2;
354 tt
->style
.cell
.rpad
= 1;
355 tt
->style
.corner
= '+';
357 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
359 /* Summary statistics calculated are:
361 * - Load factor: This is the number of elements in the table divided
362 * by the number of buckets. Since this hash table implementation
363 * uses chaining, this value can be greater than 1.
364 * This number provides information on how 'full' the table is, but
365 * does not provide information on how evenly distributed the
367 * Notably, a load factor >= 1 does not imply that every bucket has
368 * an element; with a pathological hash function, all elements could
369 * be in a single bucket.
371 * - Full load factor: this is the number of elements in the table
372 * divided by the number of buckets that have some elements in them.
374 * - Std. Dev.: This is the standard deviation calculated from the
375 * relevant load factor. If the load factor is the mean of number of
376 * elements per bucket, the standard deviation measures how much any
377 * particular bucket is likely to deviate from the mean.
378 * As a rule of thumb this number should be less than 2, and ideally
379 * <= 1 for optimal performance. A number larger than 3 generally
380 * indicates a poor hash function.
383 double lf
; // load factor
384 double flf
; // full load factor
385 double var
; // overall variance
386 double fvar
; // full variance
387 double stdv
; // overall stddev
388 double fstdv
; // full stddev
390 long double x2
; // h->count ^ 2
391 long double ldc
; // (long double) h->count
392 long double full
; // h->size - h->stats.empty
393 long double ssq
; // ssq casted to long double
395 pthread_mutex_lock(&_hashes_mtx
);
397 pthread_mutex_unlock(&_hashes_mtx
);
398 vty_out(vty
, "No hash tables in use.\n");
402 for (ALL_LIST_ELEMENTS_RO(_hashes
, ln
, h
)) {
406 ssq
= (long double)h
->stats
.ssq
;
407 x2
= h
->count
* h
->count
;
408 ldc
= (long double)h
->count
;
409 full
= h
->size
- h
->stats
.empty
;
410 lf
= h
->count
/ (double)h
->size
;
411 flf
= full
? h
->count
/ (double)(full
) : 0;
412 var
= ldc
? (1.0 / ldc
) * (ssq
- x2
/ ldc
) : 0;
413 fvar
= full
? (1.0 / full
) * (ssq
- x2
/ full
) : 0;
414 var
= (var
< .0001) ? 0 : var
;
415 fvar
= (fvar
< .0001) ? 0 : fvar
;
419 ttable_add_row(tt
, "%s|%d|%ld|%.0f%%|%.2lf|%.2lf|%.2lf|%.2lf",
420 h
->name
, h
->size
, h
->count
,
421 (h
->stats
.empty
/ (double)h
->size
) * 100, lf
,
424 pthread_mutex_unlock(&_hashes_mtx
);
427 char header
[] = "Showing hash table statistics for ";
428 char underln
[sizeof(header
) + strlen(frr_protonameinst
)];
429 memset(underln
, '-', sizeof(underln
));
430 underln
[sizeof(underln
) - 1] = '\0';
431 vty_out(vty
, "%s%s\n", header
, frr_protonameinst
);
432 vty_out(vty
, "%s\n", underln
);
434 vty_out(vty
, "# allocated: %d\n", _hashes
->count
);
435 vty_out(vty
, "# named: %d\n\n", tt
->nrows
- 1);
438 ttable_colseps(tt
, 0, RIGHT
, true, '|');
439 char *table
= ttable_dump(tt
, "\n");
440 vty_out(vty
, "%s\n", table
);
441 XFREE(MTYPE_TMP
, table
);
443 vty_out(vty
, "No named hash tables to display.\n");
452 install_element(ENABLE_NODE
, &show_hash_stats_cmd
);