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;
75 BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
77 before = clib_time_now (&tm->clib_time);
79 for (j = 0; j < 10; j++)
81 for (i = 1; i <= j * 1000 + 1; i++)
86 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
89 vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
92 for (j = 0; j < vec_len (cum_times); j++)
93 fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
100 stale_cb (BVT (clib_bihash_kv) * kv, void *ctx)
102 test_main_t *tm = ctx;
109 static clib_error_t *
110 test_bihash_stale_overwrite (test_main_t * tm)
112 BVT (clib_bihash) * h;
113 BVT (clib_bihash_kv) kv;
115 tm->noverwritten = 0;
119 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
121 fformat (stdout, "Add %d items to %d buckets\n", tm->nitems, tm->nbuckets);
123 for (i = 0; i < tm->nitems; i++)
128 BV (clib_bihash_add_or_overwrite_stale) (h, &kv, stale_cb, tm);
131 fformat (stdout, "%d items overwritten\n", tm->noverwritten);
132 fformat (stdout, "%U", BV (format_bihash), h, 0);
138 test_bihash_thread_fn (void *arg)
140 BVT (clib_bihash) * h;
141 BVT (clib_bihash_kv) kv;
142 test_main_t *tm = &test_main;
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);
150 while (tm->thread_barrier)
155 for (i = 0; i < tm->ncycles; i++)
157 for (j = 0; j < tm->nitems; j++)
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,
163 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
165 for (j = 0; j < tm->nitems; j++)
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,
171 BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
175 (void) __atomic_sub_fetch (&tm->threads_running, 1, __ATOMIC_ACQUIRE);
178 struct timespec ts, tsrem;
182 while (nanosleep (&ts, &tsrem) < 0)
185 return (0); /* not so much */
188 static clib_error_t *
189 test_bihash_threads (test_main_t * tm)
193 BVT (clib_bihash) * h;
198 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
200 tm->thread_barrier = 1;
202 /* Start the worker threads */
203 for (i = 0; i < tm->nthreads; i++)
205 rv = pthread_create (&handle, NULL, test_bihash_thread_fn,
209 clib_unix_warning ("pthread_create returned %d", rv);
212 tm->threads_running = i;
213 tm->sequence_number = 0;
214 CLIB_MEMORY_BARRIER ();
216 /* start the workers */
217 tm->thread_barrier = 0;
219 while (tm->threads_running)
221 struct timespec ts, tsrem;
223 ts.tv_nsec = 20 * 1000 * 1000; /* sleep for 20ms at a time */
225 while (nanosleep (&ts, &tsrem) < 0)
233 static clib_error_t *
234 test_bihash (test_main_t * tm)
238 uword total_searches;
240 BVT (clib_bihash) * h;
241 BVT (clib_bihash_kv) kv;
246 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
248 for (acycle = 0; acycle < tm->ncycles; acycle++)
250 if ((acycle % tm->report_every_n) == 0)
252 fformat (stdout, "Cycle %lld out of %lld...\n",
253 acycle, tm->ncycles);
255 fformat (stdout, "Pick %lld unique %s keys...\n",
256 tm->nitems, tm->non_random_keys ? "non-random" : "random");
259 for (i = 0; i < tm->nitems; i++)
263 if (tm->non_random_keys == 0)
267 rndkey = random_u64 (&tm->seed);
269 p = hash_get (tm->key_hash, rndkey);
274 rndkey = (u64) (i + 1) << 16;
277 hash_set (tm->key_hash, rndkey, i + 1);
278 vec_add1 (tm->keys, rndkey);
282 if ((acycle % tm->report_every_n) == 0)
283 fformat (stdout, "Add items...\n");
285 for (i = 0; i < tm->nitems; i++)
287 kv.key = tm->keys[i];
290 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
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 */ );
302 if ((acycle % tm->report_every_n) == 0)
304 fformat (stdout, "%U", BV (format_bihash), h,
305 0 /* very verbose */ );
307 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
310 before = clib_time_now (&tm->clib_time);
312 for (j = 0; j < tm->search_iter; j++)
314 for (i = 0; i < tm->nitems; i++)
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)
320 ("[%d] search for key %lld failed unexpectedly\n", i,
322 if (kv.value != (u64) (i + 1))
324 ("[%d] search for key %lld returned %lld, not %lld\n", i,
325 tm->keys, kv.value, (u64) (i + 1));
329 if ((acycle % tm->report_every_n) == 0)
331 delta = clib_time_now (&tm->clib_time) - before;
332 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
335 fformat (stdout, "%.f searches per second\n",
336 ((f64) total_searches) / delta);
338 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
341 fformat (stdout, "Standard E-hash search for items %d times...\n",
345 before = clib_time_now (&tm->clib_time);
347 for (j = 0; j < tm->search_iter; j++)
349 for (i = 0; i < tm->nitems; i++)
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]);
357 delta = clib_time_now (&tm->clib_time) - before;
358 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
360 if ((acycle % tm->report_every_n) == 0)
362 fformat (stdout, "%lld searches in %.6f seconds\n",
363 total_searches, delta);
366 fformat (stdout, "%.f searches per second\n",
367 ((f64) total_searches) / delta);
368 fformat (stdout, "Delete items...\n");
371 for (i = 0; i < tm->nitems; i++)
376 kv.key = tm->keys[i];
377 kv.value = (u64) (i + 1);
378 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
381 clib_warning ("delete key %lld not ok but should be",
384 if (tm->careful_delete_tests)
386 for (j = 0; j < tm->nitems; j++)
388 kv.key = tm->keys[j];
389 rv = BV (clib_bihash_search) (h, &kv, &kv);
390 if (j <= i && rv >= 0)
393 ("i %d j %d search ok but should not be, value %lld",
398 clib_warning ("i %d j %d search not ok but should be",
404 if ((acycle % tm->report_every_n) == 0)
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 */ );
412 /* Clean up side-bet hash table and random key vector */
413 hash_free (tm->key_hash);
414 vec_reset_length (tm->keys);
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));
420 fformat (stdout, "End of run, should be empty...\n");
422 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
427 test_bihash_main (test_main_t * tm)
429 unformat_input_t *i = tm->input;
433 tm->report_every_n = 1;
434 tm->hash_memory_size = 4095ULL << 20;
436 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
438 if (unformat (i, "seed %u", &tm->seed))
441 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
443 else if (unformat (i, "non-random-keys"))
444 tm->non_random_keys = 1;
445 else if (unformat (i, "nitems %d", &tm->nitems))
447 else if (unformat (i, "ncycles %d", &tm->ncycles))
449 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
451 else if (unformat (i, "verbose %d", &tm->verbose))
453 else if (unformat (i, "search %d", &tm->search_iter))
455 else if (unformat (i, "report-every %d", &tm->report_every_n))
457 else if (unformat (i, "memory-size %U",
458 unformat_memory_size, &tm->hash_memory_size))
460 else if (unformat (i, "vec64"))
462 else if (unformat (i, "threads %u", &tm->nthreads))
464 else if (unformat (i, "verbose"))
466 else if (unformat (i, "stale-overwrite"))
469 return clib_error_return (0, "unknown input '%U'",
470 format_unformat_error, i);
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;
482 error = test_bihash (tm);
486 error = test_bihash_vec64 (tm);
490 error = test_bihash_threads (tm);
494 error = test_bihash_stale_overwrite (tm);
498 return clib_error_return (0, "no such test?");
506 main (int argc, char *argv[])
510 test_main_t *tm = &test_main;
512 clib_mem_init (0, 4095ULL << 20);
514 tm->global_heap = clib_mem_get_per_cpu_heap ();
517 tm->seed = 0xdeaddabe;
524 tm->careful_delete_tests = 0;
525 clib_time_init (&tm->clib_time);
527 unformat_init_command_line (&i, argv);
528 error = test_bihash_main (tm);
533 clib_error_report (error);
538 #endif /* CLIB_UNIX */
541 * fd.io coding-style-patch-verification: ON
544 * eval: (c-set-style "gnu")