Skip to content

Commit

Permalink
packetdrill: consider relative time tolerance for timestamping
Browse files Browse the repository at this point in the history
Packetdrill checks difference between expected time and observed time
using a tolerance, which is set as the higher of a fixed value
(tolerance_usecs) or a percentage(tolerance_percent) times duration.

Extend this check for SCM_TIMESTAMPING records.
  • Loading branch information
sheepx86 committed Aug 13, 2020
1 parent 973756b commit c7413ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
12 changes: 4 additions & 8 deletions gtests/net/packetdrill/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ struct state *state_new(struct config *config,
state->code = code_new(config);
state->fds = NULL;
state->num_events = 0;
state->last_tcp_timestamp_usecs = NO_TIME_RANGE;
return state;
}

Expand Down Expand Up @@ -181,22 +182,17 @@ int verify_time(struct state *state, enum event_time_t time_type,
s64 expected_usecs_end = script_usecs_end -
state->script_start_time_usecs;
s64 actual_usecs = live_usecs - state->live_start_time_usecs;
long tolerance_usecs = state->config->tolerance_usecs;
s64 tolerance_usecs;

DEBUGP("expected: %.3f actual: %.3f (secs)\n",
usecs_to_secs(script_usecs), usecs_to_secs(actual_usecs));

if (time_type == ANY_TIME)
return STATUS_OK;

if (last_event_usecs != NO_TIME_RANGE) {
s64 delta = script_usecs - last_event_usecs;
long dynamic_tolerance;
tolerance_usecs = get_tolerance_usecs(state, script_usecs,
last_event_usecs);

dynamic_tolerance = (state->config->tolerance_percent / 100.0) * delta;
if (dynamic_tolerance > tolerance_usecs)
tolerance_usecs = dynamic_tolerance;
}
if (time_type == ABSOLUTE_RANGE_TIME ||
time_type == RELATIVE_RANGE_TIME) {
DEBUGP("expected_usecs_end %.3f\n",
Expand Down
21 changes: 21 additions & 0 deletions gtests/net/packetdrill/run.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ struct state {
s64 script_start_time_usecs; /* time of first event in script */
s64 script_last_time_usecs; /* time of previous event in script */
s64 live_start_time_usecs; /* time of first event in live test */
s64 last_tcp_timestamp_usecs; /* time of previous tcp_timestamp */
int num_events; /* events executed so far */
};

Expand Down Expand Up @@ -161,6 +162,26 @@ static inline s64 last_event_time_usecs(struct state *state)
state->last_event->time_usecs;
}

/*
* Return the greater between static and dynamic tolerance
* Static tolerance: state->config->tolerance_usecs
* Dynamic tolerance: state->config->tolerance_percent * time_delta
*/
static inline s64 get_tolerance_usecs(struct state *state, s64 script_usecs,
s64 last_event_usecs)
{
s64 tolerance_usecs = state->config->tolerance_usecs;

if (last_event_usecs != NO_TIME_RANGE) {
s64 delta = script_usecs - last_event_usecs;
s64 d_tol = (state->config->tolerance_percent / 100.0) * delta;

if (d_tol > tolerance_usecs)
tolerance_usecs = d_tol;
}
return tolerance_usecs;
}

/*
* See if something that happened at the given actual live wall time
* in microseconds happened reasonably close to the time at which we
Expand Down
19 changes: 7 additions & 12 deletions gtests/net/packetdrill/run_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1224,21 +1224,15 @@ static int verify_outbound_tcp_option(
struct config *config = state->config;
u32 script_ts_val, actual_ts_val;
int ts_val_tick_usecs;
long tolerance_usecs;
double dynamic_tolerance;
s64 delta;
s64 tolerance_usecs;

tolerance_usecs = config->tolerance_usecs;
/* Note that for TCP TS, we do not want to compute the tolerance based
* on last event (as we do in verify_time())
* last event might have happened few ms in the past.
* What matters here is the cumulative time (from the beginning of the test)
*/
delta = state->event->time_usecs - state->script_start_time_usecs;

dynamic_tolerance = (config->tolerance_percent / 100.0) * delta;
if (dynamic_tolerance > tolerance_usecs)
tolerance_usecs = dynamic_tolerance;
tolerance_usecs = get_tolerance_usecs(state, state->event->time_usecs,
state->script_start_time_usecs);

switch (actual_option->kind) {
case TCPOPT_EOL:
Expand All @@ -1255,9 +1249,10 @@ static int verify_outbound_tcp_option(
*/
if (ts_val_tick_usecs &&
((abs((s32)(actual_ts_val - script_ts_val)) *
ts_val_tick_usecs) >
tolerance_usecs)) {
asprintf(error, "bad outbound TCP timestamp value, tolerance %ld", tolerance_usecs);
ts_val_tick_usecs) > tolerance_usecs)) {
asprintf(error,
"bad outbound TCP timestamp value, tolerance %lld",
tolerance_usecs);
return STATUS_ERR;
}
break;
Expand Down
7 changes: 5 additions & 2 deletions gtests/net/packetdrill/run_system_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,12 @@ static bool scm_timestamping_expect_eq(struct state *state,
s64 exp_usecs = script_time_to_live_time_usecs(state,
timespec_to_usecs(&expected->ts[i]));
s64 actual_usecs = timespec_to_usecs(&actual->ts[i]);
s64 tolerance_usecs = get_tolerance_usecs(state, actual_usecs,
state->last_tcp_timestamp_usecs);
state->last_tcp_timestamp_usecs = actual_usecs;

/* difference exceeds configured timing tolerance */
if (llabs(exp_usecs - actual_usecs) >
state->config->tolerance_usecs) {
if (llabs(exp_usecs - actual_usecs) > tolerance_usecs) {
asprintf(error,
"Bad timestamp %d in scm_timestamping %d: "
"expected=%lld (%lld) actual=%lld (%lld) "
Expand Down

0 comments on commit c7413ff

Please sign in to comment.