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