tw: add light weight timer update function

Because it avoids pool putting/getting the timer, this function is
somewhat faster than stopping and restarting a timer.

Change-Id: Id99ed9d356b0b1f7e12facfe8da193e1cd30b3ec
Signed-off-by: Florin Coras <fcoras@cisco.com>
diff --git a/src/vnet/tcp/tcp.h b/src/vnet/tcp/tcp.h
index ba57e3a..1fd03c9 100644
--- a/src/vnet/tcp/tcp.h
+++ b/src/vnet/tcp/tcp.h
@@ -721,13 +721,14 @@
 {
   ASSERT (tc->c_thread_index == vlib_get_thread_index ());
   if (tc->timers[timer_id] != TCP_TIMER_HANDLE_INVALID)
-    tw_timer_stop_16t_2w_512sl (&tcp_main.
-				wrk_ctx[tc->c_thread_index].timer_wheel,
-				tc->timers[timer_id]);
-  tc->timers[timer_id] =
-    tw_timer_start_16t_2w_512sl (&tcp_main.
-				 wrk_ctx[tc->c_thread_index].timer_wheel,
-				 tc->c_c_index, timer_id, interval);
+    tw_timer_update_16t_2w_512sl (&tcp_main.
+				  wrk_ctx[tc->c_thread_index].timer_wheel,
+				  tc->timers[timer_id], interval);
+  else
+    tc->timers[timer_id] =
+      tw_timer_start_16t_2w_512sl (&tcp_main.
+				   wrk_ctx[tc->c_thread_index].timer_wheel,
+				   tc->c_c_index, timer_id, interval);
 }
 
 always_inline void
diff --git a/src/vpp/api/test_client.c b/src/vpp/api/test_client.c
index f0feee0..98cc18e 100644
--- a/src/vpp/api/test_client.c
+++ b/src/vpp/api/test_client.c
@@ -918,15 +918,15 @@
   mp->_vl_msg_id = ntohs (VL_API_PROXY_ARP_ADD_DEL);
   mp->client_index = tm->my_client_index;
   mp->context = 0xdeadbeef;
-  mp->vrf_id = ntohl (11);
+  mp->proxy.vrf_id = ntohl (11);
   mp->is_add = is_add;
 
   /* proxy fib 11, 1.1.1.1 -> 1.1.1.10 */
   tmp = ntohl (0x01010101);
-  clib_memcpy (mp->low_address, &tmp, 4);
+  clib_memcpy (mp->proxy.low_address, &tmp, 4);
 
   tmp = ntohl (0x0101010a);
-  clib_memcpy (mp->hi_address, &tmp, 4);
+  clib_memcpy (mp->proxy.hi_address, &tmp, 4);
 
   vl_msg_api_send_shmem (tm->vl_input_queue, (u8 *) & mp);
 }
diff --git a/src/vppinfra/cuckoo_template.h b/src/vppinfra/cuckoo_template.h
index b53a089..eccbde1 100644
--- a/src/vppinfra/cuckoo_template.h
+++ b/src/vppinfra/cuckoo_template.h
@@ -154,6 +154,8 @@
 
 typedef struct
 {
+  CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
+
   /** reduced hashes corresponding to elements */
   u8 reduced_hashes[CLIB_CUCKOO_KVP_PER_BUCKET];
 
diff --git a/src/vppinfra/test_tw_timer.c b/src/vppinfra/test_tw_timer.c
index ec0baa0..bde958f 100644
--- a/src/vppinfra/test_tw_timer.c
+++ b/src/vppinfra/test_tw_timer.c
@@ -482,6 +482,117 @@
   return 0;
 }
 
+static u32
+get_expiration_time (tw_timer_test_main_t * tm)
+{
+  u32 expiration_time;
+  do
+    {
+      expiration_time = random_u64 (&tm->seed) & ((1 << 17) - 1);
+    }
+  while (expiration_time == 0);
+  return expiration_time;
+}
+
+static clib_error_t *
+test2_double_updates (tw_timer_test_main_t * tm)
+{
+  u32 i, j;
+  tw_timer_test_elt_t *e;
+  u32 initial_wheel_offset;
+  u32 expiration_time;
+  u32 max_expiration_time = 0, updates = 0;
+  f64 before, after;
+
+  clib_time_init (&tm->clib_time);
+  tw_timer_wheel_init_16t_2w_512sl (&tm->double_wheel,
+				    expired_timer_double_callback,
+				    1.0 /* timer interval */ , ~0);
+
+  /* Prime offset */
+  initial_wheel_offset = 7577;
+  run_double_wheel (&tm->double_wheel, initial_wheel_offset);
+  fformat (stdout, "initial wheel time %d, fast index %d slow index %d\n",
+	   tm->double_wheel.current_tick,
+	   tm->double_wheel.current_index[TW_TIMER_RING_FAST],
+	   tm->double_wheel.current_index[TW_TIMER_RING_SLOW]);
+
+  initial_wheel_offset = tm->double_wheel.current_tick;
+  fformat (stdout,
+	   "test %d timers, %d iter, %d ticks per iter, 0x%llx seed\n",
+	   tm->ntimers, tm->niter, tm->ticks_per_iter, tm->seed);
+
+  before = clib_time_now (&tm->clib_time);
+
+  /* Prime the pump */
+  for (i = 0; i < tm->ntimers; i++)
+    {
+      pool_get (tm->test_elts, e);
+      memset (e, 0, sizeof (*e));
+
+      expiration_time = get_expiration_time (tm);
+      max_expiration_time = clib_max (expiration_time, max_expiration_time);
+
+      e->expected_to_expire = expiration_time + initial_wheel_offset;
+      e->stop_timer_handle = tw_timer_start_16t_2w_512sl (&tm->double_wheel,
+							  e - tm->test_elts,
+							  14 /* timer id */ ,
+							  expiration_time);
+    }
+
+  for (i = 0; i < tm->niter; i++)
+    {
+      run_double_wheel (&tm->double_wheel, tm->ticks_per_iter);
+
+      j = 0;
+
+      /* *INDENT-OFF* */
+      pool_foreach (e, tm->test_elts,
+      ({
+        expiration_time = get_expiration_time (tm);
+        max_expiration_time = clib_max (expiration_time, max_expiration_time);
+	e->expected_to_expire = expiration_time
+				+ tm->double_wheel.current_tick;
+        tw_timer_update_16t_2w_512sl (&tm->double_wheel, e->stop_timer_handle,
+                                      expiration_time);
+        if (++j >= tm->ntimers / 4)
+          goto done;
+      }));
+      /* *INDENT-ON* */
+
+    done:
+      updates += j;
+    }
+
+  run_double_wheel (&tm->double_wheel, max_expiration_time + 1);
+
+  after = clib_time_now (&tm->clib_time);
+
+  fformat (stdout, "%d updates, %d ticks\n", updates,
+	   tm->double_wheel.current_tick);
+  fformat (stdout, "test ran %.2f seconds, %.2f ops/second\n",
+	   (after - before),
+	   ((f64) updates + (f64) tm->double_wheel.current_tick) / (after -
+								    before));
+
+  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* */
+
+  pool_free (tm->test_elts);
+  tw_timer_wheel_free_16t_2w_512sl (&tm->double_wheel);
+  return 0;
+}
+
 static clib_error_t *
 test2_triple (tw_timer_test_main_t * tm)
 {
@@ -1141,7 +1252,7 @@
 timer_test_command_fn (tw_timer_test_main_t * tm, unformat_input_t * input)
 {
 
-  int is_test1 = 0;
+  int is_test1 = 0, is_updates = 0;
   int num_wheels = 1;
   int is_test2 = 0;
   int is_test3 = 0;
@@ -1172,6 +1283,8 @@
 	is_test4 = 1;
       else if (unformat (input, "linear"))
 	is_test5 = 1;
+      else if (unformat (input, "updates"))
+	is_updates = 1;
       else if (unformat (input, "wheels %d", &num_wheels))
 	;
       else if (unformat (input, "ntimers %d", &tm->ntimers))
@@ -1202,7 +1315,10 @@
       if (num_wheels == 1)
 	return test2_single (tm);
       else if (num_wheels == 2)
-	return test2_double (tm);
+	if (is_updates)
+	  return test2_double_updates (tm);
+	else
+	  return test2_double (tm);
       else if (num_wheels == 3)
 	{
 	  if (overflow == 0)
diff --git a/src/vppinfra/tw_timer_template.c b/src/vppinfra/tw_timer_template.c
index a6d26d7..da40e2c 100644
--- a/src/vppinfra/tw_timer_template.c
+++ b/src/vppinfra/tw_timer_template.c
@@ -142,9 +142,8 @@
 }
 
 static inline void
-timer_remove (TWT (tw_timer) * pool, u32 index)
+timer_remove (TWT (tw_timer) * pool, TWT (tw_timer) * elt)
 {
-  TWT (tw_timer) * elt = pool_elt_at_index (pool, index);
   TWT (tw_timer) * next_elt, *prev_elt;
 
   ASSERT (elt->user_handle != ~0);
@@ -158,17 +157,8 @@
   elt->prev = elt->next = ~0;
 }
 
-/**
- * @brief Start a Tw Timer
- * @param tw_timer_wheel_t * tw timer wheel object pointer
- * @param u32 pool_index user pool index, presumably for a tw session
- * @param u32 timer_id app-specific timer ID. 4 bits.
- * @param u64 interval timer interval in ticks
- * @returns handle needed to cancel the timer
- */
-u32
-TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
-		     u64 interval)
+static void
+timer_add (TWT (tw_timer_wheel) * tw, TWT (tw_timer) * t, u64 interval)
 {
 #if TW_TIMER_WHEELS > 1
   u16 slow_ring_offset;
@@ -182,14 +172,6 @@
 #endif
   u16 fast_ring_offset;
   tw_timer_wheel_slot_t *ts;
-  TWT (tw_timer) * t;
-
-  ASSERT (interval);
-
-  pool_get (tw->timers, t);
-  memset (t, 0xff, sizeof (*t));
-
-  t->user_handle = TW (make_internal_timer_handle) (pool_index, timer_id);
 
   /* Factor interval into 1..3 wheel offsets */
 #if TW_TIMER_WHEELS > 2
@@ -209,9 +191,9 @@
       ts = &tw->overflow;
       timer_addhead (tw->timers, ts->head_index, t - tw->timers);
 #if TW_START_STOP_TRACE_SIZE > 0
-      TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers);
+      TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
 #endif
-      return t - tw->timers;
+      return;
     }
 #endif
 
@@ -262,9 +244,9 @@
 
       timer_addhead (tw->timers, ts->head_index, t - tw->timers);
 #if TW_START_STOP_TRACE_SIZE > 0
-      TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers);
+      TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
 #endif
-      return t - tw->timers;
+      return;
     }
 #endif
 
@@ -280,9 +262,9 @@
 
       timer_addhead (tw->timers, ts->head_index, t - tw->timers);
 #if TW_START_STOP_TRACE_SIZE > 0
-      TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers);
+      TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
 #endif
-      return t - tw->timers;
+      return;
     }
 #else
   fast_ring_offset %= TW_SLOTS_PER_RING;
@@ -298,8 +280,32 @@
 					  fast_ring_offset, 1);
 #endif
 #if TW_START_STOP_TRACE_SIZE > 0
-  TW (tw_timer_trace) (tw, timer_id, pool_index, t - tw->timers);
+  TW (tw_timer_trace) (tw, timer_id, user_id, t - tw->timers);
 #endif
+}
+
+/**
+ * @brief Start a Tw Timer
+ * @param tw_timer_wheel_t * tw timer wheel object pointer
+ * @param u32 user_id user defined timer id, presumably for a tw session
+ * @param u32 timer_id app-specific timer ID. 4 bits.
+ * @param u64 interval timer interval in ticks
+ * @returns handle needed to cancel the timer
+ */
+u32
+TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 user_id, u32 timer_id,
+		     u64 interval)
+{
+  TWT (tw_timer) * t;
+
+  ASSERT (interval);
+
+  pool_get (tw->timers, t);
+  memset (t, 0xff, sizeof (*t));
+
+  t->user_handle = TW (make_internal_timer_handle) (user_id, timer_id);
+
+  timer_add (tw, t, interval);
   return t - tw->timers;
 }
 
@@ -365,12 +371,27 @@
   /* in case of idiotic handle (e.g. passing a listhead index) */
   ASSERT (t->user_handle != ~0);
 
-  timer_remove (tw->timers, handle);
+  timer_remove (tw->timers, t);
 
   pool_put_index (tw->timers, handle);
 }
 
 /**
+ * @brief Update a tw timer
+ * @param tw_timer_wheel_t * tw timer wheel object pointer
+ * @param u32 handle timer returned by tw_timer_start
+ * @param u32 interval timer interval in ticks
+ */
+void TW (tw_timer_update) (TWT (tw_timer_wheel) * tw, u32 handle,
+			   u64 interval)
+{
+  TWT (tw_timer) * t;
+  t = pool_elt_at_index (tw->timers, handle);
+  timer_remove (tw->timers, t);
+  timer_add (tw, t, interval);
+}
+
+/**
  * @brief Initialize a tw timer wheel template instance
  * @param tw_timer_wheel_t * tw timer wheel object pointer
  * @param void * expired_timer_callback. Passed a u32 * vector of
diff --git a/src/vppinfra/tw_timer_template.h b/src/vppinfra/tw_timer_template.h
index 0217644..b5cdac0 100644
--- a/src/vppinfra/tw_timer_template.h
+++ b/src/vppinfra/tw_timer_template.h
@@ -238,6 +238,8 @@
 			 u32 pool_index, u32 timer_id, u64 interval);
 
 void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle);
+void TW (tw_timer_update) (TWT (tw_timer_wheel) * tw, u32 handle,
+			   u64 interval);
 
 void TW (tw_timer_wheel_init) (TWT (tw_timer_wheel) * tw,
 			       void *expired_timer_callback,