vppinfra: add tw_timer_2t_2w_512sl variant

Type: feature
Signed-off-by: Dave Barach <dave@barachs.net>
Change-Id: I72cacfb5696dca74335f31415c0df795467615a5
diff --git a/src/vppinfra/CMakeLists.txt b/src/vppinfra/CMakeLists.txt
index 3e396e3..3128ad3 100644
--- a/src/vppinfra/CMakeLists.txt
+++ b/src/vppinfra/CMakeLists.txt
@@ -165,6 +165,7 @@
   time.h
   time_range.h
   timing_wheel.h
+  tw_timer_2t_2w_512sl.c
   tw_timer_16t_1w_2048sl.h
   tw_timer_16t_2w_512sl.h
   tw_timer_1t_3w_1024sl_ov.h
diff --git a/src/vppinfra/test_tw_timer.c b/src/vppinfra/test_tw_timer.c
index 90fbc9e..499cc01 100644
--- a/src/vppinfra/test_tw_timer.c
+++ b/src/vppinfra/test_tw_timer.c
@@ -2,6 +2,7 @@
 #include <vppinfra/cache.h>
 #include <vppinfra/error.h>
 #include <vppinfra/tw_timer_2t_1w_2048sl.h>
+#include <vppinfra/tw_timer_2t_2w_512sl.h>
 #include <vppinfra/tw_timer_16t_2w_512sl.h>
 #include <vppinfra/tw_timer_4t_3w_256sl.h>
 #include <vppinfra/tw_timer_1t_3w_1024sl_ov.h>
@@ -32,6 +33,9 @@
   /* The triple wheel with overflow vector */
   tw_timer_wheel_1t_3w_1024sl_ov_t triple_ov_wheel;
 
+  /* Another two timer wheel geometry */
+  tw_timer_wheel_2t_2w_512sl_t two_timer_double_wheel;
+
   /** random number seed */
   u64 seed;
 
@@ -77,6 +81,19 @@
 }
 
 static void
+run_two_timer_double_wheel (tw_timer_wheel_2t_2w_512sl_t * tw, u32 n_ticks)
+{
+  u32 i;
+  f64 now = tw->last_run_time + 1.01;
+
+  for (i = 0; i < n_ticks; i++)
+    {
+      tw_timer_expire_timers_2t_2w_512sl (tw, now);
+      now += 1.01;
+    }
+}
+
+static void
 run_triple_wheel (tw_timer_wheel_4t_3w_256sl_t * tw, u32 n_ticks)
 {
   u32 i;
@@ -157,6 +174,33 @@
 }
 
 static void
+expired_timer_two_timer_double_callback (u32 * expired_timers)
+{
+  int i;
+  u32 pool_index, timer_id;
+  tw_timer_test_elt_t *e;
+  tw_timer_test_main_t *tm = &tw_timer_test_main;
+
+  for (i = 0; i < vec_len (expired_timers); i++)
+    {
+      pool_index = expired_timers[i] & 0x7FFFFFFF;
+      timer_id = expired_timers[i] >> 31;
+
+      ASSERT (timer_id == 1);
+
+      e = pool_elt_at_index (tm->test_elts, pool_index);
+
+      if (e->expected_to_expire != tm->two_timer_double_wheel.current_tick)
+	{
+	  fformat (stdout, "[%d] expired at %lld not %lld\n",
+		   e - tm->test_elts, tm->two_timer_double_wheel.current_tick,
+		   e->expected_to_expire);
+	}
+      pool_put (tm->test_elts, e);
+    }
+}
+
+static void
 expired_timer_triple_callback (u32 * expired_timers)
 {
   int i;
@@ -913,7 +957,7 @@
       if (timer_arg == 0)
 	timer_arg = 1;
 
-      expected_to_expire = timer_arg + offset;
+      expected_to_expire = timer_arg + tm->single_wheel.current_tick;
 
       pool_get (tm->test_elts, e);
       clib_memset (e, 0, sizeof (*e));
@@ -975,7 +1019,7 @@
       pool_get (tm->test_elts, e);
       clib_memset (e, 0, sizeof (*e));
 
-      e->expected_to_expire = i + offset + 1;
+      e->expected_to_expire = i + tm->double_wheel.current_tick + 1;
       e->stop_timer_handle = tw_timer_start_16t_2w_512sl
 	(&tm->double_wheel, e - tm->test_elts, 14 /* timer id */ ,
 	 i + 1);
@@ -1006,6 +1050,64 @@
 }
 
 static clib_error_t *
+test1_two_timer_double (tw_timer_test_main_t * tm)
+{
+  u32 i;
+  tw_timer_test_elt_t *e;
+  u32 offset;
+
+  tw_timer_wheel_init_2t_2w_512sl (&tm->two_timer_double_wheel,
+				   expired_timer_two_timer_double_callback,
+				   1.0 /* timer interval */ , ~0);
+
+  /*
+   * Prime offset, to make sure that the wheel starts in a
+   * non-trivial position
+   */
+  offset = 2745;
+
+  run_two_timer_double_wheel (&tm->two_timer_double_wheel, offset);
+
+  fformat (stdout, "initial wheel time %d, fast index %d\n",
+	   tm->two_timer_double_wheel.current_tick,
+	   tm->two_timer_double_wheel.current_index[TW_TIMER_RING_FAST]);
+
+  for (i = 0; i < tm->ntimers; i++)
+    {
+      pool_get (tm->test_elts, e);
+      clib_memset (e, 0, sizeof (*e));
+
+      e->expected_to_expire = i + tm->two_timer_double_wheel.current_tick + 1;
+      e->stop_timer_handle = tw_timer_start_2t_2w_512sl
+	(&tm->two_timer_double_wheel, e - tm->test_elts, 1 /* timer id */ ,
+	 i + 1);
+    }
+  run_two_timer_double_wheel (&tm->two_timer_double_wheel, tm->ntimers + 3);
+
+  if (pool_elts (tm->test_elts))
+    fformat (stdout, "Note: %d elements remain in pool\n",
+	     pool_elts (tm->test_elts));
+
+  /* *INDENT-OFF* */
+  pool_foreach (e, tm->test_elts,
+  ({
+    fformat(stdout, "[%d] expected to expire %d\n",
+                     e - tm->test_elts,
+                     e->expected_to_expire);
+  }));
+  /* *INDENT-ON* */
+
+  fformat (stdout,
+	   "final wheel time %d, fast index %d\n",
+	   tm->two_timer_double_wheel.current_tick,
+	   tm->two_timer_double_wheel.current_index[TW_TIMER_RING_FAST]);
+
+  pool_free (tm->test_elts);
+  tw_timer_wheel_free_2t_2w_512sl (&tm->two_timer_double_wheel);
+  return 0;
+}
+
+static clib_error_t *
 test3_triple_double (tw_timer_test_main_t * tm)
 {
   tw_timer_test_elt_t *e;
@@ -1308,7 +1410,10 @@
       if (num_wheels == 1)
 	return test1_single (tm);
       else
-	return test1_double (tm);
+	{
+	  (void) test1_double (tm);
+	  return test1_two_timer_double (tm);
+	}
     }
   if (is_test2)
     {
diff --git a/src/vppinfra/tw_timer_2t_2w_512sl.c b/src/vppinfra/tw_timer_2t_2w_512sl.c
new file mode 100644
index 0000000..14a79c7
--- /dev/null
+++ b/src/vppinfra/tw_timer_2t_2w_512sl.c
@@ -0,0 +1,26 @@
+/*
+ * 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/error.h>
+#include "tw_timer_2t_2w_512sl.h"
+#include "tw_timer_template.c"
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vppinfra/tw_timer_2t_2w_512sl.h b/src/vppinfra/tw_timer_2t_2w_512sl.h
new file mode 100644
index 0000000..7ea88bd
--- /dev/null
+++ b/src/vppinfra/tw_timer_2t_2w_512sl.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2017-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.
+ */
+
+#ifndef __included_tw_timer_2t_2w_512sl_h__
+#define __included_tw_timer_2t_2w_512sl_h__
+
+/* ... So that a client app can create multiple wheel geometries */
+#undef TW_TIMER_WHEELS
+#undef TW_SLOTS_PER_RING
+#undef TW_RING_SHIFT
+#undef TW_RING_MASK
+#undef TW_TIMERS_PER_OBJECT
+#undef LOG2_TW_TIMERS_PER_OBJECT
+#undef TW_SUFFIX
+#undef TW_OVERFLOW_VECTOR
+#undef TW_FAST_WHEEL_BITMAP
+#undef TW_TIMER_ALLOW_DUPLICATE_STOP
+#undef TW_START_STOP_TRACE_SIZE
+
+#define TW_TIMER_WHEELS 2
+#define TW_SLOTS_PER_RING 512
+#define TW_RING_SHIFT 9
+#define TW_RING_MASK (TW_SLOTS_PER_RING -1)
+#define TW_TIMERS_PER_OBJECT 2
+#define LOG2_TW_TIMERS_PER_OBJECT 1
+#define TW_SUFFIX _2t_2w_512sl
+#define TW_FAST_WHEEL_BITMAP 0
+#define TW_TIMER_ALLOW_DUPLICATE_STOP 0
+
+#include <vppinfra/tw_timer_template.h>
+
+#endif /* __included_tw_timer_2t_2w_512sl_h__ */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */