2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
18 static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
22 /* Round to an even number of cache lines */
23 nbytes += CLIB_CACHE_LINE_BYTES - 1;
24 nbytes &= ~(CLIB_CACHE_LINE_BYTES - 1);
26 rv = alloc_arena_next (h);
27 alloc_arena_next (h) += nbytes;
29 if (alloc_arena_next (h) > alloc_arena_size (h))
32 return (void *) (uword) (rv + alloc_arena (h));
35 void BV (clib_bihash_instantiate) (BVT (clib_bihash) * h)
39 alloc_arena (h) = (uword) clib_mem_vm_alloc (h->memory_size);
40 alloc_arena_next (h) = 0;
41 alloc_arena_size (h) = h->memory_size;
43 bucket_size = h->nbuckets * sizeof (h->buckets[0]);
44 h->buckets = BV (alloc_aligned) (h, bucket_size);
45 CLIB_MEMORY_BARRIER ();
49 void BV (clib_bihash_init)
50 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
54 nbuckets = 1 << (max_log2 (nbuckets));
56 h->name = (u8 *) name;
57 h->nbuckets = nbuckets;
58 h->log2_nbuckets = max_log2 (nbuckets);
59 h->memory_size = memory_size;
64 * Make sure the requested size is rational. The max table
65 * size without playing the alignment card is 64 Gbytes.
66 * If someone starts complaining that's not enough, we can shift
67 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
69 ASSERT (memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
72 /* Add this hash table to the list */
73 for (i = 0; i < vec_len (clib_all_bihashes); i++)
74 if (clib_all_bihashes[i] == h)
77 /* Unfortunately, the heap push/pop is required.... */
78 oldheap = clib_all_bihash_set_heap ();
79 vec_add1 (clib_all_bihashes, (void *) h);
80 clib_mem_set_heap (oldheap);
84 * Set up the lock now, so we can use it to make the first add
87 h->alloc_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
88 CLIB_CACHE_LINE_BYTES);
91 #if BIHASH_INSTANTIATE_IMMEDIATELY
92 BV (clib_bihash_instantiate) (h);
97 #if !defined (MFD_ALLOW_SEALING)
98 #define MFD_ALLOW_SEALING 0x0002U
101 void BV (clib_bihash_master_init_svm)
102 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
106 vec_header_t *freelist_vh;
109 ASSERT (memory_size < (1ULL << 32));
110 /* Set up for memfd sharing */
111 if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
113 clib_unix_warning ("memfd_create");
117 if (ftruncate (fd, memory_size) < 0)
119 clib_unix_warning ("ftruncate");
123 /* Not mission-critical, complain and continue */
124 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
125 clib_unix_warning ("fcntl (F_ADD_SEALS)");
127 mmap_addr = mmap (0, memory_size,
128 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
130 if (mmap_addr == MAP_FAILED)
132 clib_unix_warning ("mmap failed");
136 h->sh = (void *) mmap_addr;
138 nbuckets = 1 << (max_log2 (nbuckets));
140 h->name = (u8 *) name;
141 h->sh->nbuckets = h->nbuckets = nbuckets;
142 h->log2_nbuckets = max_log2 (nbuckets);
144 alloc_arena (h) = (u64) (uword) mmap_addr;
145 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
146 alloc_arena_size (h) = memory_size;
148 bucket_size = nbuckets * sizeof (h->buckets[0]);
149 h->buckets = BV (alloc_aligned) (h, bucket_size);
150 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
152 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
153 h->alloc_lock[0] = 0;
155 h->sh->alloc_lock_as_u64 =
156 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
158 BV (alloc_aligned) (h,
159 sizeof (vec_header_t) +
160 BIHASH_FREELIST_LENGTH * sizeof (u64));
161 freelist_vh->len = BIHASH_FREELIST_LENGTH;
162 freelist_vh->dlmalloc_header_offset = 0xDEADBEEF;
163 h->sh->freelists_as_u64 =
164 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
165 h->freelists = (void *) (freelist_vh->vector_data);
170 void BV (clib_bihash_slave_init_svm)
171 (BVT (clib_bihash) * h, char *name, int fd)
175 BVT (clib_bihash_shared_header) * sh;
177 /* Trial mapping, to learn the segment size */
178 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
179 if (mmap_addr == MAP_FAILED)
181 clib_unix_warning ("trial mmap failed");
185 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
187 memory_size = sh->alloc_arena_size;
189 munmap (mmap_addr, 4096);
191 /* Actual mapping, at the required size */
192 mmap_addr = mmap (0, memory_size,
193 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
195 if (mmap_addr == MAP_FAILED)
197 clib_unix_warning ("mmap failed");
203 h->sh = (void *) mmap_addr;
204 alloc_arena (h) = (u64) (uword) mmap_addr;
207 h->name = (u8 *) name;
208 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
209 h->nbuckets = h->sh->nbuckets;
210 h->log2_nbuckets = max_log2 (h->nbuckets);
212 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
213 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
216 #endif /* BIHASH_32_64_SVM */
218 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
219 format_function_t * fmt_fn)
224 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
228 if (PREDICT_FALSE (h->instantiated == 0))
229 goto never_initialized;
232 vec_free (h->working_copies);
233 vec_free (h->working_copy_lengths);
234 #if BIHASH_32_64_SVM == 0
235 vec_free (h->freelists);
238 (void) close (h->memfd);
240 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)), alloc_arena_size (h));
242 clib_memset (h, 0, sizeof (*h));
243 for (i = 0; i < vec_len (clib_all_bihashes); i++)
245 if ((void *) h == clib_all_bihashes[i])
247 vec_delete (clib_all_bihashes, 1, i);
251 clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...",
256 BVT (clib_bihash_value) *
257 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
259 BVT (clib_bihash_value) * rv = 0;
261 ASSERT (h->alloc_lock[0]);
264 ASSERT (log2_pages < vec_len (h->freelists));
267 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
269 vec_validate_init_empty (h->freelists, log2_pages, 0);
270 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
273 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
274 h->freelists[log2_pages] = rv->next_free_as_u64;
279 * Latest gcc complains that the length arg is zero
280 * if we replace (1<<log2_pages) with vec_len(rv).
283 clib_memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
288 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
291 ASSERT (h->alloc_lock[0]);
293 ASSERT (vec_len (h->freelists) > log2_pages);
296 clib_memset (v, 0xFE, sizeof (*v) * (1 << log2_pages));
298 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
299 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
303 BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
305 BVT (clib_bihash_value) * v;
306 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
307 BVT (clib_bihash_value) * working_copy;
308 u32 thread_index = os_get_thread_index ();
309 int log2_working_copy_length;
311 ASSERT (h->alloc_lock[0]);
313 if (thread_index >= vec_len (h->working_copies))
315 vec_validate (h->working_copies, thread_index);
316 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
320 * working_copies are per-cpu so that near-simultaneous
321 * updates from multiple threads will not result in sporadic, spurious
324 working_copy = h->working_copies[thread_index];
325 log2_working_copy_length = h->working_copy_lengths[thread_index];
327 h->saved_bucket.as_u64 = b->as_u64;
329 if (b->log2_pages > log2_working_copy_length)
332 * It's not worth the bookkeeping to free working copies
334 * clib_mem_free (working_copy);
336 working_copy = BV (alloc_aligned)
337 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
338 h->working_copy_lengths[thread_index] = b->log2_pages;
339 h->working_copies[thread_index] = working_copy;
341 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
342 1ULL << b->log2_pages);
345 v = BV (clib_bihash_get_value) (h, b->offset);
347 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
348 working_bucket.as_u64 = b->as_u64;
349 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
350 CLIB_MEMORY_BARRIER ();
351 b->as_u64 = working_bucket.as_u64;
352 h->working_copies[thread_index] = working_copy;
356 BVT (clib_bihash_value) *
357 BV (split_and_rehash)
358 (BVT (clib_bihash) * h,
359 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
362 BVT (clib_bihash_value) * new_values, *new_v;
363 int i, j, length_in_kvs;
365 ASSERT (h->alloc_lock[0]);
367 new_values = BV (value_alloc) (h, new_log2_pages);
368 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
370 for (i = 0; i < length_in_kvs; i++)
374 /* Entry not in use? Forget it */
375 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
378 /* rehash the item onto its new home-page */
379 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
380 new_hash >>= h->log2_nbuckets;
381 new_hash &= (1 << new_log2_pages) - 1;
382 new_v = &new_values[new_hash];
384 /* Across the new home-page */
385 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
388 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
390 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
391 sizeof (new_v->kvp[j]));
395 /* Crap. Tell caller to try again */
396 BV (value_free) (h, new_values, new_log2_pages);
405 BVT (clib_bihash_value) *
406 BV (split_and_rehash_linear)
407 (BVT (clib_bihash) * h,
408 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
411 BVT (clib_bihash_value) * new_values;
412 int i, j, new_length, old_length;
414 ASSERT (h->alloc_lock[0]);
416 new_values = BV (value_alloc) (h, new_log2_pages);
417 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
418 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
421 /* Across the old value array */
422 for (i = 0; i < old_length; i++)
424 /* Find a free slot in the new linear scan bucket */
425 for (; j < new_length; j++)
427 /* Old value not in use? Forget it. */
428 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
431 /* New value should never be in use */
432 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
434 /* Copy the old value and move along */
435 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
436 sizeof (new_values->kvp[j]));
441 /* This should never happen... */
442 clib_warning ("BUG: linear rehash failed!");
443 BV (value_free) (h, new_values, new_log2_pages);
451 static inline int BV (clib_bihash_add_del_inline)
452 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
453 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
456 BVT (clib_bihash_bucket) * b, tmp_b;
457 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
460 u32 new_log2_pages, old_log2_pages;
461 u32 thread_index = os_get_thread_index ();
462 int mark_bucket_linear;
466 * Create the table (is_add=1,2), or flunk the request now (is_add=0)
467 * Use the alloc_lock to protect the instantiate operation.
469 if (PREDICT_FALSE (h->instantiated == 0))
474 BV (clib_bihash_alloc_lock) (h);
475 if (h->instantiated == 0)
476 BV (clib_bihash_instantiate) (h);
477 BV (clib_bihash_alloc_unlock) (h);
480 hash = BV (clib_bihash_hash) (add_v);
482 bucket_index = hash & (h->nbuckets - 1);
483 b = &h->buckets[bucket_index];
485 hash >>= h->log2_nbuckets;
487 BV (clib_bihash_lock_bucket) (b);
489 /* First elt in the bucket? */
490 if (BV (clib_bihash_bucket_is_empty) (b))
494 BV (clib_bihash_unlock_bucket) (b);
498 BV (clib_bihash_alloc_lock) (h);
499 v = BV (value_alloc) (h, 0);
500 BV (clib_bihash_alloc_unlock) (h);
503 tmp_b.as_u64 = 0; /* clears bucket lock */
504 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
506 CLIB_MEMORY_BARRIER ();
508 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
509 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
514 /* WARNING: we're still looking at the live copy... */
515 limit = BIHASH_KVP_PER_PAGE;
516 v = BV (clib_bihash_get_value) (h, b->offset);
518 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
519 if (b->linear_search)
520 limit <<= b->log2_pages;
525 * Because reader threads are looking at live data,
526 * we have to be extra careful. Readers do NOT hold the
527 * bucket lock. We need to be SLOWER than a search, past the
528 * point where readers CHECK the bucket lock.
532 * For obvious (in hindsight) reasons, see if we're supposed to
533 * replace an existing key, then look for an empty slot.
535 for (i = 0; i < limit; i++)
537 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
539 /* Add but do not overwrite? */
542 BV (clib_bihash_unlock_bucket) (b);
546 CLIB_MEMORY_BARRIER (); /* Add a delay */
547 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
548 BV (clib_bihash_unlock_bucket) (b);
549 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
554 * Look for an empty slot. If found, use it
556 for (i = 0; i < limit; i++)
558 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
561 * Copy the value first, so that if a reader manages
562 * to match the new key, the value will be right...
564 clib_memcpy_fast (&(v->kvp[i].value),
565 &add_v->value, sizeof (add_v->value));
566 CLIB_MEMORY_BARRIER (); /* Make sure the value has settled */
567 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
568 sizeof (add_v->key));
570 ASSERT (b->refcnt > 0);
571 BV (clib_bihash_unlock_bucket) (b);
572 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
576 /* look for stale data to overwrite */
579 for (i = 0; i < limit; i++)
581 if (is_stale_cb (&(v->kvp[i]), arg))
583 CLIB_MEMORY_BARRIER ();
584 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
585 BV (clib_bihash_unlock_bucket) (b);
586 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
591 /* Out of space in this bucket, split the bucket... */
593 else /* delete case */
595 for (i = 0; i < limit; i++)
597 /* Found the key? Kill it... */
598 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
600 clib_memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
601 /* Is the bucket empty? */
602 if (PREDICT_TRUE (b->refcnt > 1))
605 BV (clib_bihash_unlock_bucket) (b);
606 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
609 else /* yes, free it */
611 /* Save old bucket value, need log2_pages to free it */
612 tmp_b.as_u64 = b->as_u64;
613 CLIB_MEMORY_BARRIER ();
615 /* Kill and unlock the bucket */
618 /* And free the backing storage */
619 BV (clib_bihash_alloc_lock) (h);
620 /* Note: v currently points into the middle of the bucket */
621 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
622 BV (value_free) (h, v, tmp_b.log2_pages);
623 BV (clib_bihash_alloc_unlock) (h);
624 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
631 BV (clib_bihash_unlock_bucket) (b);
635 /* Move readers to a (locked) temp copy of the bucket */
636 BV (clib_bihash_alloc_lock) (h);
637 BV (make_working_copy) (h, b);
639 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
641 old_log2_pages = h->saved_bucket.log2_pages;
642 new_log2_pages = old_log2_pages + 1;
643 mark_bucket_linear = 0;
644 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
645 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
647 working_copy = h->working_copies[thread_index];
649 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
651 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
658 /* Try re-splitting. If that fails, fall back to linear search */
659 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
665 /* pinned collisions, use linear search */
667 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
669 mark_bucket_linear = 1;
670 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
672 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
673 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
677 /* Try to add the new entry */
679 new_hash = BV (clib_bihash_hash) (add_v);
680 limit = BIHASH_KVP_PER_PAGE;
681 if (mark_bucket_linear)
682 limit <<= new_log2_pages;
683 new_hash >>= h->log2_nbuckets;
684 new_hash &= (1 << new_log2_pages) - 1;
685 new_v += mark_bucket_linear ? 0 : new_hash;
687 for (i = 0; i < limit; i++)
689 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
691 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
696 /* Crap. Try again */
697 BV (value_free) (h, save_new_v, new_log2_pages);
699 * If we've already doubled the size of the bucket once,
700 * fall back to linear search now.
708 tmp_b.log2_pages = new_log2_pages;
709 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
710 tmp_b.linear_search = mark_bucket_linear;
711 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
712 ASSERT (tmp_b.refcnt > 0);
714 CLIB_MEMORY_BARRIER ();
715 b->as_u64 = tmp_b.as_u64;
716 /* free the old bucket */
717 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
718 BV (value_free) (h, v, h->saved_bucket.log2_pages);
719 BV (clib_bihash_alloc_unlock) (h);
723 int BV (clib_bihash_add_del)
724 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
726 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
729 int BV (clib_bihash_add_or_overwrite_stale)
730 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
731 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
733 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
736 int BV (clib_bihash_search)
737 (BVT (clib_bihash) * h,
738 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
742 BVT (clib_bihash_value) * v;
743 BVT (clib_bihash_bucket) * b;
748 if (PREDICT_FALSE (alloc_arena (h) == 0))
751 hash = BV (clib_bihash_hash) (search_key);
753 bucket_index = hash & (h->nbuckets - 1);
754 b = &h->buckets[bucket_index];
756 if (BV (clib_bihash_bucket_is_empty) (b))
759 if (PREDICT_FALSE (b->lock))
761 volatile BVT (clib_bihash_bucket) * bv = b;
766 hash >>= h->log2_nbuckets;
768 v = BV (clib_bihash_get_value) (h, b->offset);
769 limit = BIHASH_KVP_PER_PAGE;
770 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
771 if (PREDICT_FALSE (b->linear_search))
772 limit <<= b->log2_pages;
774 for (i = 0; i < limit; i++)
776 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
785 u8 *BV (format_bihash) (u8 * s, va_list * args)
787 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
788 int verbose = va_arg (*args, int);
789 BVT (clib_bihash_bucket) * b;
790 BVT (clib_bihash_value) * v;
792 u64 active_elements = 0;
793 u64 active_buckets = 0;
794 u64 linear_buckets = 0;
797 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
799 if (PREDICT_FALSE (alloc_arena (h) == 0))
800 return format (s, "[empty, uninitialized]");
802 for (i = 0; i < h->nbuckets; i++)
805 if (BV (clib_bihash_bucket_is_empty) (b))
808 s = format (s, "[%d]: empty\n", i);
814 if (b->linear_search)
819 s = format (s, "[%d]: heap offset %lld, len %d, linear %d\n", i,
820 b->offset, (1 << b->log2_pages), b->linear_search);
823 v = BV (clib_bihash_get_value) (h, b->offset);
824 for (j = 0; j < (1 << b->log2_pages); j++)
826 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
828 if (BV (clib_bihash_is_free) (&v->kvp[k]))
831 s = format (s, " %d: empty\n",
832 j * BIHASH_KVP_PER_PAGE + k);
839 s = format (s, " %d: %U\n",
840 j * BIHASH_KVP_PER_PAGE + k,
841 h->fmt_fn, &(v->kvp[k]), verbose);
845 s = format (s, " %d: %U\n",
846 j * BIHASH_KVP_PER_PAGE + k,
847 BV (format_bihash_kvp), &(v->kvp[k]));
856 s = format (s, " %lld active elements %lld active buckets\n",
857 active_elements, active_buckets);
858 s = format (s, " %d free lists\n", vec_len (h->freelists));
860 for (i = 0; i < vec_len (h->freelists); i++)
863 BVT (clib_bihash_value) * free_elt;
864 u64 free_elt_as_u64 = h->freelists[i];
866 while (free_elt_as_u64)
868 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
870 free_elt_as_u64 = free_elt->next_free_as_u64;
873 if (nfree || verbose)
874 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
877 s = format (s, " %lld linear search buckets\n", linear_buckets);
878 used_bytes = alloc_arena_next (h);
880 " arena: base %llx, next %llx\n"
881 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
882 alloc_arena (h), alloc_arena_next (h),
883 used_bytes, used_bytes >> 20,
884 alloc_arena_size (h), alloc_arena_size (h) >> 20);
888 void BV (clib_bihash_foreach_key_value_pair)
889 (BVT (clib_bihash) * h, void *callback, void *arg)
892 BVT (clib_bihash_bucket) * b;
893 BVT (clib_bihash_value) * v;
894 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
896 if (PREDICT_FALSE (alloc_arena (h) == 0))
899 for (i = 0; i < h->nbuckets; i++)
902 if (BV (clib_bihash_bucket_is_empty) (b))
905 v = BV (clib_bihash_get_value) (h, b->offset);
906 for (j = 0; j < (1 << b->log2_pages); j++)
908 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
910 if (BV (clib_bihash_is_free) (&v->kvp[k]))
913 (*fp) (&v->kvp[k], arg);
915 * In case the callback deletes the last entry in the bucket...
917 if (BV (clib_bihash_bucket_is_empty) (b))
930 * fd.io coding-style-patch-verification: ON
933 * eval: (c-set-style "gnu")