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);
46 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
50 void BV (clib_bihash_init)
51 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
55 nbuckets = 1 << (max_log2 (nbuckets));
57 h->name = (u8 *) name;
58 h->nbuckets = nbuckets;
59 h->log2_nbuckets = max_log2 (nbuckets);
60 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);
82 #if BIHASH_INSTANTIATE_IMMEDIATELY
83 BV (clib_bihash_instantiate) (h);
88 #if !defined (MFD_ALLOW_SEALING)
89 #define MFD_ALLOW_SEALING 0x0002U
92 void BV (clib_bihash_master_init_svm)
93 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
97 vec_header_t *freelist_vh;
100 ASSERT (memory_size < (1ULL << 32));
101 /* Set up for memfd sharing */
102 if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
104 clib_unix_warning ("memfd_create");
108 if (ftruncate (fd, memory_size) < 0)
110 clib_unix_warning ("ftruncate");
114 /* Not mission-critical, complain and continue */
115 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
116 clib_unix_warning ("fcntl (F_ADD_SEALS)");
118 mmap_addr = mmap (0, memory_size,
119 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
121 if (mmap_addr == MAP_FAILED)
123 clib_unix_warning ("mmap failed");
127 h->sh = (void *) mmap_addr;
129 nbuckets = 1 << (max_log2 (nbuckets));
131 h->name = (u8 *) name;
132 h->sh->nbuckets = h->nbuckets = nbuckets;
133 h->log2_nbuckets = max_log2 (nbuckets);
135 alloc_arena (h) = (u64) (uword) mmap_addr;
136 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
137 alloc_arena_size (h) = memory_size;
139 bucket_size = nbuckets * sizeof (h->buckets[0]);
140 h->buckets = BV (alloc_aligned) (h, bucket_size);
141 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
143 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
144 h->alloc_lock[0] = 0;
146 h->sh->alloc_lock_as_u64 =
147 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
149 BV (alloc_aligned) (h,
150 sizeof (vec_header_t) +
151 BIHASH_FREELIST_LENGTH * sizeof (u64));
152 freelist_vh->len = BIHASH_FREELIST_LENGTH;
153 freelist_vh->dlmalloc_header_offset = 0xDEADBEEF;
154 h->sh->freelists_as_u64 =
155 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
156 h->freelists = (void *) (freelist_vh->vector_data);
161 void BV (clib_bihash_slave_init_svm)
162 (BVT (clib_bihash) * h, char *name, int fd)
166 BVT (clib_bihash_shared_header) * sh;
168 /* Trial mapping, to learn the segment size */
169 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
170 if (mmap_addr == MAP_FAILED)
172 clib_unix_warning ("trial mmap failed");
176 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
178 memory_size = sh->alloc_arena_size;
180 munmap (mmap_addr, 4096);
182 /* Actual mapping, at the required size */
183 mmap_addr = mmap (0, memory_size,
184 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
186 if (mmap_addr == MAP_FAILED)
188 clib_unix_warning ("mmap failed");
194 h->sh = (void *) mmap_addr;
195 alloc_arena (h) = (u64) (uword) mmap_addr;
198 h->name = (u8 *) name;
199 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
200 h->nbuckets = h->sh->nbuckets;
201 h->log2_nbuckets = max_log2 (h->nbuckets);
203 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
204 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
207 #endif /* BIHASH_32_64_SVM */
209 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
210 format_function_t * fmt_fn)
215 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
219 if (PREDICT_FALSE (alloc_arena (h) == 0))
220 goto never_initialized;
222 vec_free (h->working_copies);
223 vec_free (h->working_copy_lengths);
224 #if BIHASH_32_64_SVM == 0
225 vec_free (h->freelists);
228 (void) close (h->memfd);
230 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)), alloc_arena_size (h));
232 clib_memset (h, 0, sizeof (*h));
233 for (i = 0; i < vec_len (clib_all_bihashes); i++)
235 if ((void *) h == clib_all_bihashes[i])
237 vec_delete (clib_all_bihashes, 1, i);
241 clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...",
246 BVT (clib_bihash_value) *
247 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
249 BVT (clib_bihash_value) * rv = 0;
251 ASSERT (h->alloc_lock[0]);
254 ASSERT (log2_pages < vec_len (h->freelists));
257 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
259 vec_validate_init_empty (h->freelists, log2_pages, 0);
260 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
263 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
264 h->freelists[log2_pages] = rv->next_free_as_u64;
269 * Latest gcc complains that the length arg is zero
270 * if we replace (1<<log2_pages) with vec_len(rv).
273 clib_memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
278 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
281 ASSERT (h->alloc_lock[0]);
283 ASSERT (vec_len (h->freelists) > log2_pages);
286 clib_memset (v, 0xFE, sizeof (*v) * (1 << log2_pages));
288 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
289 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
293 BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
295 BVT (clib_bihash_value) * v;
296 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
297 BVT (clib_bihash_value) * working_copy;
298 u32 thread_index = os_get_thread_index ();
299 int log2_working_copy_length;
301 ASSERT (h->alloc_lock[0]);
303 if (thread_index >= vec_len (h->working_copies))
305 vec_validate (h->working_copies, thread_index);
306 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
310 * working_copies are per-cpu so that near-simultaneous
311 * updates from multiple threads will not result in sporadic, spurious
314 working_copy = h->working_copies[thread_index];
315 log2_working_copy_length = h->working_copy_lengths[thread_index];
317 h->saved_bucket.as_u64 = b->as_u64;
319 if (b->log2_pages > log2_working_copy_length)
322 * It's not worth the bookkeeping to free working copies
324 * clib_mem_free (working_copy);
326 working_copy = BV (alloc_aligned)
327 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
328 h->working_copy_lengths[thread_index] = b->log2_pages;
329 h->working_copies[thread_index] = working_copy;
331 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
332 1ULL << b->log2_pages);
335 v = BV (clib_bihash_get_value) (h, b->offset);
337 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
338 working_bucket.as_u64 = b->as_u64;
339 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
340 CLIB_MEMORY_BARRIER ();
341 b->as_u64 = working_bucket.as_u64;
342 h->working_copies[thread_index] = working_copy;
346 BVT (clib_bihash_value) *
347 BV (split_and_rehash)
348 (BVT (clib_bihash) * h,
349 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
352 BVT (clib_bihash_value) * new_values, *new_v;
353 int i, j, length_in_kvs;
355 ASSERT (h->alloc_lock[0]);
357 new_values = BV (value_alloc) (h, new_log2_pages);
358 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
360 for (i = 0; i < length_in_kvs; i++)
364 /* Entry not in use? Forget it */
365 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
368 /* rehash the item onto its new home-page */
369 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
370 new_hash >>= h->log2_nbuckets;
371 new_hash &= (1 << new_log2_pages) - 1;
372 new_v = &new_values[new_hash];
374 /* Across the new home-page */
375 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
378 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
380 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
381 sizeof (new_v->kvp[j]));
385 /* Crap. Tell caller to try again */
386 BV (value_free) (h, new_values, new_log2_pages);
395 BVT (clib_bihash_value) *
396 BV (split_and_rehash_linear)
397 (BVT (clib_bihash) * h,
398 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
401 BVT (clib_bihash_value) * new_values;
402 int i, j, new_length, old_length;
404 ASSERT (h->alloc_lock[0]);
406 new_values = BV (value_alloc) (h, new_log2_pages);
407 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
408 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
411 /* Across the old value array */
412 for (i = 0; i < old_length; i++)
414 /* Find a free slot in the new linear scan bucket */
415 for (; j < new_length; j++)
417 /* Old value not in use? Forget it. */
418 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
421 /* New value should never be in use */
422 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
424 /* Copy the old value and move along */
425 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
426 sizeof (new_values->kvp[j]));
431 /* This should never happen... */
432 clib_warning ("BUG: linear rehash failed!");
433 BV (value_free) (h, new_values, new_log2_pages);
441 static inline int BV (clib_bihash_add_del_inline)
442 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
443 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
446 BVT (clib_bihash_bucket) * b, tmp_b;
447 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
450 u32 new_log2_pages, old_log2_pages;
451 u32 thread_index = os_get_thread_index ();
452 int mark_bucket_linear;
455 /* Create the table (is_add=1), or flunk the request now (is_add=0) */
456 if (PREDICT_FALSE (alloc_arena (h) == 0))
460 BV (clib_bihash_instantiate) (h);
463 hash = BV (clib_bihash_hash) (add_v);
465 bucket_index = hash & (h->nbuckets - 1);
466 b = &h->buckets[bucket_index];
468 hash >>= h->log2_nbuckets;
470 BV (clib_bihash_lock_bucket) (b);
472 /* First elt in the bucket? */
473 if (BV (clib_bihash_bucket_is_empty) (b))
477 BV (clib_bihash_unlock_bucket) (b);
481 BV (clib_bihash_alloc_lock) (h);
482 v = BV (value_alloc) (h, 0);
483 BV (clib_bihash_alloc_unlock) (h);
486 tmp_b.as_u64 = 0; /* clears bucket lock */
487 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
489 CLIB_MEMORY_BARRIER ();
491 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
492 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
497 /* WARNING: we're still looking at the live copy... */
498 limit = BIHASH_KVP_PER_PAGE;
499 v = BV (clib_bihash_get_value) (h, b->offset);
501 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
502 if (b->linear_search)
503 limit <<= b->log2_pages;
508 * Because reader threads are looking at live data,
509 * we have to be extra careful. Readers do NOT hold the
510 * bucket lock. We need to be SLOWER than a search, past the
511 * point where readers CHECK the bucket lock.
515 * For obvious (in hindsight) reasons, see if we're supposed to
516 * replace an existing key, then look for an empty slot.
518 for (i = 0; i < limit; i++)
520 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
522 /* Add but do not overwrite? */
525 BV (clib_bihash_unlock_bucket) (b);
529 CLIB_MEMORY_BARRIER (); /* Add a delay */
530 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
531 BV (clib_bihash_unlock_bucket) (b);
532 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
537 * Look for an empty slot. If found, use it
539 for (i = 0; i < limit; i++)
541 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
544 * Copy the value first, so that if a reader manages
545 * to match the new key, the value will be right...
547 clib_memcpy_fast (&(v->kvp[i].value),
548 &add_v->value, sizeof (add_v->value));
549 CLIB_MEMORY_BARRIER (); /* Make sure the value has settled */
550 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
551 sizeof (add_v->key));
553 ASSERT (b->refcnt > 0);
554 BV (clib_bihash_unlock_bucket) (b);
555 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
559 /* look for stale data to overwrite */
562 for (i = 0; i < limit; i++)
564 if (is_stale_cb (&(v->kvp[i]), arg))
566 CLIB_MEMORY_BARRIER ();
567 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
568 BV (clib_bihash_unlock_bucket) (b);
569 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
574 /* Out of space in this bucket, split the bucket... */
576 else /* delete case */
578 for (i = 0; i < limit; i++)
580 /* Found the key? Kill it... */
581 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
583 clib_memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
584 /* Is the bucket empty? */
585 if (PREDICT_TRUE (b->refcnt > 1))
588 BV (clib_bihash_unlock_bucket) (b);
589 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
592 else /* yes, free it */
594 /* Save old bucket value, need log2_pages to free it */
595 tmp_b.as_u64 = b->as_u64;
596 CLIB_MEMORY_BARRIER ();
598 /* Kill and unlock the bucket */
601 /* And free the backing storage */
602 BV (clib_bihash_alloc_lock) (h);
603 /* Note: v currently points into the middle of the bucket */
604 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
605 BV (value_free) (h, v, tmp_b.log2_pages);
606 BV (clib_bihash_alloc_unlock) (h);
607 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
614 BV (clib_bihash_unlock_bucket) (b);
618 /* Move readers to a (locked) temp copy of the bucket */
619 BV (clib_bihash_alloc_lock) (h);
620 BV (make_working_copy) (h, b);
622 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
624 old_log2_pages = h->saved_bucket.log2_pages;
625 new_log2_pages = old_log2_pages + 1;
626 mark_bucket_linear = 0;
627 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
628 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
630 working_copy = h->working_copies[thread_index];
632 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
634 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
641 /* Try re-splitting. If that fails, fall back to linear search */
642 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
648 /* pinned collisions, use linear search */
650 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
652 mark_bucket_linear = 1;
653 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
655 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
656 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
660 /* Try to add the new entry */
662 new_hash = BV (clib_bihash_hash) (add_v);
663 limit = BIHASH_KVP_PER_PAGE;
664 if (mark_bucket_linear)
665 limit <<= new_log2_pages;
666 new_hash >>= h->log2_nbuckets;
667 new_hash &= (1 << new_log2_pages) - 1;
668 new_v += mark_bucket_linear ? 0 : new_hash;
670 for (i = 0; i < limit; i++)
672 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
674 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
679 /* Crap. Try again */
680 BV (value_free) (h, save_new_v, new_log2_pages);
682 * If we've already doubled the size of the bucket once,
683 * fall back to linear search now.
691 tmp_b.log2_pages = new_log2_pages;
692 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
693 tmp_b.linear_search = mark_bucket_linear;
694 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
695 ASSERT (tmp_b.refcnt > 0);
697 CLIB_MEMORY_BARRIER ();
698 b->as_u64 = tmp_b.as_u64;
699 /* free the old bucket */
700 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
701 BV (value_free) (h, v, h->saved_bucket.log2_pages);
702 BV (clib_bihash_alloc_unlock) (h);
706 int BV (clib_bihash_add_del)
707 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
709 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
712 int BV (clib_bihash_add_or_overwrite_stale)
713 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
714 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
716 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
719 int BV (clib_bihash_search)
720 (BVT (clib_bihash) * h,
721 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
725 BVT (clib_bihash_value) * v;
726 BVT (clib_bihash_bucket) * b;
731 if (PREDICT_FALSE (alloc_arena (h) == 0))
734 hash = BV (clib_bihash_hash) (search_key);
736 bucket_index = hash & (h->nbuckets - 1);
737 b = &h->buckets[bucket_index];
739 if (BV (clib_bihash_bucket_is_empty) (b))
742 if (PREDICT_FALSE (b->lock))
744 volatile BVT (clib_bihash_bucket) * bv = b;
749 hash >>= h->log2_nbuckets;
751 v = BV (clib_bihash_get_value) (h, b->offset);
752 limit = BIHASH_KVP_PER_PAGE;
753 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
754 if (PREDICT_FALSE (b->linear_search))
755 limit <<= b->log2_pages;
757 for (i = 0; i < limit; i++)
759 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
768 u8 *BV (format_bihash) (u8 * s, va_list * args)
770 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
771 int verbose = va_arg (*args, int);
772 BVT (clib_bihash_bucket) * b;
773 BVT (clib_bihash_value) * v;
775 u64 active_elements = 0;
776 u64 active_buckets = 0;
777 u64 linear_buckets = 0;
780 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
782 if (PREDICT_FALSE (alloc_arena (h) == 0))
783 return format (s, "[empty, uninitialized]");
785 for (i = 0; i < h->nbuckets; i++)
788 if (BV (clib_bihash_bucket_is_empty) (b))
791 s = format (s, "[%d]: empty\n", i);
797 if (b->linear_search)
802 s = format (s, "[%d]: heap offset %lld, len %d, linear %d\n", i,
803 b->offset, (1 << b->log2_pages), b->linear_search);
806 v = BV (clib_bihash_get_value) (h, b->offset);
807 for (j = 0; j < (1 << b->log2_pages); j++)
809 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
811 if (BV (clib_bihash_is_free) (&v->kvp[k]))
814 s = format (s, " %d: empty\n",
815 j * BIHASH_KVP_PER_PAGE + k);
822 s = format (s, " %d: %U\n",
823 j * BIHASH_KVP_PER_PAGE + k,
824 h->fmt_fn, &(v->kvp[k]), verbose);
828 s = format (s, " %d: %U\n",
829 j * BIHASH_KVP_PER_PAGE + k,
830 BV (format_bihash_kvp), &(v->kvp[k]));
839 s = format (s, " %lld active elements %lld active buckets\n",
840 active_elements, active_buckets);
841 s = format (s, " %d free lists\n", vec_len (h->freelists));
843 for (i = 0; i < vec_len (h->freelists); i++)
846 BVT (clib_bihash_value) * free_elt;
847 u64 free_elt_as_u64 = h->freelists[i];
849 while (free_elt_as_u64)
851 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
853 free_elt_as_u64 = free_elt->next_free_as_u64;
856 if (nfree || verbose)
857 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
860 s = format (s, " %lld linear search buckets\n", linear_buckets);
861 used_bytes = alloc_arena_next (h);
863 " arena: base %llx, next %llx\n"
864 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
865 alloc_arena (h), alloc_arena_next (h),
866 used_bytes, used_bytes >> 20,
867 alloc_arena_size (h), alloc_arena_size (h) >> 20);
871 void BV (clib_bihash_foreach_key_value_pair)
872 (BVT (clib_bihash) * h, void *callback, void *arg)
875 BVT (clib_bihash_bucket) * b;
876 BVT (clib_bihash_value) * v;
877 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
879 if (PREDICT_FALSE (alloc_arena (h) == 0))
882 for (i = 0; i < h->nbuckets; i++)
885 if (BV (clib_bihash_bucket_is_empty) (b))
888 v = BV (clib_bihash_get_value) (h, b->offset);
889 for (j = 0; j < (1 << b->log2_pages); j++)
891 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
893 if (BV (clib_bihash_is_free) (&v->kvp[k]))
896 (*fp) (&v->kvp[k], arg);
898 * In case the callback deletes the last entry in the bucket...
900 if (BV (clib_bihash_bucket_is_empty) (b))
913 * fd.io coding-style-patch-verification: ON
916 * eval: (c-set-style "gnu")