interface: fix interface_types.api enums
[vpp.git] / src / vppinfra / test_bihash_template.c
index 80e1151..86039d8 100644 (file)
@@ -35,6 +35,7 @@ typedef struct
   u32 ncycles;
   u32 report_every_n;
   u32 search_iter;
+  u32 noverwritten;
   int careful_delete_tests;
   int verbose;
   int non_random_keys;
@@ -71,7 +72,13 @@ test_bihash_vec64 (test_main_t * tm)
 
   h = &tm->hash;
 
+#if BIHASH_32_64_SVM
+  BV (clib_bihash_master_init_svm) (h, "test", user_buckets,
+                                   0x30000000 /* base_addr */ ,
+                                   user_memory_size);
+#else
   BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
+#endif
 
   before = clib_time_now (&tm->clib_time);
 
@@ -95,6 +102,50 @@ test_bihash_vec64 (test_main_t * tm)
   return 0;
 }
 
+static int
+stale_cb (BVT (clib_bihash_kv) * kv, void *ctx)
+{
+  test_main_t *tm = ctx;
+
+  tm->noverwritten++;
+
+  return 1;
+}
+
+static clib_error_t *
+test_bihash_stale_overwrite (test_main_t * tm)
+{
+  BVT (clib_bihash) * h;
+  BVT (clib_bihash_kv) kv;
+  int i;
+  tm->noverwritten = 0;
+
+  h = &tm->hash;
+
+#if BIHASH_32_64_SVM
+  BV (clib_bihash_master_init_svm) (h, "test", tm->nbuckets,
+                                   0x30000000 /* base_addr */ ,
+                                   tm->hash_memory_size);
+#else
+  BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
+#endif
+
+  fformat (stdout, "Add %d items to %d buckets\n", tm->nitems, tm->nbuckets);
+
+  for (i = 0; i < tm->nitems; i++)
+    {
+      kv.key = i;
+      kv.value = 1;
+
+      BV (clib_bihash_add_or_overwrite_stale) (h, &kv, stale_cb, tm);
+    }
+
+  fformat (stdout, "%d items overwritten\n", tm->noverwritten);
+  fformat (stdout, "%U", BV (format_bihash), h, 0);
+
+  return 0;
+}
+
 void *
 test_bihash_thread_fn (void *arg)
 {
@@ -156,7 +207,13 @@ test_bihash_threads (test_main_t * tm)
 
   h = &tm->hash;
 
+#if BIHASH_32_64_SVM
+  BV (clib_bihash_master_init_svm) (h, "test", tm->nbuckets,
+                                   0x30000000 /* base_addr */ ,
+                                   tm->hash_memory_size);
+#else
   BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
+#endif
 
   tm->thread_barrier = 1;
 
@@ -204,7 +261,13 @@ test_bihash (test_main_t * tm)
 
   h = &tm->hash;
 
+#if BIHASH_32_64_SVM
+  BV (clib_bihash_master_init_svm) (h, "test", tm->nbuckets,
+                                   0x30000000 /* base_addr */ ,
+                                   tm->hash_memory_size);
+#else
   BV (clib_bihash_init) (h, "test", tm->nbuckets, tm->hash_memory_size);
+#endif
 
   for (acycle = 0; acycle < tm->ncycles; acycle++)
     {
@@ -274,6 +337,16 @@ test_bihash (test_main_t * tm)
        {
          for (i = 0; i < tm->nitems; i++)
            {
+             /* Prefetch buckets 8 iterations ahead */
+             if (1 && (i < (tm->nitems - 8)))
+               {
+                 BVT (clib_bihash_kv) pref_kv;
+                 u64 pref_hash;
+                 pref_kv.key = tm->keys[i + 8];
+                 pref_hash = BV (clib_bihash_hash) (&pref_kv);
+                 BV (clib_bihash_prefetch_bucket) (h, pref_hash);
+               }
+
              kv.key = tm->keys[i];
              if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
                if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
@@ -293,8 +366,10 @@ test_bihash (test_main_t * tm)
          total_searches = (uword) tm->search_iter * (uword) tm->nitems;
 
          if (delta > 0)
-           fformat (stdout, "%.f searches per second\n",
-                    ((f64) total_searches) / delta);
+           fformat (stdout,
+                    "%.f searches per second, %.2f nsec per search\n",
+                    ((f64) total_searches) / delta,
+                    1e9 * (delta / ((f64) total_searches)));
 
          fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
                   delta);
@@ -346,6 +421,16 @@ test_bihash (test_main_t * tm)
            {
              for (j = 0; j < tm->nitems; j++)
                {
+                 /* Prefetch buckets 8 iterations ahead */
+                 if (1 && (j < (tm->nitems - 8)))
+                   {
+                     BVT (clib_bihash_kv) pref_kv;
+                     u64 pref_hash;
+                     pref_kv.key = tm->keys[j + 8];
+                     pref_hash = BV (clib_bihash_hash) (&pref_kv);
+                     BV (clib_bihash_prefetch_bucket) (h, pref_hash);
+                   }
+
                  kv.key = tm->keys[j];
                  rv = BV (clib_bihash_search) (h, &kv, &kv);
                  if (j <= i && rv >= 0)
@@ -381,6 +466,9 @@ test_bihash (test_main_t * tm)
   fformat (stdout, "End of run, should be empty...\n");
 
   fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
+
+  BV (clib_bihash_free) (h);
+
   return 0;
 }
 
@@ -392,7 +480,7 @@ test_bihash_main (test_main_t * tm)
   int which = 0;
 
   tm->report_every_n = 1;
-  tm->hash_memory_size = 4095ULL << 20;
+  tm->hash_memory_size = 1ULL << 30;
 
   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
     {
@@ -424,6 +512,8 @@ test_bihash_main (test_main_t * tm)
        which = 2;
       else if (unformat (i, "verbose"))
        tm->verbose = 1;
+      else if (unformat (i, "stale-overwrite"))
+       which = 3;
       else
        return clib_error_return (0, "unknown input '%U'",
                                  format_unformat_error, i);
@@ -449,6 +539,10 @@ test_bihash_main (test_main_t * tm)
       error = test_bihash_threads (tm);
       break;
 
+    case 3:
+      error = test_bihash_stale_overwrite (tm);
+      break;
+
     default:
       return clib_error_return (0, "no such test?");
     }