tools/testing/selftests/net/txtimestamp.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/txtimestamp.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/txtimestamp.c
Extension
.c
Size
23087 bytes
Lines
952
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct timing_event {
	int64_t min;
	int64_t max;
	int64_t total;
	int count;
};

static struct timing_event usr_enq;
static struct timing_event usr_snd;
static struct timing_event usr_ack;

static bool test_failed;

static int64_t timespec_to_ns64(struct timespec *ts)
{
	return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
}

static int64_t timespec_to_us64(struct timespec *ts)
{
	return ts->tv_sec * USEC_PER_SEC + ts->tv_nsec / NSEC_PER_USEC;
}

static void init_timing_event(struct timing_event *te)
{
	te->min = INT64_MAX;
	te->max = 0;
	te->total = 0;
	te->count = 0;
}

static void add_timing_event(struct timing_event *te,
		struct timespec *t_start, struct timespec *t_end)
{
	int64_t ts_delta = timespec_to_ns64(t_end) - timespec_to_ns64(t_start);

	te->count++;
	if (ts_delta < te->min)
		te->min = ts_delta;
	if (ts_delta > te->max)
		te->max = ts_delta;
	te->total += ts_delta;
}

static void validate_key(int tskey, int tstype)
{
	int stepsize;

	/* compare key for each subsequent request
	 * must only test for one type, the first one requested
	 */
	if (saved_tskey == -1 || cfg_use_cmsg_opt_id)
		saved_tskey_type = tstype;
	else if (saved_tskey_type != tstype)
		return;

	stepsize = cfg_proto == SOCK_STREAM ? cfg_payload_len : 1;
	stepsize = cfg_use_cmsg_opt_id ? 0 : stepsize;
	if (tskey != saved_tskey + stepsize) {
		fprintf(stderr, "ERROR: key %d, expected %d\n",
				tskey, saved_tskey + stepsize);
		test_failed = true;
	}

	saved_tskey = tskey;
}

static void validate_timestamp(struct timespec *cur, int min_delay)
{
	int64_t cur64, start64;
	int max_delay;

	cur64 = timespec_to_us64(cur);
	start64 = timespec_to_us64(&ts_usr);
	max_delay = min_delay + cfg_delay_tolerance_usec;

	if (cur64 < start64 + min_delay || cur64 > start64 + max_delay) {
		fprintf(stderr, "ERROR: %" PRId64 " us expected between %d and %d\n",
				cur64 - start64, min_delay, max_delay);
		if (!getenv("KSFT_MACHINE_SLOW"))
			test_failed = true;
	}
}

static void __print_ts_delta_formatted(int64_t ts_delta)
{
	if (cfg_print_nsec)
		fprintf(stderr, "%" PRId64 " ns", ts_delta);
	else
		fprintf(stderr, "%" PRId64 " us", ts_delta / NSEC_PER_USEC);

Annotation

Implementation Notes