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