freq-step.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * This test checks the response of the system clock to frequency
  3. * steps made with adjtimex(). The frequency error and stability of
  4. * the CLOCK_MONOTONIC clock relative to the CLOCK_MONOTONIC_RAW clock
  5. * is measured in two intervals following the step. The test fails if
  6. * values from the second interval exceed specified limits.
  7. *
  8. * Copyright (C) Miroslav Lichvar <mlichvar@redhat.com> 2017
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include <sys/timex.h>
  22. #include <time.h>
  23. #include <unistd.h>
  24. #include "../kselftest.h"
  25. #define SAMPLES 100
  26. #define SAMPLE_READINGS 10
  27. #define MEAN_SAMPLE_INTERVAL 0.1
  28. #define STEP_INTERVAL 1.0
  29. #define MAX_PRECISION 100e-9
  30. #define MAX_FREQ_ERROR 10e-6
  31. #define MAX_STDDEV 1000e-9
  32. #ifndef ADJ_SETOFFSET
  33. #define ADJ_SETOFFSET 0x0100
  34. #endif
  35. struct sample {
  36. double offset;
  37. double time;
  38. };
  39. static time_t mono_raw_base;
  40. static time_t mono_base;
  41. static long user_hz;
  42. static double precision;
  43. static double mono_freq_offset;
  44. static double diff_timespec(struct timespec *ts1, struct timespec *ts2)
  45. {
  46. return ts1->tv_sec - ts2->tv_sec + (ts1->tv_nsec - ts2->tv_nsec) / 1e9;
  47. }
  48. static double get_sample(struct sample *sample)
  49. {
  50. double delay, mindelay = 0.0;
  51. struct timespec ts1, ts2, ts3;
  52. int i;
  53. for (i = 0; i < SAMPLE_READINGS; i++) {
  54. clock_gettime(CLOCK_MONOTONIC_RAW, &ts1);
  55. clock_gettime(CLOCK_MONOTONIC, &ts2);
  56. clock_gettime(CLOCK_MONOTONIC_RAW, &ts3);
  57. ts1.tv_sec -= mono_raw_base;
  58. ts2.tv_sec -= mono_base;
  59. ts3.tv_sec -= mono_raw_base;
  60. delay = diff_timespec(&ts3, &ts1);
  61. if (delay <= 1e-9) {
  62. i--;
  63. continue;
  64. }
  65. if (!i || delay < mindelay) {
  66. sample->offset = diff_timespec(&ts2, &ts1);
  67. sample->offset -= delay / 2.0;
  68. sample->time = ts1.tv_sec + ts1.tv_nsec / 1e9;
  69. mindelay = delay;
  70. }
  71. }
  72. return mindelay;
  73. }
  74. static void reset_ntp_error(void)
  75. {
  76. struct timex txc;
  77. txc.modes = ADJ_SETOFFSET;
  78. txc.time.tv_sec = 0;
  79. txc.time.tv_usec = 0;
  80. if (adjtimex(&txc) < 0) {
  81. perror("[FAIL] adjtimex");
  82. ksft_exit_fail();
  83. }
  84. }
  85. static void set_frequency(double freq)
  86. {
  87. struct timex txc;
  88. int tick_offset;
  89. tick_offset = 1e6 * freq / user_hz;
  90. txc.modes = ADJ_TICK | ADJ_FREQUENCY;
  91. txc.tick = 1000000 / user_hz + tick_offset;
  92. txc.freq = (1e6 * freq - user_hz * tick_offset) * (1 << 16);
  93. if (adjtimex(&txc) < 0) {
  94. perror("[FAIL] adjtimex");
  95. ksft_exit_fail();
  96. }
  97. }
  98. static void regress(struct sample *samples, int n, double *intercept,
  99. double *slope, double *r_stddev, double *r_max)
  100. {
  101. double x, y, r, x_sum, y_sum, xy_sum, x2_sum, r2_sum;
  102. int i;
  103. x_sum = 0.0, y_sum = 0.0, xy_sum = 0.0, x2_sum = 0.0;
  104. for (i = 0; i < n; i++) {
  105. x = samples[i].time;
  106. y = samples[i].offset;
  107. x_sum += x;
  108. y_sum += y;
  109. xy_sum += x * y;
  110. x2_sum += x * x;
  111. }
  112. *slope = (xy_sum - x_sum * y_sum / n) / (x2_sum - x_sum * x_sum / n);
  113. *intercept = (y_sum - *slope * x_sum) / n;
  114. *r_max = 0.0, r2_sum = 0.0;
  115. for (i = 0; i < n; i++) {
  116. x = samples[i].time;
  117. y = samples[i].offset;
  118. r = fabs(x * *slope + *intercept - y);
  119. if (*r_max < r)
  120. *r_max = r;
  121. r2_sum += r * r;
  122. }
  123. *r_stddev = sqrt(r2_sum / n);
  124. }
  125. static int run_test(int calibration, double freq_base, double freq_step)
  126. {
  127. struct sample samples[SAMPLES];
  128. double intercept, slope, stddev1, max1, stddev2, max2;
  129. double freq_error1, freq_error2;
  130. int i;
  131. set_frequency(freq_base);
  132. for (i = 0; i < 10; i++)
  133. usleep(1e6 * MEAN_SAMPLE_INTERVAL / 10);
  134. reset_ntp_error();
  135. set_frequency(freq_base + freq_step);
  136. for (i = 0; i < 10; i++)
  137. usleep(rand() % 2000000 * STEP_INTERVAL / 10);
  138. set_frequency(freq_base);
  139. for (i = 0; i < SAMPLES; i++) {
  140. usleep(rand() % 2000000 * MEAN_SAMPLE_INTERVAL);
  141. get_sample(&samples[i]);
  142. }
  143. if (calibration) {
  144. regress(samples, SAMPLES, &intercept, &slope, &stddev1, &max1);
  145. mono_freq_offset = slope;
  146. printf("CLOCK_MONOTONIC_RAW frequency offset: %11.3f ppm\n",
  147. 1e6 * mono_freq_offset);
  148. return 0;
  149. }
  150. regress(samples, SAMPLES / 2, &intercept, &slope, &stddev1, &max1);
  151. freq_error1 = slope * (1.0 - mono_freq_offset) - mono_freq_offset -
  152. freq_base;
  153. regress(samples + SAMPLES / 2, SAMPLES / 2, &intercept, &slope,
  154. &stddev2, &max2);
  155. freq_error2 = slope * (1.0 - mono_freq_offset) - mono_freq_offset -
  156. freq_base;
  157. printf("%6.0f %+10.3f %6.0f %7.0f %+10.3f %6.0f %7.0f\t",
  158. 1e6 * freq_step,
  159. 1e6 * freq_error1, 1e9 * stddev1, 1e9 * max1,
  160. 1e6 * freq_error2, 1e9 * stddev2, 1e9 * max2);
  161. if (fabs(freq_error2) > MAX_FREQ_ERROR || stddev2 > MAX_STDDEV) {
  162. printf("[FAIL]\n");
  163. return 1;
  164. }
  165. printf("[OK]\n");
  166. return 0;
  167. }
  168. static void init_test(void)
  169. {
  170. struct timespec ts;
  171. struct sample sample;
  172. if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts)) {
  173. perror("[FAIL] clock_gettime(CLOCK_MONOTONIC_RAW)");
  174. ksft_exit_fail();
  175. }
  176. mono_raw_base = ts.tv_sec;
  177. if (clock_gettime(CLOCK_MONOTONIC, &ts)) {
  178. perror("[FAIL] clock_gettime(CLOCK_MONOTONIC)");
  179. ksft_exit_fail();
  180. }
  181. mono_base = ts.tv_sec;
  182. user_hz = sysconf(_SC_CLK_TCK);
  183. precision = get_sample(&sample) / 2.0;
  184. printf("CLOCK_MONOTONIC_RAW+CLOCK_MONOTONIC precision: %.0f ns\t\t",
  185. 1e9 * precision);
  186. if (precision > MAX_PRECISION)
  187. ksft_exit_skip("precision: %.0f ns > MAX_PRECISION: %.0f ns\n",
  188. 1e9 * precision, 1e9 * MAX_PRECISION);
  189. printf("[OK]\n");
  190. srand(ts.tv_sec ^ ts.tv_nsec);
  191. run_test(1, 0.0, 0.0);
  192. }
  193. int main(int argc, char **argv)
  194. {
  195. double freq_base, freq_step;
  196. int i, j, fails = 0;
  197. init_test();
  198. printf("Checking response to frequency step:\n");
  199. printf(" Step 1st interval 2nd interval\n");
  200. printf(" Freq Dev Max Freq Dev Max\n");
  201. for (i = 2; i >= 0; i--) {
  202. for (j = 0; j < 5; j++) {
  203. freq_base = (rand() % (1 << 24) - (1 << 23)) / 65536e6;
  204. freq_step = 10e-6 * (1 << (6 * i));
  205. fails += run_test(0, freq_base, freq_step);
  206. }
  207. }
  208. set_frequency(0.0);
  209. if (fails)
  210. return ksft_exit_fail();
  211. return ksft_exit_pass();
  212. }