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