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