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