Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This test checks the response of the system clock to frequency
0004  * steps made with adjtimex(). The frequency error and stability of
0005  * the CLOCK_MONOTONIC clock relative to the CLOCK_MONOTONIC_RAW clock
0006  * is measured in two intervals following the step. The test fails if
0007  * values from the second interval exceed specified limits.
0008  *
0009  * Copyright (C) Miroslav Lichvar <mlichvar@redhat.com>  2017
0010  */
0011 
0012 #include <math.h>
0013 #include <stdio.h>
0014 #include <sys/timex.h>
0015 #include <time.h>
0016 #include <unistd.h>
0017 
0018 #include "../kselftest.h"
0019 
0020 #define SAMPLES 100
0021 #define SAMPLE_READINGS 10
0022 #define MEAN_SAMPLE_INTERVAL 0.1
0023 #define STEP_INTERVAL 1.0
0024 #define MAX_PRECISION 500e-9
0025 #define MAX_FREQ_ERROR 0.02e-6
0026 #define MAX_STDDEV 50e-9
0027 
0028 #ifndef ADJ_SETOFFSET
0029   #define ADJ_SETOFFSET 0x0100
0030 #endif
0031 
0032 struct sample {
0033     double offset;
0034     double time;
0035 };
0036 
0037 static time_t mono_raw_base;
0038 static time_t mono_base;
0039 static long user_hz;
0040 static double precision;
0041 static double mono_freq_offset;
0042 
0043 static double diff_timespec(struct timespec *ts1, struct timespec *ts2)
0044 {
0045     return ts1->tv_sec - ts2->tv_sec + (ts1->tv_nsec - ts2->tv_nsec) / 1e9;
0046 }
0047 
0048 static double get_sample(struct sample *sample)
0049 {
0050     double delay, mindelay = 0.0;
0051     struct timespec ts1, ts2, ts3;
0052     int i;
0053 
0054     for (i = 0; i < SAMPLE_READINGS; i++) {
0055         clock_gettime(CLOCK_MONOTONIC_RAW, &ts1);
0056         clock_gettime(CLOCK_MONOTONIC, &ts2);
0057         clock_gettime(CLOCK_MONOTONIC_RAW, &ts3);
0058 
0059         ts1.tv_sec -= mono_raw_base;
0060         ts2.tv_sec -= mono_base;
0061         ts3.tv_sec -= mono_raw_base;
0062 
0063         delay = diff_timespec(&ts3, &ts1);
0064         if (delay <= 1e-9) {
0065             i--;
0066             continue;
0067         }
0068 
0069         if (!i || delay < mindelay) {
0070             sample->offset = diff_timespec(&ts2, &ts1);
0071             sample->offset -= delay / 2.0;
0072             sample->time = ts1.tv_sec + ts1.tv_nsec / 1e9;
0073             mindelay = delay;
0074         }
0075     }
0076 
0077     return mindelay;
0078 }
0079 
0080 static void reset_ntp_error(void)
0081 {
0082     struct timex txc;
0083 
0084     txc.modes = ADJ_SETOFFSET;
0085     txc.time.tv_sec = 0;
0086     txc.time.tv_usec = 0;
0087 
0088     if (adjtimex(&txc) < 0) {
0089         perror("[FAIL] adjtimex");
0090         ksft_exit_fail();
0091     }
0092 }
0093 
0094 static void set_frequency(double freq)
0095 {
0096     struct timex txc;
0097     int tick_offset;
0098 
0099     tick_offset = 1e6 * freq / user_hz;
0100 
0101     txc.modes = ADJ_TICK | ADJ_FREQUENCY;
0102     txc.tick = 1000000 / user_hz + tick_offset;
0103     txc.freq = (1e6 * freq - user_hz * tick_offset) * (1 << 16);
0104 
0105     if (adjtimex(&txc) < 0) {
0106         perror("[FAIL] adjtimex");
0107         ksft_exit_fail();
0108     }
0109 }
0110 
0111 static void regress(struct sample *samples, int n, double *intercept,
0112             double *slope, double *r_stddev, double *r_max)
0113 {
0114     double x, y, r, x_sum, y_sum, xy_sum, x2_sum, r2_sum;
0115     int i;
0116 
0117     x_sum = 0.0, y_sum = 0.0, xy_sum = 0.0, x2_sum = 0.0;
0118 
0119     for (i = 0; i < n; i++) {
0120         x = samples[i].time;
0121         y = samples[i].offset;
0122 
0123         x_sum += x;
0124         y_sum += y;
0125         xy_sum += x * y;
0126         x2_sum += x * x;
0127     }
0128 
0129     *slope = (xy_sum - x_sum * y_sum / n) / (x2_sum - x_sum * x_sum / n);
0130     *intercept = (y_sum - *slope * x_sum) / n;
0131 
0132     *r_max = 0.0, r2_sum = 0.0;
0133 
0134     for (i = 0; i < n; i++) {
0135         x = samples[i].time;
0136         y = samples[i].offset;
0137         r = fabs(x * *slope + *intercept - y);
0138         if (*r_max < r)
0139             *r_max = r;
0140         r2_sum += r * r;
0141     }
0142 
0143     *r_stddev = sqrt(r2_sum / n);
0144 }
0145 
0146 static int run_test(int calibration, double freq_base, double freq_step)
0147 {
0148     struct sample samples[SAMPLES];
0149     double intercept, slope, stddev1, max1, stddev2, max2;
0150     double freq_error1, freq_error2;
0151     int i;
0152 
0153     set_frequency(freq_base);
0154 
0155     for (i = 0; i < 10; i++)
0156         usleep(1e6 * MEAN_SAMPLE_INTERVAL / 10);
0157 
0158     reset_ntp_error();
0159 
0160     set_frequency(freq_base + freq_step);
0161 
0162     for (i = 0; i < 10; i++)
0163         usleep(rand() % 2000000 * STEP_INTERVAL / 10);
0164 
0165     set_frequency(freq_base);
0166 
0167     for (i = 0; i < SAMPLES; i++) {
0168         usleep(rand() % 2000000 * MEAN_SAMPLE_INTERVAL);
0169         get_sample(&samples[i]);
0170     }
0171 
0172     if (calibration) {
0173         regress(samples, SAMPLES, &intercept, &slope, &stddev1, &max1);
0174         mono_freq_offset = slope;
0175         printf("CLOCK_MONOTONIC_RAW frequency offset: %11.3f ppm\n",
0176                1e6 * mono_freq_offset);
0177         return 0;
0178     }
0179 
0180     regress(samples, SAMPLES / 2, &intercept, &slope, &stddev1, &max1);
0181     freq_error1 = slope * (1.0 - mono_freq_offset) - mono_freq_offset -
0182             freq_base;
0183 
0184     regress(samples + SAMPLES / 2, SAMPLES / 2, &intercept, &slope,
0185         &stddev2, &max2);
0186     freq_error2 = slope * (1.0 - mono_freq_offset) - mono_freq_offset -
0187             freq_base;
0188 
0189     printf("%6.0f %+10.3f %6.0f %7.0f %+10.3f %6.0f %7.0f\t",
0190            1e6 * freq_step,
0191            1e6 * freq_error1, 1e9 * stddev1, 1e9 * max1,
0192            1e6 * freq_error2, 1e9 * stddev2, 1e9 * max2);
0193 
0194     if (fabs(freq_error2) > MAX_FREQ_ERROR || stddev2 > MAX_STDDEV) {
0195         printf("[FAIL]\n");
0196         return 1;
0197     }
0198 
0199     printf("[OK]\n");
0200     return 0;
0201 }
0202 
0203 static void init_test(void)
0204 {
0205     struct timespec ts;
0206     struct sample sample;
0207 
0208     if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts)) {
0209         perror("[FAIL] clock_gettime(CLOCK_MONOTONIC_RAW)");
0210         ksft_exit_fail();
0211     }
0212 
0213     mono_raw_base = ts.tv_sec;
0214 
0215     if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
0216         perror("[FAIL] clock_gettime(CLOCK_MONOTONIC)");
0217         ksft_exit_fail();
0218     }
0219 
0220     mono_base = ts.tv_sec;
0221 
0222     user_hz = sysconf(_SC_CLK_TCK);
0223 
0224     precision = get_sample(&sample) / 2.0;
0225     printf("CLOCK_MONOTONIC_RAW+CLOCK_MONOTONIC precision: %.0f ns\t\t",
0226            1e9 * precision);
0227 
0228     if (precision > MAX_PRECISION)
0229         ksft_exit_skip("precision: %.0f ns > MAX_PRECISION: %.0f ns\n",
0230                 1e9 * precision, 1e9 * MAX_PRECISION);
0231 
0232     printf("[OK]\n");
0233     srand(ts.tv_sec ^ ts.tv_nsec);
0234 
0235     run_test(1, 0.0, 0.0);
0236 }
0237 
0238 int main(int argc, char **argv)
0239 {
0240     double freq_base, freq_step;
0241     int i, j, fails = 0;
0242 
0243     init_test();
0244 
0245     printf("Checking response to frequency step:\n");
0246     printf("  Step           1st interval              2nd interval\n");
0247     printf("             Freq    Dev     Max       Freq    Dev     Max\n");
0248 
0249     for (i = 2; i >= 0; i--) {
0250         for (j = 0; j < 5; j++) {
0251             freq_base = (rand() % (1 << 24) - (1 << 23)) / 65536e6;
0252             freq_step = 10e-6 * (1 << (6 * i));
0253             fails += run_test(0, freq_base, freq_step);
0254         }
0255     }
0256 
0257     set_frequency(0.0);
0258 
0259     if (fails)
0260         return ksft_exit_fail();
0261 
0262     return ksft_exit_pass();
0263 }