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