Loading...
Note: File does not exist in v3.15.
1/* adjtimex() tick adjustment test
2 * by: John Stultz <john.stultz@linaro.org>
3 * (C) Copyright Linaro Limited 2015
4 * Licensed under the GPLv2
5 *
6 * To build:
7 * $ gcc adjtick.c -o adjtick -lrt
8 *
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.
13 *
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.
18 */
19#include <stdio.h>
20#include <unistd.h>
21#include <stdlib.h>
22#include <sys/time.h>
23#include <sys/timex.h>
24#include <time.h>
25#include <include/vdso/time64.h>
26
27#include "../kselftest.h"
28
29#define MILLION 1000000
30
31long systick;
32
33long long llabs(long long val)
34{
35 if (val < 0)
36 val = -val;
37 return val;
38}
39
40unsigned long long ts_to_nsec(struct timespec ts)
41{
42 return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
43}
44
45struct timespec nsec_to_ts(long long ns)
46{
47 struct timespec ts;
48
49 ts.tv_sec = ns/NSEC_PER_SEC;
50 ts.tv_nsec = ns%NSEC_PER_SEC;
51
52 return ts;
53}
54
55long long diff_timespec(struct timespec start, struct timespec end)
56{
57 long long start_ns, end_ns;
58
59 start_ns = ts_to_nsec(start);
60 end_ns = ts_to_nsec(end);
61
62 return end_ns - start_ns;
63}
64
65void get_monotonic_and_raw(struct timespec *mon, struct timespec *raw)
66{
67 struct timespec start, mid, end;
68 long long diff = 0, tmp;
69 int i;
70
71 clock_gettime(CLOCK_MONOTONIC, mon);
72 clock_gettime(CLOCK_MONOTONIC_RAW, raw);
73
74 /* Try to get a more tightly bound pairing */
75 for (i = 0; i < 3; i++) {
76 long long newdiff;
77
78 clock_gettime(CLOCK_MONOTONIC, &start);
79 clock_gettime(CLOCK_MONOTONIC_RAW, &mid);
80 clock_gettime(CLOCK_MONOTONIC, &end);
81
82 newdiff = diff_timespec(start, end);
83 if (diff == 0 || newdiff < diff) {
84 diff = newdiff;
85 *raw = mid;
86 tmp = (ts_to_nsec(start) + ts_to_nsec(end))/2;
87 *mon = nsec_to_ts(tmp);
88 }
89 }
90}
91
92long long get_ppm_drift(void)
93{
94 struct timespec mon_start, raw_start, mon_end, raw_end;
95 long long delta1, delta2, eppm;
96
97 get_monotonic_and_raw(&mon_start, &raw_start);
98
99 sleep(15);
100
101 get_monotonic_and_raw(&mon_end, &raw_end);
102
103 delta1 = diff_timespec(mon_start, mon_end);
104 delta2 = diff_timespec(raw_start, raw_end);
105
106 eppm = (delta1*MILLION)/delta2 - MILLION;
107
108 return eppm;
109}
110
111int check_tick_adj(long tickval)
112{
113 long long eppm, ppm;
114 struct timex tx1;
115
116 tx1.modes = ADJ_TICK;
117 tx1.modes |= ADJ_OFFSET;
118 tx1.modes |= ADJ_FREQUENCY;
119 tx1.modes |= ADJ_STATUS;
120
121 tx1.status = STA_PLL;
122 tx1.offset = 0;
123 tx1.freq = 0;
124 tx1.tick = tickval;
125
126 adjtimex(&tx1);
127
128 sleep(1);
129
130 ppm = ((long long)tickval * MILLION)/systick - MILLION;
131 printf("Estimating tick (act: %ld usec, %lld ppm): ", tickval, ppm);
132
133 eppm = get_ppm_drift();
134 printf("%lld usec, %lld ppm", systick + (systick * eppm / MILLION), eppm);
135 fflush(stdout);
136
137 tx1.modes = 0;
138 adjtimex(&tx1);
139
140 if (tx1.offset || tx1.freq || tx1.tick != tickval) {
141 printf(" [ERROR]\n");
142 printf("\tUnexpected adjtimex return values, make sure ntpd is not running.\n");
143 return -1;
144 }
145
146 /*
147 * Here we use 100ppm difference as an error bound.
148 * We likely should see better, but some coarse clocksources
149 * cannot match the HZ tick size accurately, so we have a
150 * internal correction factor that doesn't scale exactly
151 * with the adjustment, resulting in > 10ppm error during
152 * a 10% adjustment. 100ppm also gives us more breathing
153 * room for interruptions during the measurement.
154 */
155 if (llabs(eppm - ppm) > 100) {
156 printf(" [FAILED]\n");
157 return -1;
158 }
159 printf(" [OK]\n");
160
161 return 0;
162}
163
164int main(int argc, char **argv)
165{
166 struct timespec raw;
167 long tick, max, interval, err;
168 struct timex tx1;
169
170 err = 0;
171 setbuf(stdout, NULL);
172
173 if (clock_gettime(CLOCK_MONOTONIC_RAW, &raw)) {
174 printf("ERR: NO CLOCK_MONOTONIC_RAW\n");
175 return -1;
176 }
177
178 printf("Each iteration takes about 15 seconds\n");
179
180 systick = sysconf(_SC_CLK_TCK);
181 systick = USEC_PER_SEC/sysconf(_SC_CLK_TCK);
182 max = systick/10; /* +/- 10% */
183 interval = max/4; /* in 4 steps each side */
184
185 for (tick = (systick - max); tick < (systick + max); tick += interval) {
186 if (check_tick_adj(tick)) {
187 err = 1;
188 break;
189 }
190 }
191
192 /* Reset things to zero */
193 tx1.modes = ADJ_TICK;
194 tx1.modes |= ADJ_OFFSET;
195 tx1.modes |= ADJ_FREQUENCY;
196
197 tx1.offset = 0;
198 tx1.freq = 0;
199 tx1.tick = systick;
200
201 adjtimex(&tx1);
202
203 if (err)
204 ksft_exit_fail();
205
206 ksft_exit_pass();
207}