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