ethernet: check destination mac for L3 in ethernet-input node
[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   tm->threads_running = 0;
211   for (i = 0; i < tm->nthreads; i++)
212     {
213       rv = pthread_create (&handle, NULL, test_bihash_thread_fn,
214                            (void *) (uword) i);
215       if (rv)
216         clib_unix_warning ("pthread_create returned %d", rv);
217       else
218         tm->threads_running++;
219     }
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                 {
342                   if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
343                     {
344                       return clib_error_return (
345                         0, "[%d] search for key %lld failed unexpectedly\n", i,
346                         tm->keys[i]);
347                     }
348                 }
349               if (kv.value != (u64) (i + 1))
350                 return clib_error_return (
351                   0, "[%d] search for key %lld returned %lld, not %lld\n", i,
352                   tm->keys, kv.value, (u64) (i + 1));
353             }
354         }
355
356       if ((acycle % tm->report_every_n) == 0)
357         {
358           delta = clib_time_now (&tm->clib_time) - before;
359           total_searches = (uword) tm->search_iter * (uword) tm->nitems;
360
361           if (delta > 0)
362             fformat (stdout, "%.f searches per second\n",
363                      ((f64) total_searches) / delta);
364
365           fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
366                    delta);
367
368           fformat (stdout, "Standard E-hash search for items %d times...\n",
369                    tm->search_iter);
370         }
371
372       before = clib_time_now (&tm->clib_time);
373
374       for (j = 0; j < tm->search_iter; j++)
375         {
376           for (i = 0; i < tm->nitems; i++)
377             {
378               p = hash_get (tm->key_hash, tm->keys[i]);
379               if (p == 0 || p[0] != (uword) (i + 1))
380                 return clib_error_return (0, "ugh, couldn't find %lld\n",
381                                           tm->keys[i]);
382             }
383         }
384
385       delta = clib_time_now (&tm->clib_time) - before;
386       total_searches = (uword) tm->search_iter * (uword) tm->nitems;
387
388       if ((acycle % tm->report_every_n) == 0)
389         {
390           fformat (stdout, "%lld searches in %.6f seconds\n",
391                    total_searches, delta);
392
393           if (delta > 0)
394             fformat (stdout, "%.f searches per second\n",
395                      ((f64) total_searches) / delta);
396           fformat (stdout, "Delete items...\n");
397         }
398
399       for (i = 0; i < tm->nitems; i++)
400         {
401           int j;
402           int rv;
403
404           kv.key = tm->keys[i];
405           kv.value = (u64) (i + 1);
406           rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
407
408           if (rv < 0)
409             return clib_error_return (
410               0, "delete key %lld not ok but should be", tm->keys[i]);
411
412           if (tm->careful_delete_tests)
413             {
414               for (j = 0; j < tm->nitems; j++)
415                 {
416                   kv.key = tm->keys[j];
417                   rv = BV (clib_bihash_search) (h, &kv, &kv);
418                   if (j <= i && rv >= 0)
419                     {
420                       return clib_error_return (
421                         0, "i %d j %d search ok but should not be, value %lld",
422                         i, j, kv.value);
423                     }
424                   if (j > i && rv < 0)
425                     {
426                       return clib_error_return (
427                         0, "i %d j %d search not ok but should be", i, j);
428                     }
429                 }
430             }
431         }
432       if ((acycle % tm->report_every_n) == 0)
433         {
434           struct rusage r_usage;
435           getrusage (RUSAGE_SELF, &r_usage);
436           fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss);
437           fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ );
438         }
439
440       /* Clean up side-bet hash table and random key vector */
441       hash_free (tm->key_hash);
442       vec_reset_length (tm->keys);
443       /* Recreate hash table if we're going to need it again */
444       if (acycle != (tm->ncycles - 1))
445         tm->key_hash = hash_create (tm->nitems, sizeof (uword));
446     }
447
448   fformat (stdout, "End of run, should be empty...\n");
449
450   fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
451
452   /* ASSERTs if any items remain */
453   BV (clib_bihash_foreach_key_value_pair) (h, count_items, 0);
454
455   fformat (stdout, "Stats:\n%U", format_bihash_stats, h, 1 /* verbose */ );
456
457   BV (clib_bihash_free) (h);
458
459   vec_free (tm->keys);
460   hash_free (tm->key_hash);
461
462   return 0;
463 }
464
465 static clib_error_t *
466 test_bihash_command_fn (vlib_main_t * vm,
467                         unformat_input_t * input, vlib_cli_command_t * cmd)
468 {
469   bihash_test_main_t *tm = &bihash_test_main;
470   clib_error_t *error;
471   int which = 0;
472
473   tm->hash_memory_size = 32ULL << 20;
474   tm->nbuckets = 32000;
475   tm->nitems = 100000;
476   tm->ncycles = 10;
477   tm->report_every_n = 50000;
478   tm->seed = 0x1badf00d;
479   tm->search_iter = 1;
480
481   memset (&tm->stats, 0, sizeof (tm->stats));
482
483   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
484     {
485       if (unformat (input, "seed %u", &tm->seed))
486         ;
487
488       else if (unformat (input, "nbuckets %d", &tm->nbuckets))
489         ;
490       else if (unformat (input, "non-random-keys"))
491         tm->non_random_keys = 1;
492       else if (unformat (input, "nitems %d", &tm->nitems))
493         ;
494       else if (unformat (input, "ncycles %d", &tm->ncycles))
495         ;
496       else if (unformat (input, "careful %d", &tm->careful_delete_tests))
497         ;
498       else if (unformat (input, "verbose %d", &tm->verbose))
499         ;
500       else if (unformat (input, "search %d", &tm->search_iter))
501         ;
502       else if (unformat (input, "report-every %d", &tm->report_every_n))
503         ;
504       else if (unformat (input, "memory-size %U",
505                          unformat_memory_size, &tm->hash_memory_size))
506         ;
507       else if (unformat (input, "vec64"))
508         which = 1;
509       else if (unformat (input, "threads %u", &tm->nthreads))
510         which = 2;
511       else if (unformat (input, "verbose"))
512         tm->verbose = 1;
513       else
514         return clib_error_return (0, "unknown input '%U'",
515                                   format_unformat_error, input);
516     }
517
518   /* Preallocate hash table, key vector */
519   tm->key_hash = hash_create (tm->nitems, sizeof (uword));
520   vec_validate (tm->keys, tm->nitems - 1);
521   vec_set_len (tm->keys, 0);
522
523   switch (which)
524     {
525     case 0:
526       error = test_bihash (tm);
527       break;
528
529     case 1:
530       error = test_bihash_vec64 (tm);
531       break;
532
533     case 2:
534       error = test_bihash_threads (tm);
535       break;
536
537     default:
538       return clib_error_return (0, "no such test?");
539     }
540
541   return error;
542 }
543
544 VLIB_CLI_COMMAND (test_bihash_command, static) =
545 {
546   .path = "test bihash",
547   .short_help = "test bihash",
548   .function = test_bihash_command_fn,
549 };
550
551 static clib_error_t *
552 bihash_test_init (vlib_main_t * vm)
553 {
554   bihash_test_main_t *tm = &bihash_test_main;
555
556   clib_time_init (&tm->clib_time);
557   tm->vlib_main = vm;
558   return (0);
559 }
560
561 VLIB_INIT_FUNCTION (bihash_test_init);
562
563 /*
564  * fd.io coding-style-patch-verification: ON
565  *
566  * Local Variables:
567  * eval: (c-set-style "gnu")
568  * End:
569  */