Minimize bihash memory consumption
[vpp.git] / src / vppinfra / test_bihash_template.c
index c505bd8..2d4b553 100644 (file)
@@ -15,6 +15,8 @@
 #include <vppinfra/time.h>
 #include <vppinfra/cache.h>
 #include <vppinfra/error.h>
+#include <sys/resource.h>
+#include <stdio.h>
 
 #include <vppinfra/bihash_8_8.h>
 #include <vppinfra/bihash_template.h>
@@ -26,6 +28,8 @@ typedef struct
   u64 seed;
   u32 nbuckets;
   u32 nitems;
+  u32 ncycles;
+  u32 report_every_n;
   u32 search_iter;
   int careful_delete_tests;
   int verbose;
@@ -47,6 +51,43 @@ vl (void *v)
   return vec_len (v);
 }
 
+static clib_error_t *
+test_bihash_vec64 (test_main_t * tm)
+{
+  u32 user_buckets = 1228800;
+  u32 user_memory_size = 209715200;
+  BVT (clib_bihash_kv) kv;
+  int i, j;
+  f64 before;
+  f64 *cum_times = 0;
+  BVT (clib_bihash) * h;
+
+  h = &tm->hash;
+
+  BV (clib_bihash_init) (h, "test", user_buckets, user_memory_size);
+
+  before = clib_time_now (&tm->clib_time);
+
+  for (j = 0; j < 10; j++)
+    {
+      for (i = 1; i <= j * 1000 + 1; i++)
+       {
+         kv.key = i;
+         kv.value = 1;
+
+         BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
+       }
+
+      vec_add1 (cum_times, clib_time_now (&tm->clib_time) - before);
+    }
+
+  for (j = 0; j < vec_len (cum_times); j++)
+    fformat (stdout, "Cum time for %d: %.4f (us)\n", (j + 1) * 1000,
+            cum_times[j] * 1e6);
+
+  return 0;
+}
+
 static clib_error_t *
 test_bihash (test_main_t * tm)
 {
@@ -56,174 +97,231 @@ test_bihash (test_main_t * tm)
   f64 before, delta;
   BVT (clib_bihash) * h;
   BVT (clib_bihash_kv) kv;
+  u32 acycle;
 
   h = &tm->hash;
 
   BV (clib_bihash_init) (h, "test", tm->nbuckets, 3ULL << 30);
 
-  fformat (stdout, "Pick %lld unique %s keys...\n",
-          tm->nitems, tm->non_random_keys ? "non-random" : "random");
 
-  for (i = 0; i < tm->nitems; i++)
+  for (acycle = 0; acycle < tm->ncycles; acycle++)
     {
-      u64 rndkey;
+      if ((acycle % tm->report_every_n) == 0)
+       {
+         fformat (stdout, "Cycle %lld out of %lld...\n",
+                  acycle, tm->ncycles);
+
+         fformat (stdout, "Pick %lld unique %s keys...\n",
+                  tm->nitems, tm->non_random_keys ? "non-random" : "random");
+       }
 
-      if (tm->non_random_keys == 0)
+      for (i = 0; i < tm->nitems; i++)
        {
+         u64 rndkey;
+
+         if (tm->non_random_keys == 0)
+           {
 
-       again:
-         rndkey = random_u64 (&tm->seed);
+           again:
+             rndkey = random_u64 (&tm->seed);
 
-         p = hash_get (tm->key_hash, rndkey);
-         if (p)
-           goto again;
-       }
-      else
-       rndkey = (u64) (i + 1) << 16;
+             p = hash_get (tm->key_hash, rndkey);
+             if (p)
+               goto again;
+           }
+         else
+           rndkey = (u64) (i + 1) << 16;
+         rndkey += acycle;
 
-      hash_set (tm->key_hash, rndkey, i + 1);
-      vec_add1 (tm->keys, rndkey);
-    }
+         hash_set (tm->key_hash, rndkey, i + 1);
+         vec_add1 (tm->keys, rndkey);
+       }
 
-  fformat (stdout, "Add items...\n");
-  for (i = 0; i < tm->nitems; i++)
-    {
-      kv.key = tm->keys[i];
-      kv.value = i + 1;
 
-      BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
+      if ((acycle % tm->report_every_n) == 0)
+       fformat (stdout, "Add items...\n");
 
-      if (tm->verbose > 1)
+      for (i = 0; i < tm->nitems; i++)
        {
-         fformat (stdout, "--------------------\n");
-         fformat (stdout, "After adding key %llu value %lld...\n",
-                  tm->keys[i], (u64) (i + 1));
-         fformat (stdout, "%U", BV (format_bihash), h,
-                  2 /* very verbose */ );
-       }
-    }
+         kv.key = tm->keys[i];
+         kv.value = i + 1;
 
-  fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
+         BV (clib_bihash_add_del) (h, &kv, 1 /* is_add */ );
 
-  fformat (stdout, "Search for items %d times...\n", tm->search_iter);
+         if (tm->verbose > 1)
+           {
+             fformat (stdout, "--------------------\n");
+             fformat (stdout, "After adding key %llu value %lld...\n",
+                      tm->keys[i], (u64) (i + 1));
+             fformat (stdout, "%U", BV (format_bihash), h,
+                      2 /* very verbose */ );
+           }
+       }
 
-  before = clib_time_now (&tm->clib_time);
+      if ((acycle % tm->report_every_n) == 0)
+       {
+         fformat (stdout, "%U", BV (format_bihash), h,
+                  0 /* very verbose */ );
 
-  for (j = 0; j < tm->search_iter; j++)
-    {
-      u64 hash1 = clib_xxhash (tm->keys[0]);
+         fformat (stdout, "Search for items %d times...\n", tm->search_iter);
+       }
 
-      for (i = 0; i < tm->nitems; i++)
+      before = clib_time_now (&tm->clib_time);
+
+      for (j = 0; j < tm->search_iter; j++)
        {
-         if (i < (tm->nitems - 3))
+         for (i = 0; i < tm->nitems; i++)
            {
-             clib_bihash_bucket_t *b;
-             BVT (clib_bihash_value) * v;
-             u64 hash2 = clib_xxhash (tm->keys[i + 3]);
-             u32 bucket_index = hash2 & (h->nbuckets - 1);
-             b = &h->buckets[bucket_index];
-             CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
-
-             bucket_index = hash1 & (h->nbuckets - 1);
-             b = &h->buckets[bucket_index];
-             v = BV (clib_bihash_get_value) (h, b->offset);
-             hash1 >>= h->log2_nbuckets;
-             hash1 = hash1 & ((1 << b->log2_pages) - 1);
-             v += hash1;
-             CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
-
-             hash1 = hash2;
+             kv.key = tm->keys[i];
+             if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
+               if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
+                 clib_warning
+                   ("[%d] search for key %lld failed unexpectedly\n", i,
+                    tm->keys[i]);
+             if (kv.value != (u64) (i + 1))
+               clib_warning
+                 ("[%d] search for key %lld returned %lld, not %lld\n", i,
+                  tm->keys, kv.value, (u64) (i + 1));
            }
-
-         kv.key = tm->keys[i];
-         if (BV (clib_bihash_search) (h, &kv, &kv) < 0)
-           clib_warning ("search for key %lld failed unexpectedly\n",
-                         tm->keys[i]);
-         if (kv.value != (u64) (i + 1))
-           clib_warning ("search for key %lld returned %lld, not %lld\n",
-                         tm->keys, kv.value, (u64) (i + 1));
        }
-    }
 
-  delta = clib_time_now (&tm->clib_time) - before;
-  total_searches = (uword) tm->search_iter * (uword) tm->nitems;
+      if ((acycle % tm->report_every_n) == 0)
+       {
+         delta = clib_time_now (&tm->clib_time) - before;
+         total_searches = (uword) tm->search_iter * (uword) tm->nitems;
 
-  if (delta > 0)
-    fformat (stdout, "%.f searches per second\n",
-            ((f64) total_searches) / delta);
+         if (delta > 0)
+           fformat (stdout, "%.f searches per second\n",
+                    ((f64) total_searches) / delta);
 
-  fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
+         fformat (stdout, "%lld searches in %.6f seconds\n", total_searches,
+                  delta);
 
-  fformat (stdout, "Standard E-hash search for items %d times...\n",
-          tm->search_iter);
+         fformat (stdout, "Standard E-hash search for items %d times...\n",
+                  tm->search_iter);
+       }
 
-  before = clib_time_now (&tm->clib_time);
+      before = clib_time_now (&tm->clib_time);
 
-  for (j = 0; j < tm->search_iter; j++)
-    {
-      for (i = 0; i < tm->nitems; i++)
+      for (j = 0; j < tm->search_iter; j++)
        {
-         p = hash_get (tm->key_hash, tm->keys[i]);
-         if (p == 0 || p[0] != (uword) (i + 1))
-           clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
+         for (i = 0; i < tm->nitems; i++)
+           {
+             p = hash_get (tm->key_hash, tm->keys[i]);
+             if (p == 0 || p[0] != (uword) (i + 1))
+               clib_warning ("ugh, couldn't find %lld\n", tm->keys[i]);
+           }
        }
-    }
 
-  delta = clib_time_now (&tm->clib_time) - before;
-  total_searches = (uword) tm->search_iter * (uword) tm->nitems;
+      delta = clib_time_now (&tm->clib_time) - before;
+      total_searches = (uword) tm->search_iter * (uword) tm->nitems;
 
-  fformat (stdout, "%lld searches in %.6f seconds\n", total_searches, delta);
-
-  if (delta > 0)
-    fformat (stdout, "%.f searches per second\n",
-            ((f64) total_searches) / delta);
+      if ((acycle % tm->report_every_n) == 0)
+       {
+         fformat (stdout, "%lld searches in %.6f seconds\n",
+                  total_searches, delta);
 
-  fformat (stdout, "Delete items...\n");
+         if (delta > 0)
+           fformat (stdout, "%.f searches per second\n",
+                    ((f64) total_searches) / delta);
+         fformat (stdout, "Delete items...\n");
+       }
 
-  for (i = 0; i < tm->nitems; i++)
-    {
-      int j;
-      int rv;
+      for (i = 0; i < tm->nitems; i++)
+       {
+         int j;
+         int rv;
 
-      kv.key = tm->keys[i];
-      kv.value = (u64) (i + 1);
-      rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
+         kv.key = tm->keys[i];
+         kv.value = (u64) (i + 1);
+         rv = BV (clib_bihash_add_del) (h, &kv, 0 /* is_add */ );
 
-      if (rv < 0)
-       clib_warning ("delete key %lld not ok but should be", tm->keys[i]);
+         if (rv < 0)
+           clib_warning ("delete key %lld not ok but should be",
+                         tm->keys[i]);
 
-      if (tm->careful_delete_tests)
-       {
-         for (j = 0; j < tm->nitems; j++)
+         if (tm->careful_delete_tests)
            {
-             kv.key = tm->keys[j];
-             rv = BV (clib_bihash_search) (h, &kv, &kv);
-             if (j <= i && rv >= 0)
-               {
-                 clib_warning
-                   ("i %d j %d search ok but should not be, value %lld",
-                    i, j, kv.value);
-               }
-             if (j > i && rv < 0)
+             for (j = 0; j < tm->nitems; j++)
                {
-                 clib_warning ("i %d j %d search not ok but should be",
-                               i, j);
+                 kv.key = tm->keys[j];
+                 rv = BV (clib_bihash_search) (h, &kv, &kv);
+                 if (j <= i && rv >= 0)
+                   {
+                     clib_warning
+                       ("i %d j %d search ok but should not be, value %lld",
+                        i, j, kv.value);
+                   }
+                 if (j > i && rv < 0)
+                   {
+                     clib_warning ("i %d j %d search not ok but should be",
+                                   i, j);
+                   }
                }
            }
        }
+      if ((acycle % tm->report_every_n) == 0)
+       {
+         struct rusage r_usage;
+         getrusage (RUSAGE_SELF, &r_usage);
+         fformat (stdout, "Kernel RSS: %ld bytes\n", r_usage.ru_maxrss);
+         fformat (stdout, "%U\n", BV (format_bihash), h, 0 /* verbose */ );
+       }
+
+      /* Clean up side-bet hash table and random key vector */
+      for (i = 0; i < tm->nitems; i++)
+       hash_unset (tm->key_hash, tm->keys[i]);
+
+      vec_reset_length (tm->keys);
     }
 
-  fformat (stdout, "After deletions, should be empty...\n");
+  fformat (stdout, "End of run, should be empty...\n");
 
   fformat (stdout, "%U", BV (format_bihash), h, 0 /* very verbose */ );
   return 0;
 }
 
+clib_error_t *
+test_bihash_cache (test_main_t * tm)
+{
+  u32 lru;
+  BVT (clib_bihash_bucket) _b, *b = &_b;
+
+  BV (clib_bihash_reset_cache) (b);
+
+  fformat (stdout, "Initial LRU config: %U\n", BV (format_bihash_lru), b);
+
+  BV (clib_bihash_update_lru_not_inline) (b, 3);
+
+  fformat (stdout, "use slot 3, LRU config: %U\n", BV (format_bihash_lru), b);
+
+  BV (clib_bihash_update_lru) (b, 1);
+
+  fformat (stdout, "use slot 1 LRU config: %U\n", BV (format_bihash_lru), b);
+
+  lru = BV (clib_bihash_get_lru) (b);
+
+  fformat (stdout, "least-recently-used is %d\n", lru);
+
+  BV (clib_bihash_update_lru) (b, 4);
+
+  fformat (stdout, "use slot 4 LRU config: %U\n", BV (format_bihash_lru), b);
+
+  lru = BV (clib_bihash_get_lru) (b);
+
+  fformat (stdout, "least-recently-used is %d\n", lru);
+
+  return 0;
+}
+
 clib_error_t *
 test_bihash_main (test_main_t * tm)
 {
   unformat_input_t *i = tm->input;
   clib_error_t *error;
+  int which = 0;
+
+  tm->report_every_n = 1;
 
   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
     {
@@ -236,12 +334,21 @@ test_bihash_main (test_main_t * tm)
        tm->non_random_keys = 1;
       else if (unformat (i, "nitems %d", &tm->nitems))
        ;
+      else if (unformat (i, "ncycles %d", &tm->ncycles))
+       ;
       else if (unformat (i, "careful %d", &tm->careful_delete_tests))
        ;
       else if (unformat (i, "verbose %d", &tm->verbose))
        ;
       else if (unformat (i, "search %d", &tm->search_iter))
        ;
+      else if (unformat (i, "report-every %d", &tm->report_every_n))
+       ;
+      else if (unformat (i, "vec64"))
+       which = 1;
+      else if (unformat (i, "cache"))
+       which = 2;
+
       else if (unformat (i, "verbose"))
        tm->verbose = 1;
       else
@@ -249,7 +356,23 @@ test_bihash_main (test_main_t * tm)
                                  format_unformat_error, i);
     }
 
-  error = test_bihash (tm);
+  switch (which)
+    {
+    case 0:
+      error = test_bihash (tm);
+      break;
+
+    case 1:
+      error = test_bihash_vec64 (tm);
+      break;
+
+    case 2:
+      error = test_bihash_cache (tm);
+      break;
+
+    default:
+      return clib_error_return (0, "no such test?");
+    }
 
   return error;
 }
@@ -269,6 +392,7 @@ main (int argc, char *argv[])
 
   tm->nbuckets = 2;
   tm->nitems = 5;
+  tm->ncycles = 1;
   tm->verbose = 1;
   tm->search_iter = 1;
   tm->careful_delete_tests = 0;