1 /* adjtimex() tick adjustment test
2 * by: John Stultz <john.stultz@linaro.org>
3 * (C) Copyright Linaro Limited 2015
4 * Licensed under the GPLv2
7 * $ gcc adjtick.c -o adjtick -lrt
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
23 #include <sys/timex.h>
27 #include "../kselftest.h"
29 static inline int ksft_exit_pass(void)
33 static inline int ksft_exit_fail(void)
39 #define CLOCK_MONOTONIC_RAW 4
41 #define NSEC_PER_SEC 1000000000LL
42 #define USEC_PER_SEC 1000000
44 #define MILLION 1000000
48 long long llabs(long long val)
55 unsigned long long ts_to_nsec(struct timespec ts)
57 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
60 struct timespec nsec_to_ts(long long ns)
64 ts.tv_sec = ns/NSEC_PER_SEC;
65 ts.tv_nsec = ns%NSEC_PER_SEC;
70 long long diff_timespec(struct timespec start, struct timespec end)
72 long long start_ns, end_ns;
74 start_ns = ts_to_nsec(start);
75 end_ns = ts_to_nsec(end);
77 return end_ns - start_ns;
80 void get_monotonic_and_raw(struct timespec *mon, struct timespec *raw)
82 struct timespec start, mid, end;
83 long long diff = 0, tmp;
86 clock_gettime(CLOCK_MONOTONIC, mon);
87 clock_gettime(CLOCK_MONOTONIC_RAW, raw);
89 /* Try to get a more tightly bound pairing */
90 for (i = 0; i < 3; i++) {
93 clock_gettime(CLOCK_MONOTONIC, &start);
94 clock_gettime(CLOCK_MONOTONIC_RAW, &mid);
95 clock_gettime(CLOCK_MONOTONIC, &end);
97 newdiff = diff_timespec(start, end);
98 if (diff == 0 || newdiff < diff) {
101 tmp = (ts_to_nsec(start) + ts_to_nsec(end))/2;
102 *mon = nsec_to_ts(tmp);
107 long long get_ppm_drift(void)
109 struct timespec mon_start, raw_start, mon_end, raw_end;
110 long long delta1, delta2, eppm;
112 get_monotonic_and_raw(&mon_start, &raw_start);
116 get_monotonic_and_raw(&mon_end, &raw_end);
118 delta1 = diff_timespec(mon_start, mon_end);
119 delta2 = diff_timespec(raw_start, raw_end);
121 eppm = (delta1*MILLION)/delta2 - MILLION;
126 int check_tick_adj(long tickval)
131 tx1.modes = ADJ_TICK;
132 tx1.modes |= ADJ_OFFSET;
133 tx1.modes |= ADJ_FREQUENCY;
134 tx1.modes |= ADJ_STATUS;
136 tx1.status = STA_PLL;
145 ppm = ((long long)tickval * MILLION)/systick - MILLION;
146 printf("Estimating tick (act: %ld usec, %lld ppm): ", tickval, ppm);
148 eppm = get_ppm_drift();
149 printf("%lld usec, %lld ppm", systick + (systick * eppm / MILLION), eppm);
155 if (tx1.offset || tx1.freq || tx1.tick != tickval) {
156 printf(" [ERROR]\n");
157 printf("\tUnexpected adjtimex return values, make sure ntpd is not running.\n");
162 * Here we use 100ppm difference as an error bound.
163 * We likely should see better, but some coarse clocksources
164 * cannot match the HZ tick size accurately, so we have a
165 * internal correction factor that doesn't scale exactly
166 * with the adjustment, resulting in > 10ppm error during
167 * a 10% adjustment. 100ppm also gives us more breathing
168 * room for interruptions during the measurement.
170 if (llabs(eppm - ppm) > 100) {
171 printf(" [FAILED]\n");
179 int main(int argv, char **argc)
182 long tick, max, interval, err;
186 setbuf(stdout, NULL);
188 if (clock_gettime(CLOCK_MONOTONIC_RAW, &raw)) {
189 printf("ERR: NO CLOCK_MONOTONIC_RAW\n");
193 printf("Each iteration takes about 15 seconds\n");
195 systick = sysconf(_SC_CLK_TCK);
196 systick = USEC_PER_SEC/sysconf(_SC_CLK_TCK);
197 max = systick/10; /* +/- 10% */
198 interval = max/4; /* in 4 steps each side */
200 for (tick = (systick - max); tick < (systick + max); tick += interval) {
201 if (check_tick_adj(tick)) {
207 /* Reset things to zero */
208 tx1.modes = ADJ_TICK;
209 tx1.modes |= ADJ_OFFSET;
210 tx1.modes |= ADJ_FREQUENCY;
219 return ksft_exit_fail();
221 return ksft_exit_pass();