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;
38 int careful_delete_tests;
44 uword hash_memory_size;
45 BVT (clib_bihash) hash;
46 clib_time_t clib_time;
49 unformat_input_t *input;
53 test_main_t test_main;
62 test_bihash_vec64 (test_main_t * tm)
64 u32 user_buckets = 1228800;
65 u32 user_memory_size = 209715200;
66 BVT (clib_bihash_kv) kv;
70 BVT (clib_bihash) * h;
74 BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
76 before = clib_time_now (&tm->clib_time);
78 for (j = 0; j < 10; j++)
80 for (i = 1; i <= j * 1000 + 1; i++)
85 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
88 vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
91 for (j = 0; j < vec_len (cum_times); j++)
92 fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
99 test_bihash_thread_fn (void *arg)
101 BVT (clib_bihash) * h;
102 BVT (clib_bihash_kv) kv;
103 test_main_t *tm = &test_main;
107 u32 my_thread_index = (u32) (u64) arg;
108 __os_thread_index = my_thread_index;
109 clib_mem_set_per_cpu_heap (tm->global_heap);
111 while (tm->thread_barrier)
116 for (i = 0; i < tm->ncycles; i++)
118 for (j = 0; j < tm->nitems; j++)
120 kv.key = ((u64) my_thread_index << 32) | (u64) j;
121 kv.value = ((u64) my_thread_index << 32) | (u64) j;
122 (void) __atomic_add_fetch (&tm->sequence_number, 1,
124 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
126 for (j = 0; j < tm->nitems; j++)
128 kv.key = ((u64) my_thread_index << 32) | (u64) j;
129 kv.value = ((u64) my_thread_index << 32) | (u64) j;
130 (void) __atomic_add_fetch (&tm->sequence_number, 1,
132 BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
136 (void) __atomic_sub_fetch (&tm->threads_running, 1, __ATOMIC_ACQUIRE);
139 struct timespec ts, tsrem;
143 while (nanosleep (&ts, &tsrem) < 0)
146 return (0); /* not so much */
149 static clib_error_t *
150 test_bihash_threads (test_main_t * tm)
154 BVT (clib_bihash) * h;
159 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
161 tm->thread_barrier = 1;
163 /* Start the worker threads */
164 for (i = 0; i < tm->nthreads; i++)
166 rv = pthread_create (&handle, NULL, test_bihash_thread_fn,
170 clib_unix_warning ("pthread_create returned %d", rv);
173 tm->threads_running = i;
174 tm->sequence_number = 0;
175 CLIB_MEMORY_BARRIER ();
177 /* start the workers */
178 tm->thread_barrier = 0;
180 while (tm->threads_running)
182 struct timespec ts, tsrem;
184 ts.tv_nsec = 20 * 1000 * 1000; /* sleep for 20ms at a time */
186 while (nanosleep (&ts, &tsrem) < 0)
194 static clib_error_t *
195 test_bihash (test_main_t * tm)
199 uword total_searches;
201 BVT (clib_bihash) * h;
202 BVT (clib_bihash_kv) kv;
207 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
209 for (acycle = 0; acycle < tm->ncycles; acycle++)
211 if ((acycle % tm->report_every_n) == 0)
213 fformat (stdout, "Cycle %lld out of %lld...\n",
214 acycle, tm->ncycles);
216 fformat (stdout, "Pick %lld unique %s keys...\n",
217 tm->nitems, tm->non_random_keys ? "non-random" : "random");
220 for (i = 0; i < tm->nitems; i++)
224 if (tm->non_random_keys == 0)
228 rndkey = random_u64 (&tm->seed);
230 p = hash_get (tm->key_hash, rndkey);
235 rndkey = (u64) (i + 1) << 16;
238 hash_set (tm->key_hash, rndkey, i + 1);
239 vec_add1 (tm->keys, rndkey);
243 if ((acycle % tm->report_every_n) == 0)
244 fformat (stdout, "Add items...\n");
246 for (i = 0; i < tm->nitems; i++)
248 kv.key = tm->keys[i];
251 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
255 fformat (stdout, "--------------------\n");
256 fformat (stdout, "After adding key %llu value %lld...\n",
257 tm->keys[i], (u64) (i + 1));
258 fformat (stdout, "%U", BV (format_bihash), h,
259 2 /* very verbose */ );
263 if ((acycle % tm->report_every_n) == 0)
265 fformat (stdout, "%U", BV (format_bihash), h,
266 0 /* very verbose */ );
268 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
271 before = clib_time_now (&tm->clib_time);
273 for (j = 0; j < tm->search_iter; j++)
275 for (i = 0; i < tm->nitems; i++)
277 kv.key = tm->keys[i];
278 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
279 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
281 ("[%d] search for key %lld failed unexpectedly\n", i,
283 if (kv.value != (u64) (i + 1))
285 ("[%d] search for key %lld returned %lld, not %lld\n", i,
286 tm->keys, kv.value, (u64) (i + 1));
290 if ((acycle % tm->report_every_n) == 0)
292 delta = clib_time_now (&tm->clib_time) - before;
293 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
296 fformat (stdout, "%.f searches per second\n",
297 ((f64) total_searches) / delta);
299 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
302 fformat (stdout, "Standard E-hash search for items %d times...\n",
306 before = clib_time_now (&tm->clib_time);
308 for (j = 0; j < tm->search_iter; j++)
310 for (i = 0; i < tm->nitems; i++)
312 p = hash_get (tm->key_hash, tm->keys[i]);
313 if (p == 0 || p[0] != (uword) (i + 1))
314 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
318 delta = clib_time_now (&tm->clib_time) - before;
319 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
321 if ((acycle % tm->report_every_n) == 0)
323 fformat (stdout, "%lld searches in %.6f seconds\n",
324 total_searches, delta);
327 fformat (stdout, "%.f searches per second\n",
328 ((f64) total_searches) / delta);
329 fformat (stdout, "Delete items...\n");
332 for (i = 0; i < tm->nitems; i++)
337 kv.key = tm->keys[i];
338 kv.value = (u64) (i + 1);
339 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
342 clib_warning ("delete key %lld not ok but should be",
345 if (tm->careful_delete_tests)
347 for (j = 0; j < tm->nitems; j++)
349 kv.key = tm->keys[j];
350 rv = BV (clib_bihash_search) (h, &kv, &kv);
351 if (j <= i && rv >= 0)
354 ("i %d j %d search ok but should not be, value %lld",
359 clib_warning ("i %d j %d search not ok but should be",
365 if ((acycle % tm->report_every_n) == 0)
367 struct rusage r_usage;
368 getrusage (RUSAGE_SELF, &r_usage);
369 fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss);
370 fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ );
373 /* Clean up side-bet hash table and random key vector */
374 hash_free (tm->key_hash);
375 vec_reset_length (tm->keys);
376 /* Recreate hash table if we're going to need it again */
377 if (acycle != (tm->ncycles - 1))
378 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
381 fformat (stdout, "End of run, should be empty...\n");
383 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
388 test_bihash_main (test_main_t * tm)
390 unformat_input_t *i = tm->input;
394 tm->report_every_n = 1;
395 tm->hash_memory_size = 4095ULL << 20;
397 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
399 if (unformat (i, "seed %u", &tm->seed))
402 else if (unformat (i, "nbuckets %d", &tm->nbuckets))
404 else if (unformat (i, "non-random-keys"))
405 tm->non_random_keys = 1;
406 else if (unformat (i, "nitems %d", &tm->nitems))
408 else if (unformat (i, "ncycles %d", &tm->ncycles))
410 else if (unformat (i, "careful %d", &tm->careful_delete_tests))
412 else if (unformat (i, "verbose %d", &tm->verbose))
414 else if (unformat (i, "search %d", &tm->search_iter))
416 else if (unformat (i, "report-every %d", &tm->report_every_n))
418 else if (unformat (i, "memory-size %U",
419 unformat_memory_size, &tm->hash_memory_size))
421 else if (unformat (i, "vec64"))
423 else if (unformat (i, "threads %u", &tm->nthreads))
425 else if (unformat (i, "verbose"))
428 return clib_error_return (0, "unknown input '%U'",
429 format_unformat_error, i);
432 /* Preallocate hash table, key vector */
433 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
434 vec_validate (tm->keys, tm->nitems - 1);
435 _vec_len (tm->keys) = 0;
441 error = test_bihash (tm);
445 error = test_bihash_vec64 (tm);
449 error = test_bihash_threads (tm);
453 return clib_error_return (0, "no such test?");
461 main (int argc, char *argv[])
465 test_main_t *tm = &test_main;
467 clib_mem_init (0, 4095ULL << 20);
469 tm->global_heap = clib_mem_get_per_cpu_heap ();
472 tm->seed = 0xdeaddabe;
479 tm->careful_delete_tests = 0;
480 clib_time_init (&tm->clib_time);
482 unformat_init_command_line (&i, argv);
483 error = test_bihash_main (tm);
488 clib_error_report (error);
493 #endif /* CLIB_UNIX */
496 * fd.io coding-style-patch-verification: ON
499 * eval: (c-set-style "gnu")