18 #ifndef MAP_HUGE_SHIFT 19 #define MAP_HUGE_SHIFT 26 30 rv = alloc_arena_next (h);
31 alloc_arena_next (h) += nbytes;
33 if (alloc_arena_next (h) > alloc_arena_size (h))
36 if (alloc_arena_next (h) > alloc_arena_mapped (h))
39 uword alloc = alloc_arena_next (h) - alloc_arena_mapped (h);
40 int mmap_flags = MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS;
41 int mmap_flags_huge = (mmap_flags | MAP_HUGETLB | MAP_LOCKED |
42 BIHASH_LOG2_HUGEPAGE_SIZE << MAP_HUGE_SHIFT);
45 if (alloc_arena_mapped (h) >> 2 > alloc)
46 alloc = alloc_arena_mapped (h) >> 2;
49 alloc =
round_pow2 (alloc, 1 << BIHASH_LOG2_HUGEPAGE_SIZE);
51 base = (
void *) (
uword) (alloc_arena (h) + alloc_arena_mapped (h));
53 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags_huge, -1, 0);
56 if (rv == MAP_FAILED || mlock (base, alloc) != 0)
57 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
62 alloc_arena_mapped (h) += alloc;
65 return (
void *) (
uword) (rv + alloc_arena (h));
68 static void BV (clib_bihash_instantiate) (
BVT (clib_bihash) *
h)
73 BIHASH_LOG2_HUGEPAGE_SIZE);
74 if (alloc_arena (h) == ~0)
76 alloc_arena_next (h) = 0;
77 alloc_arena_size (h) = h->memory_size;
78 alloc_arena_mapped (h) = 0;
80 bucket_size = h->nbuckets *
sizeof (h->buckets[0]);
84 h->nbuckets * BIHASH_KVP_PER_PAGE *
sizeof (
BVT (clib_bihash_kv));
91 BVT (clib_bihash_bucket) * b;
95 for (i = 0; i < h->nbuckets; i++)
101 BIHASH_KVP_PER_PAGE *
sizeof (
BVT (clib_bihash_kv)));
104 b = (
void *) (((
uword) b) +
sizeof (*b) +
105 (BIHASH_KVP_PER_PAGE *
106 sizeof (
BVT (clib_bihash_kv))));
113 void BV (clib_bihash_init2) (
BVT (clib_bihash_init2_args) *
a)
117 BVT (clib_bihash) * h = a->h;
119 a->nbuckets = 1 << (
max_log2 (a->nbuckets));
121 h->name = (
u8 *) a->name;
122 h->nbuckets = a->nbuckets;
123 h->log2_nbuckets =
max_log2 (a->nbuckets);
124 h->memory_size = a->memory_size;
126 h->fmt_fn = a->fmt_fn;
136 ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
139 if (a->dont_add_to_all_bihash_list == 0)
159 h->alloc_lock[0] = 0;
161 #if BIHASH_LAZY_INSTANTIATE 162 if (a->instantiate_immediately)
164 BV (clib_bihash_instantiate) (
h);
170 BVT (clib_bihash_init2_args) _a, *a = &_a;
172 memset (a, 0,
sizeof (*a));
176 a->nbuckets = nbuckets;
179 BV (clib_bihash_init2) (
a);
183 #if !defined (MFD_ALLOW_SEALING) 184 #define MFD_ALLOW_SEALING 0x0002U 187 void BV (clib_bihash_master_init_svm)
195 ASSERT (memory_size < (1ULL << 32));
203 if (ftruncate (fd, memory_size) < 0)
213 mmap_addr = mmap (0, memory_size,
214 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
216 if (mmap_addr == MAP_FAILED)
222 h->sh = (
void *) mmap_addr;
224 nbuckets = 1 << (
max_log2 (nbuckets));
226 h->name = (
u8 *) name;
227 h->sh->nbuckets = h->nbuckets = nbuckets;
228 h->log2_nbuckets =
max_log2 (nbuckets);
230 alloc_arena (h) = (
u64) (
uword) mmap_addr;
234 bucket_size = nbuckets *
sizeof (h->buckets[0]);
239 h->alloc_lock[0] = 0;
241 h->sh->alloc_lock_as_u64 =
246 BIHASH_FREELIST_LENGTH *
sizeof (
u64));
247 freelist_vh->
len = BIHASH_FREELIST_LENGTH;
248 h->sh->freelists_as_u64 =
250 h->freelists = (
void *) (freelist_vh->
vector_data);
256 void BV (clib_bihash_slave_init_svm)
257 (
BVT (clib_bihash) *
h,
char *
name,
int fd)
261 BVT (clib_bihash_shared_header) * sh;
264 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 );
265 if (mmap_addr == MAP_FAILED)
271 sh = (
BVT (clib_bihash_shared_header) *) mmap_addr;
273 memory_size = sh->alloc_arena_size;
275 munmap (mmap_addr, 4096);
278 mmap_addr = mmap (0, memory_size,
279 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
281 if (mmap_addr == MAP_FAILED)
289 h->sh = (
void *) mmap_addr;
290 alloc_arena (h) = (
u64) (
uword) mmap_addr;
293 h->name = (
u8 *) name;
295 h->nbuckets = h->sh->nbuckets;
296 h->log2_nbuckets =
max_log2 (h->nbuckets);
304 void BV (clib_bihash_set_kvp_format_fn) (
BVT (clib_bihash) *
h,
315 goto never_initialized;
320 #if BIHASH_32_64_SVM == 0 324 (void) close (h->memfd);
337 clib_warning (
"Couldn't find hash table %llx on clib_all_bihashes...",
347 ASSERT (h->alloc_lock[0]);
360 h->freelists[
log2_pages] = rv->next_free_as_u64;
377 ASSERT (h->alloc_lock[0]);
392 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
395 int log2_working_copy_length;
397 ASSERT (h->alloc_lock[0]);
399 if (thread_index >=
vec_len (h->working_copies))
410 working_copy = h->working_copies[thread_index];
411 log2_working_copy_length = h->working_copy_lengths[thread_index];
413 h->saved_bucket.as_u64 = b->as_u64;
415 if (b->log2_pages > log2_working_copy_length)
423 (
h,
sizeof (working_copy[0]) * (1 << b->log2_pages));
424 h->working_copy_lengths[thread_index] = b->log2_pages;
425 h->working_copies[thread_index] = working_copy;
427 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_working_copy_lost,
428 1ULL << b->log2_pages);
434 working_bucket.as_u64 = b->as_u64;
437 b->as_u64 = working_bucket.as_u64;
438 h->working_copies[thread_index] = working_copy;
444 (
BVT (clib_bihash) *
h,
449 int i, j, length_in_kvs;
451 ASSERT (h->alloc_lock[0]);
453 new_values = BV (value_alloc) (
h, new_log2_pages);
454 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
456 for (i = 0; i < length_in_kvs; i++)
461 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
465 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[
i]));
466 new_hash =
extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
467 new_v = &new_values[new_hash];
473 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
476 sizeof (new_v->kvp[j]));
481 BV (value_free) (
h, new_values, new_log2_pages);
492 (
BVT (clib_bihash) *
h,
497 int i, j, new_length, old_length;
499 ASSERT (h->alloc_lock[0]);
501 new_values = BV (value_alloc) (
h, new_log2_pages);
502 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
503 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
507 for (i = 0; i < old_length; i++)
510 for (; j < new_length; j++)
513 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
517 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
521 sizeof (new_values->kvp[j]));
528 BV (value_free) (
h, new_values, new_log2_pages);
537 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
u64 hash,
int is_add,
538 int (*is_stale_cb) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
540 BVT (clib_bihash_bucket) * b, tmp_b;
544 u32 new_log2_pages, old_log2_pages;
546 int mark_bucket_linear;
550 static const BVT (clib_bihash_bucket) mask = {
556 #if BIHASH_LAZY_INSTANTIATE 566 BV (clib_bihash_alloc_lock) (
h);
567 if (h->instantiated == 0)
568 BV (clib_bihash_instantiate) (
h);
569 BV (clib_bihash_alloc_unlock) (
h);
573 ASSERT (h->instantiated != 0);
576 b = BV (clib_bihash_get_bucket) (
h, hash);
578 BV (clib_bihash_lock_bucket) (b);
585 BV (clib_bihash_unlock_bucket) (b);
589 BV (clib_bihash_alloc_lock) (
h);
590 v = BV (value_alloc) (
h, 0);
591 BV (clib_bihash_alloc_unlock) (
h);
599 b->as_u64 = tmp_b.as_u64;
600 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_alloc_add, 1);
612 limit <<= b->log2_pages;
614 v +=
extract_bits (hash, h->log2_nbuckets, b->log2_pages);
630 for (i = 0; i < limit; i++)
632 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, add_v->key))
637 BV (clib_bihash_unlock_bucket) (b);
642 &add_v->value, sizeof (add_v->value));
643 BV (clib_bihash_unlock_bucket) (b);
644 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_replace, 1);
651 for (i = 0; i < limit; i++)
653 if (BV (clib_bihash_is_free) (&(v->kvp[
i])))
660 &add_v->value, sizeof (add_v->value));
663 sizeof (add_v->key));
666 BV (clib_bihash_unlock_bucket) (b);
667 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_add, 1);
674 for (i = 0; i < limit; i++)
676 if (is_stale_cb (&(v->kvp[i]), arg))
680 BV (clib_bihash_unlock_bucket) (b);
681 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_replace, 1);
690 for (i = 0; i < limit; i++)
693 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, add_v->key))
702 && b->log2_pages > 0)
704 tmp_b.as_u64 = b->as_u64;
706 (
h, (
void *) (b + 1));
707 b->linear_search = 0;
711 sizeof (
BVT (clib_bihash_kv)));
713 BV (clib_bihash_unlock_bucket) (b);
714 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del, 1);
715 goto free_backing_store;
719 BV (clib_bihash_unlock_bucket) (b);
720 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del, 1);
726 tmp_b.as_u64 = b->as_u64;
733 BV (clib_bihash_alloc_lock) (
h);
736 BV (value_free) (
h, v, tmp_b.log2_pages);
737 BV (clib_bihash_alloc_unlock) (
h);
738 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del_free,
745 BV (clib_bihash_unlock_bucket) (b);
750 BV (clib_bihash_alloc_lock) (
h);
755 old_log2_pages = h->saved_bucket.log2_pages;
756 new_log2_pages = old_log2_pages + 1;
757 mark_bucket_linear = 0;
758 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_split_add, 1);
759 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits, old_log2_pages);
761 working_copy = h->working_copies[thread_index];
763 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits, 1);
783 mark_bucket_linear = 1;
784 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_linear, 1);
786 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_resplit, 1);
787 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits,
793 new_hash = BV (clib_bihash_hash) (add_v);
795 if (mark_bucket_linear)
796 limit <<= new_log2_pages;
798 new_v +=
extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
800 for (i = 0; i < limit; i++)
802 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
810 BV (value_free) (
h, save_new_v, new_log2_pages);
821 tmp_b.log2_pages = new_log2_pages;
823 tmp_b.linear_search = mark_bucket_linear;
824 #if BIHASH_KVP_AT_BUCKET_LEVEL 826 if (new_log2_pages > 0)
828 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
829 ASSERT (tmp_b.refcnt > 0);
832 b->as_u64 = tmp_b.as_u64;
834 #if BIHASH_KVP_AT_BUCKET_LEVEL 835 if (h->saved_bucket.log2_pages > 0)
841 BV (value_free) (
h, v, h->saved_bucket.log2_pages);
843 #if BIHASH_KVP_AT_BUCKET_LEVEL 848 BV (clib_bihash_alloc_unlock) (
h);
853 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add,
854 int (*is_stale_cb) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
856 u64 hash = BV (clib_bihash_hash) (add_v);
857 return BV (clib_bihash_add_del_inline_with_hash) (
h, add_v, hash, is_add,
862 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add)
864 return BV (clib_bihash_add_del_inline) (
h, add_v, is_add, 0, 0);
867 int BV (clib_bihash_add_or_overwrite_stale)
868 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
869 int (*stale_callback) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
871 return BV (clib_bihash_add_del_inline) (
h, add_v, 1, stale_callback, arg);
874 int BV (clib_bihash_search)
875 (
BVT (clib_bihash) *
h,
876 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
881 u8 *BV (format_bihash) (
u8 * s, va_list * args)
883 BVT (clib_bihash) * h = va_arg (*args,
BVT (clib_bihash) *);
884 int verbose = va_arg (*args,
int);
885 BVT (clib_bihash_bucket) * b;
888 u64 active_elements = 0;
889 u64 active_buckets = 0;
890 u64 linear_buckets = 0;
893 s =
format (s,
"Hash table %s\n", h->name ? h->name : (
u8 *)
"(unnamed)");
895 #if BIHASH_LAZY_INSTANTIATE 897 return format (s,
"[empty, uninitialized]");
900 for (i = 0; i < h->nbuckets; i++)
902 b = BV (clib_bihash_get_bucket) (
h,
i);
903 if (BV (clib_bihash_bucket_is_empty) (b))
906 s =
format (s,
"[%d]: empty\n", i);
912 if (b->linear_search)
918 (s,
"[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n", i,
919 b->offset, (1 << b->log2_pages), b->refcnt, b->linear_search);
923 for (j = 0; j < (1 << b->log2_pages); j++)
927 if (BV (clib_bihash_is_free) (&v->kvp[k]))
930 s =
format (s,
" %d: empty\n",
931 j * BIHASH_KVP_PER_PAGE + k);
938 s =
format (s,
" %d: %U\n",
939 j * BIHASH_KVP_PER_PAGE + k,
940 h->fmt_fn, &(v->kvp[k]), verbose);
944 s =
format (s,
" %d: %U\n",
945 j * BIHASH_KVP_PER_PAGE + k,
946 BV (format_bihash_kvp), &(v->kvp[k]));
955 s =
format (s,
" %lld active elements %lld active buckets\n",
956 active_elements, active_buckets);
959 for (i = 0; i <
vec_len (h->freelists); i++)
963 u64 free_elt_as_u64 = h->freelists[
i];
965 while (free_elt_as_u64)
969 free_elt_as_u64 = free_elt->next_free_as_u64;
972 if (nfree || verbose)
973 s =
format (s,
" [len %d] %u free elts\n", 1 << i, nfree);
976 s =
format (s,
" %lld linear search buckets\n", linear_buckets);
977 used_bytes = alloc_arena_next (h);
979 " arena: base %llx, next %llx\n" 980 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
981 alloc_arena (h), alloc_arena_next (h),
982 used_bytes, used_bytes >> 20,
983 alloc_arena_size (h), alloc_arena_size (h) >> 20);
988 (
BVT (clib_bihash) *
h,
992 BVT (clib_bihash_bucket) * b;
996 #if BIHASH_LAZY_INSTANTIATE 1001 for (i = 0; i < h->nbuckets; i++)
1003 b = BV (clib_bihash_get_bucket) (
h,
i);
1004 if (BV (clib_bihash_bucket_is_empty) (b))
1008 for (j = 0; j < (1 << b->log2_pages); j++)
1012 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1015 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1020 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_MEMORY_STORE_BARRIER()
#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).
#define MFD_ALLOW_SEALING
#define static_always_inline
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.
int(* clib_bihash_foreach_key_value_pair_cb)(clib_bihash_kv *kv, void *ctx)
void clib_bihash_foreach_key_value_pair(clib_bihash *h, clib_bihash_foreach_key_value_pair_cb *callback, void *arg)
Visit active (key,value) pairs in a bi-hash table.
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
uword clib_mem_vm_reserve(uword start, uword size, u32 log2_page_sz)
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 BIHASH_KVP_AT_BUCKET_LEVEL
sll srl srl sll sra u16x4 i
#define vec_free(V)
Free vector's memory (no header).
static void * clib_mem_set_heap(void *heap)
#define clib_warning(format, args...)
static uword round_pow2(uword x, uword pow2)
static void make_working_copy(vnet_classify_table_t *t, vnet_classify_bucket_t *b)
int clib_bihash_search_inline_2(clib_bihash *h, clib_bihash_kv *search_key, clib_bihash_kv *valuep)
Search a bi-hash table.
#define vec_delete(V, N, M)
Delete N elements starting at element M.
static void clib_mem_free(void *p)
static_always_inline void clib_memset_u8(void *p, u8 val, uword count)
static uword extract_bits(uword x, int start, int count)
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)
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 * alloc_aligned(uword size, uword log2_align, void **ptr_to_free)