vppinfra: mem bulk test 40/30540/3
authorFlorin Coras <fcoras@cisco.com>
Wed, 23 Dec 2020 18:18:16 +0000 (10:18 -0800)
committerFlorin Coras <fcoras@cisco.com>
Wed, 23 Dec 2020 18:40:08 +0000 (10:40 -0800)
Type: improvement

Signed-off-by: Florin Coras <fcoras@cisco.com>
Change-Id: Icd44ede9604c29839af250a2be93ecf467467aa0

src/plugins/unittest/CMakeLists.txt
src/plugins/unittest/mem_bulk_test.c [new file with mode: 0644]
src/vppinfra/mem_bulk.c

index ba6a822..2b358a5 100644 (file)
@@ -35,6 +35,7 @@ add_vpp_plugin(unittest
   ipsec_test.c
   llist_test.c
   mactime_test.c
+  mem_bulk_test.c
   mfib_test.c
   mpcap_node.c
   punt_test.c
diff --git a/src/plugins/unittest/mem_bulk_test.c b/src/plugins/unittest/mem_bulk_test.c
new file mode 100644 (file)
index 0000000..9d2f60d
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) 2020 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <vppinfra/mem.h>
+#include <vlib/vlib.h>
+
+#define MB_TEST_I(_cond, _comment, _args...)                                  \
+  ({                                                                          \
+    int _evald = (_cond);                                                     \
+    if (!(_evald))                                                            \
+      {                                                                       \
+       fformat (stderr, "FAIL:%d: " _comment "\n", __LINE__, ##_args);       \
+      }                                                                       \
+    else                                                                      \
+      {                                                                       \
+       fformat (stderr, "PASS:%d: " _comment "\n", __LINE__, ##_args);       \
+      }                                                                       \
+    _evald;                                                                   \
+  })
+
+#define MB_TEST(_cond, _comment, _args...)                                    \
+  {                                                                           \
+    if (!MB_TEST_I (_cond, _comment, ##_args))                                \
+      {                                                                       \
+       return 1;                                                             \
+      }                                                                       \
+  }
+
+typedef struct test_struct_
+{
+  u32 data;
+} test_struct_t;
+
+static int
+mem_bulk_test_basic (vlib_main_t *vm, unformat_input_t *input)
+{
+  int __clib_unused verbose, i, rv, n_iter = 1000;
+  test_struct_t *elt, **elts = 0;
+  clib_mem_bulk_handle_t mb;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "verbose"))
+       verbose = 1;
+      else
+       {
+         vlib_cli_output (vm, "parse error: '%U'", format_unformat_error,
+                          input);
+         return -1;
+       }
+    }
+
+  mb = clib_mem_bulk_init (sizeof (test_struct_t), 0, 0);
+
+  for (i = 0; i < n_iter; i++)
+    {
+      elt = clib_mem_bulk_alloc (mb);
+      vec_add1 (elts, elt);
+    }
+
+  for (i = 0; i < n_iter; i++)
+    elts[i]->data = i;
+
+  for (i = 0; i < n_iter; i++)
+    if (elts[i]->data != i)
+      MB_TEST (0, "data corrupted");
+
+  for (i = 0; i < n_iter; i++)
+    clib_mem_bulk_free (mb, elts[i]);
+
+  /*
+   * realloc all
+   */
+  for (i = 0; i < n_iter; i++)
+    {
+      elt = clib_mem_bulk_alloc (mb);
+      vec_add1 (elts, elt);
+    }
+
+  for (i = n_iter - 1; i >= 0; i--)
+    elts[i]->data = i;
+
+  for (i = n_iter - 1; i >= 0; i--)
+    if (elts[i]->data != i)
+      MB_TEST (0, "data corrupted");
+
+  for (i = 0; i < n_iter; i++)
+    clib_mem_bulk_free (mb, elts[i]);
+
+  clib_mem_bulk_destroy (mb);
+  vec_free (elts);
+
+  return 0;
+}
+
+static clib_error_t *
+mem_bulk_test (vlib_main_t *vm, unformat_input_t *input,
+              vlib_cli_command_t *cmd_arg)
+{
+  int res = 0;
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "basic"))
+       {
+         res = mem_bulk_test_basic (vm, input);
+       }
+      else if (unformat (input, "all"))
+       {
+         if ((res = mem_bulk_test_basic (vm, input)))
+           goto done;
+       }
+      else
+       break;
+    }
+
+done:
+  if (res)
+    return clib_error_return (0, "llist unit test failed");
+  return 0;
+}
+
+VLIB_CLI_COMMAND (mem_bulk_test_command, static) = {
+  .path = "test membulk",
+  .short_help = "internal membulk unit tests",
+  .function = mem_bulk_test,
+};
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
index 76fc758..2f236b4 100644 (file)
@@ -173,7 +173,7 @@ clib_mem_bulk_free (clib_mem_bulk_handle_t h, void *p)
   u32 elt_idx = (offset - b->chunk_hdr_sz) / b->elt_sz;
 
   ASSERT (elt_idx < b->elts_per_chunk);
-  ASSERT (get_chunk_elt_ptr (b, c, elt_idx) != p);
+  ASSERT (get_chunk_elt_ptr (b, c, elt_idx) == p);
 
   c->n_free++;