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