aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra
diff options
context:
space:
mode:
Diffstat (limited to 'src/vppinfra')
-rw-r--r--src/vppinfra/cuckoo_template.h2
-rw-r--r--src/vppinfra/test_tw_timer.c120
-rw-r--r--src/vppinfra/tw_timer_template.c79
-rw-r--r--src/vppinfra/tw_timer_template.h2
4 files changed, 172 insertions, 31 deletions
diff --git a/src/vppinfra/cuckoo_template.h b/src/vppinfra/cuckoo_template.h
index b53a089771d..eccbde108c4 100644
--- a/src/vppinfra/cuckoo_template.h
+++ b/src/vppinfra/cuckoo_template.h
@@ -154,6 +154,8 @@ typedef struct
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 ec0baa07fad..bde958f3ca2 100644
--- a/src/vppinfra/test_tw_timer.c
+++ b/src/vppinfra/test_tw_timer.c
@@ -482,6 +482,117 @@ test2_double (tw_timer_test_main_t * tm)
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 @@ static clib_error_t *
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 @@ timer_test_command_fn (tw_timer_test_main_t * tm, unformat_input_t * input)
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 @@ timer_test_command_fn (tw_timer_test_main_t * tm, unformat_input_t * input)
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 a6d26d7991d..da40e2c0b3e 100644
--- a/src/vppinfra/tw_timer_template.c
+++ b/src/vppinfra/tw_timer_template.c
@@ -142,9 +142,8 @@ timer_addhead (TWT (tw_timer) * pool, u32 head_index, u32 new_index)
}
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 @@ timer_remove (TWT (tw_timer) * pool, u32 index)
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 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
#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 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
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 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
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 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
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 @@ TW (tw_timer_start) (TWT (tw_timer_wheel) * tw, u32 pool_index, u32 timer_id,
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 @@ void TW (tw_timer_stop) (TWT (tw_timer_wheel) * tw, u32 handle)
/* 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 0217644df6e..b5cdac05506 100644
--- a/src/vppinfra/tw_timer_template.h
+++ b/src/vppinfra/tw_timer_template.h
@@ -238,6 +238,8 @@ u32 TW (tw_timer_start) (TWT (tw_timer_wheel) * tw,
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,