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 <vppinfra/time.h>
16 #include <vppinfra/cache.h>
17 #include <vppinfra/error.h>
18 #include <sys/resource.h>
22 #include <vppinfra/bihash_8_8.h>
23 #include <vppinfra/bihash_template.h>
25 #include <vppinfra/bihash_template.c>
29 volatile u32 thread_barrier;
30 volatile u32 threads_running;
31 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;
54 test_main_t test_main;
63 test_bihash_vec64 (test_main_t * tm)
65 u32 user_buckets = 1228800;
66 u32 user_memory_size = 209715200;
67 BVT (clib_bihash_kv) kv;
71 BVT (clib_bihash) * h;
76 BV (clib_bihash_initiator_init_svm) (h, "test", user_buckets,
77 0x30000000 /* base_addr */ ,
80 BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
83 before = clib_time_now (&tm->clib_time);
85 for (j = 0; j < 10; j++)
87 for (i = 1; i <= j * 1000 + 1; i++)
92 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
95 vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
98 for (j = 0; j < vec_len (cum_times); j++)
99 fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
106 stale_cb (BVT (clib_bihash_kv) * kv, void *ctx)
108 test_main_t *tm = ctx;
115 static clib_error_t *
116 test_bihash_stale_overwrite (test_main_t * tm)
118 BVT (clib_bihash) * h;
119 BVT (clib_bihash_kv) kv;
121 tm->noverwritten = 0;
126 BV (clib_bihash_initiator_init_svm) (h, "test", tm->nbuckets,
127 0x30000000 /* base_addr */ ,
128 tm->hash_memory_size);
130 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
133 fformat (stdout, "Add %d items to %d buckets\n", tm->nitems, tm->nbuckets);
135 for (i = 0; i < tm->nitems; i++)
140 BV (clib_bihash_add_or_overwrite_stale) (h, &kv, stale_cb, tm);
143 fformat (stdout, "%d items overwritten\n", tm->noverwritten);
144 fformat (stdout, "%U", BV (format_bihash), h, 0);
150 test_bihash_thread_fn (void *arg)
152 BVT (clib_bihash) * h;
153 BVT (clib_bihash_kv) kv;
154 test_main_t *tm = &test_main;
158 u32 my_thread_index = (u32) (u64) arg;
159 __os_thread_index = my_thread_index;
160 clib_mem_set_per_cpu_heap (tm->global_heap);
162 while (tm->thread_barrier)
167 for (i = 0; i < tm->ncycles; i++)
169 for (j = 0; j < tm->nitems; j++)
171 kv.key = ((u64) my_thread_index << 32) | (u64) j;
172 kv.value = ((u64) my_thread_index << 32) | (u64) j;
173 (void) __atomic_add_fetch (&tm->sequence_number, 1,
175 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
177 for (j = 0; j < tm->nitems; j++)
179 kv.key = ((u64) my_thread_index << 32) | (u64) j;
180 kv.value = ((u64) my_thread_index << 32) | (u64) j;
181 (void) __atomic_add_fetch (&tm->sequence_number, 1,
183 BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
187 (void) __atomic_sub_fetch (&tm->threads_running, 1, __ATOMIC_ACQUIRE);
190 struct timespec ts, tsrem;
194 while (nanosleep (&ts, &tsrem) < 0)
197 return (0); /* not so much */
200 static clib_error_t *
201 test_bihash_threads (test_main_t * tm)
205 BVT (clib_bihash) * h;
211 BV (clib_bihash_initiator_init_svm) (h, "test", tm->nbuckets,
212 0x30000000 /* base_addr */ ,
213 tm->hash_memory_size);
215 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
218 tm->thread_barrier = 1;
220 /* Start the worker threads */
221 for (i = 0; i < tm->nthreads; i++)
223 rv = pthread_create (&handle, NULL, test_bihash_thread_fn,
227 clib_unix_warning ("pthread_create returned %d", rv);
230 tm->threads_running = i;
231 tm->sequence_number = 0;
232 CLIB_MEMORY_BARRIER ();
234 /* start the workers */
235 tm->thread_barrier = 0;
237 while (tm->threads_running)
239 struct timespec ts, tsrem;
241 ts.tv_nsec = 20 * 1000 * 1000; /* sleep for 20ms at a time */
243 while (nanosleep (&ts, &tsrem) < 0)
250 static clib_error_t *
251 test_bihash_vanilla_overwrite (test_main_t *tm)
254 BVT (clib_bihash) * h;
255 BVT (clib_bihash_kv) kv;
260 BV (clib_bihash_initiator_init_svm)
261 (h, "test", tm->nbuckets, 0x30000000 /* base_addr */, tm->hash_memory_size);
263 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
266 for (i = 0; i < 100; i++)
271 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */);
274 fformat (stdout, "End of run, should one item...\n");
275 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */);
276 BV (clib_bihash_free) (h);
280 static clib_error_t *
281 test_bihash_value_assert (test_main_t *tm)
283 BVT (clib_bihash) * h;
284 BVT (clib_bihash_kv) kv;
289 BV (clib_bihash_initiator_init_svm)
290 (h, "test", tm->nbuckets, 0x30000000 /* base_addr */, tm->hash_memory_size);
292 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
296 kv.value = 0xFEEDFACE8BADF00DULL;
298 fformat (stderr, "The following add should ASSERT...\n");
299 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */);
304 static clib_error_t *
305 test_bihash (test_main_t * tm)
309 uword total_searches;
311 BVT (clib_bihash) * h;
312 BVT (clib_bihash_kv) kv;
318 BV (clib_bihash_initiator_init_svm) (h, "test", tm->nbuckets,
319 0x30000000 /* base_addr */ ,
320 tm->hash_memory_size);
322 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
325 for (acycle = 0; acycle < tm->ncycles; acycle++)
327 if ((acycle % tm->report_every_n) == 0)
329 fformat (stdout, "Cycle %lld out of %lld...\n",
330 acycle, tm->ncycles);
332 fformat (stdout, "Pick %lld unique %s keys...\n",
333 tm->nitems, tm->non_random_keys ? "non-random" : "random");
336 for (i = 0; i < tm->nitems; i++)
340 if (tm->non_random_keys == 0)
344 rndkey = random_u64 (&tm->seed);
346 p = hash_get (tm->key_hash, rndkey);
351 rndkey = (u64) (i + 1) << 16;
354 hash_set (tm->key_hash, rndkey, i + 1);
355 vec_add1 (tm->keys, rndkey);
359 if ((acycle % tm->report_every_n) == 0)
360 fformat (stdout, "Add items...\n");
362 for (i = 0; i < tm->nitems; i++)
364 kv.key = tm->keys[i];
367 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
371 fformat (stdout, "--------------------\n");
372 fformat (stdout, "After adding key %llu value %lld...\n",
373 tm->keys[i], (u64) (i + 1));
374 fformat (stdout, "%U", BV (format_bihash), h,
375 2 /* very verbose */ );
379 if ((acycle % tm->report_every_n) == 0)
381 fformat (stdout, "%U", BV (format_bihash), h,
382 0 /* very verbose */ );
384 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
387 before = clib_time_now (&tm->clib_time);
389 for (j = 0; j < tm->search_iter; j++)
391 for (i = 0; i < tm->nitems; i++)
393 /* Prefetch buckets 8 iterations ahead */
394 if (1 && (i < (tm->nitems - 8)))
396 BVT (clib_bihash_kv) pref_kv;
398 pref_kv.key = tm->keys[i + 8];
399 pref_hash = BV (clib_bihash_hash) (&pref_kv);
400 BV (clib_bihash_prefetch_bucket) (h, pref_hash);
403 kv.key = tm->keys[i];
404 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
405 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
407 ("[%d] search for key %lld failed unexpectedly\n", i,
409 if (kv.value != (u64) (i + 1))
411 ("[%d] search for key %lld returned %lld, not %lld\n", i,
412 tm->keys, kv.value, (u64) (i + 1));
416 if ((acycle % tm->report_every_n) == 0)
418 delta = clib_time_now (&tm->clib_time) - before;
419 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
423 "%.f searches per second, %.2f nsec per search\n",
424 ((f64) total_searches) / delta,
425 1e9 * (delta / ((f64) total_searches)));
427 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
430 fformat (stdout, "Standard E-hash search for items %d times...\n",
434 before = clib_time_now (&tm->clib_time);
436 for (j = 0; j < tm->search_iter; j++)
438 for (i = 0; i < tm->nitems; i++)
440 p = hash_get (tm->key_hash, tm->keys[i]);
441 if (p == 0 || p[0] != (uword) (i + 1))
442 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
446 delta = clib_time_now (&tm->clib_time) - before;
447 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
449 if ((acycle % tm->report_every_n) == 0)
451 fformat (stdout, "%lld searches in %.6f seconds\n",
452 total_searches, delta);
455 fformat (stdout, "%.f searches per second\n",
456 ((f64) total_searches) / delta);
457 fformat (stdout, "Delete items...\n");
460 for (i = 0; i < tm->nitems; i++)
465 kv.key = tm->keys[i];
466 kv.value = (u64) (i + 1);
467 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
470 clib_warning ("delete key %lld not ok but should be",
473 if (tm->careful_delete_tests)
475 for (j = 0; j < tm->nitems; j++)
477 /* Prefetch buckets 8 iterations ahead */
478 if (1 && (j < (tm->nitems - 8)))
480 BVT (clib_bihash_kv) pref_kv;
482 pref_kv.key = tm->keys[j + 8];
483 pref_hash = BV (clib_bihash_hash) (&pref_kv);
484 BV (clib_bihash_prefetch_bucket) (h, pref_hash);
487 kv.key = tm->keys[j];
488 rv = BV (clib_bihash_search) (h, &kv, &kv);
489 if (j <= i && rv >= 0)
492 ("i %d j %d search ok but should not be, value %lld",
497 clib_warning ("i %d j %d search not ok but should be",
503 if ((acycle % tm->report_every_n) == 0)
505 struct rusage r_usage;
506 getrusage (RUSAGE_SELF, &r_usage);
507 fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss);
508 fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ );
511 /* Clean up side-bet hash table and random key vector */
512 hash_free (tm->key_hash);
513 vec_reset_length (tm->keys);
514 /* Recreate hash table if we're going to need it again */
515 if (acycle != (tm->ncycles - 1))
516 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
519 fformat (stdout, "End of run, should be empty...\n");
521 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
523 BV (clib_bihash_free) (h);
529 test_bihash_main (test_main_t * tm)
531 unformat_input_t *i = tm->input;
535 tm->report_every_n = 1;
536 tm->hash_memory_size = 1ULL << 30;
538 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
540 if (unformat (i, "seed %u", &tm->seed))
543 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
545 else if (unformat (i, "non-random-keys"))
546 tm->non_random_keys = 1;
547 else if (unformat (i, "nitems %d", &tm->nitems))
549 else if (unformat (i, "ncycles %d", &tm->ncycles))
551 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
553 else if (unformat (i, "verbose %d", &tm->verbose))
555 else if (unformat (i, "search %d", &tm->search_iter))
557 else if (unformat (i, "report-every %d", &tm->report_every_n))
559 else if (unformat (i, "memory-size %U",
560 unformat_memory_size, &tm->hash_memory_size))
562 else if (unformat (i, "vec64"))
564 else if (unformat (i, "threads %u", &tm->nthreads))
566 else if (unformat (i, "verbose"))
568 else if (unformat (i, "stale-overwrite"))
570 else if (unformat (i, "overwrite"))
572 else if (unformat (i, "value-assert"))
575 return clib_error_return (0, "unknown input '%U'",
576 format_unformat_error, i);
579 /* Preallocate hash table, key vector */
580 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
581 vec_validate (tm->keys, tm->nitems - 1);
582 vec_set_len (tm->keys, 0);
587 error = test_bihash (tm);
591 error = test_bihash_vec64 (tm);
595 error = test_bihash_threads (tm);
599 error = test_bihash_stale_overwrite (tm);
603 error = test_bihash_vanilla_overwrite (tm);
607 error = test_bihash_value_assert (tm);
611 return clib_error_return (0, "no such test?");
619 main (int argc, char *argv[])
623 test_main_t *tm = &test_main;
625 clib_mem_init (0, 4095ULL << 20);
627 tm->global_heap = clib_mem_get_per_cpu_heap ();
630 tm->seed = 0xdeaddabe;
637 tm->careful_delete_tests = 0;
638 clib_time_init (&tm->clib_time);
640 unformat_init_command_line (&i, argv);
641 error = test_bihash_main (tm);
646 clib_error_report (error);
651 #endif /* CLIB_UNIX */
654 * fd.io coding-style-patch-verification: ON
657 * eval: (c-set-style "gnu")