X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvppinfra%2Ftest_tw_timer.c;h=47e5e49bf1ff27d762dde8564c10d169eee92073;hb=5c1e48c01b50ddbd7623228e3dbc94d835d23813;hp=ec0baa07faddd6364686e2bbd974f214c17259ee;hpb=4af9ba1dabe3dbd4a2dd3d8c71434477c5ea81b9;p=vpp.git diff --git a/src/vppinfra/test_tw_timer.c b/src/vppinfra/test_tw_timer.c index ec0baa07fad..47e5e49bf1f 100644 --- a/src/vppinfra/test_tw_timer.c +++ b/src/vppinfra/test_tw_timer.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,9 @@ typedef struct /* 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; @@ -76,6 +80,19 @@ run_double_wheel (tw_timer_wheel_16t_2w_512sl_t * tw, u32 n_ticks) } } +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) { @@ -156,6 +173,33 @@ expired_timer_double_callback (u32 * expired_timers) } } +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) { @@ -246,7 +290,7 @@ test2_single (tw_timer_test_main_t * tm) for (i = 0; i < tm->ntimers; i++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); do { @@ -273,13 +317,13 @@ test2_single (tw_timer_test_main_t * tm) j = 0; vec_reset_length (deleted_indices); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + pool_foreach (e, tm->test_elts) + { tw_timer_stop_2t_1w_2048sl (&tm->single_wheel, e->stop_timer_handle); vec_add1 (deleted_indices, e - tm->test_elts); if (++j >= tm->ntimers / 4) goto del_and_re_add; - })); + } /* *INDENT-ON* */ del_and_re_add: @@ -293,7 +337,7 @@ test2_single (tw_timer_test_main_t * tm) for (j = 0; j < tm->ntimers / 4; j++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); do { @@ -331,12 +375,12 @@ test2_single (tw_timer_test_main_t * tm) pool_elts (tm->test_elts)); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + 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); @@ -384,7 +428,7 @@ test2_double (tw_timer_test_main_t * tm) for (i = 0; i < tm->ntimers; i++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); do { @@ -412,13 +456,13 @@ test2_double (tw_timer_test_main_t * tm) j = 0; vec_reset_length (deleted_indices); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + pool_foreach (e, tm->test_elts) + { tw_timer_stop_16t_2w_512sl (&tm->double_wheel, e->stop_timer_handle); vec_add1 (deleted_indices, e - tm->test_elts); if (++j >= tm->ntimers / 4) goto del_and_re_add; - })); + } /* *INDENT-ON* */ del_and_re_add: @@ -430,7 +474,7 @@ test2_double (tw_timer_test_main_t * tm) for (j = 0; j < tm->ntimers / 4; j++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); do { @@ -469,12 +513,123 @@ test2_double (tw_timer_test_main_t * tm) pool_elts (tm->test_elts)); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + 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 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); + clib_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); @@ -524,7 +679,7 @@ test2_triple (tw_timer_test_main_t * tm) for (i = 0; i < tm->ntimers; i++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); do { @@ -552,13 +707,13 @@ test2_triple (tw_timer_test_main_t * tm) j = 0; vec_reset_length (deleted_indices); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + pool_foreach (e, tm->test_elts) + { tw_timer_stop_4t_3w_256sl (&tm->triple_wheel, e->stop_timer_handle); vec_add1 (deleted_indices, e - tm->test_elts); if (++j >= tm->ntimers / 4) goto del_and_re_add; - })); + } /* *INDENT-ON* */ del_and_re_add: @@ -570,7 +725,7 @@ test2_triple (tw_timer_test_main_t * tm) for (j = 0; j < tm->ntimers / 4; j++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); do { @@ -609,12 +764,12 @@ test2_triple (tw_timer_test_main_t * tm) pool_elts (tm->test_elts)); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + 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); @@ -664,7 +819,7 @@ test2_triple_ov (tw_timer_test_main_t * tm) for (i = 0; i < tm->ntimers; i++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); do { @@ -692,14 +847,14 @@ test2_triple_ov (tw_timer_test_main_t * tm) j = 0; vec_reset_length (deleted_indices); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + pool_foreach (e, tm->test_elts) + { tw_timer_stop_1t_3w_1024sl_ov (&tm->triple_ov_wheel, e->stop_timer_handle); vec_add1 (deleted_indices, e - tm->test_elts); if (++j >= tm->ntimers / 4) goto del_and_re_add; - })); + } /* *INDENT-ON* */ del_and_re_add: @@ -711,7 +866,7 @@ test2_triple_ov (tw_timer_test_main_t * tm) for (j = 0; j < tm->ntimers / 4; j++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); do { @@ -750,8 +905,8 @@ test2_triple_ov (tw_timer_test_main_t * tm) pool_elts (tm->test_elts)); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + pool_foreach (e, tm->test_elts) + { TWT (tw_timer) * t; fformat (stdout, "[%d] expected to expire %d\n", @@ -759,7 +914,7 @@ test2_triple_ov (tw_timer_test_main_t * tm) e->expected_to_expire); t = pool_elt_at_index (tm->triple_ov_wheel.timers, e->stop_timer_handle); fformat (stdout, " expiration_time %lld\n", t->expiration_time); - })); + } /* *INDENT-ON* */ pool_free (tm->test_elts); @@ -802,10 +957,10 @@ test1_single (tw_timer_test_main_t * tm) 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); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); e->expected_to_expire = expected_to_expire; e->stop_timer_handle = tw_timer_start_2t_1w_2048sl (&tm->single_wheel, e - tm->test_elts, 1 /* timer id */ , @@ -818,12 +973,12 @@ test1_single (tw_timer_test_main_t * tm) pool_elts (tm->test_elts)); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + 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, @@ -862,9 +1017,9 @@ test1_double (tw_timer_test_main_t * tm) for (i = 0; i < tm->ntimers; i++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*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); @@ -876,12 +1031,12 @@ test1_double (tw_timer_test_main_t * tm) pool_elts (tm->test_elts)); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + 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, @@ -894,6 +1049,64 @@ test1_double (tw_timer_test_main_t * tm) return 0; } +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) { @@ -928,7 +1141,7 @@ test3_triple_double (tw_timer_test_main_t * tm) /* Prime the pump */ pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); /* 1 glacier ring tick from now */ expiration_time = TW_SLOTS_PER_RING * TW_SLOTS_PER_RING; @@ -956,12 +1169,12 @@ test3_triple_double (tw_timer_test_main_t * tm) pool_elts (tm->test_elts)); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + 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); @@ -1006,7 +1219,7 @@ test4_double_double (tw_timer_test_main_t * tm) for (i = 0; i < tm->ntimers; i++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); expiration_time = 512; @@ -1040,12 +1253,12 @@ test4_double_double (tw_timer_test_main_t * tm) pool_elts (tm->test_elts)); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + 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); @@ -1092,7 +1305,7 @@ test5_double (tw_timer_test_main_t * tm) for (i = 0; i < tm->ntimers; i++) { pool_get (tm->test_elts, e); - memset (e, 0, sizeof (*e)); + clib_memset (e, 0, sizeof (*e)); expiration_time = i + 1; @@ -1124,12 +1337,12 @@ test5_double (tw_timer_test_main_t * tm) pool_elts (tm->test_elts)); /* *INDENT-OFF* */ - pool_foreach (e, tm->test_elts, - ({ + 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); @@ -1141,7 +1354,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; @@ -1149,7 +1362,7 @@ timer_test_command_fn (tw_timer_test_main_t * tm, unformat_input_t * input) int is_test5 = 0; int overflow = 0; - memset (tm, 0, sizeof (*tm)); + clib_memset (tm, 0, sizeof (*tm)); /* Default values */ tm->ntimers = 100000; tm->seed = 0xDEADDABEB00BFACE; @@ -1172,6 +1385,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)) @@ -1195,14 +1410,20 @@ timer_test_command_fn (tw_timer_test_main_t * tm, unformat_input_t * input) 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) { 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)