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"
31 #include "frr_pthread.h"
33 DEFINE_MTYPE_STATIC(LIB
, HASH
, "Hash")
34 DEFINE_MTYPE_STATIC(LIB
, HASH_BACKET
, "Hash Bucket")
35 DEFINE_MTYPE_STATIC(LIB
, HASH_INDEX
, "Hash Index")
37 static pthread_mutex_t _hashes_mtx
= PTHREAD_MUTEX_INITIALIZER
;
38 static struct list
*_hashes
;
40 struct hash
*hash_create_size(unsigned int size
,
41 unsigned int (*hash_key
)(const void *),
42 bool (*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_bucket
*) * 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 frr_with_mutex(&_hashes_mtx
) {
62 listnode_add(_hashes
, hash
);
68 struct hash
*hash_create(unsigned int (*hash_key
)(const void *),
69 bool (*hash_cmp
)(const void *, const void *),
72 return hash_create_size(HASH_INITIAL_SIZE
, hash_key
, hash_cmp
, name
);
75 void *hash_alloc_intern(void *arg
)
80 #define hash_update_ssq(hz, old, new) \
81 atomic_fetch_add_explicit(&hz->stats.ssq, (new + old) * (new - old), \
82 memory_order_relaxed);
84 /* Expand hash if the chain length exceeds the threshold. */
85 static void hash_expand(struct hash
*hash
)
87 unsigned int i
, new_size
;
88 struct hash_bucket
*hb
, *hbnext
, **new_index
;
90 new_size
= hash
->size
* 2;
92 if (hash
->max_size
&& new_size
> hash
->max_size
)
95 new_index
= XCALLOC(MTYPE_HASH_INDEX
,
96 sizeof(struct hash_bucket
*) * new_size
);
98 hash
->stats
.empty
= new_size
;
100 for (i
= 0; i
< hash
->size
; i
++)
101 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
) {
102 unsigned int h
= hb
->key
& (new_size
- 1);
105 hb
->next
= new_index
[h
];
107 int oldlen
= hb
->next
? hb
->next
->len
: 0;
108 int newlen
= oldlen
+ 1;
117 hash_update_ssq(hash
, oldlen
, newlen
);
122 /* Switch to new table */
123 XFREE(MTYPE_HASH_INDEX
, hash
->index
);
124 hash
->size
= new_size
;
125 hash
->index
= new_index
;
128 void *hash_get(struct hash
*hash
, void *data
, void *(*alloc_func
)(void *))
133 struct hash_bucket
*bucket
;
135 if (!alloc_func
&& !hash
->count
)
138 key
= (*hash
->hash_key
)(data
);
139 index
= key
& (hash
->size
- 1);
141 for (bucket
= hash
->index
[index
]; bucket
!= NULL
;
142 bucket
= bucket
->next
) {
143 if (bucket
->key
== key
&& (*hash
->hash_cmp
)(bucket
->data
, data
))
148 newdata
= (*alloc_func
)(data
);
152 if (HASH_THRESHOLD(hash
->count
+ 1, hash
->size
)) {
154 index
= key
& (hash
->size
- 1);
157 bucket
= XCALLOC(MTYPE_HASH_BACKET
, sizeof(struct hash_bucket
));
158 bucket
->data
= newdata
;
160 bucket
->next
= hash
->index
[index
];
161 hash
->index
[index
] = bucket
;
164 int oldlen
= bucket
->next
? bucket
->next
->len
: 0;
165 int newlen
= oldlen
+ 1;
170 bucket
->next
->len
= 0;
172 bucket
->len
= newlen
;
174 hash_update_ssq(hash
, oldlen
, newlen
);
181 void *hash_lookup(struct hash
*hash
, void *data
)
183 return hash_get(hash
, data
, NULL
);
186 unsigned int string_hash_make(const char *str
)
188 unsigned int hash
= 0;
191 hash
= (hash
* 33) ^ (unsigned int)*str
++;
196 void *hash_release(struct hash
*hash
, void *data
)
201 struct hash_bucket
*bucket
;
202 struct hash_bucket
*pp
;
204 key
= (*hash
->hash_key
)(data
);
205 index
= key
& (hash
->size
- 1);
207 for (bucket
= pp
= hash
->index
[index
]; bucket
; bucket
= bucket
->next
) {
208 if (bucket
->key
== key
209 && (*hash
->hash_cmp
)(bucket
->data
, data
)) {
210 int oldlen
= hash
->index
[index
]->len
;
211 int newlen
= oldlen
- 1;
214 hash
->index
[index
] = bucket
->next
;
216 pp
->next
= bucket
->next
;
218 if (hash
->index
[index
])
219 hash
->index
[index
]->len
= newlen
;
223 hash_update_ssq(hash
, oldlen
, newlen
);
226 XFREE(MTYPE_HASH_BACKET
, bucket
);
235 void hash_iterate(struct hash
*hash
, void (*func
)(struct hash_bucket
*, void *),
239 struct hash_bucket
*hb
;
240 struct hash_bucket
*hbnext
;
242 for (i
= 0; i
< hash
->size
; i
++)
243 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
) {
244 /* get pointer to next hash bucket here, in case (*func)
245 * decides to delete hb by calling hash_release
252 void hash_walk(struct hash
*hash
, int (*func
)(struct hash_bucket
*, void *),
256 struct hash_bucket
*hb
;
257 struct hash_bucket
*hbnext
;
258 int ret
= HASHWALK_CONTINUE
;
260 for (i
= 0; i
< hash
->size
; i
++) {
261 for (hb
= hash
->index
[i
]; hb
; hb
= hbnext
) {
262 /* get pointer to next hash bucket here, in case (*func)
263 * decides to delete hb by calling hash_release
266 ret
= (*func
)(hb
, arg
);
267 if (ret
== HASHWALK_ABORT
)
273 void hash_clean(struct hash
*hash
, void (*free_func
)(void *))
276 struct hash_bucket
*hb
;
277 struct hash_bucket
*next
;
279 for (i
= 0; i
< hash
->size
; i
++) {
280 for (hb
= hash
->index
[i
]; hb
; hb
= next
) {
284 (*free_func
)(hb
->data
);
286 XFREE(MTYPE_HASH_BACKET
, hb
);
289 hash
->index
[i
] = NULL
;
293 hash
->stats
.empty
= hash
->size
;
296 static void hash_to_list_iter(struct hash_bucket
*hb
, void *arg
)
298 struct list
*list
= arg
;
300 listnode_add(list
, hb
->data
);
303 struct list
*hash_to_list(struct hash
*hash
)
305 struct list
*list
= list_new();
307 hash_iterate(hash
, hash_to_list_iter
, list
);
311 void hash_free(struct hash
*hash
)
313 frr_with_mutex(&_hashes_mtx
) {
315 listnode_delete(_hashes
, hash
);
316 if (_hashes
->count
== 0) {
317 list_delete(&_hashes
);
322 XFREE(MTYPE_HASH
, hash
->name
);
324 XFREE(MTYPE_HASH_INDEX
, hash
->index
);
325 XFREE(MTYPE_HASH
, hash
);
329 /* CLI commands ------------------------------------------------------------ */
331 DEFUN_NOSH(show_hash_stats
,
333 "show debugging hashtable [statistics]",
336 "Statistics about hash tables\n"
337 "Statistics about hash tables\n")
341 struct ttable
*tt
= ttable_new(&ttable_styles
[TTSTYLE_BLANK
]);
343 ttable_add_row(tt
, "Hash table|Buckets|Entries|Empty|LF|SD|FLF|SD");
344 tt
->style
.cell
.lpad
= 2;
345 tt
->style
.cell
.rpad
= 1;
346 tt
->style
.corner
= '+';
348 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
350 /* Summary statistics calculated are:
352 * - Load factor: This is the number of elements in the table divided
353 * by the number of buckets. Since this hash table implementation
354 * uses chaining, this value can be greater than 1.
355 * This number provides information on how 'full' the table is, but
356 * does not provide information on how evenly distributed the
358 * Notably, a load factor >= 1 does not imply that every bucket has
359 * an element; with a pathological hash function, all elements could
360 * be in a single bucket.
362 * - Full load factor: this is the number of elements in the table
363 * divided by the number of buckets that have some elements in them.
365 * - Std. Dev.: This is the standard deviation calculated from the
366 * relevant load factor. If the load factor is the mean of number of
367 * elements per bucket, the standard deviation measures how much any
368 * particular bucket is likely to deviate from the mean.
369 * As a rule of thumb this number should be less than 2, and ideally
370 * <= 1 for optimal performance. A number larger than 3 generally
371 * indicates a poor hash function.
374 double lf
; // load factor
375 double flf
; // full load factor
376 double var
; // overall variance
377 double fvar
; // full variance
378 double stdv
; // overall stddev
379 double fstdv
; // full stddev
381 long double x2
; // h->count ^ 2
382 long double ldc
; // (long double) h->count
383 long double full
; // h->size - h->stats.empty
384 long double ssq
; // ssq casted to long double
386 pthread_mutex_lock(&_hashes_mtx
);
388 pthread_mutex_unlock(&_hashes_mtx
);
390 vty_out(vty
, "No hash tables in use.\n");
394 for (ALL_LIST_ELEMENTS_RO(_hashes
, ln
, h
)) {
398 ssq
= (long double)h
->stats
.ssq
;
399 x2
= h
->count
* h
->count
;
400 ldc
= (long double)h
->count
;
401 full
= h
->size
- h
->stats
.empty
;
402 lf
= h
->count
/ (double)h
->size
;
403 flf
= full
? h
->count
/ (double)(full
) : 0;
404 var
= ldc
? (1.0 / ldc
) * (ssq
- x2
/ ldc
) : 0;
405 fvar
= full
? (1.0 / full
) * (ssq
- x2
/ full
) : 0;
406 var
= (var
< .0001) ? 0 : var
;
407 fvar
= (fvar
< .0001) ? 0 : fvar
;
411 ttable_add_row(tt
, "%s|%d|%ld|%.0f%%|%.2lf|%.2lf|%.2lf|%.2lf",
412 h
->name
, h
->size
, h
->count
,
413 (h
->stats
.empty
/ (double)h
->size
) * 100, lf
,
416 pthread_mutex_unlock(&_hashes_mtx
);
419 char header
[] = "Showing hash table statistics for ";
420 char underln
[sizeof(header
) + strlen(frr_protonameinst
)];
421 memset(underln
, '-', sizeof(underln
));
422 underln
[sizeof(underln
) - 1] = '\0';
423 vty_out(vty
, "%s%s\n", header
, frr_protonameinst
);
424 vty_out(vty
, "%s\n", underln
);
426 vty_out(vty
, "# allocated: %d\n", _hashes
->count
);
427 vty_out(vty
, "# named: %d\n\n", tt
->nrows
- 1);
430 ttable_colseps(tt
, 0, RIGHT
, true, '|');
431 char *table
= ttable_dump(tt
, "\n");
432 vty_out(vty
, "%s\n", table
);
433 XFREE(MTYPE_TMP
, table
);
435 vty_out(vty
, "No named hash tables to display.\n");
442 void hash_cmd_init(void)
444 install_element(ENABLE_NODE
, &show_hash_stats_cmd
);