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
44 BVT (clib_bihash_alloc_chunk) * chunk =
h->chunks;
47 if (chunk && chunk->bytes_left >= nbytes)
49 rv = chunk->next_alloc;
50 chunk->bytes_left -= nbytes;
51 chunk->next_alloc += nbytes;
56 if (nbytes >= chunk_sz)
64 rv = (
u8 *) (chunk + 1);
68 chunk->next =
h->chunks->next;
69 chunk->prev =
h->chunks;
70 h->chunks->next = chunk;
72 chunk->next->prev = chunk;
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 |
113 if (alloc_arena_mapped (
h) >> 2 > alloc)
114 alloc = alloc_arena_mapped (
h) >> 2;
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);
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)
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]);
169 BVT (clib_bihash_bucket) *
b;
173 for (
i = 0;
i <
h->nbuckets;
i++)
179 sizeof (
BVT (clib_bihash_kv)));
182 b = (
void *) (((
uword)
b) +
sizeof (*b) +
184 sizeof (
BVT (clib_bihash_kv))));
191 void BV (clib_bihash_init2) (
BVT (clib_bihash_init2_args) *
a)
195 BVT (clib_bihash) *
h =
a->h;
199 h->name = (
u8 *)
a->name;
200 h->nbuckets =
a->nbuckets;
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;
217 ASSERT (
h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
220 if (
a->dont_add_to_all_bihash_list == 0)
240 h->alloc_lock[0] = 0;
242 #if BIHASH_LAZY_INSTANTIATE
243 if (
a->instantiate_immediately)
245 BV (clib_bihash_instantiate) (
h);
251 BVT (clib_bihash_init2_args) _a, *
a = &_a;
253 memset (
a, 0,
sizeof (*
a));
257 a->nbuckets = nbuckets;
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)
297 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
299 if (mmap_addr == MAP_FAILED)
305 h->sh = (
void *) mmap_addr;
307 nbuckets = 1 << (
max_log2 (nbuckets));
310 h->sh->nbuckets =
h->nbuckets = nbuckets;
313 alloc_arena (
h) = (
u64) (
uword) mmap_addr;
317 bucket_size = nbuckets *
sizeof (
h->buckets[0]);
323 h->alloc_lock[0] = 0;
325 h->sh->alloc_lock_as_u64 =
330 BIHASH_FREELIST_LENGTH *
sizeof (
u64));
331 freelist_vh->
len = BIHASH_FREELIST_LENGTH;
332 h->sh->freelists_as_u64 =
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;
351 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 );
352 if (mmap_addr == MAP_FAILED)
358 sh = (
BVT (clib_bihash_shared_header) *) mmap_addr;
362 munmap (mmap_addr, 4096);
366 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
368 if (mmap_addr == MAP_FAILED)
376 h->sh = (
void *) mmap_addr;
377 alloc_arena (
h) = (
u64) (
uword) mmap_addr;
382 h->nbuckets =
h->sh->nbuckets;
387 h->fmt_fn = BV (format_bihash);
388 h->kvp_fmt_fn = NULL;
392 void BV (clib_bihash_set_kvp_format_fn) (
BVT (clib_bihash) *
h,
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);
408 goto never_initialized;
414 BVT (clib_bihash_alloc_chunk) *
next, *chunk;
429 #if BIHASH_32_64_SVM == 0
433 (void) close (
h->memfd);
437 alloc_arena_size (
h));
439 if (
h->dont_add_to_all_bihash_list)
453 clib_warning (
"Couldn't find hash table %llx on clib_all_bihashes...",
502 BVT (clib_bihash_alloc_chunk) *
c;
503 c = (
BVT (clib_bihash_alloc_chunk) *) v - 1;
506 c->prev->next =
c->next;
511 c->next->prev =
c->prev;
530 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
533 int log2_working_copy_length;
549 log2_working_copy_length =
h->working_copy_lengths[
thread_index];
551 h->saved_bucket.as_u64 =
b->as_u64;
553 if (
b->log2_pages > log2_working_copy_length)
561 (
h,
sizeof (working_copy[0]) * (1 <<
b->log2_pages));
565 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_working_copy_lost,
566 1ULL <<
b->log2_pages);
572 working_bucket.as_u64 =
b->as_u64;
575 b->as_u64 = working_bucket.as_u64;
582 (
BVT (clib_bihash) *
h,
587 int i, j, length_in_kvs;
591 new_values = BV (value_alloc) (
h, new_log2_pages);
594 for (
i = 0;
i < length_in_kvs;
i++)
599 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
603 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[
i]));
604 new_hash =
extract_bits (new_hash,
h->log2_nbuckets, new_log2_pages);
605 new_v = &new_values[new_hash];
611 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
614 sizeof (new_v->kvp[j]));
619 BV (value_free) (
h, new_values, new_log2_pages);
630 (
BVT (clib_bihash) *
h,
635 int i, j, new_length, old_length;
639 new_values = BV (value_alloc) (
h, new_log2_pages);
645 for (
i = 0;
i < old_length;
i++)
648 for (; j < new_length; j++)
651 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
655 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
659 sizeof (new_values->kvp[j]));
666 BV (value_free) (
h, new_values, new_log2_pages);
675 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
u64 hash,
int is_add,
676 int (*is_stale_cb) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
678 BVT (clib_bihash_bucket) *
b, tmp_b;
682 u32 new_log2_pages, old_log2_pages;
684 int mark_bucket_linear;
688 static const BVT (clib_bihash_bucket)
mask = {
694 #if BIHASH_LAZY_INSTANTIATE
704 BV (clib_bihash_alloc_lock) (
h);
705 if (
h->instantiated == 0)
706 BV (clib_bihash_instantiate) (
h);
707 BV (clib_bihash_alloc_unlock) (
h);
714 b = BV (clib_bihash_get_bucket) (
h, hash);
716 BV (clib_bihash_lock_bucket) (
b);
723 BV (clib_bihash_unlock_bucket) (
b);
727 BV (clib_bihash_alloc_lock) (
h);
728 v = BV (value_alloc) (
h, 0);
729 BV (clib_bihash_alloc_unlock) (
h);
737 b->as_u64 = tmp_b.as_u64;
738 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_alloc_add, 1);
750 limit <<=
b->log2_pages;
768 for (
i = 0;
i < limit;
i++)
770 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, add_v->key))
775 BV (clib_bihash_unlock_bucket) (
b);
780 &add_v->value, sizeof (add_v->value));
781 BV (clib_bihash_unlock_bucket) (
b);
782 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_replace, 1);
789 for (
i = 0;
i < limit;
i++)
791 if (BV (clib_bihash_is_free) (&(v->kvp[
i])))
798 &add_v->value, sizeof (add_v->value));
801 sizeof (add_v->key));
804 BV (clib_bihash_unlock_bucket) (
b);
805 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_add, 1);
812 for (
i = 0;
i < limit;
i++)
814 if (is_stale_cb (&(v->kvp[
i]), arg))
818 BV (clib_bihash_unlock_bucket) (
b);
819 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_replace, 1);
828 for (
i = 0;
i < limit;
i++)
831 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, add_v->key))
840 &&
b->log2_pages > 0)
842 tmp_b.as_u64 =
b->as_u64;
844 (
h, (
void *) (
b + 1));
845 b->linear_search = 0;
849 sizeof (
BVT (clib_bihash_kv)));
851 BV (clib_bihash_unlock_bucket) (
b);
852 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del, 1);
853 goto free_backing_store;
857 BV (clib_bihash_unlock_bucket) (
b);
858 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del, 1);
864 tmp_b.as_u64 =
b->as_u64;
871 BV (clib_bihash_alloc_lock) (
h);
874 BV (value_free) (
h, v, tmp_b.log2_pages);
875 BV (clib_bihash_alloc_unlock) (
h);
876 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del_free,
883 BV (clib_bihash_unlock_bucket) (
b);
888 BV (clib_bihash_alloc_lock) (
h);
893 old_log2_pages =
h->saved_bucket.log2_pages;
894 new_log2_pages = old_log2_pages + 1;
895 mark_bucket_linear = 0;
896 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_split_add, 1);
897 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits, old_log2_pages);
901 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits, 1);
921 mark_bucket_linear = 1;
922 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_linear, 1);
924 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_resplit, 1);
925 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits,
931 new_hash = BV (clib_bihash_hash) (add_v);
933 if (mark_bucket_linear)
934 limit <<= new_log2_pages;
936 new_v +=
extract_bits (new_hash,
h->log2_nbuckets, new_log2_pages);
938 for (
i = 0;
i < limit;
i++)
940 if (BV (clib_bihash_is_free) (&(new_v->kvp[
i])))
948 BV (value_free) (
h, save_new_v, new_log2_pages);
959 tmp_b.log2_pages = new_log2_pages;
961 tmp_b.linear_search = mark_bucket_linear;
962 #if BIHASH_KVP_AT_BUCKET_LEVEL
964 if (new_log2_pages > 0)
966 tmp_b.refcnt =
h->saved_bucket.refcnt + 1;
967 ASSERT (tmp_b.refcnt > 0);
970 b->as_u64 = tmp_b.as_u64;
972 #if BIHASH_KVP_AT_BUCKET_LEVEL
973 if (
h->saved_bucket.log2_pages > 0)
979 BV (value_free) (
h, v,
h->saved_bucket.log2_pages);
981 #if BIHASH_KVP_AT_BUCKET_LEVEL
986 BV (clib_bihash_alloc_unlock) (
h);
991 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add,
992 int (*is_stale_cb) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
994 u64 hash = BV (clib_bihash_hash) (add_v);
995 return BV (clib_bihash_add_del_inline_with_hash) (
h, add_v, hash, is_add,
1000 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add)
1002 return BV (clib_bihash_add_del_inline) (
h, add_v, is_add, 0, 0);
1005 int BV (clib_bihash_add_or_overwrite_stale)
1006 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
1007 int (*stale_callback) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
1009 return BV (clib_bihash_add_del_inline) (
h, add_v, 1, stale_callback, arg);
1012 int BV (clib_bihash_search)
1013 (
BVT (clib_bihash) *
h,
1014 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
1019 u8 *BV (format_bihash) (
u8 * s, va_list * args)
1021 BVT (clib_bihash) *
h = va_arg (*args,
BVT (clib_bihash) *);
1022 int verbose = va_arg (*args,
int);
1023 BVT (clib_bihash_bucket) *
b;
1026 u64 active_elements = 0;
1027 u64 active_buckets = 0;
1028 u64 linear_buckets = 0;
1030 s =
format (s,
"Hash table '%s'\n",
h->name ?
h->name : (
u8 *)
"(unnamed)");
1032 #if BIHASH_LAZY_INSTANTIATE
1034 return format (s,
" empty, uninitialized");
1037 for (
i = 0;
i <
h->nbuckets;
i++)
1039 b = BV (clib_bihash_get_bucket) (
h,
i);
1040 if (BV (clib_bihash_bucket_is_empty) (
b))
1043 s =
format (s,
"[%d]: empty\n",
i);
1049 if (
b->linear_search)
1055 (s,
"[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n",
i,
1056 b->offset, (1 <<
b->log2_pages),
b->refcnt,
b->linear_search);
1060 for (j = 0; j < (1 <<
b->log2_pages); j++)
1064 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1067 s =
format (s,
" %d: empty\n",
1075 s =
format (s,
" %d: %U\n",
1077 h->kvp_fmt_fn, &(v->kvp[k]), verbose);
1081 s =
format (s,
" %d: %U\n",
1083 BV (format_bihash_kvp), &(v->kvp[k]));
1092 s =
format (s,
" %lld active elements %lld active buckets\n",
1093 active_elements, active_buckets);
1100 u64 free_elt_as_u64 =
h->freelists[
i];
1102 while (free_elt_as_u64)
1106 free_elt_as_u64 = free_elt->next_free_as_u64;
1109 if (nfree || verbose)
1110 s =
format (s,
" [len %d] %u free elts\n", 1 <<
i, nfree);
1113 s =
format (s,
" %lld linear search buckets\n", linear_buckets);
1116 BVT (clib_bihash_alloc_chunk) *
c =
h->chunks;
1117 uword bytes_left = 0, total_size = 0, n_chunks = 0;
1121 bytes_left +=
c->bytes_left;
1122 total_size +=
c->size;
1127 " heap: %u chunk(s) allocated\n"
1128 " bytes: used %U, scrap %U\n", n_chunks,
1134 u64 used_bytes = alloc_arena_next (
h);
1136 " arena: base %llx, next %llx\n"
1137 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
1138 alloc_arena (
h), alloc_arena_next (
h),
1139 used_bytes, used_bytes >> 20,
1140 alloc_arena_size (
h), alloc_arena_size (
h) >> 20);
1146 (
BVT (clib_bihash) *
h,
1150 BVT (clib_bihash_bucket) *
b;
1154 #if BIHASH_LAZY_INSTANTIATE
1159 for (
i = 0;
i <
h->nbuckets;
i++)
1161 b = BV (clib_bihash_get_bucket) (
h,
i);
1162 if (BV (clib_bihash_bucket_is_empty) (
b))
1166 for (j = 0; j < (1 <<
b->log2_pages); j++)
1170 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1173 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1178 if (BV (clib_bihash_bucket_is_empty) (
b))