File Index
Files and directories
Sign in
FILE

raw_skew.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/timers/raw_skew.c C
  1/* CLOCK_MONOTONIC vs CLOCK_MONOTONIC_RAW skew test
  2 *		by: john stultz (johnstul@us.ibm.com)
  3 *		    John Stultz <john.stultz@linaro.org>
  4 *		(C) Copyright IBM 2012
  5 *		(C) Copyright Linaro Limited 2015
  6 *		Licensed under the GPLv2
  7 *
  8 *  To build:
  9 *	$ gcc raw_skew.c -o raw_skew -lrt
 10 *
 11 *   This program is free software: you can redistribute it and/or modify
 12 *   it under the terms of the GNU General Public License as published by
 13 *   the Free Software Foundation, either version 2 of the License, or
 14 *   (at your option) any later version.
 15 *
 16 *   This program is distributed in the hope that it will be useful,
 17 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 *   GNU General Public License for more details.
 20 */
 21
 22#include <stdio.h>
 23#include <unistd.h>
 24#include <stdlib.h>
 25#include <sys/time.h>
 26#include <sys/timex.h>
 27#include <time.h>
 28#include <include/vdso/time64.h>
 29#include "kselftest.h"
 30
 31#define shift_right(x, s) ({		\
 32	__typeof__(x) __x = (x);	\
 33	__typeof__(s) __s = (s);	\
 34	__x < 0 ? -(-__x >> __s) : __x >> __s; \
 35})
 36
 37long long llabs(long long val)
 38{
 39	if (val < 0)
 40		val = -val;
 41	return val;
 42}
 43
 44unsigned long long ts_to_nsec(struct timespec ts)
 45{
 46	return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
 47}
 48
 49struct timespec nsec_to_ts(long long ns)
 50{
 51	struct timespec ts;
 52
 53	ts.tv_sec = ns/NSEC_PER_SEC;
 54	ts.tv_nsec = ns%NSEC_PER_SEC;
 55	return ts;
 56}
 57
 58long long diff_timespec(struct timespec start, struct timespec end)
 59{
 60	long long start_ns, end_ns;
 61
 62	start_ns = ts_to_nsec(start);
 63	end_ns = ts_to_nsec(end);
 64	return end_ns - start_ns;
 65}
 66
 67void get_monotonic_and_raw(struct timespec *mon, struct timespec *raw)
 68{
 69	struct timespec start, mid, end;
 70	long long diff = 0, tmp;
 71	int i;
 72
 73	for (i = 0; i < 3; i++) {
 74		long long newdiff;
 75
 76		clock_gettime(CLOCK_MONOTONIC, &start);
 77		clock_gettime(CLOCK_MONOTONIC_RAW, &mid);
 78		clock_gettime(CLOCK_MONOTONIC, &end);
 79
 80		newdiff = diff_timespec(start, end);
 81		if (diff == 0 || newdiff < diff) {
 82			diff = newdiff;
 83			*raw = mid;
 84			tmp = (ts_to_nsec(start) + ts_to_nsec(end))/2;
 85			*mon = nsec_to_ts(tmp);
 86		}
 87	}
 88}
 89
 90int main(int argc, char **argv)
 91{
 92	struct timespec mon, raw, start, end;
 93	long long delta1, delta2, interval, eppm, ppm;
 94	struct timex tx1, tx2;
 95
 96	setbuf(stdout, NULL);
 97
 98	if (clock_gettime(CLOCK_MONOTONIC_RAW, &raw)) {
 99		printf("ERR: NO CLOCK_MONOTONIC_RAW\n");
100		return -1;
101	}
102
103	tx1.modes = 0;
104	adjtimex(&tx1);
105	get_monotonic_and_raw(&mon, &raw);
106	start = mon;
107	delta1 = diff_timespec(mon, raw);
108
109	if (tx1.offset)
110		printf("WARNING: ADJ_OFFSET in progress, this will cause inaccurate results\n");
111
112	printf("Estimating clock drift: ");
113	fflush(stdout);
114	sleep(120);
115
116	get_monotonic_and_raw(&mon, &raw);
117	end = mon;
118	tx2.modes = 0;
119	adjtimex(&tx2);
120	delta2 = diff_timespec(mon, raw);
121
122	interval = diff_timespec(start, end);
123
124	/* calculate measured ppm between MONOTONIC and MONOTONIC_RAW */
125	eppm = ((delta2-delta1)*NSEC_PER_SEC)/interval;
126	eppm = -eppm;
127	printf("%lld.%i(est)", eppm/1000, abs((int)(eppm%1000)));
128
129	/* Avg the two actual freq samples adjtimex gave us */
130	ppm = (long long)(tx1.freq + tx2.freq) * 1000 / 2;
131	ppm = shift_right(ppm, 16);
132	printf(" %lld.%i(act)", ppm/1000, abs((int)(ppm%1000)));
133
134	if (llabs(eppm - ppm) > 1000) {
135		if (tx1.offset || tx2.offset ||
136		    tx1.freq != tx2.freq || tx1.tick != tx2.tick) {
137			printf("	[SKIP]\n");
138			ksft_exit_skip("The clock was adjusted externally. Shutdown NTPd or other time sync daemons\n");
139		}
140		printf("	[FAILED]\n");
141		ksft_exit_fail();
142	}
143	printf("	[OK]\n");
144	ksft_exit_pass();
145}
~/ Read-only file preview.