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->fmt_fn = BV (format_bihash);
205 h->kvp_fmt_fn =
a->kvp_fmt_fn;
216 ASSERT (
h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
219 if (
a->dont_add_to_all_bihash_list == 0)
239 h->alloc_lock[0] = 0;
241 #if BIHASH_LAZY_INSTANTIATE
242 if (
a->instantiate_immediately)
244 BV (clib_bihash_instantiate) (
h);
250 BVT (clib_bihash_init2_args) _a, *
a = &_a;
252 memset (
a, 0,
sizeof (*
a));
256 a->nbuckets = nbuckets;
259 BV (clib_bihash_init2) (
a);
263 #if !defined (MFD_ALLOW_SEALING)
264 #define MFD_ALLOW_SEALING 0x0002U
267 void BV (clib_bihash_initiator_init_svm)
296 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
298 if (mmap_addr == MAP_FAILED)
304 h->sh = (
void *) mmap_addr;
306 nbuckets = 1 << (
max_log2 (nbuckets));
309 h->sh->nbuckets =
h->nbuckets = nbuckets;
312 alloc_arena (
h) = (
u64) (
uword) mmap_addr;
316 bucket_size = nbuckets *
sizeof (
h->buckets[0]);
322 h->alloc_lock[0] = 0;
324 h->sh->alloc_lock_as_u64 =
329 BIHASH_FREELIST_LENGTH *
sizeof (
u64));
330 freelist_vh->
len = BIHASH_FREELIST_LENGTH;
331 h->sh->freelists_as_u64 =
335 h->fmt_fn = BV (format_bihash);
336 h->kvp_fmt_fn = NULL;
340 void BV (clib_bihash_responder_init_svm)
341 (
BVT (clib_bihash) *
h,
char *
name,
int fd)
345 BVT (clib_bihash_shared_header) * sh;
350 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 );
351 if (mmap_addr == MAP_FAILED)
357 sh = (
BVT (clib_bihash_shared_header) *) mmap_addr;
361 munmap (mmap_addr, 4096);
365 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
367 if (mmap_addr == MAP_FAILED)
375 h->sh = (
void *) mmap_addr;
376 alloc_arena (
h) = (
u64) (
uword) mmap_addr;
381 h->nbuckets =
h->sh->nbuckets;
386 h->fmt_fn = BV (format_bihash);
387 h->kvp_fmt_fn = NULL;
391 void BV (clib_bihash_set_kvp_format_fn) (
BVT (clib_bihash) *
h,
394 h->kvp_fmt_fn = kvp_fmt_fn;
397 int BV (clib_bihash_is_initialised) (
const BVT (clib_bihash) *
h)
399 return (
h->instantiated != 0);
407 goto never_initialized;
413 BVT (clib_bihash_alloc_chunk) *
next, *chunk;
428 #if BIHASH_32_64_SVM == 0
432 (void) close (
h->memfd);
436 alloc_arena_size (
h));
447 clib_warning (
"Couldn't find hash table %llx on clib_all_bihashes...",
496 BVT (clib_bihash_alloc_chunk) *
c;
497 c = (
BVT (clib_bihash_alloc_chunk) *) v - 1;
500 c->prev->next =
c->next;
505 c->next->prev =
c->prev;
524 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
527 int log2_working_copy_length;
543 log2_working_copy_length =
h->working_copy_lengths[
thread_index];
545 h->saved_bucket.as_u64 =
b->as_u64;
547 if (
b->log2_pages > log2_working_copy_length)
555 (
h,
sizeof (working_copy[0]) * (1 <<
b->log2_pages));
559 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_working_copy_lost,
560 1ULL <<
b->log2_pages);
566 working_bucket.as_u64 =
b->as_u64;
569 b->as_u64 = working_bucket.as_u64;
576 (
BVT (clib_bihash) *
h,
581 int i, j, length_in_kvs;
585 new_values = BV (value_alloc) (
h, new_log2_pages);
588 for (
i = 0;
i < length_in_kvs;
i++)
593 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
597 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[
i]));
598 new_hash =
extract_bits (new_hash,
h->log2_nbuckets, new_log2_pages);
599 new_v = &new_values[new_hash];
605 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
608 sizeof (new_v->kvp[j]));
613 BV (value_free) (
h, new_values, new_log2_pages);
624 (
BVT (clib_bihash) *
h,
629 int i, j, new_length, old_length;
633 new_values = BV (value_alloc) (
h, new_log2_pages);
639 for (
i = 0;
i < old_length;
i++)
642 for (; j < new_length; j++)
645 if (BV (clib_bihash_is_free) (&(old_values->kvp[
i])))
649 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
653 sizeof (new_values->kvp[j]));
660 BV (value_free) (
h, new_values, new_log2_pages);
669 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
u64 hash,
int is_add,
670 int (*is_stale_cb) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
672 BVT (clib_bihash_bucket) *
b, tmp_b;
676 u32 new_log2_pages, old_log2_pages;
678 int mark_bucket_linear;
682 static const BVT (clib_bihash_bucket)
mask = {
688 #if BIHASH_LAZY_INSTANTIATE
698 BV (clib_bihash_alloc_lock) (
h);
699 if (
h->instantiated == 0)
700 BV (clib_bihash_instantiate) (
h);
701 BV (clib_bihash_alloc_unlock) (
h);
708 b = BV (clib_bihash_get_bucket) (
h, hash);
710 BV (clib_bihash_lock_bucket) (
b);
717 BV (clib_bihash_unlock_bucket) (
b);
721 BV (clib_bihash_alloc_lock) (
h);
722 v = BV (value_alloc) (
h, 0);
723 BV (clib_bihash_alloc_unlock) (
h);
731 b->as_u64 = tmp_b.as_u64;
732 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_alloc_add, 1);
744 limit <<=
b->log2_pages;
762 for (
i = 0;
i < limit;
i++)
764 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, add_v->key))
769 BV (clib_bihash_unlock_bucket) (
b);
774 &add_v->value, sizeof (add_v->value));
775 BV (clib_bihash_unlock_bucket) (
b);
776 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_replace, 1);
783 for (
i = 0;
i < limit;
i++)
785 if (BV (clib_bihash_is_free) (&(v->kvp[
i])))
792 &add_v->value, sizeof (add_v->value));
795 sizeof (add_v->key));
798 BV (clib_bihash_unlock_bucket) (
b);
799 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_add, 1);
806 for (
i = 0;
i < limit;
i++)
808 if (is_stale_cb (&(v->kvp[
i]), arg))
812 BV (clib_bihash_unlock_bucket) (
b);
813 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_replace, 1);
822 for (
i = 0;
i < limit;
i++)
825 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, add_v->key))
834 &&
b->log2_pages > 0)
836 tmp_b.as_u64 =
b->as_u64;
838 (
h, (
void *) (
b + 1));
839 b->linear_search = 0;
843 sizeof (
BVT (clib_bihash_kv)));
845 BV (clib_bihash_unlock_bucket) (
b);
846 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del, 1);
847 goto free_backing_store;
851 BV (clib_bihash_unlock_bucket) (
b);
852 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del, 1);
858 tmp_b.as_u64 =
b->as_u64;
865 BV (clib_bihash_alloc_lock) (
h);
868 BV (value_free) (
h, v, tmp_b.log2_pages);
869 BV (clib_bihash_alloc_unlock) (
h);
870 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_del_free,
877 BV (clib_bihash_unlock_bucket) (
b);
882 BV (clib_bihash_alloc_lock) (
h);
887 old_log2_pages =
h->saved_bucket.log2_pages;
888 new_log2_pages = old_log2_pages + 1;
889 mark_bucket_linear = 0;
890 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_split_add, 1);
891 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits, old_log2_pages);
895 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits, 1);
915 mark_bucket_linear = 1;
916 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_linear, 1);
918 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_resplit, 1);
919 BV (clib_bihash_increment_stat) (
h, BIHASH_STAT_splits,
925 new_hash = BV (clib_bihash_hash) (add_v);
927 if (mark_bucket_linear)
928 limit <<= new_log2_pages;
930 new_v +=
extract_bits (new_hash,
h->log2_nbuckets, new_log2_pages);
932 for (
i = 0;
i < limit;
i++)
934 if (BV (clib_bihash_is_free) (&(new_v->kvp[
i])))
942 BV (value_free) (
h, save_new_v, new_log2_pages);
953 tmp_b.log2_pages = new_log2_pages;
955 tmp_b.linear_search = mark_bucket_linear;
956 #if BIHASH_KVP_AT_BUCKET_LEVEL
958 if (new_log2_pages > 0)
960 tmp_b.refcnt =
h->saved_bucket.refcnt + 1;
961 ASSERT (tmp_b.refcnt > 0);
964 b->as_u64 = tmp_b.as_u64;
966 #if BIHASH_KVP_AT_BUCKET_LEVEL
967 if (
h->saved_bucket.log2_pages > 0)
973 BV (value_free) (
h, v,
h->saved_bucket.log2_pages);
975 #if BIHASH_KVP_AT_BUCKET_LEVEL
980 BV (clib_bihash_alloc_unlock) (
h);
985 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add,
986 int (*is_stale_cb) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
988 u64 hash = BV (clib_bihash_hash) (add_v);
989 return BV (clib_bihash_add_del_inline_with_hash) (
h, add_v, hash, is_add,
994 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
int is_add)
996 return BV (clib_bihash_add_del_inline) (
h, add_v, is_add, 0, 0);
999 int BV (clib_bihash_add_or_overwrite_stale)
1000 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * add_v,
1001 int (*stale_callback) (
BVT (clib_bihash_kv) *,
void *),
void *arg)
1003 return BV (clib_bihash_add_del_inline) (
h, add_v, 1, stale_callback, arg);
1006 int BV (clib_bihash_search)
1007 (
BVT (clib_bihash) *
h,
1008 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
1013 u8 *BV (format_bihash) (
u8 * s, va_list * args)
1015 BVT (clib_bihash) *
h = va_arg (*args,
BVT (clib_bihash) *);
1016 int verbose = va_arg (*args,
int);
1017 BVT (clib_bihash_bucket) *
b;
1020 u64 active_elements = 0;
1021 u64 active_buckets = 0;
1022 u64 linear_buckets = 0;
1024 s =
format (s,
"Hash table '%s'\n",
h->name ?
h->name : (
u8 *)
"(unnamed)");
1026 #if BIHASH_LAZY_INSTANTIATE
1028 return format (s,
" empty, uninitialized");
1031 for (
i = 0;
i <
h->nbuckets;
i++)
1033 b = BV (clib_bihash_get_bucket) (
h,
i);
1034 if (BV (clib_bihash_bucket_is_empty) (
b))
1037 s =
format (s,
"[%d]: empty\n",
i);
1043 if (
b->linear_search)
1049 (s,
"[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n",
i,
1050 b->offset, (1 <<
b->log2_pages),
b->refcnt,
b->linear_search);
1054 for (j = 0; j < (1 <<
b->log2_pages); j++)
1058 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1061 s =
format (s,
" %d: empty\n",
1069 s =
format (s,
" %d: %U\n",
1071 h->kvp_fmt_fn, &(v->kvp[k]), verbose);
1075 s =
format (s,
" %d: %U\n",
1077 BV (format_bihash_kvp), &(v->kvp[k]));
1086 s =
format (s,
" %lld active elements %lld active buckets\n",
1087 active_elements, active_buckets);
1094 u64 free_elt_as_u64 =
h->freelists[
i];
1096 while (free_elt_as_u64)
1100 free_elt_as_u64 = free_elt->next_free_as_u64;
1103 if (nfree || verbose)
1104 s =
format (s,
" [len %d] %u free elts\n", 1 <<
i, nfree);
1107 s =
format (s,
" %lld linear search buckets\n", linear_buckets);
1110 BVT (clib_bihash_alloc_chunk) *
c =
h->chunks;
1111 uword bytes_left = 0, total_size = 0, n_chunks = 0;
1115 bytes_left +=
c->bytes_left;
1116 total_size +=
c->size;
1121 " heap: %u chunk(s) allocated\n"
1122 " bytes: used %U, scrap %U\n", n_chunks,
1128 u64 used_bytes = alloc_arena_next (
h);
1130 " arena: base %llx, next %llx\n"
1131 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
1132 alloc_arena (
h), alloc_arena_next (
h),
1133 used_bytes, used_bytes >> 20,
1134 alloc_arena_size (
h), alloc_arena_size (
h) >> 20);
1140 (
BVT (clib_bihash) *
h,
1144 BVT (clib_bihash_bucket) *
b;
1148 #if BIHASH_LAZY_INSTANTIATE
1153 for (
i = 0;
i <
h->nbuckets;
i++)
1155 b = BV (clib_bihash_get_bucket) (
h,
i);
1156 if (BV (clib_bihash_bucket_is_empty) (
b))
1160 for (j = 0; j < (1 <<
b->log2_pages); j++)
1164 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1167 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1172 if (BV (clib_bihash_bucket_is_empty) (
b))