VPP-1032: fix coverity warnings
[vpp.git] / src / vppinfra / test_bihash_template.c
index c505bd8..589c815 100644 (file)
@@ -47,6 +47,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)
 {
@@ -111,37 +148,17 @@ test_bihash (test_main_t * tm)
 
   for (j = 0; j < tm->search_iter; j++)
     {
-      u64 hash1 = clib_xxhash (tm->keys[0]);
-
       for (i = 0; i < tm->nitems; i++)
        {
-         if (i < (tm->nitems - 3))
-           {
-             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)
-           clib_warning ("search for key %lld failed unexpectedly\n",
-                         tm->keys[i]);
+           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 ("search for key %lld returned %lld, not %lld\n",
-                         tm->keys, 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));
        }
     }
 
@@ -219,11 +236,45 @@ test_bihash (test_main_t * tm)
   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;
 
   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
     {
@@ -242,6 +293,11 @@ test_bihash_main (test_main_t * tm)
        ;
       else if (unformat (i, "search %d", &tm->search_iter))
        ;
+      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 +305,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;
 }