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> |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 20 | #include <pthread.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 21 | |
| 22 | #include <vppinfra/bihash_8_8.h> |
| 23 | #include <vppinfra/bihash_template.h> |
| 24 | |
| 25 | #include <vppinfra/bihash_template.c> |
| 26 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 27 | typedef struct |
| 28 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 29 | volatile u32 thread_barrier; |
| 30 | volatile u32 threads_running; |
| 31 | volatile u64 sequence_number; |
Filip Tehlar | 397fd7d | 2016-10-26 14:31:24 +0200 | [diff] [blame] | 32 | u64 seed; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 33 | u32 nbuckets; |
| 34 | u32 nitems; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 35 | u32 ncycles; |
| 36 | u32 report_every_n; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | u32 search_iter; |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 38 | u32 noverwritten; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | int careful_delete_tests; |
| 40 | int verbose; |
| 41 | int non_random_keys; |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 42 | u32 nthreads; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 43 | uword *key_hash; |
| 44 | u64 *keys; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 45 | uword hash_memory_size; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 46 | BVT (clib_bihash) hash; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | clib_time_t clib_time; |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 48 | void *global_heap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 50 | unformat_input_t *input; |
| 51 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | } test_main_t; |
| 53 | |
| 54 | test_main_t test_main; |
| 55 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 56 | uword |
| 57 | vl (void *v) |
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 | return vec_len (v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 62 | static clib_error_t * |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 63 | test_bihash_vec64 (test_main_t * tm) |
| 64 | { |
| 65 | u32 user_buckets = 1228800; |
| 66 | u32 user_memory_size = 209715200; |
| 67 | BVT (clib_bihash_kv) kv; |
| 68 | int i, j; |
| 69 | f64 before; |
| 70 | f64 *cum_times = 0; |
| 71 | BVT (clib_bihash) * h; |
| 72 | |
| 73 | h = &tm->hash; |
| 74 | |
| 75 | BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size); |
| 76 | |
| 77 | before = clib_time_now (&tm->clib_time); |
| 78 | |
| 79 | for (j = 0; j < 10; j++) |
| 80 | { |
| 81 | for (i = 1; i <= j * 1000 + 1; i++) |
| 82 | { |
| 83 | kv.key = i; |
| 84 | kv.value = 1; |
| 85 | |
| 86 | BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ ); |
| 87 | } |
| 88 | |
| 89 | vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before); |
| 90 | } |
| 91 | |
| 92 | for (j = 0; j < vec_len (cum_times); j++) |
| 93 | fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000, |
| 94 | cum_times[j] * 1e6); |
| 95 | |
| 96 | return 0; |
| 97 | } |
| 98 | |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 99 | static int |
| 100 | stale_cb (BVT (clib_bihash_kv) * kv, void *ctx) |
| 101 | { |
| 102 | test_main_t *tm = ctx; |
| 103 | |
| 104 | tm->noverwritten++; |
| 105 | |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | static clib_error_t * |
| 110 | test_bihash_stale_overwrite (test_main_t * tm) |
| 111 | { |
| 112 | BVT (clib_bihash) * h; |
| 113 | BVT (clib_bihash_kv) kv; |
| 114 | int i; |
| 115 | tm->noverwritten = 0; |
| 116 | |
| 117 | h = &tm->hash; |
| 118 | |
| 119 | BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size); |
| 120 | |
| 121 | fformat (stdout, "Add %d items to %d buckets\n", tm->nitems, tm->nbuckets); |
| 122 | |
| 123 | for (i = 0; i < tm->nitems; i++) |
| 124 | { |
| 125 | kv.key = i; |
| 126 | kv.value = 1; |
| 127 | |
| 128 | BV (clib_bihash_add_or_overwrite_stale) (h, &kv, stale_cb, tm); |
| 129 | } |
| 130 | |
| 131 | fformat (stdout, "%d items overwritten\n", tm->noverwritten); |
| 132 | fformat (stdout, "%U", BV (format_bihash), h, 0); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 137 | void * |
| 138 | test_bihash_thread_fn (void *arg) |
| 139 | { |
| 140 | BVT (clib_bihash) * h; |
| 141 | BVT (clib_bihash_kv) kv; |
| 142 | test_main_t *tm = &test_main; |
| 143 | |
| 144 | int i, j; |
| 145 | |
| 146 | u32 my_thread_index = (u32) (u64) arg; |
| 147 | __os_thread_index = my_thread_index; |
| 148 | clib_mem_set_per_cpu_heap (tm->global_heap); |
| 149 | |
| 150 | while (tm->thread_barrier) |
| 151 | ; |
| 152 | |
| 153 | h = &tm->hash; |
| 154 | |
| 155 | for (i = 0; i < tm->ncycles; i++) |
| 156 | { |
| 157 | for (j = 0; j < tm->nitems; j++) |
| 158 | { |
| 159 | kv.key = ((u64) my_thread_index << 32) | (u64) j; |
| 160 | kv.value = ((u64) my_thread_index << 32) | (u64) j; |
| 161 | (void) __atomic_add_fetch (&tm->sequence_number, 1, |
| 162 | __ATOMIC_ACQUIRE); |
| 163 | BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ ); |
| 164 | } |
| 165 | for (j = 0; j < tm->nitems; j++) |
| 166 | { |
| 167 | kv.key = ((u64) my_thread_index << 32) | (u64) j; |
| 168 | kv.value = ((u64) my_thread_index << 32) | (u64) j; |
| 169 | (void) __atomic_add_fetch (&tm->sequence_number, 1, |
| 170 | __ATOMIC_ACQUIRE); |
| 171 | BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ ); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | (void) __atomic_sub_fetch (&tm->threads_running, 1, __ATOMIC_ACQUIRE); |
| 176 | while (1) |
| 177 | { |
| 178 | struct timespec ts, tsrem; |
| 179 | ts.tv_sec = 1; |
| 180 | ts.tv_nsec = 0; |
| 181 | |
| 182 | while (nanosleep (&ts, &tsrem) < 0) |
| 183 | ts = tsrem; |
| 184 | } |
| 185 | return (0); /* not so much */ |
| 186 | } |
| 187 | |
| 188 | static clib_error_t * |
| 189 | test_bihash_threads (test_main_t * tm) |
| 190 | { |
| 191 | int i; |
| 192 | pthread_t handle; |
| 193 | BVT (clib_bihash) * h; |
| 194 | int rv; |
| 195 | |
| 196 | h = &tm->hash; |
| 197 | |
| 198 | BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size); |
| 199 | |
| 200 | tm->thread_barrier = 1; |
| 201 | |
| 202 | /* Start the worker threads */ |
| 203 | for (i = 0; i < tm->nthreads; i++) |
| 204 | { |
| 205 | rv = pthread_create (&handle, NULL, test_bihash_thread_fn, |
| 206 | (void *) (u64) i); |
| 207 | if (rv) |
| 208 | { |
| 209 | clib_unix_warning ("pthread_create returned %d", rv); |
| 210 | } |
| 211 | } |
| 212 | tm->threads_running = i; |
| 213 | tm->sequence_number = 0; |
| 214 | CLIB_MEMORY_BARRIER (); |
| 215 | |
| 216 | /* start the workers */ |
| 217 | tm->thread_barrier = 0; |
| 218 | |
| 219 | while (tm->threads_running) |
| 220 | { |
| 221 | struct timespec ts, tsrem; |
| 222 | ts.tv_sec = 0; |
| 223 | ts.tv_nsec = 20 * 1000 * 1000; /* sleep for 20ms at a time */ |
| 224 | |
| 225 | while (nanosleep (&ts, &tsrem) < 0) |
| 226 | ts = tsrem; |
| 227 | } |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 233 | static clib_error_t * |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 234 | test_bihash (test_main_t * tm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | { |
| 236 | int i, j; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 237 | uword *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | uword total_searches; |
| 239 | f64 before, delta; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 240 | BVT (clib_bihash) * h; |
| 241 | BVT (clib_bihash_kv) kv; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 242 | u32 acycle; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | |
| 244 | h = &tm->hash; |
| 245 | |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 246 | BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 248 | for (acycle = 0; acycle < tm->ncycles; acycle++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 250 | if ((acycle % tm->report_every_n) == 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 251 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 252 | fformat (stdout, "Cycle %lld out of %lld...\n", |
| 253 | acycle, tm->ncycles); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 255 | fformat (stdout, "Pick %lld unique %s keys...\n", |
| 256 | tm->nitems, tm->non_random_keys ? "non-random" : "random"); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 257 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 259 | for (i = 0; i < tm->nitems; i++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 260 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 261 | u64 rndkey; |
| 262 | |
| 263 | if (tm->non_random_keys == 0) |
| 264 | { |
| 265 | |
| 266 | again: |
| 267 | rndkey = random_u64 (&tm->seed); |
| 268 | |
| 269 | p = hash_get (tm->key_hash, rndkey); |
| 270 | if (p) |
| 271 | goto again; |
| 272 | } |
| 273 | else |
| 274 | rndkey = (u64) (i + 1) << 16; |
| 275 | rndkey += acycle; |
| 276 | |
| 277 | hash_set (tm->key_hash, rndkey, i + 1); |
| 278 | vec_add1 (tm->keys, rndkey); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 279 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 280 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 281 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 282 | if ((acycle % tm->report_every_n) == 0) |
| 283 | fformat (stdout, "Add items...\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | for (i = 0; i < tm->nitems; i++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 286 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 287 | kv.key = tm->keys[i]; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 288 | kv.value = i + 1; |
| 289 | |
| 290 | BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ ); |
| 291 | |
| 292 | if (tm->verbose > 1) |
| 293 | { |
| 294 | fformat (stdout, "--------------------\n"); |
| 295 | fformat (stdout, "After adding key %llu value %lld...\n", |
| 296 | tm->keys[i], (u64) (i + 1)); |
| 297 | fformat (stdout, "%U", BV (format_bihash), h, |
| 298 | 2 /* very verbose */ ); |
| 299 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 300 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 302 | if ((acycle % tm->report_every_n) == 0) |
| 303 | { |
| 304 | fformat (stdout, "%U", BV (format_bihash), h, |
| 305 | 0 /* very verbose */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 306 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 307 | fformat (stdout, "Search for items %d times...\n", tm->search_iter); |
| 308 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 310 | before = clib_time_now (&tm->clib_time); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 311 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 312 | for (j = 0; j < tm->search_iter; j++) |
| 313 | { |
| 314 | for (i = 0; i < tm->nitems; i++) |
| 315 | { |
| 316 | kv.key = tm->keys[i]; |
| 317 | if (BV (clib_bihash_search) (h, &kv, &kv) < 0) |
| 318 | if (BV (clib_bihash_search) (h, &kv, &kv) < 0) |
| 319 | clib_warning |
| 320 | ("[%d] search for key %lld failed unexpectedly\n", i, |
| 321 | tm->keys[i]); |
| 322 | if (kv.value != (u64) (i + 1)) |
| 323 | clib_warning |
| 324 | ("[%d] search for key %lld returned %lld, not %lld\n", i, |
| 325 | tm->keys, kv.value, (u64) (i + 1)); |
| 326 | } |
| 327 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 329 | if ((acycle % tm->report_every_n) == 0) |
| 330 | { |
| 331 | delta = clib_time_now (&tm->clib_time) - before; |
| 332 | total_searches = (uword) tm->search_iter * (uword) tm->nitems; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 333 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 334 | if (delta > 0) |
| 335 | fformat (stdout, "%.f searches per second\n", |
| 336 | ((f64) total_searches) / delta); |
| 337 | |
| 338 | fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, |
| 339 | delta); |
| 340 | |
| 341 | fformat (stdout, "Standard E-hash search for items %d times...\n", |
| 342 | tm->search_iter); |
| 343 | } |
| 344 | |
| 345 | before = clib_time_now (&tm->clib_time); |
| 346 | |
| 347 | for (j = 0; j < tm->search_iter; j++) |
| 348 | { |
| 349 | for (i = 0; i < tm->nitems; i++) |
| 350 | { |
| 351 | p = hash_get (tm->key_hash, tm->keys[i]); |
| 352 | if (p == 0 || p[0] != (uword) (i + 1)) |
| 353 | clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | delta = clib_time_now (&tm->clib_time) - before; |
| 358 | total_searches = (uword) tm->search_iter * (uword) tm->nitems; |
| 359 | |
| 360 | if ((acycle % tm->report_every_n) == 0) |
| 361 | { |
| 362 | fformat (stdout, "%lld searches in %.6f seconds\n", |
| 363 | total_searches, delta); |
| 364 | |
| 365 | if (delta > 0) |
| 366 | fformat (stdout, "%.f searches per second\n", |
| 367 | ((f64) total_searches) / delta); |
| 368 | fformat (stdout, "Delete items...\n"); |
| 369 | } |
| 370 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 371 | for (i = 0; i < tm->nitems; i++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 372 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 373 | int j; |
| 374 | int rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 376 | kv.key = tm->keys[i]; |
| 377 | kv.value = (u64) (i + 1); |
| 378 | rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 379 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 380 | if (rv < 0) |
| 381 | clib_warning ("delete key %lld not ok but should be", |
| 382 | tm->keys[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 384 | if (tm->careful_delete_tests) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 385 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 386 | for (j = 0; j < tm->nitems; j++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 387 | { |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 388 | kv.key = tm->keys[j]; |
| 389 | rv = BV (clib_bihash_search) (h, &kv, &kv); |
| 390 | if (j <= i && rv >= 0) |
| 391 | { |
| 392 | clib_warning |
| 393 | ("i %d j %d search ok but should not be, value %lld", |
| 394 | i, j, kv.value); |
| 395 | } |
| 396 | if (j > i && rv < 0) |
| 397 | { |
| 398 | clib_warning ("i %d j %d search not ok but should be", |
| 399 | i, j); |
| 400 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 404 | if ((acycle % tm->report_every_n) == 0) |
| 405 | { |
| 406 | struct rusage r_usage; |
| 407 | getrusage (RUSAGE_SELF, &r_usage); |
| 408 | fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss); |
| 409 | fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ ); |
| 410 | } |
| 411 | |
| 412 | /* Clean up side-bet hash table and random key vector */ |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 413 | hash_free (tm->key_hash); |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 414 | vec_reset_length (tm->keys); |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 415 | /* Recreate hash table if we're going to need it again */ |
| 416 | if (acycle != (tm->ncycles - 1)) |
| 417 | tm->key_hash = hash_create (tm->nitems, sizeof (uword)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 420 | fformat (stdout, "End of run, should be empty...\n"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 422 | fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 423 | return 0; |
| 424 | } |
| 425 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 426 | clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | test_bihash_main (test_main_t * tm) |
| 428 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 429 | unformat_input_t *i = tm->input; |
| 430 | clib_error_t *error; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 431 | int which = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 433 | tm->report_every_n = 1; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 434 | tm->hash_memory_size = 4095ULL << 20; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 435 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 436 | while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) |
| 437 | { |
| 438 | if (unformat (i, "seed %u", &tm->seed)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 439 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 440 | |
| 441 | else if (unformat (i, "nbuckets %d", &tm->nbuckets)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 442 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 443 | else if (unformat (i, "non-random-keys")) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 444 | tm->non_random_keys = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | else if (unformat (i, "nitems %d", &tm->nitems)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 446 | ; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 447 | else if (unformat (i, "ncycles %d", &tm->ncycles)) |
| 448 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | else if (unformat (i, "careful %d", &tm->careful_delete_tests)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 450 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | else if (unformat (i, "verbose %d", &tm->verbose)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 452 | ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | else if (unformat (i, "search %d", &tm->search_iter)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 454 | ; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 455 | else if (unformat (i, "report-every %d", &tm->report_every_n)) |
| 456 | ; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 457 | else if (unformat (i, "memory-size %U", |
| 458 | unformat_memory_size, &tm->hash_memory_size)) |
| 459 | ; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 460 | else if (unformat (i, "vec64")) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 461 | which = 1; |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 462 | else if (unformat (i, "threads %u", &tm->nthreads)) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 463 | which = 2; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 464 | else if (unformat (i, "verbose")) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 465 | tm->verbose = 1; |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 466 | else if (unformat (i, "stale-overwrite")) |
| 467 | which = 3; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 468 | else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 469 | return clib_error_return (0, "unknown input '%U'", |
| 470 | format_unformat_error, i); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 471 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 472 | |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 473 | /* Preallocate hash table, key vector */ |
| 474 | tm->key_hash = hash_create (tm->nitems, sizeof (uword)); |
| 475 | vec_validate (tm->keys, tm->nitems - 1); |
| 476 | _vec_len (tm->keys) = 0; |
| 477 | |
| 478 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 479 | switch (which) |
| 480 | { |
| 481 | case 0: |
| 482 | error = test_bihash (tm); |
| 483 | break; |
| 484 | |
| 485 | case 1: |
| 486 | error = test_bihash_vec64 (tm); |
| 487 | break; |
| 488 | |
| 489 | case 2: |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 490 | error = test_bihash_threads (tm); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 491 | break; |
| 492 | |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 493 | case 3: |
| 494 | error = test_bihash_stale_overwrite (tm); |
| 495 | break; |
| 496 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 497 | default: |
| 498 | return clib_error_return (0, "no such test?"); |
| 499 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | |
| 501 | return error; |
| 502 | } |
| 503 | |
| 504 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 505 | int |
| 506 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 507 | { |
| 508 | unformat_input_t i; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 509 | clib_error_t *error; |
| 510 | test_main_t *tm = &test_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 512 | clib_mem_init (0, 4095ULL << 20); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 513 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 514 | tm->global_heap = clib_mem_get_per_cpu_heap (); |
| 515 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 516 | tm->input = &i; |
| 517 | tm->seed = 0xdeaddabe; |
| 518 | |
| 519 | tm->nbuckets = 2; |
| 520 | tm->nitems = 5; |
Dave Barach | e7d212f | 2018-02-07 13:14:06 -0500 | [diff] [blame] | 521 | tm->ncycles = 1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 522 | tm->verbose = 1; |
| 523 | tm->search_iter = 1; |
| 524 | tm->careful_delete_tests = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 525 | clib_time_init (&tm->clib_time); |
| 526 | |
| 527 | unformat_init_command_line (&i, argv); |
| 528 | error = test_bihash_main (tm); |
| 529 | unformat_free (&i); |
| 530 | |
| 531 | if (error) |
| 532 | { |
| 533 | clib_error_report (error); |
| 534 | return 1; |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | #endif /* CLIB_UNIX */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 539 | |
| 540 | /* |
| 541 | * fd.io coding-style-patch-verification: ON |
| 542 | * |
| 543 | * Local Variables: |
| 544 | * eval: (c-set-style "gnu") |
| 545 | * End: |
| 546 | */ |