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