unittest: test_bihash call clib_time_init(...)
[vpp.git] / src / plugins / unittest / bihash_test.c
1 /*
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
23 #include <vppinfra/bihash_8_8_stats.h>
24 #include <vppinfra/bihash_template.h>
25
26 #include <vppinfra/bihash_template.c>
27
28 typedef struct
29 {
30   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
43 typedef struct
44 {
45   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;
69
70     CLIB_CACHE_LINE_ALIGN_MARK (stat_align);
71   bihash_stats_t stats;
72
73 } bihash_test_main_t;
74
75 static bihash_test_main_t bihash_test_main;
76
77 u8 *
78 format_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
97 static void
98 inc_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
116 static clib_error_t *
117 test_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);
130   BV (clib_bihash_set_stats_callback) (h, inc_stats_callback, &tm->stats);
131
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
154 void *
155 test_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;
161   u32 my_thread_index = (uword) arg;
162
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
193 static clib_error_t *
194 test_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);
205   BV (clib_bihash_set_stats_callback) (h, inc_stats_callback, &tm->stats);
206
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,
213                            (void *) (uword) i);
214       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);
238
239   fformat (stdout, "Stats:\n%U", format_bihash_stats, h, 1 /* verbose */ );
240   BV (clib_bihash_free) (h);
241   return 0;
242 }
243
244 /*
245  * Callback to blow up spectacularly if anything remains in the table
246  */
247 static int
248 count_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   return (BIHASH_WALK_CONTINUE);
253 }
254
255 static clib_error_t *
256 test_bihash (bihash_test_main_t * tm)
257 {
258   int i, j;
259   uword *p;
260   uword total_searches;
261   f64 before, delta;
262   BVT (clib_bihash) * h;
263   BVT (clib_bihash_kv) kv;
264   u32 acycle;
265
266   h = &tm->hash;
267
268   BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
269   BV (clib_bihash_set_stats_callback) (h, inc_stats_callback, &tm->stats);
270
271   for (acycle = 0; acycle < tm->ncycles; acycle++)
272     {
273       if ((acycle % tm->report_every_n) == 0)
274         {
275           fformat (stdout, "Cycle %lld out of %lld...\n",
276                    acycle, tm->ncycles);
277
278           fformat (stdout, "Pick %lld unique %s keys...\n",
279                    tm->nitems, tm->non_random_keys ? "non-random" : "random");
280         }
281
282       for (i = 0; i < tm->nitems; i++)
283         {
284           u64 rndkey;
285
286           if (tm->non_random_keys == 0)
287             {
288
289             again:
290               rndkey = random_u64 (&tm->seed);
291
292               p = hash_get (tm->key_hash, rndkey);
293               if (p)
294                 goto again;
295             }
296           else
297             rndkey = (u64) (i + 1) << 16;
298           rndkey += acycle;
299
300           hash_set (tm->key_hash, rndkey, i + 1);
301           vec_add1 (tm->keys, rndkey);
302         }
303
304
305       if ((acycle % tm->report_every_n) == 0)
306         fformat (stdout, "Add items...\n");
307
308       for (i = 0; i < tm->nitems; i++)
309         {
310           kv.key = tm->keys[i];
311           kv.value = i + 1;
312
313           BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
314
315           if (tm->verbose > 1)
316             {
317               fformat (stdout, "--------------------\n");
318               fformat (stdout, "After adding key %llu value %lld...\n",
319                        tm->keys[i], (u64) (i + 1));
320               fformat (stdout, "%U", BV (format_bihash), h,
321                        2 /* very verbose */ );
322             }
323         }
324
325       if ((acycle % tm->report_every_n) == 0)
326         {
327           fformat (stdout, "%U", BV (format_bihash), h,
328                    0 /* very verbose */ );
329
330           fformat (stdout, "Search for items %d times...\n", tm->search_iter);
331         }
332
333       before = clib_time_now (&tm->clib_time);
334
335       for (j = 0; j < tm->search_iter; j++)
336         {
337           for (i = 0; i < tm->nitems; i++)
338             {
339               kv.key = tm->keys[i];
340               if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
341                 if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
342                   clib_warning
343                     ("[%d] search for key %lld failed unexpectedly\n", i,
344                      tm->keys[i]);
345               if (kv.value != (u64) (i + 1))
346                 clib_warning
347                   ("[%d] search for key %lld returned %lld, not %lld\n", i,
348                    tm->keys, kv.value, (u64) (i + 1));
349             }
350         }
351
352       if ((acycle % tm->report_every_n) == 0)
353         {
354           delta = clib_time_now (&tm->clib_time) - before;
355           total_searches = (uword) tm->search_iter * (uword) tm->nitems;
356
357           if (delta > 0)
358             fformat (stdout, "%.f searches per second\n",
359                      ((f64) total_searches) / delta);
360
361           fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
362                    delta);
363
364           fformat (stdout, "Standard E-hash search for items %d times...\n",
365                    tm->search_iter);
366         }
367
368       before = clib_time_now (&tm->clib_time);
369
370       for (j = 0; j < tm->search_iter; j++)
371         {
372           for (i = 0; i < tm->nitems; i++)
373             {
374               p = hash_get (tm->key_hash, tm->keys[i]);
375               if (p == 0 || p[0] != (uword) (i + 1))
376                 clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
377             }
378         }
379
380       delta = clib_time_now (&tm->clib_time) - before;
381       total_searches = (uword) tm->search_iter * (uword) tm->nitems;
382
383       if ((acycle % tm->report_every_n) == 0)
384         {
385           fformat (stdout, "%lld searches in %.6f seconds\n",
386                    total_searches, delta);
387
388           if (delta > 0)
389             fformat (stdout, "%.f searches per second\n",
390                      ((f64) total_searches) / delta);
391           fformat (stdout, "Delete items...\n");
392         }
393
394       for (i = 0; i < tm->nitems; i++)
395         {
396           int j;
397           int rv;
398
399           kv.key = tm->keys[i];
400           kv.value = (u64) (i + 1);
401           rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
402
403           if (rv < 0)
404             clib_warning ("delete key %lld not ok but should be",
405                           tm->keys[i]);
406
407           if (tm->careful_delete_tests)
408             {
409               for (j = 0; j < tm->nitems; j++)
410                 {
411                   kv.key = tm->keys[j];
412                   rv = BV (clib_bihash_search) (h, &kv, &kv);
413                   if (j <= i && rv >= 0)
414                     {
415                       clib_warning
416                         ("i %d j %d search ok but should not be, value %lld",
417                          i, j, kv.value);
418                     }
419                   if (j > i && rv < 0)
420                     {
421                       clib_warning ("i %d j %d search not ok but should be",
422                                     i, j);
423                     }
424                 }
425             }
426         }
427       if ((acycle % tm->report_every_n) == 0)
428         {
429           struct rusage r_usage;
430           getrusage (RUSAGE_SELF, &r_usage);
431           fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss);
432           fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ );
433         }
434
435       /* Clean up side-bet hash table and random key vector */
436       hash_free (tm->key_hash);
437       vec_reset_length (tm->keys);
438       /* Recreate hash table if we're going to need it again */
439       if (acycle != (tm->ncycles - 1))
440         tm->key_hash = hash_create (tm->nitems, sizeof (uword));
441     }
442
443   fformat (stdout, "End of run, should be empty...\n");
444
445   fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
446
447   /* ASSERTs if any items remain */
448   BV (clib_bihash_foreach_key_value_pair) (h, count_items, 0);
449
450   fformat (stdout, "Stats:\n%U", format_bihash_stats, h, 1 /* verbose */ );
451
452   BV (clib_bihash_free) (h);
453
454   vec_free (tm->keys);
455   hash_free (tm->key_hash);
456
457   return 0;
458 }
459
460 static clib_error_t *
461 test_bihash_command_fn (vlib_main_t * vm,
462                         unformat_input_t * input, vlib_cli_command_t * cmd)
463 {
464   bihash_test_main_t *tm = &bihash_test_main;
465   clib_error_t *error;
466   int which = 0;
467
468   tm->hash_memory_size = 32ULL << 20;
469   tm->nbuckets = 32000;
470   tm->nitems = 100000;
471   tm->ncycles = 10;
472   tm->report_every_n = 50000;
473   tm->seed = 0x1badf00d;
474
475   memset (&tm->stats, 0, sizeof (tm->stats));
476
477   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
478     {
479       if (unformat (input, "seed %u", &tm->seed))
480         ;
481
482       else if (unformat (input, "nbuckets %d", &tm->nbuckets))
483         ;
484       else if (unformat (input, "non-random-keys"))
485         tm->non_random_keys = 1;
486       else if (unformat (input, "nitems %d", &tm->nitems))
487         ;
488       else if (unformat (input, "ncycles %d", &tm->ncycles))
489         ;
490       else if (unformat (input, "careful %d", &tm->careful_delete_tests))
491         ;
492       else if (unformat (input, "verbose %d", &tm->verbose))
493         ;
494       else if (unformat (input, "search %d", &tm->search_iter))
495         ;
496       else if (unformat (input, "report-every %d", &tm->report_every_n))
497         ;
498       else if (unformat (input, "memory-size %U",
499                          unformat_memory_size, &tm->hash_memory_size))
500         ;
501       else if (unformat (input, "vec64"))
502         which = 1;
503       else if (unformat (input, "threads %u", &tm->nthreads))
504         which = 2;
505       else if (unformat (input, "verbose"))
506         tm->verbose = 1;
507       else
508         return clib_error_return (0, "unknown input '%U'",
509                                   format_unformat_error, input);
510     }
511
512   /* Preallocate hash table, key vector */
513   tm->key_hash = hash_create (tm->nitems, sizeof (uword));
514   vec_validate (tm->keys, tm->nitems - 1);
515   _vec_len (tm->keys) = 0;
516
517   switch (which)
518     {
519     case 0:
520       error = test_bihash (tm);
521       break;
522
523     case 1:
524       error = test_bihash_vec64 (tm);
525       break;
526
527     case 2:
528       error = test_bihash_threads (tm);
529       break;
530
531     default:
532       return clib_error_return (0, "no such test?");
533     }
534
535   return error;
536 }
537
538 /* *INDENT-OFF* */
539 VLIB_CLI_COMMAND (test_bihash_command, static) =
540 {
541   .path = "test bihash",
542   .short_help = "test bihash",
543   .function = test_bihash_command_fn,
544 };
545 /* *INDENT-ON* */
546
547 static clib_error_t *
548 bihash_test_init (vlib_main_t * vm)
549 {
550   bihash_test_main_t *tm = &bihash_test_main;
551
552   clib_time_init (&tm->clib_time);
553   tm->vlib_main = vm;
554   return (0);
555 }
556
557 VLIB_INIT_FUNCTION (bihash_test_init);
558
559 /*
560  * fd.io coding-style-patch-verification: ON
561  *
562  * Local Variables:
563  * eval: (c-set-style "gnu")
564  * End:
565  */