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