svm: remove fifo segment heap
[vpp.git] / src / plugins / unittest / svm_fifo_test.c
index ccaffa1..a223e13 100644 (file)
@@ -190,11 +190,13 @@ static svm_fifo_t *
 fifo_prepare (fifo_segment_t * fs, u32 fifo_size)
 {
   svm_fifo_t *f;
+  svm_fifo_chunk_t *c;
 
   f = fifo_segment_alloc_fifo (fs, fifo_size, FIFO_SEGMENT_RX_FIFO);
 
-  /* Paint fifo data vector with -1's */
-  clib_memset (svm_fifo_head_chunk (f)->data, 0xFF, fifo_size);
+  /* Paint 1st fifo chunk with -1's */
+  c = svm_fifo_head_chunk (f);
+  clib_memset (c->data, 0xFF, c->length);
 
   svm_fifo_init_ooo_lookup (f, 1 /* deq ooo */ );
   return f;
@@ -1596,9 +1598,12 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input)
   SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
 
   /*
-   * Dequeue just a part of data
+   * Dequeue just a part of data. Because we're tracking ooo data, we can't
+   * call dequeue. Therefore, first peek and then dequeue drop
    */
-  rv = svm_fifo_dequeue (f, fifo_inc, data_buf);
+  rv = svm_fifo_peek (f, 0, fifo_inc, data_buf);
+  SFIFO_TEST (rv == fifo_inc, "should dequeue all data");
+  rv = svm_fifo_dequeue_drop (f, fifo_inc);
   SFIFO_TEST (rv == fifo_inc, "should dequeue all data");
   rv = svm_fifo_n_chunks (f);
   SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
@@ -1624,10 +1629,11 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input)
   SFIFO_TEST (c->length == 4096, "end chunk length should be %u", 4096);
 
   /*
-   * Dequeue all
+   * Dequeue all. Don't call dequeue see above
    */
-  rv = svm_fifo_dequeue (f, fifo_size, data_buf + fifo_inc);
+  rv = svm_fifo_peek (f, 0, fifo_size, data_buf + fifo_inc);
   SFIFO_TEST (rv == fifo_size, "should dequeue all data");
+  SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
 
   rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
                     (u32 *) & i);
@@ -1635,6 +1641,10 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input)
     vlib_cli_output (vm, "[%d] dequeued %u expected %u", i, data_buf[i],
                     test_data[i]);
   SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
+
+  rv = svm_fifo_dequeue_drop (f, fifo_size);
+  SFIFO_TEST (rv == fifo_size, "should dequeue all data");
+
   SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
   /* fifo does not end on chunk boundary because of the - 100 */
   SFIFO_TEST (f->head_chunk != 0, "should have head chunk");
@@ -1711,7 +1721,10 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input)
   /*
    * Dequeue all
    */
-  rv = svm_fifo_dequeue (f, fifo_size, data_buf);
+
+  /* Because we're tracking ooo data, we can't call dequeue. Therefore,
+   * first peek and then dequeue drop */
+  rv = svm_fifo_peek (f, 0, fifo_size, data_buf);
   SFIFO_TEST (rv == fifo_size, "should dequeue all data");
 
   rv = compare_data (data_buf, test_data, 0, vec_len (test_data),
@@ -1721,6 +1734,11 @@ sfifo_test_fifo_grow (vlib_main_t * vm, unformat_input_t * input)
                     test_data[i]);
   SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
   SFIFO_TEST ((rv == 0), "dequeued compared to original returned %d", rv);
+
+
+  rv = svm_fifo_dequeue_drop (f, fifo_size);
+  SFIFO_TEST ((rv == fifo_size), "all bytes should be dropped %u", rv);
+  SFIFO_TEST (svm_fifo_is_sane (f), "fifo should be sane");
   SFIFO_TEST (f->ooo_deq == 0, "should have no ooo deq chunk");
   rv = svm_fifo_n_chunks (f);
   SFIFO_TEST (rv == 1, "should have %u chunks has %u", 1, rv);
@@ -2517,6 +2535,14 @@ sfifo_test_fifo_segment_mempig (int verbose)
   return 0;
 }
 
+static int
+approx_leq (uword a, uword b, u32 margin)
+{
+  if (a - margin <= b && b <= a)
+    return 1;
+  return 0;
+}
+
 static int
 sfifo_test_fifo_segment_prealloc (int verbose)
 {
@@ -2550,8 +2576,10 @@ sfifo_test_fifo_segment_prealloc (int verbose)
   SFIFO_TEST (rv == 50, "prealloc chunks expected %u is %u", 50, rv);
   rv = fifo_segment_free_bytes (fs);
   free_space -= (sizeof (svm_fifo_chunk_t) + 4096) * 50;
-  SFIFO_TEST (rv == free_space, "free space expected %u is %u", free_space,
-             rv);
+  /* Memory alloc alignment accounts for the difference */
+  SFIFO_TEST (approx_leq (free_space, rv, 16), "free space expected %u is %u",
+             free_space, rv);
+  free_space = rv;
   rv = fifo_segment_fl_chunk_bytes (fs);
   SFIFO_TEST (rv == 4096 * 50, "chunk free space expected %u is %u",
              4096 * 50, rv);
@@ -2562,10 +2590,11 @@ sfifo_test_fifo_segment_prealloc (int verbose)
   SFIFO_TEST (rv == 50, "prealloc fifo hdrs expected %u is %u", 50, rv);
   rv = fifo_segment_free_bytes (fs);
   free_space -= sizeof (svm_fifo_t) * 50;
-  SFIFO_TEST (rv == free_space, "free space expected %u is %u", free_space,
-             rv);
+  /* Memory alloc alignment accounts for the difference */
+  SFIFO_TEST (approx_leq (free_space, rv, 16), "free space expected %u is %u",
+             free_space, rv);
+  free_space = rv;
 
-  fifo_segment_update_free_bytes (fs);
   rv = fifo_segment_free_bytes (fs);
   SFIFO_TEST (clib_abs (rv - (int) free_space) < 512,
              "free space expected %u is %u", free_space, rv);
@@ -2588,7 +2617,6 @@ sfifo_test_fifo_segment_prealloc (int verbose)
   /*
    * Multiple preallocs that consume the remaining space
    */
-  fifo_segment_update_free_bytes (fs);
   free_space = fifo_segment_free_bytes (fs);
   pair_mem = 2 * (4096 + sizeof (*f) + sizeof (svm_fifo_chunk_t));
   max_pairs = pairs_req = (free_space / pair_mem) - 1;
@@ -2598,7 +2626,6 @@ sfifo_test_fifo_segment_prealloc (int verbose)
   SFIFO_TEST (rv == max_pairs * 2, "prealloc chunks expected %u is %u",
              max_pairs * 2, rv);
 
-  fifo_segment_update_free_bytes (fs);
   rv = fifo_segment_free_bytes (fs);
   SFIFO_TEST (rv < 2 * pair_mem, "free bytes %u less than %u", rv,
              2 * pair_mem);
@@ -2710,8 +2737,8 @@ svm_fifo_test (vlib_main_t * vm, unformat_input_t * input,
   int res = 0;
   char *str;
 
-  clib_warning ("high mem %lu", HIGH_SEGMENT_BASEVA << 1);
-  fifo_segment_main_init (&segment_main, HIGH_SEGMENT_BASEVA << 1, 5);
+  clib_warning ("high mem %lu", HIGH_SEGMENT_BASEVA);
+  fifo_segment_main_init (&segment_main, HIGH_SEGMENT_BASEVA, 5);
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
       if (unformat (input, "fifo1"))