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 #ifndef BIHASH_USE_HEAP
27 #define BIHASH_USE_HEAP 1
30 static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
34 /* Round to an even number of cache lines */
35 nbytes = round_pow2 (nbytes, CLIB_CACHE_LINE_BYTES);
40 uword page_sz = sizeof (BVT (clib_bihash_value));
41 uword chunk_sz = round_pow2 (page_sz << BIIHASH_MIN_ALLOC_LOG2_PAGES,
42 CLIB_CACHE_LINE_BYTES);
44 BVT (clib_bihash_alloc_chunk) * chunk = h->chunks;
46 /* if there is enough space in the currenrt chunk */
47 if (chunk && chunk->bytes_left >= nbytes)
49 rv = chunk->next_alloc;
50 chunk->bytes_left -= nbytes;
51 chunk->next_alloc += nbytes;
55 /* requested allocation is bigger than chunk size */
56 if (nbytes >= chunk_sz)
58 oldheap = clib_mem_set_heap (h->heap);
59 chunk = clib_mem_alloc_aligned (nbytes + sizeof (*chunk),
60 CLIB_CACHE_LINE_BYTES);
61 clib_mem_set_heap (oldheap);
62 clib_memset_u8 (chunk, 0, sizeof (*chunk));
64 rv = (u8 *) (chunk + 1);
67 /* take 2nd place in the list */
68 chunk->next = h->chunks->next;
69 chunk->prev = h->chunks;
70 h->chunks->next = chunk;
72 chunk->next->prev = chunk;
80 oldheap = clib_mem_set_heap (h->heap);
81 chunk = clib_mem_alloc_aligned (chunk_sz + sizeof (*chunk),
82 CLIB_CACHE_LINE_BYTES);
83 clib_mem_set_heap (oldheap);
84 chunk->size = chunk_sz;
85 chunk->bytes_left = chunk_sz;
86 chunk->next_alloc = (u8 *) (chunk + 1);
87 chunk->next = h->chunks;
90 chunk->next->prev = chunk;
92 rv = chunk->next_alloc;
93 chunk->bytes_left -= nbytes;
94 chunk->next_alloc += nbytes;
98 rv = alloc_arena_next (h);
99 alloc_arena_next (h) += nbytes;
101 if (alloc_arena_next (h) > alloc_arena_size (h))
104 if (alloc_arena_next (h) > alloc_arena_mapped (h))
107 uword alloc = alloc_arena_next (h) - alloc_arena_mapped (h);
108 int mmap_flags = MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS;
109 int mmap_flags_huge = (mmap_flags | MAP_HUGETLB | MAP_LOCKED |
110 BIHASH_LOG2_HUGEPAGE_SIZE << MAP_HUGE_SHIFT);
112 /* new allocation is 25% of existing one */
113 if (alloc_arena_mapped (h) >> 2 > alloc)
114 alloc = alloc_arena_mapped (h) >> 2;
116 /* round allocation to page size */
117 alloc = round_pow2 (alloc, 1 << BIHASH_LOG2_HUGEPAGE_SIZE);
119 base = (void *) (uword) (alloc_arena (h) + alloc_arena_mapped (h));
121 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags_huge, -1, 0);
123 /* fallback - maybe we are still able to allocate normal pages */
124 if (rv == MAP_FAILED || mlock (base, alloc) != 0)
125 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
127 if (rv == MAP_FAILED)
130 alloc_arena_mapped (h) += alloc;
133 return (void *) (uword) (rv + alloc_arena (h));
136 static void BV (clib_bihash_instantiate) (BVT (clib_bihash) * h)
142 h->heap = clib_mem_get_heap ();
144 alloc_arena (h) = (uword) clib_mem_get_heap_base (h->heap);
148 alloc_arena (h) = clib_mem_vm_reserve (0, h->memory_size,
149 BIHASH_LOG2_HUGEPAGE_SIZE);
150 if (alloc_arena (h) == ~0)
152 alloc_arena_next (h) = 0;
153 alloc_arena_size (h) = h->memory_size;
154 alloc_arena_mapped (h) = 0;
157 bucket_size = h->nbuckets * sizeof (h->buckets[0]);
159 if (BIHASH_KVP_AT_BUCKET_LEVEL)
161 h->nbuckets * BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv));
163 h->buckets = BV (alloc_aligned) (h, bucket_size);
164 clib_memset_u8 (h->buckets, 0, bucket_size);
166 if (BIHASH_KVP_AT_BUCKET_LEVEL)
169 BVT (clib_bihash_bucket) * b;
173 for (i = 0; i < h->nbuckets; i++)
175 b->offset = BV (clib_bihash_get_offset) (h, (void *) (b + 1));
177 /* Mark all elements free */
178 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
179 sizeof (BVT (clib_bihash_kv)));
181 /* Compute next bucket start address */
182 b = (void *) (((uword) b) + sizeof (*b) +
183 (BIHASH_KVP_PER_PAGE *
184 sizeof (BVT (clib_bihash_kv))));
187 CLIB_MEMORY_STORE_BARRIER ();
191 void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a)
195 BVT (clib_bihash) * h = a->h;
197 a->nbuckets = 1 << (max_log2 (a->nbuckets));
199 h->name = (u8 *) a->name;
200 h->nbuckets = a->nbuckets;
201 h->log2_nbuckets = max_log2 (a->nbuckets);
202 h->memory_size = BIHASH_USE_HEAP ? 0 : a->memory_size;
204 h->dont_add_to_all_bihash_list = a->dont_add_to_all_bihash_list;
205 h->fmt_fn = BV (format_bihash);
206 h->kvp_fmt_fn = a->kvp_fmt_fn;
211 * Make sure the requested size is rational. The max table
212 * size without playing the alignment card is 64 Gbytes.
213 * If someone starts complaining that's not enough, we can shift
214 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
217 ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
219 /* Add this hash table to the list */
220 if (a->dont_add_to_all_bihash_list == 0)
222 for (i = 0; i < vec_len (clib_all_bihashes); i++)
223 if (clib_all_bihashes[i] == h)
225 oldheap = clib_all_bihash_set_heap ();
226 vec_add1 (clib_all_bihashes, (void *) h);
227 clib_mem_set_heap (oldheap);
232 clib_mem_free ((void *) h->alloc_lock);
235 * Set up the lock now, so we can use it to make the first add
238 h->alloc_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
239 CLIB_CACHE_LINE_BYTES);
240 h->alloc_lock[0] = 0;
242 #if BIHASH_LAZY_INSTANTIATE
243 if (a->instantiate_immediately)
245 BV (clib_bihash_instantiate) (h);
248 void BV (clib_bihash_init)
249 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
251 BVT (clib_bihash_init2_args) _a, *a = &_a;
253 memset (a, 0, sizeof (*a));
257 a->nbuckets = nbuckets;
258 a->memory_size = memory_size;
260 BV (clib_bihash_init2) (a);
264 #if !defined (MFD_ALLOW_SEALING)
265 #define MFD_ALLOW_SEALING 0x0002U
268 void BV (clib_bihash_initiator_init_svm)
269 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
273 vec_header_t *freelist_vh;
276 ASSERT (BIHASH_USE_HEAP == 0);
278 ASSERT (memory_size < (1ULL << 32));
279 /* Set up for memfd sharing */
280 if ((fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, name) == -1)
282 clib_unix_warning ("memfd_create");
286 if (ftruncate (fd, memory_size) < 0)
288 clib_unix_warning ("ftruncate");
292 /* Not mission-critical, complain and continue */
293 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
294 clib_unix_warning ("fcntl (F_ADD_SEALS)");
296 mmap_addr = mmap (0, memory_size,
297 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
299 if (mmap_addr == MAP_FAILED)
301 clib_unix_warning ("mmap failed");
305 h->sh = (void *) mmap_addr;
307 nbuckets = 1 << (max_log2 (nbuckets));
309 h->name = (u8 *) name;
310 h->sh->nbuckets = h->nbuckets = nbuckets;
311 h->log2_nbuckets = max_log2 (nbuckets);
313 alloc_arena (h) = (u64) (uword) mmap_addr;
314 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
315 alloc_arena_size (h) = memory_size;
317 bucket_size = nbuckets * sizeof (h->buckets[0]);
318 h->buckets = BV (alloc_aligned) (h, bucket_size);
319 clib_memset_u8 (h->buckets, 0, bucket_size);
320 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
322 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
323 h->alloc_lock[0] = 0;
325 h->sh->alloc_lock_as_u64 =
326 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
328 BV (alloc_aligned) (h,
329 sizeof (vec_header_t) +
330 BIHASH_FREELIST_LENGTH * sizeof (u64));
331 freelist_vh->len = BIHASH_FREELIST_LENGTH;
332 h->sh->freelists_as_u64 =
333 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
334 h->freelists = (void *) (freelist_vh->vector_data);
336 h->fmt_fn = BV (format_bihash);
337 h->kvp_fmt_fn = NULL;
341 void BV (clib_bihash_responder_init_svm)
342 (BVT (clib_bihash) * h, char *name, int fd)
346 BVT (clib_bihash_shared_header) * sh;
348 ASSERT (BIHASH_USE_HEAP == 0);
350 /* Trial mapping, to learn the segment size */
351 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
352 if (mmap_addr == MAP_FAILED)
354 clib_unix_warning ("trial mmap failed");
358 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
360 memory_size = sh->alloc_arena_size;
362 munmap (mmap_addr, 4096);
364 /* Actual mapping, at the required size */
365 mmap_addr = mmap (0, memory_size,
366 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
368 if (mmap_addr == MAP_FAILED)
370 clib_unix_warning ("mmap failed");
376 h->sh = (void *) mmap_addr;
377 alloc_arena (h) = (u64) (uword) mmap_addr;
380 h->name = (u8 *) name;
381 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
382 h->nbuckets = h->sh->nbuckets;
383 h->log2_nbuckets = max_log2 (h->nbuckets);
385 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
386 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
387 h->fmt_fn = BV (format_bihash);
388 h->kvp_fmt_fn = NULL;
390 #endif /* BIHASH_32_64_SVM */
392 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
393 format_function_t * kvp_fmt_fn)
395 h->kvp_fmt_fn = kvp_fmt_fn;
398 int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h)
400 return (h->instantiated != 0);
403 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
407 if (PREDICT_FALSE (h->instantiated == 0))
408 goto never_initialized;
414 BVT (clib_bihash_alloc_chunk) * next, *chunk;
415 void *oldheap = clib_mem_set_heap (h->heap);
421 clib_mem_free (chunk);
424 clib_mem_set_heap (oldheap);
427 vec_free (h->working_copies);
428 vec_free (h->working_copy_lengths);
429 clib_mem_free ((void *) h->alloc_lock);
430 #if BIHASH_32_64_SVM == 0
431 vec_free (h->freelists);
434 (void) close (h->memfd);
436 if (BIHASH_USE_HEAP == 0)
437 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)),
438 alloc_arena_size (h));
440 if (h->dont_add_to_all_bihash_list)
442 clib_memset_u8 (h, 0, sizeof (*h));
445 clib_memset_u8 (h, 0, sizeof (*h));
446 for (i = 0; i < vec_len (clib_all_bihashes); i++)
448 if ((void *) h == clib_all_bihashes[i])
450 vec_delete (clib_all_bihashes, 1, i);
454 clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...",
459 BVT (clib_bihash_value) *
460 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
462 BVT (clib_bihash_value) * rv = 0;
464 ASSERT (h->alloc_lock[0]);
467 ASSERT (log2_pages < vec_len (h->freelists));
470 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
472 vec_validate_init_empty (h->freelists, log2_pages, 0);
473 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
476 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
477 h->freelists[log2_pages] = rv->next_free_as_u64;
482 * Latest gcc complains that the length arg is zero
483 * if we replace (1<<log2_pages) with vec_len(rv).
486 clib_memset_u8 (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
491 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
494 ASSERT (h->alloc_lock[0]);
496 ASSERT (vec_len (h->freelists) > log2_pages);
498 if (BIHASH_USE_HEAP && log2_pages >= BIIHASH_MIN_ALLOC_LOG2_PAGES)
500 /* allocations bigger or equal to chunk size always contain single
501 * alloc and they can be given back to heap */
503 BVT (clib_bihash_alloc_chunk) * c;
504 c = (BVT (clib_bihash_alloc_chunk) *) v - 1;
507 c->prev->next = c->next;
512 c->next->prev = c->prev;
514 oldheap = clib_mem_set_heap (h->heap);
516 clib_mem_set_heap (oldheap);
521 clib_memset_u8 (v, 0xFE, sizeof (*v) * (1 << log2_pages));
523 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
524 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
528 BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
530 BVT (clib_bihash_value) * v;
531 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
532 BVT (clib_bihash_value) * working_copy;
533 u32 thread_index = os_get_thread_index ();
534 int log2_working_copy_length;
536 ASSERT (h->alloc_lock[0]);
538 if (thread_index >= vec_len (h->working_copies))
540 vec_validate (h->working_copies, thread_index);
541 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
545 * working_copies are per-cpu so that near-simultaneous
546 * updates from multiple threads will not result in sporadic, spurious
549 working_copy = h->working_copies[thread_index];
550 log2_working_copy_length = h->working_copy_lengths[thread_index];
552 h->saved_bucket.as_u64 = b->as_u64;
554 if (b->log2_pages > log2_working_copy_length)
557 * It's not worth the bookkeeping to free working copies
559 * clib_mem_free (working_copy);
561 working_copy = BV (alloc_aligned)
562 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
563 h->working_copy_lengths[thread_index] = b->log2_pages;
564 h->working_copies[thread_index] = working_copy;
566 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
567 1ULL << b->log2_pages);
570 v = BV (clib_bihash_get_value) (h, b->offset);
572 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
573 working_bucket.as_u64 = b->as_u64;
574 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
575 CLIB_MEMORY_STORE_BARRIER ();
576 b->as_u64 = working_bucket.as_u64;
577 h->working_copies[thread_index] = working_copy;
581 BVT (clib_bihash_value) *
582 BV (split_and_rehash)
583 (BVT (clib_bihash) * h,
584 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
587 BVT (clib_bihash_value) * new_values, *new_v;
588 int i, j, length_in_kvs;
590 ASSERT (h->alloc_lock[0]);
592 new_values = BV (value_alloc) (h, new_log2_pages);
593 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
595 for (i = 0; i < length_in_kvs; i++)
599 /* Entry not in use? Forget it */
600 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
603 /* rehash the item onto its new home-page */
604 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
605 new_hash = extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
606 new_v = &new_values[new_hash];
608 /* Across the new home-page */
609 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
612 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
614 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
615 sizeof (new_v->kvp[j]));
619 /* Crap. Tell caller to try again */
620 BV (value_free) (h, new_values, new_log2_pages);
629 BVT (clib_bihash_value) *
630 BV (split_and_rehash_linear)
631 (BVT (clib_bihash) * h,
632 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
635 BVT (clib_bihash_value) * new_values;
636 int i, j, new_length, old_length;
638 ASSERT (h->alloc_lock[0]);
640 new_values = BV (value_alloc) (h, new_log2_pages);
641 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
642 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
645 /* Across the old value array */
646 for (i = 0; i < old_length; i++)
648 /* Find a free slot in the new linear scan bucket */
649 for (; j < new_length; j++)
651 /* Old value not in use? Forget it. */
652 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
655 /* New value should never be in use */
656 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
658 /* Copy the old value and move along */
659 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
660 sizeof (new_values->kvp[j]));
665 /* This should never happen... */
666 clib_warning ("BUG: linear rehash failed!");
667 BV (value_free) (h, new_values, new_log2_pages);
675 static_always_inline int BV (clib_bihash_add_del_inline_with_hash) (
676 BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, u64 hash, int is_add,
677 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *is_stale_arg,
678 void (*overwrite_cb) (BVT (clib_bihash_kv) *, void *), void *overwrite_arg)
680 BVT (clib_bihash_bucket) * b, tmp_b;
681 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
684 u32 new_log2_pages, old_log2_pages;
685 u32 thread_index = os_get_thread_index ();
686 int mark_bucket_linear;
690 static const BVT (clib_bihash_bucket) mask = {
696 #if BIHASH_LAZY_INSTANTIATE
698 * Create the table (is_add=1,2), or flunk the request now (is_add=0)
699 * Use the alloc_lock to protect the instantiate operation.
701 if (PREDICT_FALSE (h->instantiated == 0))
706 BV (clib_bihash_alloc_lock) (h);
707 if (h->instantiated == 0)
708 BV (clib_bihash_instantiate) (h);
709 BV (clib_bihash_alloc_unlock) (h);
712 /* Debug image: make sure the table has been instantiated */
713 ASSERT (h->instantiated != 0);
716 b = BV (clib_bihash_get_bucket) (h, hash);
718 BV (clib_bihash_lock_bucket) (b);
720 /* First elt in the bucket? */
721 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0 && BV (clib_bihash_bucket_is_empty) (b))
725 BV (clib_bihash_unlock_bucket) (b);
729 BV (clib_bihash_alloc_lock) (h);
730 v = BV (value_alloc) (h, 0);
731 BV (clib_bihash_alloc_unlock) (h);
734 tmp_b.as_u64 = 0; /* clears bucket lock */
735 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
737 CLIB_MEMORY_STORE_BARRIER ();
739 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
740 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
745 /* WARNING: we're still looking at the live copy... */
746 limit = BIHASH_KVP_PER_PAGE;
747 v = BV (clib_bihash_get_value) (h, b->offset);
749 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
751 if (PREDICT_FALSE (b->linear_search))
752 limit <<= b->log2_pages;
754 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
760 * Because reader threads are looking at live data,
761 * we have to be extra careful. Readers do NOT hold the
762 * bucket lock. We need to be SLOWER than a search, past the
763 * point where readers CHECK the bucket lock.
767 * For obvious (in hindsight) reasons, see if we're supposed to
768 * replace an existing key, then look for an empty slot.
770 for (i = 0; i < limit; i++)
772 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
774 /* Add but do not overwrite? */
777 BV (clib_bihash_unlock_bucket) (b);
781 overwrite_cb (&(v->kvp[i]), overwrite_arg);
782 clib_memcpy_fast (&(v->kvp[i].value),
783 &add_v->value, sizeof (add_v->value));
784 BV (clib_bihash_unlock_bucket) (b);
785 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
790 * Look for an empty slot. If found, use it
792 for (i = 0; i < limit; i++)
794 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
797 * Copy the value first, so that if a reader manages
798 * to match the new key, the value will be right...
800 clib_memcpy_fast (&(v->kvp[i].value),
801 &add_v->value, sizeof (add_v->value));
802 CLIB_MEMORY_STORE_BARRIER (); /* Make sure the value has settled */
803 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
804 sizeof (add_v->key));
806 ASSERT (b->refcnt > 0);
807 BV (clib_bihash_unlock_bucket) (b);
808 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
812 /* look for stale data to overwrite */
815 for (i = 0; i < limit; i++)
817 if (is_stale_cb (&(v->kvp[i]), is_stale_arg))
819 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
820 CLIB_MEMORY_STORE_BARRIER ();
821 BV (clib_bihash_unlock_bucket) (b);
822 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
827 /* Out of space in this bucket, split the bucket... */
829 else /* delete case */
831 for (i = 0; i < limit; i++)
833 /* Found the key? Kill it... */
834 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
836 clib_memset_u8 (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
837 /* Is the bucket empty? */
838 if (PREDICT_TRUE (b->refcnt > 1))
841 /* Switch back to the bucket-level kvp array? */
842 if (BIHASH_KVP_AT_BUCKET_LEVEL && b->refcnt == 1
843 && b->log2_pages > 0)
845 tmp_b.as_u64 = b->as_u64;
846 b->offset = BV (clib_bihash_get_offset)
847 (h, (void *) (b + 1));
848 b->linear_search = 0;
850 /* Clean up the bucket-level kvp array */
851 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
852 sizeof (BVT (clib_bihash_kv)));
853 CLIB_MEMORY_STORE_BARRIER ();
854 BV (clib_bihash_unlock_bucket) (b);
855 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
856 goto free_backing_store;
859 CLIB_MEMORY_STORE_BARRIER ();
860 BV (clib_bihash_unlock_bucket) (b);
861 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
864 else /* yes, free it */
866 /* Save old bucket value, need log2_pages to free it */
867 tmp_b.as_u64 = b->as_u64;
869 /* Kill and unlock the bucket */
873 /* And free the backing storage */
874 BV (clib_bihash_alloc_lock) (h);
875 /* Note: v currently points into the middle of the bucket */
876 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
877 BV (value_free) (h, v, tmp_b.log2_pages);
878 BV (clib_bihash_alloc_unlock) (h);
879 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
886 BV (clib_bihash_unlock_bucket) (b);
890 /* Move readers to a (locked) temp copy of the bucket */
891 BV (clib_bihash_alloc_lock) (h);
892 BV (make_working_copy) (h, b);
894 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
896 old_log2_pages = h->saved_bucket.log2_pages;
897 new_log2_pages = old_log2_pages + 1;
898 mark_bucket_linear = 0;
899 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
900 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
902 working_copy = h->working_copies[thread_index];
904 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
906 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
913 /* Try re-splitting. If that fails, fall back to linear search */
914 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
920 /* pinned collisions, use linear search */
922 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
924 mark_bucket_linear = 1;
925 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
927 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
928 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
932 /* Try to add the new entry */
934 new_hash = BV (clib_bihash_hash) (add_v);
935 limit = BIHASH_KVP_PER_PAGE;
936 if (mark_bucket_linear)
937 limit <<= new_log2_pages;
939 new_v += extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
941 for (i = 0; i < limit; i++)
943 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
945 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
950 /* Crap. Try again */
951 BV (value_free) (h, save_new_v, new_log2_pages);
953 * If we've already doubled the size of the bucket once,
954 * fall back to linear search now.
962 tmp_b.log2_pages = new_log2_pages;
963 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
964 tmp_b.linear_search = mark_bucket_linear;
965 #if BIHASH_KVP_AT_BUCKET_LEVEL
966 /* Compensate for permanent refcount bump at the bucket level */
967 if (new_log2_pages > 0)
969 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
970 ASSERT (tmp_b.refcnt > 0);
972 CLIB_MEMORY_STORE_BARRIER ();
973 b->as_u64 = tmp_b.as_u64;
975 #if BIHASH_KVP_AT_BUCKET_LEVEL
976 if (h->saved_bucket.log2_pages > 0)
980 /* free the old bucket, except at the bucket level if so configured */
981 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
982 BV (value_free) (h, v, h->saved_bucket.log2_pages);
984 #if BIHASH_KVP_AT_BUCKET_LEVEL
989 BV (clib_bihash_alloc_unlock) (h);
993 static_always_inline int BV (clib_bihash_add_del_inline)
994 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
995 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
997 u64 hash = BV (clib_bihash_hash) (add_v);
998 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add,
999 is_stale_cb, arg, 0, 0);
1002 int BV (clib_bihash_add_del_with_hash) (BVT (clib_bihash) * h,
1003 BVT (clib_bihash_kv) * add_v, u64 hash,
1006 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add, 0,
1010 int BV (clib_bihash_add_del)
1011 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
1013 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
1016 int BV (clib_bihash_add_or_overwrite_stale)
1017 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
1018 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
1020 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
1023 int BV (clib_bihash_add_with_overwrite_cb) (
1024 BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
1025 void (overwrite_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
1027 u64 hash = BV (clib_bihash_hash) (add_v);
1028 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, 1, 0, 0,
1032 int BV (clib_bihash_search)
1033 (BVT (clib_bihash) * h,
1034 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
1036 return BV (clib_bihash_search_inline_2) (h, search_key, valuep);
1039 u8 *BV (format_bihash) (u8 * s, va_list * args)
1041 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
1042 int verbose = va_arg (*args, int);
1043 BVT (clib_bihash_bucket) * b;
1044 BVT (clib_bihash_value) * v;
1046 u64 active_elements = 0;
1047 u64 active_buckets = 0;
1048 u64 linear_buckets = 0;
1050 s = format (s, "Hash table '%s'\n", h->name ? h->name : (u8 *) "(unnamed)");
1052 #if BIHASH_LAZY_INSTANTIATE
1053 if (PREDICT_FALSE (h->instantiated == 0))
1054 return format (s, " empty, uninitialized");
1057 for (i = 0; i < h->nbuckets; i++)
1059 b = BV (clib_bihash_get_bucket) (h, i);
1060 if (BV (clib_bihash_bucket_is_empty) (b))
1063 s = format (s, "[%d]: empty\n", i);
1069 if (b->linear_search)
1075 (s, "[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n", i,
1076 b->offset, (1 << b->log2_pages), b->refcnt, b->linear_search);
1079 v = BV (clib_bihash_get_value) (h, b->offset);
1080 for (j = 0; j < (1 << b->log2_pages); j++)
1082 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1084 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1087 s = format (s, " %d: empty\n",
1088 j * BIHASH_KVP_PER_PAGE + k);
1095 s = format (s, " %d: %U\n",
1096 j * BIHASH_KVP_PER_PAGE + k,
1097 h->kvp_fmt_fn, &(v->kvp[k]), verbose);
1101 s = format (s, " %d: %U\n",
1102 j * BIHASH_KVP_PER_PAGE + k,
1103 BV (format_bihash_kvp), &(v->kvp[k]));
1112 s = format (s, " %lld active elements %lld active buckets\n",
1113 active_elements, active_buckets);
1114 s = format (s, " %d free lists\n", vec_len (h->freelists));
1116 for (i = 0; i < vec_len (h->freelists); i++)
1119 BVT (clib_bihash_value) * free_elt;
1120 u64 free_elt_as_u64 = h->freelists[i];
1122 while (free_elt_as_u64)
1124 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
1126 free_elt_as_u64 = free_elt->next_free_as_u64;
1129 if (nfree || verbose)
1130 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
1133 s = format (s, " %lld linear search buckets\n", linear_buckets);
1134 if (BIHASH_USE_HEAP)
1136 BVT (clib_bihash_alloc_chunk) * c = h->chunks;
1137 uword bytes_left = 0, total_size = 0, n_chunks = 0;
1141 bytes_left += c->bytes_left;
1142 total_size += c->size;
1147 " heap: %u chunk(s) allocated\n"
1148 " bytes: used %U, scrap %U\n", n_chunks,
1149 format_memory_size, total_size,
1150 format_memory_size, bytes_left);
1154 u64 used_bytes = alloc_arena_next (h);
1156 " arena: base %llx, next %llx\n"
1157 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
1158 alloc_arena (h), alloc_arena_next (h),
1159 used_bytes, used_bytes >> 20,
1160 alloc_arena_size (h), alloc_arena_size (h) >> 20);
1165 void BV (clib_bihash_foreach_key_value_pair)
1166 (BVT (clib_bihash) * h,
1167 BV (clib_bihash_foreach_key_value_pair_cb) cb, void *arg)
1170 BVT (clib_bihash_bucket) * b;
1171 BVT (clib_bihash_value) * v;
1174 #if BIHASH_LAZY_INSTANTIATE
1175 if (PREDICT_FALSE (h->instantiated == 0))
1179 for (i = 0; i < h->nbuckets; i++)
1181 b = BV (clib_bihash_get_bucket) (h, i);
1182 if (BV (clib_bihash_bucket_is_empty) (b))
1185 v = BV (clib_bihash_get_value) (h, b->offset);
1186 for (j = 0; j < (1 << b->log2_pages); j++)
1188 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1190 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1193 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1196 * In case the callback deletes the last entry in the bucket...
1198 if (BV (clib_bihash_bucket_is_empty) (b))
1211 * fd.io coding-style-patch-verification: ON
1214 * eval: (c-set-style "gnu")