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 #ifndef MAP_HUGE_SHIFT
19 #define MAP_HUGE_SHIFT 26
22 #ifndef BIIHASH_MIN_ALLOC_LOG2_PAGES
23 #define BIIHASH_MIN_ALLOC_LOG2_PAGES 10
26 static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
30 /* Round to an even number of cache lines */
31 nbytes = round_pow2 (nbytes, CLIB_CACHE_LINE_BYTES);
36 uword page_sz = sizeof (BVT (clib_bihash_value));
37 uword chunk_sz = round_pow2 (page_sz << BIIHASH_MIN_ALLOC_LOG2_PAGES,
38 CLIB_CACHE_LINE_BYTES);
40 BVT (clib_bihash_alloc_chunk) * chunk = h->chunks;
42 /* if there is enough space in the currenrt chunk */
43 if (chunk && chunk->bytes_left >= nbytes)
45 rv = chunk->next_alloc;
46 chunk->bytes_left -= nbytes;
47 chunk->next_alloc += nbytes;
51 /* requested allocation is bigger than chunk size */
52 if (nbytes >= chunk_sz)
54 oldheap = clib_mem_set_heap (h->heap);
55 chunk = clib_mem_alloc_aligned (nbytes + sizeof (*chunk),
56 CLIB_CACHE_LINE_BYTES);
57 clib_mem_set_heap (oldheap);
58 clib_memset_u8 (chunk, 0, sizeof (*chunk));
60 rv = (u8 *) (chunk + 1);
63 /* take 2nd place in the list */
64 chunk->next = h->chunks->next;
65 chunk->prev = h->chunks;
66 h->chunks->next = chunk;
68 chunk->next->prev = chunk;
76 oldheap = clib_mem_set_heap (h->heap);
77 chunk = clib_mem_alloc_aligned (chunk_sz + sizeof (*chunk),
78 CLIB_CACHE_LINE_BYTES);
79 clib_mem_set_heap (oldheap);
80 chunk->size = chunk_sz;
81 chunk->bytes_left = chunk_sz;
82 chunk->next_alloc = (u8 *) (chunk + 1);
83 chunk->next = h->chunks;
86 chunk->next->prev = chunk;
88 rv = chunk->next_alloc;
89 chunk->bytes_left -= nbytes;
90 chunk->next_alloc += nbytes;
94 rv = alloc_arena_next (h);
95 alloc_arena_next (h) += nbytes;
97 if (alloc_arena_next (h) > alloc_arena_size (h))
100 if (alloc_arena_next (h) > alloc_arena_mapped (h))
103 uword alloc = alloc_arena_next (h) - alloc_arena_mapped (h);
104 int mmap_flags = MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS;
105 int mmap_flags_huge = (mmap_flags | MAP_HUGETLB | MAP_LOCKED |
106 BIHASH_LOG2_HUGEPAGE_SIZE << MAP_HUGE_SHIFT);
108 /* new allocation is 25% of existing one */
109 if (alloc_arena_mapped (h) >> 2 > alloc)
110 alloc = alloc_arena_mapped (h) >> 2;
112 /* round allocation to page size */
113 alloc = round_pow2 (alloc, 1 << BIHASH_LOG2_HUGEPAGE_SIZE);
115 base = (void *) (uword) (alloc_arena (h) + alloc_arena_mapped (h));
117 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags_huge, -1, 0);
119 /* fallback - maybe we are still able to allocate normal pages */
120 if (rv == MAP_FAILED || mlock (base, alloc) != 0)
121 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
123 if (rv == MAP_FAILED)
126 alloc_arena_mapped (h) += alloc;
129 return (void *) (uword) (rv + alloc_arena (h));
132 static void BV (clib_bihash_instantiate) (BVT (clib_bihash) * h)
138 h->heap = clib_mem_get_heap ();
140 alloc_arena (h) = (uword) clib_mem_get_heap_base (h->heap);
144 alloc_arena (h) = clib_mem_vm_reserve (0, h->memory_size,
145 BIHASH_LOG2_HUGEPAGE_SIZE);
146 if (alloc_arena (h) == ~0)
148 alloc_arena_next (h) = 0;
149 alloc_arena_size (h) = h->memory_size;
150 alloc_arena_mapped (h) = 0;
153 bucket_size = h->nbuckets * sizeof (h->buckets[0]);
155 if (BIHASH_KVP_AT_BUCKET_LEVEL)
157 h->nbuckets * BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv));
159 h->buckets = BV (alloc_aligned) (h, bucket_size);
160 clib_memset_u8 (h->buckets, 0, bucket_size);
162 if (BIHASH_KVP_AT_BUCKET_LEVEL)
165 BVT (clib_bihash_bucket) * b;
169 for (i = 0; i < h->nbuckets; i++)
171 b->offset = BV (clib_bihash_get_offset) (h, (void *) (b + 1));
173 /* Mark all elements free */
174 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
175 sizeof (BVT (clib_bihash_kv)));
177 /* Compute next bucket start address */
178 b = (void *) (((uword) b) + sizeof (*b) +
179 (BIHASH_KVP_PER_PAGE *
180 sizeof (BVT (clib_bihash_kv))));
183 CLIB_MEMORY_STORE_BARRIER ();
187 void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a)
191 BVT (clib_bihash) * h = a->h;
193 a->nbuckets = 1 << (max_log2 (a->nbuckets));
195 h->name = (u8 *) a->name;
196 h->nbuckets = a->nbuckets;
197 h->log2_nbuckets = max_log2 (a->nbuckets);
198 h->memory_size = BIHASH_USE_HEAP ? 0 : a->memory_size;
200 h->fmt_fn = BV (format_bihash);
201 h->kvp_fmt_fn = a->kvp_fmt_fn;
206 * Make sure the requested size is rational. The max table
207 * size without playing the alignment card is 64 Gbytes.
208 * If someone starts complaining that's not enough, we can shift
209 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
212 ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
214 /* Add this hash table to the list */
215 if (a->dont_add_to_all_bihash_list == 0)
217 for (i = 0; i < vec_len (clib_all_bihashes); i++)
218 if (clib_all_bihashes[i] == h)
220 oldheap = clib_all_bihash_set_heap ();
221 vec_add1 (clib_all_bihashes, (void *) h);
222 clib_mem_set_heap (oldheap);
227 clib_mem_free ((void *) h->alloc_lock);
230 * Set up the lock now, so we can use it to make the first add
233 h->alloc_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
234 CLIB_CACHE_LINE_BYTES);
235 h->alloc_lock[0] = 0;
237 #if BIHASH_LAZY_INSTANTIATE
238 if (a->instantiate_immediately)
240 BV (clib_bihash_instantiate) (h);
243 void BV (clib_bihash_init)
244 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
246 BVT (clib_bihash_init2_args) _a, *a = &_a;
248 memset (a, 0, sizeof (*a));
252 a->nbuckets = nbuckets;
253 a->memory_size = memory_size;
255 BV (clib_bihash_init2) (a);
259 #if !defined (MFD_ALLOW_SEALING)
260 #define MFD_ALLOW_SEALING 0x0002U
263 void BV (clib_bihash_initiator_init_svm)
264 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
268 vec_header_t *freelist_vh;
271 ASSERT (BIHASH_USE_HEAP == 0);
273 ASSERT (memory_size < (1ULL << 32));
274 /* Set up for memfd sharing */
275 if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
277 clib_unix_warning ("memfd_create");
281 if (ftruncate (fd, memory_size) < 0)
283 clib_unix_warning ("ftruncate");
287 /* Not mission-critical, complain and continue */
288 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
289 clib_unix_warning ("fcntl (F_ADD_SEALS)");
291 mmap_addr = mmap (0, memory_size,
292 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
294 if (mmap_addr == MAP_FAILED)
296 clib_unix_warning ("mmap failed");
300 h->sh = (void *) mmap_addr;
302 nbuckets = 1 << (max_log2 (nbuckets));
304 h->name = (u8 *) name;
305 h->sh->nbuckets = h->nbuckets = nbuckets;
306 h->log2_nbuckets = max_log2 (nbuckets);
308 alloc_arena (h) = (u64) (uword) mmap_addr;
309 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
310 alloc_arena_size (h) = memory_size;
312 bucket_size = nbuckets * sizeof (h->buckets[0]);
313 h->buckets = BV (alloc_aligned) (h, bucket_size);
314 clib_memset_u8 (h->buckets, 0, bucket_size);
315 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
317 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
318 h->alloc_lock[0] = 0;
320 h->sh->alloc_lock_as_u64 =
321 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
323 BV (alloc_aligned) (h,
324 sizeof (vec_header_t) +
325 BIHASH_FREELIST_LENGTH * sizeof (u64));
326 freelist_vh->len = BIHASH_FREELIST_LENGTH;
327 h->sh->freelists_as_u64 =
328 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
329 h->freelists = (void *) (freelist_vh->vector_data);
331 h->fmt_fn = BV (format_bihash);
332 h->kvp_fmt_fn = NULL;
336 void BV (clib_bihash_responder_init_svm)
337 (BVT (clib_bihash) * h, char *name, int fd)
341 BVT (clib_bihash_shared_header) * sh;
343 ASSERT (BIHASH_USE_HEAP == 0);
345 /* Trial mapping, to learn the segment size */
346 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
347 if (mmap_addr == MAP_FAILED)
349 clib_unix_warning ("trial mmap failed");
353 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
355 memory_size = sh->alloc_arena_size;
357 munmap (mmap_addr, 4096);
359 /* Actual mapping, at the required size */
360 mmap_addr = mmap (0, memory_size,
361 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
363 if (mmap_addr == MAP_FAILED)
365 clib_unix_warning ("mmap failed");
371 h->sh = (void *) mmap_addr;
372 alloc_arena (h) = (u64) (uword) mmap_addr;
375 h->name = (u8 *) name;
376 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
377 h->nbuckets = h->sh->nbuckets;
378 h->log2_nbuckets = max_log2 (h->nbuckets);
380 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
381 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
382 h->fmt_fn = BV (format_bihash);
383 h->kvp_fmt_fn = NULL;
385 #endif /* BIHASH_32_64_SVM */
387 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
388 format_function_t * kvp_fmt_fn)
390 h->kvp_fmt_fn = kvp_fmt_fn;
393 int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h)
395 return (h->instantiated != 0);
398 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
402 if (PREDICT_FALSE (h->instantiated == 0))
403 goto never_initialized;
409 BVT (clib_bihash_alloc_chunk) * next, *chunk;
410 void *oldheap = clib_mem_set_heap (h->heap);
416 clib_mem_free (chunk);
419 clib_mem_set_heap (oldheap);
422 vec_free (h->working_copies);
423 vec_free (h->working_copy_lengths);
424 #if BIHASH_32_64_SVM == 0
425 vec_free (h->freelists);
428 (void) close (h->memfd);
430 if (BIHASH_USE_HEAP == 0)
431 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)),
432 alloc_arena_size (h));
434 clib_memset_u8 (h, 0, sizeof (*h));
435 for (i = 0; i < vec_len (clib_all_bihashes); i++)
437 if ((void *) h == clib_all_bihashes[i])
439 vec_delete (clib_all_bihashes, 1, i);
443 clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...",
448 BVT (clib_bihash_value) *
449 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
451 BVT (clib_bihash_value) * rv = 0;
453 ASSERT (h->alloc_lock[0]);
456 ASSERT (log2_pages < vec_len (h->freelists));
459 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
461 vec_validate_init_empty (h->freelists, log2_pages, 0);
462 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
465 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
466 h->freelists[log2_pages] = rv->next_free_as_u64;
471 * Latest gcc complains that the length arg is zero
472 * if we replace (1<<log2_pages) with vec_len(rv).
475 clib_memset_u8 (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
480 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
483 ASSERT (h->alloc_lock[0]);
485 ASSERT (vec_len (h->freelists) > log2_pages);
487 if (BIHASH_USE_HEAP && log2_pages >= BIIHASH_MIN_ALLOC_LOG2_PAGES)
489 /* allocations bigger or equal to chunk size always contain single
490 * alloc and they can be given back to heap */
492 BVT (clib_bihash_alloc_chunk) * c;
493 c = (BVT (clib_bihash_alloc_chunk) *) v - 1;
496 c->prev->next = c->next;
501 c->next->prev = c->prev;
503 oldheap = clib_mem_set_heap (h->heap);
505 clib_mem_set_heap (oldheap);
510 clib_memset_u8 (v, 0xFE, sizeof (*v) * (1 << log2_pages));
512 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
513 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
517 BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
519 BVT (clib_bihash_value) * v;
520 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
521 BVT (clib_bihash_value) * working_copy;
522 u32 thread_index = os_get_thread_index ();
523 int log2_working_copy_length;
525 ASSERT (h->alloc_lock[0]);
527 if (thread_index >= vec_len (h->working_copies))
529 vec_validate (h->working_copies, thread_index);
530 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
534 * working_copies are per-cpu so that near-simultaneous
535 * updates from multiple threads will not result in sporadic, spurious
538 working_copy = h->working_copies[thread_index];
539 log2_working_copy_length = h->working_copy_lengths[thread_index];
541 h->saved_bucket.as_u64 = b->as_u64;
543 if (b->log2_pages > log2_working_copy_length)
546 * It's not worth the bookkeeping to free working copies
548 * clib_mem_free (working_copy);
550 working_copy = BV (alloc_aligned)
551 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
552 h->working_copy_lengths[thread_index] = b->log2_pages;
553 h->working_copies[thread_index] = working_copy;
555 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
556 1ULL << b->log2_pages);
559 v = BV (clib_bihash_get_value) (h, b->offset);
561 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
562 working_bucket.as_u64 = b->as_u64;
563 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
564 CLIB_MEMORY_STORE_BARRIER ();
565 b->as_u64 = working_bucket.as_u64;
566 h->working_copies[thread_index] = working_copy;
570 BVT (clib_bihash_value) *
571 BV (split_and_rehash)
572 (BVT (clib_bihash) * h,
573 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
576 BVT (clib_bihash_value) * new_values, *new_v;
577 int i, j, length_in_kvs;
579 ASSERT (h->alloc_lock[0]);
581 new_values = BV (value_alloc) (h, new_log2_pages);
582 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
584 for (i = 0; i < length_in_kvs; i++)
588 /* Entry not in use? Forget it */
589 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
592 /* rehash the item onto its new home-page */
593 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
594 new_hash = extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
595 new_v = &new_values[new_hash];
597 /* Across the new home-page */
598 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
601 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
603 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
604 sizeof (new_v->kvp[j]));
608 /* Crap. Tell caller to try again */
609 BV (value_free) (h, new_values, new_log2_pages);
618 BVT (clib_bihash_value) *
619 BV (split_and_rehash_linear)
620 (BVT (clib_bihash) * h,
621 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
624 BVT (clib_bihash_value) * new_values;
625 int i, j, new_length, old_length;
627 ASSERT (h->alloc_lock[0]);
629 new_values = BV (value_alloc) (h, new_log2_pages);
630 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
631 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
634 /* Across the old value array */
635 for (i = 0; i < old_length; i++)
637 /* Find a free slot in the new linear scan bucket */
638 for (; j < new_length; j++)
640 /* Old value not in use? Forget it. */
641 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
644 /* New value should never be in use */
645 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
647 /* Copy the old value and move along */
648 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
649 sizeof (new_values->kvp[j]));
654 /* This should never happen... */
655 clib_warning ("BUG: linear rehash failed!");
656 BV (value_free) (h, new_values, new_log2_pages);
664 static_always_inline int BV (clib_bihash_add_del_inline_with_hash)
665 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, u64 hash, int is_add,
666 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
668 BVT (clib_bihash_bucket) * b, tmp_b;
669 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
672 u32 new_log2_pages, old_log2_pages;
673 u32 thread_index = os_get_thread_index ();
674 int mark_bucket_linear;
678 static const BVT (clib_bihash_bucket) mask = {
684 #if BIHASH_LAZY_INSTANTIATE
686 * Create the table (is_add=1,2), or flunk the request now (is_add=0)
687 * Use the alloc_lock to protect the instantiate operation.
689 if (PREDICT_FALSE (h->instantiated == 0))
694 BV (clib_bihash_alloc_lock) (h);
695 if (h->instantiated == 0)
696 BV (clib_bihash_instantiate) (h);
697 BV (clib_bihash_alloc_unlock) (h);
700 /* Debug image: make sure the table has been instantiated */
701 ASSERT (h->instantiated != 0);
704 b = BV (clib_bihash_get_bucket) (h, hash);
706 BV (clib_bihash_lock_bucket) (b);
708 /* First elt in the bucket? */
709 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0 && BV (clib_bihash_bucket_is_empty) (b))
713 BV (clib_bihash_unlock_bucket) (b);
717 BV (clib_bihash_alloc_lock) (h);
718 v = BV (value_alloc) (h, 0);
719 BV (clib_bihash_alloc_unlock) (h);
722 tmp_b.as_u64 = 0; /* clears bucket lock */
723 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
725 CLIB_MEMORY_STORE_BARRIER ();
727 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
728 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
733 /* WARNING: we're still looking at the live copy... */
734 limit = BIHASH_KVP_PER_PAGE;
735 v = BV (clib_bihash_get_value) (h, b->offset);
737 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
739 if (PREDICT_FALSE (b->linear_search))
740 limit <<= b->log2_pages;
742 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
748 * Because reader threads are looking at live data,
749 * we have to be extra careful. Readers do NOT hold the
750 * bucket lock. We need to be SLOWER than a search, past the
751 * point where readers CHECK the bucket lock.
755 * For obvious (in hindsight) reasons, see if we're supposed to
756 * replace an existing key, then look for an empty slot.
758 for (i = 0; i < limit; i++)
760 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
762 /* Add but do not overwrite? */
765 BV (clib_bihash_unlock_bucket) (b);
769 clib_memcpy_fast (&(v->kvp[i].value),
770 &add_v->value, sizeof (add_v->value));
771 BV (clib_bihash_unlock_bucket) (b);
772 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
777 * Look for an empty slot. If found, use it
779 for (i = 0; i < limit; i++)
781 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
784 * Copy the value first, so that if a reader manages
785 * to match the new key, the value will be right...
787 clib_memcpy_fast (&(v->kvp[i].value),
788 &add_v->value, sizeof (add_v->value));
789 CLIB_MEMORY_STORE_BARRIER (); /* Make sure the value has settled */
790 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
791 sizeof (add_v->key));
793 ASSERT (b->refcnt > 0);
794 BV (clib_bihash_unlock_bucket) (b);
795 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
799 /* look for stale data to overwrite */
802 for (i = 0; i < limit; i++)
804 if (is_stale_cb (&(v->kvp[i]), arg))
806 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
807 CLIB_MEMORY_STORE_BARRIER ();
808 BV (clib_bihash_unlock_bucket) (b);
809 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
814 /* Out of space in this bucket, split the bucket... */
816 else /* delete case */
818 for (i = 0; i < limit; i++)
820 /* Found the key? Kill it... */
821 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
823 clib_memset_u8 (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
824 /* Is the bucket empty? */
825 if (PREDICT_TRUE (b->refcnt > 1))
828 /* Switch back to the bucket-level kvp array? */
829 if (BIHASH_KVP_AT_BUCKET_LEVEL && b->refcnt == 1
830 && b->log2_pages > 0)
832 tmp_b.as_u64 = b->as_u64;
833 b->offset = BV (clib_bihash_get_offset)
834 (h, (void *) (b + 1));
835 b->linear_search = 0;
837 /* Clean up the bucket-level kvp array */
838 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
839 sizeof (BVT (clib_bihash_kv)));
840 CLIB_MEMORY_STORE_BARRIER ();
841 BV (clib_bihash_unlock_bucket) (b);
842 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
843 goto free_backing_store;
846 CLIB_MEMORY_STORE_BARRIER ();
847 BV (clib_bihash_unlock_bucket) (b);
848 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
851 else /* yes, free it */
853 /* Save old bucket value, need log2_pages to free it */
854 tmp_b.as_u64 = b->as_u64;
856 /* Kill and unlock the bucket */
860 /* And free the backing storage */
861 BV (clib_bihash_alloc_lock) (h);
862 /* Note: v currently points into the middle of the bucket */
863 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
864 BV (value_free) (h, v, tmp_b.log2_pages);
865 BV (clib_bihash_alloc_unlock) (h);
866 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
873 BV (clib_bihash_unlock_bucket) (b);
877 /* Move readers to a (locked) temp copy of the bucket */
878 BV (clib_bihash_alloc_lock) (h);
879 BV (make_working_copy) (h, b);
881 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
883 old_log2_pages = h->saved_bucket.log2_pages;
884 new_log2_pages = old_log2_pages + 1;
885 mark_bucket_linear = 0;
886 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
887 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
889 working_copy = h->working_copies[thread_index];
891 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
893 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
900 /* Try re-splitting. If that fails, fall back to linear search */
901 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
907 /* pinned collisions, use linear search */
909 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
911 mark_bucket_linear = 1;
912 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
914 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
915 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
919 /* Try to add the new entry */
921 new_hash = BV (clib_bihash_hash) (add_v);
922 limit = BIHASH_KVP_PER_PAGE;
923 if (mark_bucket_linear)
924 limit <<= new_log2_pages;
926 new_v += extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
928 for (i = 0; i < limit; i++)
930 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
932 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
937 /* Crap. Try again */
938 BV (value_free) (h, save_new_v, new_log2_pages);
940 * If we've already doubled the size of the bucket once,
941 * fall back to linear search now.
949 tmp_b.log2_pages = new_log2_pages;
950 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
951 tmp_b.linear_search = mark_bucket_linear;
952 #if BIHASH_KVP_AT_BUCKET_LEVEL
953 /* Compensate for permanent refcount bump at the bucket level */
954 if (new_log2_pages > 0)
956 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
957 ASSERT (tmp_b.refcnt > 0);
959 CLIB_MEMORY_STORE_BARRIER ();
960 b->as_u64 = tmp_b.as_u64;
962 #if BIHASH_KVP_AT_BUCKET_LEVEL
963 if (h->saved_bucket.log2_pages > 0)
967 /* free the old bucket, except at the bucket level if so configured */
968 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
969 BV (value_free) (h, v, h->saved_bucket.log2_pages);
971 #if BIHASH_KVP_AT_BUCKET_LEVEL
976 BV (clib_bihash_alloc_unlock) (h);
980 static_always_inline int BV (clib_bihash_add_del_inline)
981 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
982 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
984 u64 hash = BV (clib_bihash_hash) (add_v);
985 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add,
989 int BV (clib_bihash_add_del)
990 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
992 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
995 int BV (clib_bihash_add_or_overwrite_stale)
996 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
997 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
999 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
1002 int BV (clib_bihash_search)
1003 (BVT (clib_bihash) * h,
1004 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
1006 return BV (clib_bihash_search_inline_2) (h, search_key, valuep);
1009 u8 *BV (format_bihash) (u8 * s, va_list * args)
1011 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
1012 int verbose = va_arg (*args, int);
1013 BVT (clib_bihash_bucket) * b;
1014 BVT (clib_bihash_value) * v;
1016 u64 active_elements = 0;
1017 u64 active_buckets = 0;
1018 u64 linear_buckets = 0;
1020 s = format (s, "Hash table '%s'\n", h->name ? h->name : (u8 *) "(unnamed)");
1022 #if BIHASH_LAZY_INSTANTIATE
1023 if (PREDICT_FALSE (h->instantiated == 0))
1024 return format (s, " empty, uninitialized");
1027 for (i = 0; i < h->nbuckets; i++)
1029 b = BV (clib_bihash_get_bucket) (h, i);
1030 if (BV (clib_bihash_bucket_is_empty) (b))
1033 s = format (s, "[%d]: empty\n", i);
1039 if (b->linear_search)
1045 (s, "[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n", i,
1046 b->offset, (1 << b->log2_pages), b->refcnt, b->linear_search);
1049 v = BV (clib_bihash_get_value) (h, b->offset);
1050 for (j = 0; j < (1 << b->log2_pages); j++)
1052 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1054 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1057 s = format (s, " %d: empty\n",
1058 j * BIHASH_KVP_PER_PAGE + k);
1065 s = format (s, " %d: %U\n",
1066 j * BIHASH_KVP_PER_PAGE + k,
1067 h->kvp_fmt_fn, &(v->kvp[k]), verbose);
1071 s = format (s, " %d: %U\n",
1072 j * BIHASH_KVP_PER_PAGE + k,
1073 BV (format_bihash_kvp), &(v->kvp[k]));
1082 s = format (s, " %lld active elements %lld active buckets\n",
1083 active_elements, active_buckets);
1084 s = format (s, " %d free lists\n", vec_len (h->freelists));
1086 for (i = 0; i < vec_len (h->freelists); i++)
1089 BVT (clib_bihash_value) * free_elt;
1090 u64 free_elt_as_u64 = h->freelists[i];
1092 while (free_elt_as_u64)
1094 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
1096 free_elt_as_u64 = free_elt->next_free_as_u64;
1099 if (nfree || verbose)
1100 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
1103 s = format (s, " %lld linear search buckets\n", linear_buckets);
1104 if (BIHASH_USE_HEAP)
1106 BVT (clib_bihash_alloc_chunk) * c = h->chunks;
1107 uword bytes_left = 0, total_size = 0, n_chunks = 0;
1111 bytes_left += c->bytes_left;
1112 total_size += c->size;
1117 " heap: %u chunk(s) allocated\n"
1118 " bytes: used %U, scrap %U\n", n_chunks,
1119 format_memory_size, total_size,
1120 format_memory_size, bytes_left);
1124 u64 used_bytes = alloc_arena_next (h);
1126 " arena: base %llx, next %llx\n"
1127 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
1128 alloc_arena (h), alloc_arena_next (h),
1129 used_bytes, used_bytes >> 20,
1130 alloc_arena_size (h), alloc_arena_size (h) >> 20);
1135 void BV (clib_bihash_foreach_key_value_pair)
1136 (BVT (clib_bihash) * h,
1137 BV (clib_bihash_foreach_key_value_pair_cb) cb, void *arg)
1140 BVT (clib_bihash_bucket) * b;
1141 BVT (clib_bihash_value) * v;
1144 #if BIHASH_LAZY_INSTANTIATE
1145 if (PREDICT_FALSE (h->instantiated == 0))
1149 for (i = 0; i < h->nbuckets; i++)
1151 b = BV (clib_bihash_get_bucket) (h, i);
1152 if (BV (clib_bihash_bucket_is_empty) (b))
1155 v = BV (clib_bihash_get_value) (h, b->offset);
1156 for (j = 0; j < (1 << b->log2_pages); j++)
1158 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1160 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1163 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1166 * In case the callback deletes the last entry in the bucket...
1168 if (BV (clib_bihash_bucket_is_empty) (b))
1181 * fd.io coding-style-patch-verification: ON
1184 * eval: (c-set-style "gnu")