Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 16 | /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */ |
Dave Barach | dd3a57f | 2016-07-27 16:58:51 -0400 | [diff] [blame] | 17 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 18 | #ifndef MAP_HUGE_SHIFT |
| 19 | #define MAP_HUGE_SHIFT 26 |
| 20 | #endif |
| 21 | |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 22 | #ifndef BIIHASH_MIN_ALLOC_LOG2_PAGES |
| 23 | #define BIIHASH_MIN_ALLOC_LOG2_PAGES 10 |
| 24 | #endif |
| 25 | |
Damjan Marion | 4a251d0 | 2021-05-06 17:28:12 +0200 | [diff] [blame] | 26 | #ifndef BIHASH_USE_HEAP |
| 27 | #define BIHASH_USE_HEAP 1 |
| 28 | #endif |
| 29 | |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 30 | static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes) |
| 31 | { |
| 32 | uword rv; |
| 33 | |
| 34 | /* Round to an even number of cache lines */ |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 35 | nbytes = round_pow2 (nbytes, CLIB_CACHE_LINE_BYTES); |
| 36 | |
| 37 | if (BIHASH_USE_HEAP) |
| 38 | { |
| 39 | void *rv, *oldheap; |
| 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); |
| 43 | |
| 44 | BVT (clib_bihash_alloc_chunk) * chunk = h->chunks; |
| 45 | |
| 46 | /* if there is enough space in the currenrt chunk */ |
| 47 | if (chunk && chunk->bytes_left >= nbytes) |
| 48 | { |
| 49 | rv = chunk->next_alloc; |
| 50 | chunk->bytes_left -= nbytes; |
| 51 | chunk->next_alloc += nbytes; |
| 52 | return rv; |
| 53 | } |
| 54 | |
| 55 | /* requested allocation is bigger than chunk size */ |
| 56 | if (nbytes >= chunk_sz) |
| 57 | { |
| 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)); |
| 63 | chunk->size = nbytes; |
| 64 | rv = (u8 *) (chunk + 1); |
| 65 | if (h->chunks) |
| 66 | { |
| 67 | /* take 2nd place in the list */ |
| 68 | chunk->next = h->chunks->next; |
| 69 | chunk->prev = h->chunks; |
| 70 | h->chunks->next = chunk; |
| 71 | if (chunk->next) |
| 72 | chunk->next->prev = chunk; |
| 73 | } |
| 74 | else |
| 75 | h->chunks = chunk; |
| 76 | |
| 77 | return rv; |
| 78 | } |
| 79 | |
| 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; |
| 88 | chunk->prev = 0; |
| 89 | if (chunk->next) |
| 90 | chunk->next->prev = chunk; |
| 91 | h->chunks = chunk; |
| 92 | rv = chunk->next_alloc; |
| 93 | chunk->bytes_left -= nbytes; |
| 94 | chunk->next_alloc += nbytes; |
| 95 | return rv; |
| 96 | } |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 97 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 98 | rv = alloc_arena_next (h); |
| 99 | alloc_arena_next (h) += nbytes; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 100 | |
Andreas Schultz | b4b525e | 2019-07-19 11:14:50 +0200 | [diff] [blame] | 101 | if (alloc_arena_next (h) > alloc_arena_size (h)) |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 102 | os_out_of_memory (); |
| 103 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 104 | if (alloc_arena_next (h) > alloc_arena_mapped (h)) |
| 105 | { |
| 106 | void *base, *rv; |
| 107 | uword alloc = alloc_arena_next (h) - alloc_arena_mapped (h); |
| 108 | int mmap_flags = MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS; |
Damjan Marion | e6db775 | 2020-05-24 20:43:10 +0200 | [diff] [blame] | 109 | int mmap_flags_huge = (mmap_flags | MAP_HUGETLB | MAP_LOCKED | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 110 | BIHASH_LOG2_HUGEPAGE_SIZE << MAP_HUGE_SHIFT); |
| 111 | |
| 112 | /* new allocation is 25% of existing one */ |
| 113 | if (alloc_arena_mapped (h) >> 2 > alloc) |
| 114 | alloc = alloc_arena_mapped (h) >> 2; |
| 115 | |
| 116 | /* round allocation to page size */ |
| 117 | alloc = round_pow2 (alloc, 1 << BIHASH_LOG2_HUGEPAGE_SIZE); |
| 118 | |
| 119 | base = (void *) (uword) (alloc_arena (h) + alloc_arena_mapped (h)); |
| 120 | |
| 121 | rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags_huge, -1, 0); |
| 122 | |
| 123 | /* fallback - maybe we are still able to allocate normal pages */ |
Damjan Marion | 6183cf4 | 2020-05-27 16:43:35 +0200 | [diff] [blame] | 124 | if (rv == MAP_FAILED || mlock (base, alloc) != 0) |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 125 | rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags, -1, 0); |
| 126 | |
| 127 | if (rv == MAP_FAILED) |
| 128 | os_out_of_memory (); |
| 129 | |
| 130 | alloc_arena_mapped (h) += alloc; |
| 131 | } |
| 132 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 133 | return (void *) (uword) (rv + alloc_arena (h)); |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Vijayabhaskar Katamreddy | f0bae64 | 2020-01-15 13:45:19 -0800 | [diff] [blame] | 136 | static void BV (clib_bihash_instantiate) (BVT (clib_bihash) * h) |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 137 | { |
| 138 | uword bucket_size; |
| 139 | |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 140 | if (BIHASH_USE_HEAP) |
| 141 | { |
| 142 | h->heap = clib_mem_get_heap (); |
| 143 | h->chunks = 0; |
| 144 | alloc_arena (h) = (uword) clib_mem_get_heap_base (h->heap); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | alloc_arena (h) = clib_mem_vm_reserve (0, h->memory_size, |
| 149 | BIHASH_LOG2_HUGEPAGE_SIZE); |
| 150 | if (alloc_arena (h) == ~0) |
| 151 | os_out_of_memory (); |
| 152 | alloc_arena_next (h) = 0; |
| 153 | alloc_arena_size (h) = h->memory_size; |
| 154 | alloc_arena_mapped (h) = 0; |
| 155 | } |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 156 | |
| 157 | bucket_size = h->nbuckets * sizeof (h->buckets[0]); |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 158 | |
| 159 | if (BIHASH_KVP_AT_BUCKET_LEVEL) |
| 160 | bucket_size += |
| 161 | h->nbuckets * BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)); |
| 162 | |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 163 | h->buckets = BV (alloc_aligned) (h, bucket_size); |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 164 | clib_memset_u8 (h->buckets, 0, bucket_size); |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 165 | |
| 166 | if (BIHASH_KVP_AT_BUCKET_LEVEL) |
| 167 | { |
| 168 | int i; |
| 169 | BVT (clib_bihash_bucket) * b; |
| 170 | |
| 171 | b = h->buckets; |
| 172 | |
| 173 | for (i = 0; i < h->nbuckets; i++) |
| 174 | { |
| 175 | b->offset = BV (clib_bihash_get_offset) (h, (void *) (b + 1)); |
| 176 | b->refcnt = 1; |
| 177 | /* Mark all elements free */ |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 178 | clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE * |
| 179 | sizeof (BVT (clib_bihash_kv))); |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 180 | |
| 181 | /* Compute next bucket start address */ |
| 182 | b = (void *) (((uword) b) + sizeof (*b) + |
| 183 | (BIHASH_KVP_PER_PAGE * |
| 184 | sizeof (BVT (clib_bihash_kv)))); |
| 185 | } |
| 186 | } |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 187 | CLIB_MEMORY_STORE_BARRIER (); |
Dave Barach | 67d09e0 | 2019-08-01 08:15:01 -0400 | [diff] [blame] | 188 | h->instantiated = 1; |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 189 | } |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 190 | |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 191 | void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | { |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 193 | int i; |
| 194 | void *oldheap; |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 195 | BVT (clib_bihash) * h = a->h; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 197 | a->nbuckets = 1 << (max_log2 (a->nbuckets)); |
| 198 | |
| 199 | h->name = (u8 *) a->name; |
| 200 | h->nbuckets = a->nbuckets; |
| 201 | h->log2_nbuckets = max_log2 (a->nbuckets); |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 202 | h->memory_size = BIHASH_USE_HEAP ? 0 : a->memory_size; |
Dave Barach | 67d09e0 | 2019-08-01 08:15:01 -0400 | [diff] [blame] | 203 | h->instantiated = 0; |
Nathan Skrzypczak | a8c720e | 2021-08-06 12:03:11 +0200 | [diff] [blame] | 204 | h->dont_add_to_all_bihash_list = a->dont_add_to_all_bihash_list; |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 205 | h->fmt_fn = BV (format_bihash); |
| 206 | h->kvp_fmt_fn = a->kvp_fmt_fn; |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 207 | |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 208 | alloc_arena (h) = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 210 | /* |
| 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... |
| 215 | */ |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 216 | if (BIHASH_USE_HEAP) |
| 217 | ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS)); |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 218 | |
| 219 | /* Add this hash table to the list */ |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 220 | if (a->dont_add_to_all_bihash_list == 0) |
| 221 | { |
| 222 | for (i = 0; i < vec_len (clib_all_bihashes); i++) |
| 223 | if (clib_all_bihashes[i] == h) |
| 224 | goto do_lock; |
| 225 | oldheap = clib_all_bihash_set_heap (); |
| 226 | vec_add1 (clib_all_bihashes, (void *) h); |
| 227 | clib_mem_set_heap (oldheap); |
| 228 | } |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 229 | |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 230 | do_lock: |
| 231 | if (h->alloc_lock) |
| 232 | clib_mem_free ((void *) h->alloc_lock); |
Dave Barach | 67d09e0 | 2019-08-01 08:15:01 -0400 | [diff] [blame] | 233 | |
| 234 | /* |
| 235 | * Set up the lock now, so we can use it to make the first add |
| 236 | * thread-safe |
| 237 | */ |
| 238 | h->alloc_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, |
| 239 | CLIB_CACHE_LINE_BYTES); |
| 240 | h->alloc_lock[0] = 0; |
| 241 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 242 | #if BIHASH_LAZY_INSTANTIATE |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 243 | if (a->instantiate_immediately) |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 244 | #endif |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 245 | BV (clib_bihash_instantiate) (h); |
| 246 | } |
| 247 | |
| 248 | void BV (clib_bihash_init) |
| 249 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size) |
| 250 | { |
| 251 | BVT (clib_bihash_init2_args) _a, *a = &_a; |
| 252 | |
| 253 | memset (a, 0, sizeof (*a)); |
| 254 | |
| 255 | a->h = h; |
| 256 | a->name = name; |
| 257 | a->nbuckets = nbuckets; |
| 258 | a->memory_size = memory_size; |
| 259 | |
| 260 | BV (clib_bihash_init2) (a); |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 263 | #if BIHASH_32_64_SVM |
| 264 | #if !defined (MFD_ALLOW_SEALING) |
| 265 | #define MFD_ALLOW_SEALING 0x0002U |
| 266 | #endif |
| 267 | |
Dave Barach | d4a639b | 2020-08-06 11:38:40 -0400 | [diff] [blame] | 268 | void BV (clib_bihash_initiator_init_svm) |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 269 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size) |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 270 | { |
| 271 | uword bucket_size; |
| 272 | u8 *mmap_addr; |
| 273 | vec_header_t *freelist_vh; |
| 274 | int fd; |
| 275 | |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 276 | ASSERT (BIHASH_USE_HEAP == 0); |
| 277 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 278 | ASSERT (memory_size < (1ULL << 32)); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 279 | /* Set up for memfd sharing */ |
Damjan Marion | f8cb701 | 2020-10-09 17:16:55 +0200 | [diff] [blame] | 280 | if ((fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, name) == -1) |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 281 | { |
| 282 | clib_unix_warning ("memfd_create"); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | if (ftruncate (fd, memory_size) < 0) |
| 287 | { |
| 288 | clib_unix_warning ("ftruncate"); |
| 289 | return; |
| 290 | } |
| 291 | |
| 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)"); |
| 295 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 296 | mmap_addr = mmap (0, memory_size, |
| 297 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ ); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 298 | |
| 299 | if (mmap_addr == MAP_FAILED) |
| 300 | { |
| 301 | clib_unix_warning ("mmap failed"); |
| 302 | ASSERT (0); |
| 303 | } |
| 304 | |
| 305 | h->sh = (void *) mmap_addr; |
| 306 | h->memfd = fd; |
| 307 | nbuckets = 1 << (max_log2 (nbuckets)); |
| 308 | |
| 309 | h->name = (u8 *) name; |
| 310 | h->sh->nbuckets = h->nbuckets = nbuckets; |
| 311 | h->log2_nbuckets = max_log2 (nbuckets); |
| 312 | |
| 313 | alloc_arena (h) = (u64) (uword) mmap_addr; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 314 | alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 315 | alloc_arena_size (h) = memory_size; |
| 316 | |
| 317 | bucket_size = nbuckets * sizeof (h->buckets[0]); |
| 318 | h->buckets = BV (alloc_aligned) (h, bucket_size); |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 319 | clib_memset_u8 (h->buckets, 0, bucket_size); |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 320 | h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 321 | |
| 322 | h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES); |
| 323 | h->alloc_lock[0] = 0; |
| 324 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 325 | h->sh->alloc_lock_as_u64 = |
| 326 | (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock); |
| 327 | freelist_vh = |
| 328 | BV (alloc_aligned) (h, |
| 329 | sizeof (vec_header_t) + |
| 330 | BIHASH_FREELIST_LENGTH * sizeof (u64)); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 331 | freelist_vh->len = BIHASH_FREELIST_LENGTH; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 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); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 335 | |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 336 | h->fmt_fn = BV (format_bihash); |
| 337 | h->kvp_fmt_fn = NULL; |
Vijayabhaskar Katamreddy | f0bae64 | 2020-01-15 13:45:19 -0800 | [diff] [blame] | 338 | h->instantiated = 1; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Dave Barach | d4a639b | 2020-08-06 11:38:40 -0400 | [diff] [blame] | 341 | void BV (clib_bihash_responder_init_svm) |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 342 | (BVT (clib_bihash) * h, char *name, int fd) |
| 343 | { |
| 344 | u8 *mmap_addr; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 345 | u64 memory_size; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 346 | BVT (clib_bihash_shared_header) * sh; |
| 347 | |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 348 | ASSERT (BIHASH_USE_HEAP == 0); |
| 349 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 350 | /* Trial mapping, to learn the segment size */ |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 351 | mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ ); |
| 352 | if (mmap_addr == MAP_FAILED) |
| 353 | { |
| 354 | clib_unix_warning ("trial mmap failed"); |
| 355 | ASSERT (0); |
| 356 | } |
| 357 | |
| 358 | sh = (BVT (clib_bihash_shared_header) *) mmap_addr; |
| 359 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 360 | memory_size = sh->alloc_arena_size; |
| 361 | |
| 362 | munmap (mmap_addr, 4096); |
| 363 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 364 | /* Actual mapping, at the required size */ |
| 365 | mmap_addr = mmap (0, memory_size, |
| 366 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ ); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 367 | |
| 368 | if (mmap_addr == MAP_FAILED) |
| 369 | { |
| 370 | clib_unix_warning ("mmap failed"); |
| 371 | ASSERT (0); |
| 372 | } |
| 373 | |
| 374 | (void) close (fd); |
| 375 | |
| 376 | h->sh = (void *) mmap_addr; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 377 | alloc_arena (h) = (u64) (uword) mmap_addr; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 378 | h->memfd = -1; |
| 379 | |
| 380 | h->name = (u8 *) name; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 381 | h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 382 | h->nbuckets = h->sh->nbuckets; |
| 383 | h->log2_nbuckets = max_log2 (h->nbuckets); |
| 384 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 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); |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 387 | h->fmt_fn = BV (format_bihash); |
| 388 | h->kvp_fmt_fn = NULL; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 389 | } |
| 390 | #endif /* BIHASH_32_64_SVM */ |
| 391 | |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 392 | void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h, |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 393 | format_function_t * kvp_fmt_fn) |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 394 | { |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 395 | h->kvp_fmt_fn = kvp_fmt_fn; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Neale Ranns | eb69648 | 2020-09-29 14:44:23 +0000 | [diff] [blame] | 398 | int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h) |
| 399 | { |
| 400 | return (h->instantiated != 0); |
| 401 | } |
| 402 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 403 | void BV (clib_bihash_free) (BVT (clib_bihash) * h) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 404 | { |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 405 | int i; |
| 406 | |
Dave Barach | 67d09e0 | 2019-08-01 08:15:01 -0400 | [diff] [blame] | 407 | if (PREDICT_FALSE (h->instantiated == 0)) |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 408 | goto never_initialized; |
| 409 | |
Dave Barach | 67d09e0 | 2019-08-01 08:15:01 -0400 | [diff] [blame] | 410 | h->instantiated = 0; |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 411 | |
| 412 | if (BIHASH_USE_HEAP) |
| 413 | { |
| 414 | BVT (clib_bihash_alloc_chunk) * next, *chunk; |
| 415 | void *oldheap = clib_mem_set_heap (h->heap); |
| 416 | |
| 417 | chunk = h->chunks; |
| 418 | while (chunk) |
| 419 | { |
| 420 | next = chunk->next; |
| 421 | clib_mem_free (chunk); |
| 422 | chunk = next; |
| 423 | } |
| 424 | clib_mem_set_heap (oldheap); |
| 425 | } |
| 426 | |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 427 | vec_free (h->working_copies); |
Vijayabhaskar Katamreddy | 72739a6 | 2019-05-07 13:27:32 -0700 | [diff] [blame] | 428 | vec_free (h->working_copy_lengths); |
Han Wu | 3d5e48b | 2021-11-19 17:45:40 +0800 | [diff] [blame] | 429 | clib_mem_free ((void *) h->alloc_lock); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 430 | #if BIHASH_32_64_SVM == 0 |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 431 | vec_free (h->freelists); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 432 | #else |
| 433 | if (h->memfd > 0) |
| 434 | (void) close (h->memfd); |
| 435 | #endif |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 436 | if (BIHASH_USE_HEAP == 0) |
| 437 | clib_mem_vm_free ((void *) (uword) (alloc_arena (h)), |
| 438 | alloc_arena_size (h)); |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 439 | never_initialized: |
Nathan Skrzypczak | a8c720e | 2021-08-06 12:03:11 +0200 | [diff] [blame] | 440 | if (h->dont_add_to_all_bihash_list) |
| 441 | { |
| 442 | clib_memset_u8 (h, 0, sizeof (*h)); |
| 443 | return; |
| 444 | } |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 445 | clib_memset_u8 (h, 0, sizeof (*h)); |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 446 | for (i = 0; i < vec_len (clib_all_bihashes); i++) |
| 447 | { |
| 448 | if ((void *) h == clib_all_bihashes[i]) |
| 449 | { |
| 450 | vec_delete (clib_all_bihashes, 1, i); |
| 451 | return; |
| 452 | } |
| 453 | } |
| 454 | clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...", |
Vijayabhaskar Katamreddy | f0bae64 | 2020-01-15 13:45:19 -0800 | [diff] [blame] | 455 | (u64) (uword) h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 458 | static |
| 459 | BVT (clib_bihash_value) * |
| 460 | BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 462 | BVT (clib_bihash_value) * rv = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 464 | ASSERT (h->alloc_lock[0]); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 465 | |
| 466 | #if BIHASH_32_64_SVM |
| 467 | ASSERT (log2_pages < vec_len (h->freelists)); |
| 468 | #endif |
| 469 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 470 | if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | { |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 472 | vec_validate_init_empty (h->freelists, log2_pages, 0); |
| 473 | rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages))); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 474 | goto initialize; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 475 | } |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 476 | rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 477 | h->freelists[log2_pages] = rv->next_free_as_u64; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 478 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 479 | initialize: |
| 480 | ASSERT (rv); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 481 | /* |
| 482 | * Latest gcc complains that the length arg is zero |
| 483 | * if we replace (1<<log2_pages) with vec_len(rv). |
| 484 | * No clue. |
| 485 | */ |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 486 | clib_memset_u8 (rv, 0xff, sizeof (*rv) * (1 << log2_pages)); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 487 | return rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | static void |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 491 | BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v, |
| 492 | u32 log2_pages) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 494 | ASSERT (h->alloc_lock[0]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 496 | ASSERT (vec_len (h->freelists) > log2_pages); |
| 497 | |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 498 | if (BIHASH_USE_HEAP && log2_pages >= BIIHASH_MIN_ALLOC_LOG2_PAGES) |
| 499 | { |
| 500 | /* allocations bigger or equal to chunk size always contain single |
| 501 | * alloc and they can be given back to heap */ |
| 502 | void *oldheap; |
| 503 | BVT (clib_bihash_alloc_chunk) * c; |
| 504 | c = (BVT (clib_bihash_alloc_chunk) *) v - 1; |
| 505 | |
| 506 | if (c->prev) |
| 507 | c->prev->next = c->next; |
| 508 | else |
| 509 | h->chunks = c->next; |
| 510 | |
| 511 | if (c->next) |
| 512 | c->next->prev = c->prev; |
| 513 | |
| 514 | oldheap = clib_mem_set_heap (h->heap); |
| 515 | clib_mem_free (c); |
| 516 | clib_mem_set_heap (oldheap); |
| 517 | return; |
| 518 | } |
| 519 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 520 | if (CLIB_DEBUG > 0) |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 521 | clib_memset_u8 (v, 0xFE, sizeof (*v) * (1 << log2_pages)); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 522 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 523 | v->next_free_as_u64 = (u64) h->freelists[log2_pages]; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 524 | h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | static inline void |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 528 | BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 529 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 530 | BVT (clib_bihash_value) * v; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 531 | BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8))); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 532 | BVT (clib_bihash_value) * working_copy; |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 533 | u32 thread_index = os_get_thread_index (); |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 534 | int log2_working_copy_length; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 535 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 536 | ASSERT (h->alloc_lock[0]); |
| 537 | |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 538 | if (thread_index >= vec_len (h->working_copies)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 539 | { |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 540 | vec_validate (h->working_copies, thread_index); |
Steve Shin | 871cdec | 2017-06-02 10:09:02 -0700 | [diff] [blame] | 541 | vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 544 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 545 | * working_copies are per-cpu so that near-simultaneous |
| 546 | * updates from multiple threads will not result in sporadic, spurious |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 547 | * lookup failures. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 548 | */ |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 549 | working_copy = h->working_copies[thread_index]; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 550 | log2_working_copy_length = h->working_copy_lengths[thread_index]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 551 | |
| 552 | h->saved_bucket.as_u64 = b->as_u64; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 553 | |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 554 | if (b->log2_pages > log2_working_copy_length) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 555 | { |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 556 | /* |
| 557 | * It's not worth the bookkeeping to free working copies |
| 558 | * if (working_copy) |
| 559 | * clib_mem_free (working_copy); |
| 560 | */ |
| 561 | working_copy = BV (alloc_aligned) |
| 562 | (h, sizeof (working_copy[0]) * (1 << b->log2_pages)); |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 563 | h->working_copy_lengths[thread_index] = b->log2_pages; |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 564 | h->working_copies[thread_index] = working_copy; |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 565 | |
| 566 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost, |
| 567 | 1ULL << b->log2_pages); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 570 | v = BV (clib_bihash_get_value) (h, b->offset); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 571 | |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 572 | clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | working_bucket.as_u64 = b->as_u64; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 574 | working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy); |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 575 | CLIB_MEMORY_STORE_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 576 | b->as_u64 = working_bucket.as_u64; |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 577 | h->working_copies[thread_index] = working_copy; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 580 | static |
| 581 | BVT (clib_bihash_value) * |
| 582 | BV (split_and_rehash) |
| 583 | (BVT (clib_bihash) * h, |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 584 | BVT (clib_bihash_value) * old_values, u32 old_log2_pages, |
| 585 | u32 new_log2_pages) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 586 | { |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 587 | BVT (clib_bihash_value) * new_values, *new_v; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 588 | int i, j, length_in_kvs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 589 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 590 | ASSERT (h->alloc_lock[0]); |
| 591 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 592 | new_values = BV (value_alloc) (h, new_log2_pages); |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 593 | length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 594 | |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 595 | for (i = 0; i < length_in_kvs; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 596 | { |
| 597 | u64 new_hash; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 598 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 599 | /* Entry not in use? Forget it */ |
| 600 | if (BV (clib_bihash_is_free) (&(old_values->kvp[i]))) |
| 601 | continue; |
| 602 | |
| 603 | /* rehash the item onto its new home-page */ |
| 604 | new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i])); |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 605 | new_hash = extract_bits (new_hash, h->log2_nbuckets, new_log2_pages); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 606 | new_v = &new_values[new_hash]; |
| 607 | |
| 608 | /* Across the new home-page */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 609 | for (j = 0; j < BIHASH_KVP_PER_PAGE; j++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 610 | { |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 611 | /* Empty slot */ |
| 612 | if (BV (clib_bihash_is_free) (&(new_v->kvp[j]))) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 613 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 614 | clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]), |
| 615 | sizeof (new_v->kvp[j])); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 616 | goto doublebreak; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 617 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 618 | } |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 619 | /* Crap. Tell caller to try again */ |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 620 | BV (value_free) (h, new_values, new_log2_pages); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 621 | return 0; |
| 622 | doublebreak:; |
| 623 | } |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 624 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 625 | return new_values; |
| 626 | } |
| 627 | |
| 628 | static |
| 629 | BVT (clib_bihash_value) * |
| 630 | BV (split_and_rehash_linear) |
| 631 | (BVT (clib_bihash) * h, |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 632 | BVT (clib_bihash_value) * old_values, u32 old_log2_pages, |
| 633 | u32 new_log2_pages) |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 634 | { |
| 635 | BVT (clib_bihash_value) * new_values; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 636 | int i, j, new_length, old_length; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 637 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 638 | ASSERT (h->alloc_lock[0]); |
| 639 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 640 | new_values = BV (value_alloc) (h, new_log2_pages); |
| 641 | new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 642 | old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 643 | |
| 644 | j = 0; |
| 645 | /* Across the old value array */ |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 646 | for (i = 0; i < old_length; i++) |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 647 | { |
| 648 | /* Find a free slot in the new linear scan bucket */ |
| 649 | for (; j < new_length; j++) |
| 650 | { |
Dave Barach | 8f54496 | 2017-01-18 10:23:22 -0500 | [diff] [blame] | 651 | /* Old value not in use? Forget it. */ |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 652 | if (BV (clib_bihash_is_free) (&(old_values->kvp[i]))) |
| 653 | goto doublebreak; |
| 654 | |
| 655 | /* New value should never be in use */ |
| 656 | if (BV (clib_bihash_is_free) (&(new_values->kvp[j]))) |
| 657 | { |
| 658 | /* Copy the old value and move along */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 659 | clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]), |
| 660 | sizeof (new_values->kvp[j])); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 661 | j++; |
| 662 | goto doublebreak; |
| 663 | } |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 664 | } |
Dave Barach | 8f54496 | 2017-01-18 10:23:22 -0500 | [diff] [blame] | 665 | /* This should never happen... */ |
| 666 | clib_warning ("BUG: linear rehash failed!"); |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 667 | BV (value_free) (h, new_values, new_log2_pages); |
Dave Barach | 8f54496 | 2017-01-18 10:23:22 -0500 | [diff] [blame] | 668 | return 0; |
| 669 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 670 | doublebreak:; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 671 | } |
| 672 | return new_values; |
| 673 | } |
| 674 | |
Nathan Skrzypczak | 17ecd85 | 2021-12-02 14:40:06 +0100 | [diff] [blame] | 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) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 679 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 680 | BVT (clib_bihash_bucket) * b, tmp_b; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 681 | BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 682 | int i, limit; |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 683 | u64 new_hash; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 684 | u32 new_log2_pages, old_log2_pages; |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 685 | u32 thread_index = os_get_thread_index (); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 686 | int mark_bucket_linear; |
| 687 | int resplit_once; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 688 | |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 689 | /* *INDENT-OFF* */ |
| 690 | static const BVT (clib_bihash_bucket) mask = { |
| 691 | .linear_search = 1, |
| 692 | .log2_pages = -1 |
| 693 | }; |
| 694 | /* *INDENT-ON* */ |
| 695 | |
| 696 | #if BIHASH_LAZY_INSTANTIATE |
Dave Barach | 67d09e0 | 2019-08-01 08:15:01 -0400 | [diff] [blame] | 697 | /* |
| 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. |
| 700 | */ |
| 701 | if (PREDICT_FALSE (h->instantiated == 0)) |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 702 | { |
| 703 | if (is_add == 0) |
| 704 | return (-1); |
Dave Barach | 67d09e0 | 2019-08-01 08:15:01 -0400 | [diff] [blame] | 705 | |
| 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); |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 710 | } |
Dave Barach | a90ba64 | 2020-04-23 16:56:15 -0400 | [diff] [blame] | 711 | #else |
| 712 | /* Debug image: make sure the table has been instantiated */ |
| 713 | ASSERT (h->instantiated != 0); |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 714 | #endif |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 715 | |
Damjan Marion | 4e14977 | 2020-03-27 16:57:28 +0100 | [diff] [blame] | 716 | b = BV (clib_bihash_get_bucket) (h, hash); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 717 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 718 | BV (clib_bihash_lock_bucket) (b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 719 | |
| 720 | /* First elt in the bucket? */ |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 721 | if (BIHASH_KVP_AT_BUCKET_LEVEL == 0 && BV (clib_bihash_bucket_is_empty) (b)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 722 | { |
| 723 | if (is_add == 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 724 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 725 | BV (clib_bihash_unlock_bucket) (b); |
| 726 | return (-1); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 727 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 728 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 729 | BV (clib_bihash_alloc_lock) (h); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 730 | v = BV (value_alloc) (h, 0); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 731 | BV (clib_bihash_alloc_unlock) (h); |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 732 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 733 | *v->kvp = *add_v; |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 734 | tmp_b.as_u64 = 0; /* clears bucket lock */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 735 | tmp_b.offset = BV (clib_bihash_get_offset) (h, v); |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 736 | tmp_b.refcnt = 1; |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 737 | CLIB_MEMORY_STORE_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 738 | |
Tom Seidenberg | 97f8ae9 | 2019-03-15 10:15:26 -0400 | [diff] [blame] | 739 | b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */ |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 740 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1); |
| 741 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 742 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 745 | /* WARNING: we're still looking at the live copy... */ |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 746 | limit = BIHASH_KVP_PER_PAGE; |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 747 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 748 | |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 749 | if (PREDICT_FALSE (b->as_u64 & mask.as_u64)) |
| 750 | { |
| 751 | if (PREDICT_FALSE (b->linear_search)) |
| 752 | limit <<= b->log2_pages; |
| 753 | else |
| 754 | v += extract_bits (hash, h->log2_nbuckets, b->log2_pages); |
| 755 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 756 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 757 | if (is_add) |
| 758 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 759 | /* |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 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. |
| 764 | */ |
| 765 | |
| 766 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 767 | * For obvious (in hindsight) reasons, see if we're supposed to |
| 768 | * replace an existing key, then look for an empty slot. |
| 769 | */ |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 770 | for (i = 0; i < limit; i++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 771 | { |
Dave Barach | a11bf45 | 2019-04-17 17:27:31 -0400 | [diff] [blame] | 772 | if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 773 | { |
Dave Barach | 9e4946b | 2019-07-08 14:47:44 -0400 | [diff] [blame] | 774 | /* Add but do not overwrite? */ |
| 775 | if (is_add == 2) |
| 776 | { |
| 777 | BV (clib_bihash_unlock_bucket) (b); |
| 778 | return (-2); |
| 779 | } |
Nathan Skrzypczak | 17ecd85 | 2021-12-02 14:40:06 +0100 | [diff] [blame] | 780 | if (overwrite_cb) |
| 781 | overwrite_cb (&(v->kvp[i]), overwrite_arg); |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 782 | clib_memcpy_fast (&(v->kvp[i].value), |
| 783 | &add_v->value, sizeof (add_v->value)); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 784 | BV (clib_bihash_unlock_bucket) (b); |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 785 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 786 | return (0); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 787 | } |
| 788 | } |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 789 | /* |
| 790 | * Look for an empty slot. If found, use it |
| 791 | */ |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 792 | for (i = 0; i < limit; i++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 793 | { |
| 794 | if (BV (clib_bihash_is_free) (&(v->kvp[i]))) |
| 795 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 796 | /* |
| 797 | * Copy the value first, so that if a reader manages |
| 798 | * to match the new key, the value will be right... |
| 799 | */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 800 | clib_memcpy_fast (&(v->kvp[i].value), |
| 801 | &add_v->value, sizeof (add_v->value)); |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 802 | CLIB_MEMORY_STORE_BARRIER (); /* Make sure the value has settled */ |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 803 | clib_memcpy_fast (&(v->kvp[i]), &add_v->key, |
| 804 | sizeof (add_v->key)); |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 805 | b->refcnt++; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 806 | ASSERT (b->refcnt > 0); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 807 | BV (clib_bihash_unlock_bucket) (b); |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 808 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 809 | return (0); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 810 | } |
| 811 | } |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 812 | /* look for stale data to overwrite */ |
| 813 | if (is_stale_cb) |
| 814 | { |
| 815 | for (i = 0; i < limit; i++) |
| 816 | { |
Nathan Skrzypczak | 17ecd85 | 2021-12-02 14:40:06 +0100 | [diff] [blame] | 817 | if (is_stale_cb (&(v->kvp[i]), is_stale_arg)) |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 818 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 819 | clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v)); |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 820 | CLIB_MEMORY_STORE_BARRIER (); |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 821 | BV (clib_bihash_unlock_bucket) (b); |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 822 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1); |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 823 | return (0); |
| 824 | } |
| 825 | } |
| 826 | } |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 827 | /* Out of space in this bucket, split the bucket... */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 828 | } |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 829 | else /* delete case */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 830 | { |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 831 | for (i = 0; i < limit; i++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 832 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 833 | /* Found the key? Kill it... */ |
Dave Barach | a11bf45 | 2019-04-17 17:27:31 -0400 | [diff] [blame] | 834 | if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 835 | { |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 836 | clib_memset_u8 (&(v->kvp[i]), 0xff, sizeof (*(add_v))); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 837 | /* Is the bucket empty? */ |
| 838 | if (PREDICT_TRUE (b->refcnt > 1)) |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 839 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 840 | b->refcnt--; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 841 | /* Switch back to the bucket-level kvp array? */ |
| 842 | if (BIHASH_KVP_AT_BUCKET_LEVEL && b->refcnt == 1 |
| 843 | && b->log2_pages > 0) |
| 844 | { |
| 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; |
| 849 | b->log2_pages = 0; |
| 850 | /* Clean up the bucket-level kvp array */ |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 851 | clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE * |
| 852 | sizeof (BVT (clib_bihash_kv))); |
| 853 | CLIB_MEMORY_STORE_BARRIER (); |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 854 | BV (clib_bihash_unlock_bucket) (b); |
| 855 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1); |
| 856 | goto free_backing_store; |
| 857 | } |
| 858 | |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 859 | CLIB_MEMORY_STORE_BARRIER (); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 860 | BV (clib_bihash_unlock_bucket) (b); |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 861 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 862 | return (0); |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 863 | } |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 864 | else /* yes, free it */ |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 865 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 866 | /* Save old bucket value, need log2_pages to free it */ |
| 867 | tmp_b.as_u64 = b->as_u64; |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 868 | |
| 869 | /* Kill and unlock the bucket */ |
| 870 | b->as_u64 = 0; |
| 871 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 872 | free_backing_store: |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 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); |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 879 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free, |
| 880 | 1); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 881 | return (0); |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 882 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 883 | } |
| 884 | } |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 885 | /* Not found... */ |
| 886 | BV (clib_bihash_unlock_bucket) (b); |
| 887 | return (-3); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 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); |
| 893 | |
| 894 | v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset); |
| 895 | |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 896 | old_log2_pages = h->saved_bucket.log2_pages; |
| 897 | new_log2_pages = old_log2_pages + 1; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 898 | mark_bucket_linear = 0; |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 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); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 901 | |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 902 | working_copy = h->working_copies[thread_index]; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 903 | resplit_once = 0; |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 904 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 905 | |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 906 | new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages, |
| 907 | new_log2_pages); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 908 | if (new_v == 0) |
| 909 | { |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 910 | try_resplit: |
| 911 | resplit_once = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 912 | new_log2_pages++; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 913 | /* Try re-splitting. If that fails, fall back to linear search */ |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 914 | new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages, |
| 915 | new_log2_pages); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 916 | if (new_v == 0) |
| 917 | { |
| 918 | mark_linear: |
| 919 | new_log2_pages--; |
| 920 | /* pinned collisions, use linear search */ |
| 921 | new_v = |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 922 | BV (split_and_rehash_linear) (h, working_copy, old_log2_pages, |
| 923 | new_log2_pages); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 924 | mark_bucket_linear = 1; |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 925 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 926 | } |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 927 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1); |
| 928 | BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, |
| 929 | old_log2_pages + 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | /* Try to add the new entry */ |
| 933 | save_new_v = new_v; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 934 | new_hash = BV (clib_bihash_hash) (add_v); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 935 | limit = BIHASH_KVP_PER_PAGE; |
| 936 | if (mark_bucket_linear) |
| 937 | limit <<= new_log2_pages; |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 938 | else |
| 939 | new_v += extract_bits (new_hash, h->log2_nbuckets, new_log2_pages); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 940 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 941 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 942 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 943 | if (BV (clib_bihash_is_free) (&(new_v->kvp[i]))) |
| 944 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 945 | clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v)); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 946 | goto expand_ok; |
| 947 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 948 | } |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 949 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 950 | /* Crap. Try again */ |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 951 | BV (value_free) (h, save_new_v, new_log2_pages); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 952 | /* |
| 953 | * If we've already doubled the size of the bucket once, |
| 954 | * fall back to linear search now. |
| 955 | */ |
| 956 | if (resplit_once) |
| 957 | goto mark_linear; |
| 958 | else |
| 959 | goto try_resplit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 960 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 961 | expand_ok: |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 962 | tmp_b.log2_pages = new_log2_pages; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 963 | tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v); |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 964 | tmp_b.linear_search = mark_bucket_linear; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 965 | #if BIHASH_KVP_AT_BUCKET_LEVEL |
| 966 | /* Compensate for permanent refcount bump at the bucket level */ |
| 967 | if (new_log2_pages > 0) |
| 968 | #endif |
| 969 | tmp_b.refcnt = h->saved_bucket.refcnt + 1; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 970 | ASSERT (tmp_b.refcnt > 0); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 971 | tmp_b.lock = 0; |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 972 | CLIB_MEMORY_STORE_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 973 | b->as_u64 = tmp_b.as_u64; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 974 | |
| 975 | #if BIHASH_KVP_AT_BUCKET_LEVEL |
| 976 | if (h->saved_bucket.log2_pages > 0) |
| 977 | { |
| 978 | #endif |
| 979 | |
| 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); |
| 983 | |
| 984 | #if BIHASH_KVP_AT_BUCKET_LEVEL |
| 985 | } |
| 986 | #endif |
| 987 | |
| 988 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 989 | BV (clib_bihash_alloc_unlock) (h); |
| 990 | return (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 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) |
| 996 | { |
| 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, |
Nathan Skrzypczak | 17ecd85 | 2021-12-02 14:40:06 +0100 | [diff] [blame] | 999 | is_stale_cb, arg, 0, 0); |
| 1000 | } |
| 1001 | |
| 1002 | int BV (clib_bihash_add_del_with_hash) (BVT (clib_bihash) * h, |
| 1003 | BVT (clib_bihash_kv) * add_v, u64 hash, |
| 1004 | int is_add) |
| 1005 | { |
| 1006 | return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add, 0, |
| 1007 | 0, 0, 0); |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 1008 | } |
| 1009 | |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 1010 | int BV (clib_bihash_add_del) |
| 1011 | (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add) |
| 1012 | { |
| 1013 | return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0); |
| 1014 | } |
| 1015 | |
| 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) |
| 1019 | { |
| 1020 | return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg); |
| 1021 | } |
| 1022 | |
Nathan Skrzypczak | 17ecd85 | 2021-12-02 14:40:06 +0100 | [diff] [blame] | 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) |
| 1026 | { |
| 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, |
| 1029 | overwrite_cb, arg); |
| 1030 | } |
| 1031 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1032 | int BV (clib_bihash_search) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 1033 | (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1034 | BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1035 | { |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 1036 | return BV (clib_bihash_search_inline_2) (h, search_key, valuep); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1039 | u8 *BV (format_bihash) (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1040 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1041 | BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1042 | int verbose = va_arg (*args, int); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 1043 | BVT (clib_bihash_bucket) * b; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1044 | BVT (clib_bihash_value) * v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1045 | int i, j, k; |
| 1046 | u64 active_elements = 0; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 1047 | u64 active_buckets = 0; |
| 1048 | u64 linear_buckets = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1049 | |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 1050 | s = format (s, "Hash table '%s'\n", h->name ? h->name : (u8 *) "(unnamed)"); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1051 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1052 | #if BIHASH_LAZY_INSTANTIATE |
Nathan Skrzypczak | 42b29ba | 2020-08-17 14:14:56 +0200 | [diff] [blame] | 1053 | if (PREDICT_FALSE (h->instantiated == 0)) |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 1054 | return format (s, " empty, uninitialized"); |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1055 | #endif |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 1056 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1057 | for (i = 0; i < h->nbuckets; i++) |
| 1058 | { |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1059 | b = BV (clib_bihash_get_bucket) (h, i); |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 1060 | if (BV (clib_bihash_bucket_is_empty) (b)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1061 | { |
| 1062 | if (verbose > 1) |
| 1063 | s = format (s, "[%d]: empty\n", i); |
| 1064 | continue; |
| 1065 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1066 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 1067 | active_buckets++; |
| 1068 | |
| 1069 | if (b->linear_search) |
| 1070 | linear_buckets++; |
| 1071 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1072 | if (verbose) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1073 | { |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1074 | s = format |
| 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); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1077 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1078 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1079 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 1080 | for (j = 0; j < (1 << b->log2_pages); j++) |
| 1081 | { |
| 1082 | for (k = 0; k < BIHASH_KVP_PER_PAGE; k++) |
| 1083 | { |
| 1084 | if (BV (clib_bihash_is_free) (&v->kvp[k])) |
| 1085 | { |
| 1086 | if (verbose > 1) |
| 1087 | s = format (s, " %d: empty\n", |
| 1088 | j * BIHASH_KVP_PER_PAGE + k); |
| 1089 | continue; |
| 1090 | } |
| 1091 | if (verbose) |
| 1092 | { |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 1093 | if (h->kvp_fmt_fn) |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 1094 | { |
| 1095 | s = format (s, " %d: %U\n", |
| 1096 | j * BIHASH_KVP_PER_PAGE + k, |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 1097 | h->kvp_fmt_fn, &(v->kvp[k]), verbose); |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 1098 | } |
| 1099 | else |
| 1100 | { |
| 1101 | s = format (s, " %d: %U\n", |
| 1102 | j * BIHASH_KVP_PER_PAGE + k, |
| 1103 | BV (format_bihash_kvp), &(v->kvp[k])); |
| 1104 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1105 | } |
| 1106 | active_elements++; |
| 1107 | } |
| 1108 | v++; |
| 1109 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 1112 | s = format (s, " %lld active elements %lld active buckets\n", |
| 1113 | active_elements, active_buckets); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1114 | s = format (s, " %d free lists\n", vec_len (h->freelists)); |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 1115 | |
| 1116 | for (i = 0; i < vec_len (h->freelists); i++) |
| 1117 | { |
| 1118 | u32 nfree = 0; |
| 1119 | BVT (clib_bihash_value) * free_elt; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 1120 | u64 free_elt_as_u64 = h->freelists[i]; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 1121 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 1122 | while (free_elt_as_u64) |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 1123 | { |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 1124 | free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64); |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 1125 | nfree++; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 1126 | free_elt_as_u64 = free_elt->next_free_as_u64; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 1127 | } |
| 1128 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 1129 | if (nfree || verbose) |
| 1130 | s = format (s, " [len %d] %u free elts\n", 1 << i, nfree); |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | s = format (s, " %lld linear search buckets\n", linear_buckets); |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 1134 | if (BIHASH_USE_HEAP) |
| 1135 | { |
| 1136 | BVT (clib_bihash_alloc_chunk) * c = h->chunks; |
| 1137 | uword bytes_left = 0, total_size = 0, n_chunks = 0; |
| 1138 | |
| 1139 | while (c) |
| 1140 | { |
| 1141 | bytes_left += c->bytes_left; |
| 1142 | total_size += c->size; |
| 1143 | n_chunks += 1; |
| 1144 | c = c->next; |
| 1145 | } |
| 1146 | s = format (s, |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 1147 | " heap: %u chunk(s) allocated\n" |
| 1148 | " bytes: used %U, scrap %U\n", n_chunks, |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 1149 | format_memory_size, total_size, |
| 1150 | format_memory_size, bytes_left); |
| 1151 | } |
| 1152 | else |
| 1153 | { |
| 1154 | u64 used_bytes = alloc_arena_next (h); |
| 1155 | s = format (s, |
| 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); |
| 1161 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1162 | return s; |
| 1163 | } |
| 1164 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1165 | void BV (clib_bihash_foreach_key_value_pair) |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 1166 | (BVT (clib_bihash) * h, |
| 1167 | BV (clib_bihash_foreach_key_value_pair_cb) cb, void *arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1168 | { |
| 1169 | int i, j, k; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 1170 | BVT (clib_bihash_bucket) * b; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1171 | BVT (clib_bihash_value) * v; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1172 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1173 | |
| 1174 | #if BIHASH_LAZY_INSTANTIATE |
Nathan Skrzypczak | 42b29ba | 2020-08-17 14:14:56 +0200 | [diff] [blame] | 1175 | if (PREDICT_FALSE (h->instantiated == 0)) |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 1176 | return; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1177 | #endif |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 1178 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1179 | for (i = 0; i < h->nbuckets; i++) |
| 1180 | { |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 1181 | b = BV (clib_bihash_get_bucket) (h, i); |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 1182 | if (BV (clib_bihash_bucket_is_empty) (b)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1183 | continue; |
| 1184 | |
| 1185 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 1186 | for (j = 0; j < (1 << b->log2_pages); j++) |
| 1187 | { |
| 1188 | for (k = 0; k < BIHASH_KVP_PER_PAGE; k++) |
| 1189 | { |
| 1190 | if (BV (clib_bihash_is_free) (&v->kvp[k])) |
| 1191 | continue; |
| 1192 | |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 1193 | if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg)) |
| 1194 | return; |
Dave Barach | ca45ee7 | 2018-08-06 08:43:47 -0400 | [diff] [blame] | 1195 | /* |
| 1196 | * In case the callback deletes the last entry in the bucket... |
| 1197 | */ |
| 1198 | if (BV (clib_bihash_bucket_is_empty) (b)) |
| 1199 | goto doublebreak; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1200 | } |
| 1201 | v++; |
| 1202 | } |
Dave Barach | ca45ee7 | 2018-08-06 08:43:47 -0400 | [diff] [blame] | 1203 | doublebreak: |
| 1204 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
Dave Barach | dd3a57f | 2016-07-27 16:58:51 -0400 | [diff] [blame] | 1207 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 1208 | /** @endcond */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 1209 | |
| 1210 | /* |
| 1211 | * fd.io coding-style-patch-verification: ON |
| 1212 | * |
| 1213 | * Local Variables: |
| 1214 | * eval: (c-set-style "gnu") |
| 1215 | * End: |
| 1216 | */ |