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 | #include <vppinfra/time.h> |
| 16 | #include <vppinfra/cache.h> |
| 17 | #include <vppinfra/error.h> |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 18 | #include <sys/resource.h> |
| 19 | #include <stdio.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | |
| 21 | #include <vppinfra/bihash_8_8.h> |
| 22 | #include <vppinfra/bihash_template.h> |
| 23 | |
| 24 | #include <vppinfra/bihash_template.c> |
| 25 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 26 | typedef struct |
| 27 | { |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 28 | u64 seed; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 29 | u32 nbuckets; |
| 30 | u32 nitems; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 31 | u32 ncycles; |
| 32 | u32 report_every_n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | u32 search_iter; |
| 34 | int careful_delete_tests; |
| 35 | int verbose; |
| 36 | int non_random_keys; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 37 | uword *key_hash; |
| 38 | u64 *keys; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 39 | uword hash_memory_size; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 40 | BVT (clib_bihash) hash; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | clib_time_t clib_time; |
| 42 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 43 | unformat_input_t *input; |
| 44 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | } test_main_t; |
| 46 | |
| 47 | test_main_t test_main; |
| 48 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 49 | uword |
| 50 | vl (void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 52 | return vec_len (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | static clib_error_t * |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 56 | test_bihash_vec64 (test_main_t * tm) |
| 57 | { |
| 58 | u32 user_buckets = 1228800; |
| 59 | u32 user_memory_size = 209715200; |
| 60 | BVT (clib_bihash_kv) kv; |
| 61 | int i, j; |
| 62 | f64 before; |
| 63 | f64 *cum_times = 0; |
| 64 | BVT (clib_bihash) * h; |
| 65 | |
| 66 | h = &tm->hash; |
| 67 | |
| 68 | BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size); |
| 69 | |
| 70 | before = clib_time_now (&tm->clib_time); |
| 71 | |
| 72 | for (j = 0; j < 10; j++) |
| 73 | { |
| 74 | for (i = 1; i <= j * 1000 + 1; i++) |
| 75 | { |
| 76 | kv.key = i; |
| 77 | kv.value = 1; |
| 78 | |
| 79 | BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ ); |
| 80 | } |
| 81 | |
| 82 | vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before); |
| 83 | } |
| 84 | |
| 85 | for (j = 0; j < vec_len (cum_times); j++) |
| 86 | fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000, |
| 87 | cum_times[j] * 1e6); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static clib_error_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 93 | test_bihash (test_main_t * tm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | { |
| 95 | int i, j; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 96 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 97 | uword total_searches; |
| 98 | f64 before, delta; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 99 | BVT (clib_bihash) * h; |
| 100 | BVT (clib_bihash_kv) kv; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 101 | u32 acycle; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | |
| 103 | h = &tm->hash; |
| 104 | |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 105 | BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 107 | for (acycle = 0; acycle < tm->ncycles; acycle++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 108 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 109 | if ((acycle % tm->report_every_n) == 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 110 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 111 | fformat (stdout, "Cycle %lld out of %lld...\n", |
| 112 | acycle, tm->ncycles); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 114 | fformat (stdout, "Pick %lld unique %s keys...\n", |
| 115 | tm->nitems, tm->non_random_keys ? "non-random" : "random"); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 116 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 118 | for (i = 0; i < tm->nitems; i++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 119 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 120 | u64 rndkey; |
| 121 | |
| 122 | if (tm->non_random_keys == 0) |
| 123 | { |
| 124 | |
| 125 | again: |
| 126 | rndkey = random_u64 (&tm->seed); |
| 127 | |
| 128 | p = hash_get (tm->key_hash, rndkey); |
| 129 | if (p) |
| 130 | goto again; |
| 131 | } |
| 132 | else |
| 133 | rndkey = (u64) (i + 1) << 16; |
| 134 | rndkey += acycle; |
| 135 | |
| 136 | hash_set (tm->key_hash, rndkey, i + 1); |
| 137 | vec_add1 (tm->keys, rndkey); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 138 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 141 | if ((acycle % tm->report_every_n) == 0) |
| 142 | fformat (stdout, "Add items...\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | for (i = 0; i < tm->nitems; i++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 145 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 146 | kv.key = tm->keys[i]; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 147 | kv.value = i + 1; |
| 148 | |
| 149 | BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ ); |
| 150 | |
| 151 | if (tm->verbose > 1) |
| 152 | { |
| 153 | fformat (stdout, "--------------------\n"); |
| 154 | fformat (stdout, "After adding key %llu value %lld...\n", |
| 155 | tm->keys[i], (u64) (i + 1)); |
| 156 | fformat (stdout, "%U", BV (format_bihash), h, |
| 157 | 2 /* very verbose */ ); |
| 158 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 159 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 161 | if ((acycle % tm->report_every_n) == 0) |
| 162 | { |
| 163 | fformat (stdout, "%U", BV (format_bihash), h, |
| 164 | 0 /* very verbose */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 166 | fformat (stdout, "Search for items %d times...\n", tm->search_iter); |
| 167 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 168 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 169 | before = clib_time_now (&tm->clib_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 171 | for (j = 0; j < tm->search_iter; j++) |
| 172 | { |
| 173 | for (i = 0; i < tm->nitems; i++) |
| 174 | { |
| 175 | kv.key = tm->keys[i]; |
| 176 | if (BV (clib_bihash_search) (h, &kv, &kv) < 0) |
| 177 | if (BV (clib_bihash_search) (h, &kv, &kv) < 0) |
| 178 | clib_warning |
| 179 | ("[%d] search for key %lld failed unexpectedly\n", i, |
| 180 | tm->keys[i]); |
| 181 | if (kv.value != (u64) (i + 1)) |
| 182 | clib_warning |
| 183 | ("[%d] search for key %lld returned %lld, not %lld\n", i, |
| 184 | tm->keys, kv.value, (u64) (i + 1)); |
| 185 | } |
| 186 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 188 | if ((acycle % tm->report_every_n) == 0) |
| 189 | { |
| 190 | delta = clib_time_now (&tm->clib_time) - before; |
| 191 | total_searches = (uword) tm->search_iter * (uword) tm->nitems; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 192 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 193 | if (delta > 0) |
| 194 | fformat (stdout, "%.f searches per second\n", |
| 195 | ((f64) total_searches) / delta); |
| 196 | |
| 197 | fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, |
| 198 | delta); |
| 199 | |
| 200 | fformat (stdout, "Standard E-hash search for items %d times...\n", |
| 201 | tm->search_iter); |
| 202 | } |
| 203 | |
| 204 | before = clib_time_now (&tm->clib_time); |
| 205 | |
| 206 | for (j = 0; j < tm->search_iter; j++) |
| 207 | { |
| 208 | for (i = 0; i < tm->nitems; i++) |
| 209 | { |
| 210 | p = hash_get (tm->key_hash, tm->keys[i]); |
| 211 | if (p == 0 || p[0] != (uword) (i + 1)) |
| 212 | clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | delta = clib_time_now (&tm->clib_time) - before; |
| 217 | total_searches = (uword) tm->search_iter * (uword) tm->nitems; |
| 218 | |
| 219 | if ((acycle % tm->report_every_n) == 0) |
| 220 | { |
| 221 | fformat (stdout, "%lld searches in %.6f seconds\n", |
| 222 | total_searches, delta); |
| 223 | |
| 224 | if (delta > 0) |
| 225 | fformat (stdout, "%.f searches per second\n", |
| 226 | ((f64) total_searches) / delta); |
| 227 | fformat (stdout, "Delete items...\n"); |
| 228 | } |
| 229 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | for (i = 0; i < tm->nitems; i++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 231 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 232 | int j; |
| 233 | int rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 235 | kv.key = tm->keys[i]; |
| 236 | kv.value = (u64) (i + 1); |
| 237 | rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 239 | if (rv < 0) |
| 240 | clib_warning ("delete key %lld not ok but should be", |
| 241 | tm->keys[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 243 | if (tm->careful_delete_tests) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 244 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 245 | for (j = 0; j < tm->nitems; j++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 246 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 247 | kv.key = tm->keys[j]; |
| 248 | rv = BV (clib_bihash_search) (h, &kv, &kv); |
| 249 | if (j <= i && rv >= 0) |
| 250 | { |
| 251 | clib_warning |
| 252 | ("i %d j %d search ok but should not be, value %lld", |
| 253 | i, j, kv.value); |
| 254 | } |
| 255 | if (j > i && rv < 0) |
| 256 | { |
| 257 | clib_warning ("i %d j %d search not ok but should be", |
| 258 | i, j); |
| 259 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | } |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 263 | if ((acycle % tm->report_every_n) == 0) |
| 264 | { |
| 265 | struct rusage r_usage; |
| 266 | getrusage (RUSAGE_SELF, &r_usage); |
| 267 | fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss); |
| 268 | fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ ); |
| 269 | } |
| 270 | |
| 271 | /* Clean up side-bet hash table and random key vector */ |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 272 | hash_free (tm->key_hash); |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 273 | vec_reset_length (tm->keys); |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 274 | /* Recreate hash table if we're going to need it again */ |
| 275 | if (acycle != (tm->ncycles - 1)) |
| 276 | tm->key_hash = hash_create (tm->nitems, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 279 | fformat (stdout, "End of run, should be empty...\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 281 | fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 285 | clib_error_t * |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 286 | test_bihash_cache (test_main_t * tm) |
| 287 | { |
| 288 | u32 lru; |
| 289 | BVT (clib_bihash_bucket) _b, *b = &_b; |
| 290 | |
| 291 | BV (clib_bihash_reset_cache) (b); |
| 292 | |
| 293 | fformat (stdout, "Initial LRU config: %U\n", BV (format_bihash_lru), b); |
| 294 | |
| 295 | BV (clib_bihash_update_lru_not_inline) (b, 3); |
| 296 | |
| 297 | fformat (stdout, "use slot 3, LRU config: %U\n", BV (format_bihash_lru), b); |
| 298 | |
| 299 | BV (clib_bihash_update_lru) (b, 1); |
| 300 | |
| 301 | fformat (stdout, "use slot 1 LRU config: %U\n", BV (format_bihash_lru), b); |
| 302 | |
| 303 | lru = BV (clib_bihash_get_lru) (b); |
| 304 | |
| 305 | fformat (stdout, "least-recently-used is %d\n", lru); |
| 306 | |
| 307 | BV (clib_bihash_update_lru) (b, 4); |
| 308 | |
| 309 | fformat (stdout, "use slot 4 LRU config: %U\n", BV (format_bihash_lru), b); |
| 310 | |
| 311 | lru = BV (clib_bihash_get_lru) (b); |
| 312 | |
| 313 | fformat (stdout, "least-recently-used is %d\n", lru); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | test_bihash_main (test_main_t * tm) |
| 320 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 321 | unformat_input_t *i = tm->input; |
| 322 | clib_error_t *error; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 323 | int which = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 324 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 325 | tm->report_every_n = 1; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 326 | tm->hash_memory_size = 4095ULL << 20; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 327 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) |
| 329 | { |
| 330 | if (unformat (i, "seed %u", &tm->seed)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 331 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | |
| 333 | else if (unformat (i, "nbuckets %d", &tm->nbuckets)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 334 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | else if (unformat (i, "non-random-keys")) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 336 | tm->non_random_keys = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | else if (unformat (i, "nitems %d", &tm->nitems)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 338 | ; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 339 | else if (unformat (i, "ncycles %d", &tm->ncycles)) |
| 340 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | else if (unformat (i, "careful %d", &tm->careful_delete_tests)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 342 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | else if (unformat (i, "verbose %d", &tm->verbose)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 344 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 345 | else if (unformat (i, "search %d", &tm->search_iter)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 346 | ; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 347 | else if (unformat (i, "report-every %d", &tm->report_every_n)) |
| 348 | ; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 349 | else if (unformat (i, "memory-size %U", |
| 350 | unformat_memory_size, &tm->hash_memory_size)) |
| 351 | ; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 352 | else if (unformat (i, "vec64")) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 353 | which = 1; |
| 354 | else if (unformat (i, "cache")) |
| 355 | which = 2; |
| 356 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 357 | else if (unformat (i, "verbose")) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 358 | tm->verbose = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 359 | else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 360 | return clib_error_return (0, "unknown input '%U'", |
| 361 | format_unformat_error, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 363 | |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 364 | /* Preallocate hash table, key vector */ |
| 365 | tm->key_hash = hash_create (tm->nitems, sizeof (uword)); |
| 366 | vec_validate (tm->keys, tm->nitems - 1); |
| 367 | _vec_len (tm->keys) = 0; |
| 368 | |
| 369 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 370 | switch (which) |
| 371 | { |
| 372 | case 0: |
| 373 | error = test_bihash (tm); |
| 374 | break; |
| 375 | |
| 376 | case 1: |
| 377 | error = test_bihash_vec64 (tm); |
| 378 | break; |
| 379 | |
| 380 | case 2: |
| 381 | error = test_bihash_cache (tm); |
| 382 | break; |
| 383 | |
| 384 | default: |
| 385 | return clib_error_return (0, "no such test?"); |
| 386 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 387 | |
| 388 | return error; |
| 389 | } |
| 390 | |
| 391 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 392 | int |
| 393 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | { |
| 395 | unformat_input_t i; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 396 | clib_error_t *error; |
| 397 | test_main_t *tm = &test_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 398 | |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 399 | clib_mem_init (0, 4095ULL << 20); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | |
| 401 | tm->input = &i; |
| 402 | tm->seed = 0xdeaddabe; |
| 403 | |
| 404 | tm->nbuckets = 2; |
| 405 | tm->nitems = 5; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 406 | tm->ncycles = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | tm->verbose = 1; |
| 408 | tm->search_iter = 1; |
| 409 | tm->careful_delete_tests = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 410 | clib_time_init (&tm->clib_time); |
| 411 | |
| 412 | unformat_init_command_line (&i, argv); |
| 413 | error = test_bihash_main (tm); |
| 414 | unformat_free (&i); |
| 415 | |
| 416 | if (error) |
| 417 | { |
| 418 | clib_error_report (error); |
| 419 | return 1; |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | #endif /* CLIB_UNIX */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * fd.io coding-style-patch-verification: ON |
| 427 | * |
| 428 | * Local Variables: |
| 429 | * eval: (c-set-style "gnu") |
| 430 | * End: |
| 431 | */ |