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
9 * $ gcc raw_skew.c -o raw_skew -lrt
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.
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.
26 #include <sys/timex.h>
28 #include "../kselftest.h"
30 #define CLOCK_MONOTONIC_RAW 4
31 #define NSEC_PER_SEC 1000000000LL
33 #define shift_right(x, s) ({ \
34 __typeof__(x) __x = (x); \
35 __typeof__(s) __s = (s); \
36 __x < 0 ? -(-__x >> __s) : __x >> __s; \
39 long long llabs(long long val
)
46 unsigned long long ts_to_nsec(struct timespec ts
)
48 return ts
.tv_sec
* NSEC_PER_SEC
+ ts
.tv_nsec
;
51 struct timespec
nsec_to_ts(long long ns
)
55 ts
.tv_sec
= ns
/NSEC_PER_SEC
;
56 ts
.tv_nsec
= ns
%NSEC_PER_SEC
;
60 long long diff_timespec(struct timespec start
, struct timespec end
)
62 long long start_ns
, end_ns
;
64 start_ns
= ts_to_nsec(start
);
65 end_ns
= ts_to_nsec(end
);
66 return end_ns
- start_ns
;
69 void get_monotonic_and_raw(struct timespec
*mon
, struct timespec
*raw
)
71 struct timespec start
, mid
, end
;
72 long long diff
= 0, tmp
;
75 for (i
= 0; i
< 3; i
++) {
78 clock_gettime(CLOCK_MONOTONIC
, &start
);
79 clock_gettime(CLOCK_MONOTONIC_RAW
, &mid
);
80 clock_gettime(CLOCK_MONOTONIC
, &end
);
82 newdiff
= diff_timespec(start
, end
);
83 if (diff
== 0 || newdiff
< diff
) {
86 tmp
= (ts_to_nsec(start
) + ts_to_nsec(end
))/2;
87 *mon
= nsec_to_ts(tmp
);
92 int main(int argv
, char **argc
)
94 struct timespec mon
, raw
, start
, end
;
95 long long delta1
, delta2
, interval
, eppm
, ppm
;
96 struct timex tx1
, tx2
;
100 if (clock_gettime(CLOCK_MONOTONIC_RAW
, &raw
)) {
101 printf("ERR: NO CLOCK_MONOTONIC_RAW\n");
107 get_monotonic_and_raw(&mon
, &raw
);
109 delta1
= diff_timespec(mon
, raw
);
112 printf("WARNING: ADJ_OFFSET in progress, this will cause inaccurate results\n");
114 printf("Estimating clock drift: ");
118 get_monotonic_and_raw(&mon
, &raw
);
122 delta2
= diff_timespec(mon
, raw
);
124 interval
= diff_timespec(start
, end
);
126 /* calculate measured ppm between MONOTONIC and MONOTONIC_RAW */
127 eppm
= ((delta2
-delta1
)*NSEC_PER_SEC
)/interval
;
129 printf("%lld.%i(est)", eppm
/1000, abs((int)(eppm
%1000)));
131 /* Avg the two actual freq samples adjtimex gave us */
132 ppm
= (tx1
.freq
+ tx2
.freq
) * 1000 / 2;
133 ppm
= (long long)tx1
.freq
* 1000;
134 ppm
= shift_right(ppm
, 16);
135 printf(" %lld.%i(act)", ppm
/1000, abs((int)(ppm
%1000)));
137 if (llabs(eppm
- ppm
) > 1000) {
138 if (tx1
.offset
|| tx2
.offset
||
139 tx1
.freq
!= tx2
.freq
|| tx1
.tick
!= tx2
.tick
) {
141 return ksft_exit_skip("The clock was adjusted externally. Shutdown NTPd or other time sync daemons\n");
143 printf(" [FAILED]\n");
144 return ksft_exit_fail();
147 return ksft_exit_pass();