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 static void BV (clib_bihash_instantiate) (
BVT (clib_bihash) *
h)
40 alloc_arena_next (h) = 0;
41 alloc_arena_size (h) = h->memory_size;
43 bucket_size = h->nbuckets *
sizeof (h->buckets[0]);
49 void BV (clib_bihash_init2) (
BVT (clib_bihash_init2_args) *
a)
53 BVT (clib_bihash) * h = a->h;
55 a->nbuckets = 1 << (
max_log2 (a->nbuckets));
57 h->name = (
u8 *) a->name;
58 h->nbuckets = a->nbuckets;
59 h->log2_nbuckets =
max_log2 (a->nbuckets);
60 h->memory_size = a->memory_size;
62 h->fmt_fn = a->fmt_fn;
72 ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
75 if (a->dont_add_to_all_bihash_list == 0)
97 if (a->instantiate_immediately)
98 BV (clib_bihash_instantiate) (
h);
104 BVT (clib_bihash_init2_args) _a, *a = &_a;
106 memset (a, 0,
sizeof (*a));
110 a->nbuckets = nbuckets;
113 BV (clib_bihash_init2) (
a);
117 #if !defined (MFD_ALLOW_SEALING) 118 #define MFD_ALLOW_SEALING 0x0002U 121 void BV (clib_bihash_initiator_init_svm)
129 ASSERT (memory_size < (1ULL << 32));
137 if (ftruncate (fd, memory_size) < 0)
147 mmap_addr = mmap (0, memory_size,
148 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
150 if (mmap_addr == MAP_FAILED)
156 h->sh = (
void *) mmap_addr;
158 nbuckets = 1 << (
max_log2 (nbuckets));
160 h->name = (
u8 *) name;
161 h->sh->nbuckets = h->nbuckets = nbuckets;
162 h->log2_nbuckets =
max_log2 (nbuckets);
164 alloc_arena (h) = (
u64) (
uword) mmap_addr;
168 bucket_size = nbuckets *
sizeof (h->buckets[0]);
173 h->alloc_lock[0] = 0;
175 h->sh->alloc_lock_as_u64 =
180 BIHASH_FREELIST_LENGTH *
sizeof (
u64));
181 freelist_vh->
len = BIHASH_FREELIST_LENGTH;
183 h->sh->freelists_as_u64 =
185 h->freelists = (
void *) (freelist_vh->
vector_data);
191 void BV (clib_bihash_responder_init_svm)
192 (
BVT (clib_bihash) *
h,
char *
name,
int fd)
196 BVT (clib_bihash_shared_header) * sh;
199 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 );
200 if (mmap_addr == MAP_FAILED)
206 sh = (
BVT (clib_bihash_shared_header) *) mmap_addr;
208 memory_size = sh->alloc_arena_size;
210 munmap (mmap_addr, 4096);
213 mmap_addr = mmap (0, memory_size,
214 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
216 if (mmap_addr == MAP_FAILED)
224 h->sh = (
void *) mmap_addr;
225 alloc_arena (h) = (
u64) (
uword) mmap_addr;
228 h->name = (
u8 *) name;
230 h->nbuckets = h->sh->nbuckets;
231 h->log2_nbuckets =
max_log2 (h->nbuckets);
239 void BV (clib_bihash_set_kvp_format_fn) (
BVT (clib_bihash) *
h,
250 goto never_initialized;
255 #if BIHASH_32_64_SVM == 0 259 (void) close (h->memfd);
272 clib_warning (
"Couldn't find hash table %llx on clib_all_bihashes...",
282 ASSERT (h->alloc_lock[0]);
295 h->freelists[
log2_pages] = rv->next_free_as_u64;
312 ASSERT (h->alloc_lock[0]);
327 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
330 int log2_working_copy_length;
332 ASSERT (h->alloc_lock[0]);
334 if (thread_index >=
vec_len (h->working_copies))
345 working_copy = h->working_copies[thread_index];
346 log2_working_copy_length = h->working_copy_lengths[thread_index];
348 h->saved_bucket.as_u64 = b->as_u64;
350 if (b->log2_pages > log2_working_copy_length)
358 (
h,
sizeof (working_copy[0]) * (1 << b->log2_pages));
359 h->working_copy_lengths[thread_index] = b->log2_pages;
360 h->working_copies[thread_index] = working_copy;
362 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_working_copy_lost,
363 1ULL << b->log2_pages);
369 working_bucket.as_u64 = b->as_u64;
372 b->as_u64 = working_bucket.as_u64;
373 h->working_copies[thread_index] = working_copy;
379 (
BVT (clib_bihash) *
h,
384 int i, j, length_in_kvs;
386 ASSERT (h->alloc_lock[0]);
388 new_values = BV (value_alloc) (
h, new_log2_pages);
389 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
391 for (i = 0; i < length_in_kvs; i++)
396 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
400 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[
i]));
401 new_hash >>= h->log2_nbuckets;
402 new_hash &= (1 << new_log2_pages) - 1;
403 new_v = &new_values[new_hash];
409 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
412 sizeof (new_v->kvp[j]));
417 BV (value_free) (
h, new_values, new_log2_pages);
428 (
BVT (clib_bihash) *
h,
433 int i, j, new_length, old_length;
435 ASSERT (h->alloc_lock[0]);
437 new_values = BV (value_alloc) (
h, new_log2_pages);
438 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
439 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
443 for (i = 0; i < old_length; i++)
446 for (; j < new_length; j++)
449 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
453 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
457 sizeof (new_values->kvp[j]));
464 BV (value_free) (
h, new_values, new_log2_pages);
472 static inline int BV (clib_bihash_add_del_inline)
473 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add,
474 int (*is_stale_cb) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
477 BVT (clib_bihash_bucket) * b, tmp_b;
481 u32 new_log2_pages, old_log2_pages;
483 int mark_bucket_linear;
495 BV (clib_bihash_alloc_lock) (
h);
496 if (h->instantiated == 0)
497 BV (clib_bihash_instantiate) (
h);
498 BV (clib_bihash_alloc_unlock) (
h);
501 hash = BV (clib_bihash_hash) (add_v);
503 bucket_index = hash & (h->nbuckets - 1);
504 b = &h->buckets[bucket_index];
506 hash >>= h->log2_nbuckets;
508 BV (clib_bihash_lock_bucket) (b);
511 if (BV (clib_bihash_bucket_is_empty) (b))
515 BV (clib_bihash_unlock_bucket) (b);
519 BV (clib_bihash_alloc_lock) (
h);
520 v = BV (value_alloc) (
h, 0);
521 BV (clib_bihash_alloc_unlock) (
h);
529 b->as_u64 = tmp_b.as_u64;
530 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_alloc_add, 1);
539 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
540 if (b->linear_search)
541 limit <<= b->log2_pages;
556 for (i = 0; i < limit; i++)
558 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, add_v->key))
563 BV (clib_bihash_unlock_bucket) (b);
569 BV (clib_bihash_unlock_bucket) (b);
570 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_replace, 1);
577 for (i = 0; i < limit; i++)
579 if (BV (clib_bihash_is_free) (&(v->kvp[
i])))
586 &add_v->value, sizeof (add_v->value));
589 sizeof (add_v->key));
592 BV (clib_bihash_unlock_bucket) (b);
593 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_add, 1);
600 for (i = 0; i < limit; i++)
602 if (is_stale_cb (&(v->kvp[i]), arg))
606 BV (clib_bihash_unlock_bucket) (b);
607 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_replace, 1);
616 for (i = 0; i < limit; i++)
619 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, add_v->key))
621 clib_memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
626 BV (clib_bihash_unlock_bucket) (b);
627 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del, 1);
633 tmp_b.as_u64 = b->as_u64;
640 BV (clib_bihash_alloc_lock) (
h);
643 BV (value_free) (
h, v, tmp_b.log2_pages);
644 BV (clib_bihash_alloc_unlock) (
h);
645 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del_free,
652 BV (clib_bihash_unlock_bucket) (b);
657 BV (clib_bihash_alloc_lock) (
h);
662 old_log2_pages = h->saved_bucket.log2_pages;
663 new_log2_pages = old_log2_pages + 1;
664 mark_bucket_linear = 0;
665 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_split_add, 1);
666 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits, old_log2_pages);
668 working_copy = h->working_copies[thread_index];
670 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits, 1);
690 mark_bucket_linear = 1;
691 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_linear, 1);
693 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_resplit, 1);
694 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits,
700 new_hash = BV (clib_bihash_hash) (add_v);
702 if (mark_bucket_linear)
703 limit <<= new_log2_pages;
704 new_hash >>= h->log2_nbuckets;
705 new_hash &= (1 << new_log2_pages) - 1;
706 new_v += mark_bucket_linear ? 0 : new_hash;
708 for (i = 0; i < limit; i++)
710 if (BV (clib_bihash_is_free) (&(new_v->kvp[
i])))
718 BV (value_free) (
h, save_new_v, new_log2_pages);
729 tmp_b.log2_pages = new_log2_pages;
731 tmp_b.linear_search = mark_bucket_linear;
732 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
733 ASSERT (tmp_b.refcnt > 0);
736 b->as_u64 = tmp_b.as_u64;
739 BV (value_free) (
h, v, h->saved_bucket.log2_pages);
740 BV (clib_bihash_alloc_unlock) (
h);
745 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add)
747 return BV (clib_bihash_add_del_inline) (
h, add_v, is_add, 0, 0);
750 int BV (clib_bihash_add_or_overwrite_stale)
751 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
752 int (*stale_callback) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
754 return BV (clib_bihash_add_del_inline) (
h, add_v, 1, stale_callback, arg);
757 int BV (clib_bihash_search)
758 (
BVT (clib_bihash) *
h,
759 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
764 BVT (clib_bihash_bucket) * b;
772 hash = BV (clib_bihash_hash) (search_key);
774 bucket_index = hash & (h->nbuckets - 1);
775 b = &h->buckets[bucket_index];
777 if (BV (clib_bihash_bucket_is_empty) (b))
782 volatile BVT (clib_bihash_bucket) * bv = b;
787 hash >>= h->log2_nbuckets;
791 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
793 limit <<= b->log2_pages;
795 for (i = 0; i < limit; i++)
797 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, search_key->key))
806 u8 *BV (format_bihash) (
u8 * s, va_list * args)
808 BVT (clib_bihash) * h = va_arg (*args,
BVT (clib_bihash) *);
809 int verbose = va_arg (*args,
int);
810 BVT (clib_bihash_bucket) * b;
813 u64 active_elements = 0;
814 u64 active_buckets = 0;
815 u64 linear_buckets = 0;
818 s =
format (s,
"Hash table %s\n", h->name ? h->name : (
u8 *)
"(unnamed)");
821 return format (s,
"[empty, uninitialized]");
823 for (i = 0; i < h->nbuckets; i++)
826 if (BV (clib_bihash_bucket_is_empty) (b))
829 s =
format (s,
"[%d]: empty\n", i);
835 if (b->linear_search)
840 s =
format (s,
"[%d]: heap offset %lld, len %d, linear %d\n", i,
841 b->offset, (1 << b->log2_pages), b->linear_search);
845 for (j = 0; j < (1 << b->log2_pages); j++)
849 if (BV (clib_bihash_is_free) (&v->kvp[k]))
852 s =
format (s,
" %d: empty\n",
853 j * BIHASH_KVP_PER_PAGE + k);
860 s =
format (s,
" %d: %U\n",
861 j * BIHASH_KVP_PER_PAGE + k,
862 h->fmt_fn, &(v->kvp[k]), verbose);
866 s =
format (s,
" %d: %U\n",
867 j * BIHASH_KVP_PER_PAGE + k,
868 BV (format_bihash_kvp), &(v->kvp[k]));
877 s =
format (s,
" %lld active elements %lld active buckets\n",
878 active_elements, active_buckets);
881 for (i = 0; i <
vec_len (h->freelists); i++)
885 u64 free_elt_as_u64 = h->freelists[
i];
887 while (free_elt_as_u64)
891 free_elt_as_u64 = free_elt->next_free_as_u64;
894 if (nfree || verbose)
895 s =
format (s,
" [len %d] %u free elts\n", 1 << i, nfree);
898 s =
format (s,
" %lld linear search buckets\n", linear_buckets);
899 used_bytes = alloc_arena_next (h);
901 " arena: base %llx, next %llx\n" 902 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
903 alloc_arena (h), alloc_arena_next (h),
904 used_bytes, used_bytes >> 20,
905 alloc_arena_size (h), alloc_arena_size (h) >> 20);
910 (
BVT (clib_bihash) *
h,
void *callback,
void *arg)
913 BVT (clib_bihash_bucket) * b;
915 void (*fp) (
BVT (clib_bihash_kv) *,
void *) = callback;
920 for (i = 0; i < h->nbuckets; i++)
923 if (BV (clib_bihash_bucket_is_empty) (b))
927 for (j = 0; j < (1 << b->log2_pages); j++)
931 if (BV (clib_bihash_is_free) (&v->kvp[k]))
934 (*fp) (&v->kvp[k], arg);
938 if (BV (clib_bihash_bucket_is_empty) (b))
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
#define BIHASH_KVP_PER_PAGE
void clib_bihash_free(clib_bihash *h)
Destroy a bounded index extensible hash table.
#define clib_memcpy_fast(a, b, c)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static int memfd_create(const char *name, unsigned int flags)
void os_out_of_memory(void)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
for(i=1;i<=collision_buckets;i++)
#define MFD_ALLOW_SEALING
int clib_bihash_add_del(clib_bihash *h, clib_bihash_kv *add_v, int is_add)
Add or delete a (key,value) pair from a bi-hash table.
static uword clib_bihash_get_offset(clib_bihash *h, void *v)
Get clib mheap offset given a pointer.
static vnet_classify_entry_t * split_and_rehash_linear(vnet_classify_table_t *t, vnet_classify_entry_t *old_values, u32 old_log2_pages, u32 new_log2_pages)
void clib_bihash_init(clib_bihash *h, char *name, u32 nbuckets, uword memory_size)
initialize a bounded index extensible hash table
void clib_bihash_foreach_key_value_pair(clib_bihash *h, void *callback, void *arg)
Visit active (key,value) pairs in a bi-hash table.
static vnet_classify_entry_t * split_and_rehash(vnet_classify_table_t *t, vnet_classify_entry_t *old_values, u32 old_log2_pages, u32 new_log2_pages)
#define vec_free(V)
Free vector's memory (no header).
static void * clib_mem_set_heap(void *heap)
#define clib_warning(format, args...)
static void make_working_copy(vnet_classify_table_t *t, vnet_classify_bucket_t *b)
#define vec_delete(V, N, M)
Delete N elements starting at element M.
static void clib_mem_free(void *p)
template key/value backing page structure
static void clib_mem_vm_free(void *addr, uword size)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
static uword max_log2(uword x)
#define clib_unix_warning(format, args...)
static_always_inline uword os_get_thread_index(void)
static void * clib_mem_alloc_aligned(uword size, uword align)
#define CLIB_MEMORY_BARRIER()
static void * clib_bihash_get_value(clib_bihash *h, uword offset)
Get pointer to value page given its clib mheap offset.
void ** clib_all_bihashes
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
#define CLIB_CACHE_LINE_BYTES
void * clib_all_bihash_set_heap(void)
static void * clib_mem_vm_alloc(uword size)
static void * alloc_aligned(uword size, uword log2_align, void **ptr_to_free)