blob: 2dbc6b1b002465a7af62b6089cc2648f21539837 [file] [log] [blame]
Dave Barach8b5dc4f2018-07-23 18:00:54 -04001/*
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 <vlib/vlib.h>
16#include <vppinfra/time.h>
17#include <vppinfra/cache.h>
18#include <vppinfra/error.h>
19#include <sys/resource.h>
20#include <stdio.h>
21#include <pthread.h>
22
Dave Barach2ce28d62019-05-03 12:58:01 -040023#include <vppinfra/bihash_8_8_stats.h>
Dave Barach8b5dc4f2018-07-23 18:00:54 -040024#include <vppinfra/bihash_template.h>
25
26#include <vppinfra/bihash_template.c>
27
28typedef struct
29{
Dave Barach2ce28d62019-05-03 12:58:01 -040030 u64 alloc_add;
31 u64 add;
32 u64 split_add;
33 u64 replace;
34 u64 update;
35 u64 del;
36 u64 del_free;
37 u64 linear;
38 u64 resplit;
39 u64 working_copy_lost;
40 u64 *splits;
41} bihash_stats_t;
42
43typedef struct
44{
Dave Barach8b5dc4f2018-07-23 18:00:54 -040045 volatile u32 thread_barrier;
46 volatile u32 threads_running;
47 volatile u64 sequence_number;
48 u64 seed;
49 u32 nbuckets;
50 u32 nitems;
51 u32 ncycles;
52 u32 report_every_n;
53 u32 search_iter;
54 int careful_delete_tests;
55 int verbose;
56 int non_random_keys;
57 u32 nthreads;
58 uword *key_hash;
59 u64 *keys;
60 uword hash_memory_size;
61 BVT (clib_bihash) hash;
62 clib_time_t clib_time;
63 void *global_heap;
64
65 unformat_input_t *input;
66
67 /* convenience */
68 vlib_main_t *vlib_main;
Dave Barach2ce28d62019-05-03 12:58:01 -040069
70 CLIB_CACHE_LINE_ALIGN_MARK (stat_align);
71 bihash_stats_t stats;
72
Dave Barach8b5dc4f2018-07-23 18:00:54 -040073} bihash_test_main_t;
74
75static bihash_test_main_t bihash_test_main;
76
Dave Barach2ce28d62019-05-03 12:58:01 -040077u8 *
78format_bihash_stats (u8 * s, va_list * args)
79{
80 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
81 int verbose = va_arg (*args, int);
82 int i;
83 bihash_stats_t *sp = h->inc_stats_context;
84
85#define _(a) s = format (s, "%20s: %lld\n", #a, sp->a);
86 foreach_bihash_stat;
87#undef _
88 for (i = 0; i < vec_len (sp->splits); i++)
89 {
90 if (sp->splits[i] > 0 || verbose)
91 s = format (s, " splits[%d]: %lld\n", 1 << i, sp->splits[i]);
92 }
93 return s;
94}
95
96
97static void
98inc_stats_callback (BVT (clib_bihash) * h, int stat_id, u64 count)
99{
100 uword *statp = h->inc_stats_context;
101 bihash_stats_t *for_splits;
102
103 if (PREDICT_TRUE (stat_id * sizeof (u64)
104 < STRUCT_OFFSET_OF (bihash_stats_t, splits)))
105 {
106 statp[stat_id] += count;
107 return;
108 }
109
110 for_splits = h->inc_stats_context;
111 vec_validate (for_splits->splits, count);
112 for_splits->splits[count] += 1;
113}
114
115
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400116static clib_error_t *
117test_bihash_vec64 (bihash_test_main_t * tm)
118{
119 u32 user_buckets = 1228800;
120 u32 user_memory_size = 209715200;
121 BVT (clib_bihash_kv) kv;
122 int i, j;
123 f64 before;
124 f64 *cum_times = 0;
125 BVT (clib_bihash) * h;
126
127 h = &tm->hash;
128
129 BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
Dave Barach2ce28d62019-05-03 12:58:01 -0400130 BV (clib_bihash_set_stats_callback) (h, inc_stats_callback, &tm->stats);
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400131
132 before = clib_time_now (&tm->clib_time);
133
134 for (j = 0; j < 10; j++)
135 {
136 for (i = 1; i <= j * 1000 + 1; i++)
137 {
138 kv.key = i;
139 kv.value = 1;
140
141 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
142 }
143
144 vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
145 }
146
147 for (j = 0; j < vec_len (cum_times); j++)
148 fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
149 cum_times[j] * 1e6);
150
151 return 0;
152}
153
154void *
155test_bihash_thread_fn (void *arg)
156{
157 BVT (clib_bihash) * h;
158 BVT (clib_bihash_kv) kv;
159 bihash_test_main_t *tm = &bihash_test_main;
160 int i, j;
David Johnsond9818dd2018-12-14 14:53:41 -0500161 u32 my_thread_index = (uword) arg;
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400162
163 while (tm->thread_barrier)
164 ;
165
166 h = &tm->hash;
167
168 for (i = 0; i < tm->ncycles; i++)
169 {
170 for (j = 0; j < tm->nitems; j++)
171 {
172 kv.key = ((u64) my_thread_index << 32) | (u64) j;
173 kv.value = ((u64) my_thread_index << 32) | (u64) j;
174 (void) __atomic_add_fetch (&tm->sequence_number, 1,
175 __ATOMIC_ACQUIRE);
176 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
177 }
178 for (j = 0; j < tm->nitems; j++)
179 {
180 kv.key = ((u64) my_thread_index << 32) | (u64) j;
181 kv.value = ((u64) my_thread_index << 32) | (u64) j;
182 (void) __atomic_add_fetch (&tm->sequence_number, 1,
183 __ATOMIC_ACQUIRE);
184 BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
185 }
186 }
187
188 (void) __atomic_sub_fetch (&tm->threads_running, 1, __ATOMIC_ACQUIRE);
189 pthread_exit (0);
190 return (0); /* not so much */
191}
192
193static clib_error_t *
194test_bihash_threads (bihash_test_main_t * tm)
195{
196 int i;
197 pthread_t handle;
198 BVT (clib_bihash) * h;
199 int rv;
200 f64 before, after, delta;
201
202 h = &tm->hash;
203
204 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
Dave Barach2ce28d62019-05-03 12:58:01 -0400205 BV (clib_bihash_set_stats_callback) (h, inc_stats_callback, &tm->stats);
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400206
207 tm->thread_barrier = 1;
208
209 /* Start the worker threads */
210 for (i = 0; i < tm->nthreads; i++)
211 {
212 rv = pthread_create (&handle, NULL, test_bihash_thread_fn,
David Johnsond9818dd2018-12-14 14:53:41 -0500213 (void *) (uword) i);
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400214 if (rv)
215 {
216 clib_unix_warning ("pthread_create returned %d", rv);
217 }
218 }
219 tm->threads_running = i;
220 tm->sequence_number = 0;
221 CLIB_MEMORY_BARRIER ();
222
223 /* start the workers */
224 before = vlib_time_now (tm->vlib_main);
225 tm->thread_barrier = 0;
226
227 while (tm->threads_running > 0)
228 CLIB_PAUSE ();
229
230 after = vlib_time_now (tm->vlib_main);
231 delta = after - before;
232
233 clib_warning ("%lld items in %.2f seconds, %.2f items/sec",
234 (u64) tm->nthreads * (u64) tm->nitems, delta,
235 delta >
236 0.0 ? ((f64) ((u64) tm->nthreads * (u64) tm->nitems)) /
237 delta : 0.0);
Dave Barach48a30d32018-09-12 12:08:13 -0400238
Dave Barach2ce28d62019-05-03 12:58:01 -0400239 fformat (stdout, "Stats:\n%U", format_bihash_stats, h, 1 /* verbose */ );
Dave Barach48a30d32018-09-12 12:08:13 -0400240 BV (clib_bihash_free) (h);
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400241 return 0;
242}
243
244/*
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700245 * Callback to blow up spectacularly if anything remains in the table
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400246 */
247static void
248count_items (BVT (clib_bihash_kv) * kvp, void *notused)
249{
250 _clib_error (CLIB_ERROR_ABORT, 0, 0,
251 "bihash test FAILED, items left in table!");
252}
253
254static clib_error_t *
255test_bihash (bihash_test_main_t * tm)
256{
257 int i, j;
258 uword *p;
259 uword total_searches;
260 f64 before, delta;
261 BVT (clib_bihash) * h;
262 BVT (clib_bihash_kv) kv;
263 u32 acycle;
264
265 h = &tm->hash;
266
267 BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
Dave Barach2ce28d62019-05-03 12:58:01 -0400268 BV (clib_bihash_set_stats_callback) (h, inc_stats_callback, &tm->stats);
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400269
270 for (acycle = 0; acycle < tm->ncycles; acycle++)
271 {
272 if ((acycle % tm->report_every_n) == 0)
273 {
274 fformat (stdout, "Cycle %lld out of %lld...\n",
275 acycle, tm->ncycles);
276
277 fformat (stdout, "Pick %lld unique %s keys...\n",
278 tm->nitems, tm->non_random_keys ? "non-random" : "random");
279 }
280
281 for (i = 0; i < tm->nitems; i++)
282 {
283 u64 rndkey;
284
285 if (tm->non_random_keys == 0)
286 {
287
288 again:
289 rndkey = random_u64 (&tm->seed);
290
291 p = hash_get (tm->key_hash, rndkey);
292 if (p)
293 goto again;
294 }
295 else
296 rndkey = (u64) (i + 1) << 16;
297 rndkey += acycle;
298
299 hash_set (tm->key_hash, rndkey, i + 1);
300 vec_add1 (tm->keys, rndkey);
301 }
302
303
304 if ((acycle % tm->report_every_n) == 0)
305 fformat (stdout, "Add items...\n");
306
307 for (i = 0; i < tm->nitems; i++)
308 {
309 kv.key = tm->keys[i];
310 kv.value = i + 1;
311
312 BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
313
314 if (tm->verbose > 1)
315 {
316 fformat (stdout, "--------------------\n");
317 fformat (stdout, "After adding key %llu value %lld...\n",
318 tm->keys[i], (u64) (i + 1));
319 fformat (stdout, "%U", BV (format_bihash), h,
320 2 /* very verbose */ );
321 }
322 }
323
324 if ((acycle % tm->report_every_n) == 0)
325 {
326 fformat (stdout, "%U", BV (format_bihash), h,
327 0 /* very verbose */ );
328
329 fformat (stdout, "Search for items %d times...\n", tm->search_iter);
330 }
331
332 before = clib_time_now (&tm->clib_time);
333
334 for (j = 0; j < tm->search_iter; j++)
335 {
336 for (i = 0; i < tm->nitems; i++)
337 {
338 kv.key = tm->keys[i];
339 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
340 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
341 clib_warning
342 ("[%d] search for key %lld failed unexpectedly\n", i,
343 tm->keys[i]);
344 if (kv.value != (u64) (i + 1))
345 clib_warning
346 ("[%d] search for key %lld returned %lld, not %lld\n", i,
347 tm->keys, kv.value, (u64) (i + 1));
348 }
349 }
350
351 if ((acycle % tm->report_every_n) == 0)
352 {
353 delta = clib_time_now (&tm->clib_time) - before;
354 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
355
356 if (delta > 0)
357 fformat (stdout, "%.f searches per second\n",
358 ((f64) total_searches) / delta);
359
360 fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
361 delta);
362
363 fformat (stdout, "Standard E-hash search for items %d times...\n",
364 tm->search_iter);
365 }
366
367 before = clib_time_now (&tm->clib_time);
368
369 for (j = 0; j < tm->search_iter; j++)
370 {
371 for (i = 0; i < tm->nitems; i++)
372 {
373 p = hash_get (tm->key_hash, tm->keys[i]);
374 if (p == 0 || p[0] != (uword) (i + 1))
375 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
376 }
377 }
378
379 delta = clib_time_now (&tm->clib_time) - before;
380 total_searches = (uword) tm->search_iter * (uword) tm->nitems;
381
382 if ((acycle % tm->report_every_n) == 0)
383 {
384 fformat (stdout, "%lld searches in %.6f seconds\n",
385 total_searches, delta);
386
387 if (delta > 0)
388 fformat (stdout, "%.f searches per second\n",
389 ((f64) total_searches) / delta);
390 fformat (stdout, "Delete items...\n");
391 }
392
393 for (i = 0; i < tm->nitems; i++)
394 {
395 int j;
396 int rv;
397
398 kv.key = tm->keys[i];
399 kv.value = (u64) (i + 1);
400 rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
401
402 if (rv < 0)
403 clib_warning ("delete key %lld not ok but should be",
404 tm->keys[i]);
405
406 if (tm->careful_delete_tests)
407 {
408 for (j = 0; j < tm->nitems; j++)
409 {
410 kv.key = tm->keys[j];
411 rv = BV (clib_bihash_search) (h, &kv, &kv);
412 if (j <= i && rv >= 0)
413 {
414 clib_warning
415 ("i %d j %d search ok but should not be, value %lld",
416 i, j, kv.value);
417 }
418 if (j > i && rv < 0)
419 {
420 clib_warning ("i %d j %d search not ok but should be",
421 i, j);
422 }
423 }
424 }
425 }
426 if ((acycle % tm->report_every_n) == 0)
427 {
428 struct rusage r_usage;
429 getrusage (RUSAGE_SELF, &r_usage);
430 fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss);
431 fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ );
432 }
433
434 /* Clean up side-bet hash table and random key vector */
435 hash_free (tm->key_hash);
436 vec_reset_length (tm->keys);
437 /* Recreate hash table if we're going to need it again */
438 if (acycle != (tm->ncycles - 1))
439 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
440 }
441
442 fformat (stdout, "End of run, should be empty...\n");
443
444 fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
445
446 /* ASSERTs if any items remain */
447 BV (clib_bihash_foreach_key_value_pair) (h, count_items, 0);
448
Dave Barach2ce28d62019-05-03 12:58:01 -0400449 fformat (stdout, "Stats:\n%U", format_bihash_stats, h, 1 /* verbose */ );
450
Dave Barach48a30d32018-09-12 12:08:13 -0400451 BV (clib_bihash_free) (h);
452
453 vec_free (tm->keys);
454 hash_free (tm->key_hash);
455
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400456 return 0;
457}
458
459static clib_error_t *
460test_bihash_command_fn (vlib_main_t * vm,
461 unformat_input_t * input, vlib_cli_command_t * cmd)
462{
463 bihash_test_main_t *tm = &bihash_test_main;
464 clib_error_t *error;
465 int which = 0;
466
467 tm->hash_memory_size = 32ULL << 20;
468 tm->nbuckets = 32000;
469 tm->nitems = 100000;
470 tm->ncycles = 10;
471 tm->report_every_n = 50000;
472 tm->seed = 0x1badf00d;
473
Dave Barach2ce28d62019-05-03 12:58:01 -0400474 memset (&tm->stats, 0, sizeof (tm->stats));
475
Dave Barach8b5dc4f2018-07-23 18:00:54 -0400476 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
477 {
478 if (unformat (input, "seed %u", &tm->seed))
479 ;
480
481 else if (unformat (input, "nbuckets %d", &tm->nbuckets))
482 ;
483 else if (unformat (input, "non-random-keys"))
484 tm->non_random_keys = 1;
485 else if (unformat (input, "nitems %d", &tm->nitems))
486 ;
487 else if (unformat (input, "ncycles %d", &tm->ncycles))
488 ;
489 else if (unformat (input, "careful %d", &tm->careful_delete_tests))
490 ;
491 else if (unformat (input, "verbose %d", &tm->verbose))
492 ;
493 else if (unformat (input, "search %d", &tm->search_iter))
494 ;
495 else if (unformat (input, "report-every %d", &tm->report_every_n))
496 ;
497 else if (unformat (input, "memory-size %U",
498 unformat_memory_size, &tm->hash_memory_size))
499 ;
500 else if (unformat (input, "vec64"))
501 which = 1;
502 else if (unformat (input, "threads %u", &tm->nthreads))
503 which = 2;
504 else if (unformat (input, "verbose"))
505 tm->verbose = 1;
506 else
507 return clib_error_return (0, "unknown input '%U'",
508 format_unformat_error, input);
509 }
510
511 /* Preallocate hash table, key vector */
512 tm->key_hash = hash_create (tm->nitems, sizeof (uword));
513 vec_validate (tm->keys, tm->nitems - 1);
514 _vec_len (tm->keys) = 0;
515
516 switch (which)
517 {
518 case 0:
519 error = test_bihash (tm);
520 break;
521
522 case 1:
523 error = test_bihash_vec64 (tm);
524 break;
525
526 case 2:
527 error = test_bihash_threads (tm);
528 break;
529
530 default:
531 return clib_error_return (0, "no such test?");
532 }
533
534 return error;
535}
536
537/* *INDENT-OFF* */
538VLIB_CLI_COMMAND (test_bihash_command, static) =
539{
540 .path = "test bihash",
541 .short_help = "test bihash",
542 .function = test_bihash_command_fn,
543};
544/* *INDENT-ON* */
545
546static clib_error_t *
547bihash_test_init (vlib_main_t * vm)
548{
549 bihash_test_main_t *tm = &bihash_test_main;
550
551 tm->vlib_main = vm;
552 return (0);
553}
554
555VLIB_INIT_FUNCTION (bihash_test_init);
556
557/*
558 * fd.io coding-style-patch-verification: ON
559 *
560 * Local Variables:
561 * eval: (c-set-style "gnu")
562 * End:
563 */