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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vlib/vlib.h>
16 #include <vppinfra/time.h>
17 #include <vppinfra/cache.h>
18 #include <vppinfra/error.h>
19 #include <sys/resource.h>
23 #include <vppinfra/bihash_8_8.h>
24 #include <vppinfra/bihash_template.h>
26 #include <vppinfra/bihash_template.c>
30 volatile u32 thread_barrier;
31 volatile u32 threads_running;
32 volatile u64 sequence_number;
39 int careful_delete_tests;
45 uword hash_memory_size;
46 BVT (clib_bihash) hash;
47 clib_time_t clib_time;
50 unformat_input_t *input;
53 vlib_main_t *vlib_main;
56 static bihash_test_main_t bihash_test_main;
59 test_bihash_vec64 (bihash_test_main_t * tm)
61 u32 user_buckets = 1228800;
62 u32 user_memory_size = 209715200;
63 BVT (clib_bihash_kv) kv;
67 BVT (clib_bihash) * h;
71 BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
73 before = clib_time_now (&tm->clib_time);
75 for (j = 0; j < 10; j++)
77 for (i = 1; i <= j * 1000 + 1; i++)
82 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
85 vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
88 for (j = 0; j < vec_len (cum_times); j++)
89 fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
96 test_bihash_thread_fn (void *arg)
98 BVT (clib_bihash) * h;
99 BVT (clib_bihash_kv) kv;
100 bihash_test_main_t *tm = &bihash_test_main;
102 u32 my_thread_index = (u32) (u64) arg;
104 while (tm->thread_barrier)
109 for (i = 0; i < tm->ncycles; i++)
111 for (j = 0; j < tm->nitems; j++)
113 kv.key = ((u64) my_thread_index << 32) | (u64) j;
114 kv.value = ((u64) my_thread_index << 32) | (u64) j;
115 (void) __atomic_add_fetch (&tm->sequence_number, 1,
117 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
119 for (j = 0; j < tm->nitems; j++)
121 kv.key = ((u64) my_thread_index << 32) | (u64) j;
122 kv.value = ((u64) my_thread_index << 32) | (u64) j;
123 (void) __atomic_add_fetch (&tm->sequence_number, 1,
125 BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
129 (void) __atomic_sub_fetch (&tm->threads_running, 1, __ATOMIC_ACQUIRE);
131 return (0); /* not so much */
134 static clib_error_t *
135 test_bihash_threads (bihash_test_main_t * tm)
139 BVT (clib_bihash) * h;
141 f64 before, after, delta;
145 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
147 tm->thread_barrier = 1;
149 /* Start the worker threads */
150 for (i = 0; i < tm->nthreads; i++)
152 rv = pthread_create (&handle, NULL, test_bihash_thread_fn,
156 clib_unix_warning ("pthread_create returned %d", rv);
159 tm->threads_running = i;
160 tm->sequence_number = 0;
161 CLIB_MEMORY_BARRIER ();
163 /* start the workers */
164 before = vlib_time_now (tm->vlib_main);
165 tm->thread_barrier = 0;
167 while (tm->threads_running > 0)
170 after = vlib_time_now (tm->vlib_main);
171 delta = after - before;
173 clib_warning ("%lld items in %.2f seconds, %.2f items/sec",
174 (u64) tm->nthreads * (u64) tm->nitems, delta,
176 0.0 ? ((f64) ((u64) tm->nthreads * (u64) tm->nitems)) /
179 BV (clib_bihash_free) (h);
184 * Callback to blow up spectacularly if anthing remains in the table
187 count_items (BVT (clib_bihash_kv) * kvp, void *notused)
189 _clib_error (CLIB_ERROR_ABORT, 0, 0,
190 "bihash test FAILED, items left in table!");
193 static clib_error_t *
194 test_bihash (bihash_test_main_t * tm)
198 uword total_searches;
200 BVT (clib_bihash) * h;
201 BVT (clib_bihash_kv) kv;
206 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
208 for (acycle = 0; acycle < tm->ncycles; acycle++)
210 if ((acycle % tm->report_every_n) == 0)
212 fformat (stdout, "Cycle %lld out of %lld...\n",
213 acycle, tm->ncycles);
215 fformat (stdout, "Pick %lld unique %s keys...\n",
216 tm->nitems, tm->non_random_keys ? "non-random" : "random");
219 for (i = 0; i < tm->nitems; i++)
223 if (tm->non_random_keys == 0)
227 rndkey = random_u64 (&tm->seed);
229 p = hash_get (tm->key_hash, rndkey);
234 rndkey = (u64) (i + 1) << 16;
237 hash_set (tm->key_hash, rndkey, i + 1);
238 vec_add1 (tm->keys, rndkey);
242 if ((acycle % tm->report_every_n) == 0)
243 fformat (stdout, "Add items...\n");
245 for (i = 0; i < tm->nitems; i++)
247 kv.key = tm->keys[i];
250 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
254 fformat (stdout, "--------------------\n");
255 fformat (stdout, "After adding key %llu value %lld...\n",
256 tm->keys[i], (u64) (i + 1));
257 fformat (stdout, "%U", BV (format_bihash), h,
258 2 /* very verbose */ );
262 if ((acycle % tm->report_every_n) == 0)
264 fformat (stdout, "%U", BV (format_bihash), h,
265 0 /* very verbose */ );
267 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
270 before = clib_time_now (&tm->clib_time);
272 for (j = 0; j < tm->search_iter; j++)
274 for (i = 0; i < tm->nitems; i++)
276 kv.key = tm->keys[i];
277 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
278 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
280 ("[%d] search for key %lld failed unexpectedly\n", i,
282 if (kv.value != (u64) (i + 1))
284 ("[%d] search for key %lld returned %lld, not %lld\n", i,
285 tm->keys, kv.value, (u64) (i + 1));
289 if ((acycle % tm->report_every_n) == 0)
291 delta = clib_time_now (&tm->clib_time) - before;
292 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
295 fformat (stdout, "%.f searches per second\n",
296 ((f64) total_searches) / delta);
298 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
301 fformat (stdout, "Standard E-hash search for items %d times...\n",
305 before = clib_time_now (&tm->clib_time);
307 for (j = 0; j < tm->search_iter; j++)
309 for (i = 0; i < tm->nitems; i++)
311 p = hash_get (tm->key_hash, tm->keys[i]);
312 if (p == 0 || p[0] != (uword) (i + 1))
313 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
317 delta = clib_time_now (&tm->clib_time) - before;
318 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
320 if ((acycle % tm->report_every_n) == 0)
322 fformat (stdout, "%lld searches in %.6f seconds\n",
323 total_searches, delta);
326 fformat (stdout, "%.f searches per second\n",
327 ((f64) total_searches) / delta);
328 fformat (stdout, "Delete items...\n");
331 for (i = 0; i < tm->nitems; i++)
336 kv.key = tm->keys[i];
337 kv.value = (u64) (i + 1);
338 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
341 clib_warning ("delete key %lld not ok but should be",
344 if (tm->careful_delete_tests)
346 for (j = 0; j < tm->nitems; j++)
348 kv.key = tm->keys[j];
349 rv = BV (clib_bihash_search) (h, &kv, &kv);
350 if (j <= i && rv >= 0)
353 ("i %d j %d search ok but should not be, value %lld",
358 clib_warning ("i %d j %d search not ok but should be",
364 if ((acycle % tm->report_every_n) == 0)
366 struct rusage r_usage;
367 getrusage (RUSAGE_SELF, &r_usage);
368 fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss);
369 fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ );
372 /* Clean up side-bet hash table and random key vector */
373 hash_free (tm->key_hash);
374 vec_reset_length (tm->keys);
375 /* Recreate hash table if we're going to need it again */
376 if (acycle != (tm->ncycles - 1))
377 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
380 fformat (stdout, "End of run, should be empty...\n");
382 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
384 /* ASSERTs if any items remain */
385 BV (clib_bihash_foreach_key_value_pair) (h, count_items, 0);
387 BV (clib_bihash_free) (h);
390 hash_free (tm->key_hash);
395 static clib_error_t *
396 test_bihash_command_fn (vlib_main_t * vm,
397 unformat_input_t * input, vlib_cli_command_t * cmd)
399 bihash_test_main_t *tm = &bihash_test_main;
403 tm->hash_memory_size = 32ULL << 20;
404 tm->nbuckets = 32000;
407 tm->report_every_n = 50000;
408 tm->seed = 0x1badf00d;
410 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
412 if (unformat (input, "seed %u", &tm->seed))
415 else if (unformat (input, "nbuckets %d", &tm->nbuckets))
417 else if (unformat (input, "non-random-keys"))
418 tm->non_random_keys = 1;
419 else if (unformat (input, "nitems %d", &tm->nitems))
421 else if (unformat (input, "ncycles %d", &tm->ncycles))
423 else if (unformat (input, "careful %d", &tm->careful_delete_tests))
425 else if (unformat (input, "verbose %d", &tm->verbose))
427 else if (unformat (input, "search %d", &tm->search_iter))
429 else if (unformat (input, "report-every %d", &tm->report_every_n))
431 else if (unformat (input, "memory-size %U",
432 unformat_memory_size, &tm->hash_memory_size))
434 else if (unformat (input, "vec64"))
436 else if (unformat (input, "threads %u", &tm->nthreads))
438 else if (unformat (input, "verbose"))
441 return clib_error_return (0, "unknown input '%U'",
442 format_unformat_error, input);
445 /* Preallocate hash table, key vector */
446 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
447 vec_validate (tm->keys, tm->nitems - 1);
448 _vec_len (tm->keys) = 0;
453 error = test_bihash (tm);
457 error = test_bihash_vec64 (tm);
461 error = test_bihash_threads (tm);
465 return clib_error_return (0, "no such test?");
472 VLIB_CLI_COMMAND (test_bihash_command, static) =
474 .path = "test bihash",
475 .short_help = "test bihash",
476 .function = test_bihash_command_fn,
480 static clib_error_t *
481 bihash_test_init (vlib_main_t * vm)
483 bihash_test_main_t *tm = &bihash_test_main;
489 VLIB_INIT_FUNCTION (bihash_test_init);
492 * fd.io coding-style-patch-verification: ON
495 * eval: (c-set-style "gnu")