raw_skew.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* CLOCK_MONOTONIC vs CLOCK_MONOTONIC_RAW skew test
  2. * by: john stultz ([email protected])
  3. * John Stultz <[email protected]>
  4. * (C) Copyright IBM 2012
  5. * (C) Copyright Linaro Limited 2015
  6. * Licensed under the GPLv2
  7. *
  8. * To build:
  9. * $ gcc raw_skew.c -o raw_skew -lrt
  10. *
  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.
  15. *
  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.
  20. */
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <sys/time.h>
  25. #include <sys/timex.h>
  26. #include <time.h>
  27. #include "../kselftest.h"
  28. #define CLOCK_MONOTONIC_RAW 4
  29. #define NSEC_PER_SEC 1000000000LL
  30. #define shift_right(x, s) ({ \
  31. __typeof__(x) __x = (x); \
  32. __typeof__(s) __s = (s); \
  33. __x < 0 ? -(-__x >> __s) : __x >> __s; \
  34. })
  35. long long llabs(long long val)
  36. {
  37. if (val < 0)
  38. val = -val;
  39. return val;
  40. }
  41. unsigned long long ts_to_nsec(struct timespec ts)
  42. {
  43. return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
  44. }
  45. struct timespec nsec_to_ts(long long ns)
  46. {
  47. struct timespec ts;
  48. ts.tv_sec = ns/NSEC_PER_SEC;
  49. ts.tv_nsec = ns%NSEC_PER_SEC;
  50. return ts;
  51. }
  52. long long diff_timespec(struct timespec start, struct timespec end)
  53. {
  54. long long start_ns, end_ns;
  55. start_ns = ts_to_nsec(start);
  56. end_ns = ts_to_nsec(end);
  57. return end_ns - start_ns;
  58. }
  59. void get_monotonic_and_raw(struct timespec *mon, struct timespec *raw)
  60. {
  61. struct timespec start, mid, end;
  62. long long diff = 0, tmp;
  63. int i;
  64. for (i = 0; i < 3; i++) {
  65. long long newdiff;
  66. clock_gettime(CLOCK_MONOTONIC, &start);
  67. clock_gettime(CLOCK_MONOTONIC_RAW, &mid);
  68. clock_gettime(CLOCK_MONOTONIC, &end);
  69. newdiff = diff_timespec(start, end);
  70. if (diff == 0 || newdiff < diff) {
  71. diff = newdiff;
  72. *raw = mid;
  73. tmp = (ts_to_nsec(start) + ts_to_nsec(end))/2;
  74. *mon = nsec_to_ts(tmp);
  75. }
  76. }
  77. }
  78. int main(int argc, char **argv)
  79. {
  80. struct timespec mon, raw, start, end;
  81. long long delta1, delta2, interval, eppm, ppm;
  82. struct timex tx1, tx2;
  83. setbuf(stdout, NULL);
  84. if (clock_gettime(CLOCK_MONOTONIC_RAW, &raw)) {
  85. printf("ERR: NO CLOCK_MONOTONIC_RAW\n");
  86. return -1;
  87. }
  88. tx1.modes = 0;
  89. adjtimex(&tx1);
  90. get_monotonic_and_raw(&mon, &raw);
  91. start = mon;
  92. delta1 = diff_timespec(mon, raw);
  93. if (tx1.offset)
  94. printf("WARNING: ADJ_OFFSET in progress, this will cause inaccurate results\n");
  95. printf("Estimating clock drift: ");
  96. fflush(stdout);
  97. sleep(120);
  98. get_monotonic_and_raw(&mon, &raw);
  99. end = mon;
  100. tx2.modes = 0;
  101. adjtimex(&tx2);
  102. delta2 = diff_timespec(mon, raw);
  103. interval = diff_timespec(start, end);
  104. /* calculate measured ppm between MONOTONIC and MONOTONIC_RAW */
  105. eppm = ((delta2-delta1)*NSEC_PER_SEC)/interval;
  106. eppm = -eppm;
  107. printf("%lld.%i(est)", eppm/1000, abs((int)(eppm%1000)));
  108. /* Avg the two actual freq samples adjtimex gave us */
  109. ppm = (tx1.freq + tx2.freq) * 1000 / 2;
  110. ppm = (long long)tx1.freq * 1000;
  111. ppm = shift_right(ppm, 16);
  112. printf(" %lld.%i(act)", ppm/1000, abs((int)(ppm%1000)));
  113. if (llabs(eppm - ppm) > 1000) {
  114. if (tx1.offset || tx2.offset ||
  115. tx1.freq != tx2.freq || tx1.tick != tx2.tick) {
  116. printf(" [SKIP]\n");
  117. return ksft_exit_skip("The clock was adjusted externally. Shutdown NTPd or other time sync daemons\n");
  118. }
  119. printf(" [FAILED]\n");
  120. return ksft_exit_fail();
  121. }
  122. printf(" [OK]\n");
  123. return ksft_exit_pass();
  124. }