raw_skew.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* CLOCK_MONOTONIC vs CLOCK_MONOTONIC_RAW skew test
  2. * by: john stultz (johnstul@us.ibm.com)
  3. * John Stultz <john.stultz@linaro.org>
  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. #ifdef KTEST
  28. #include "../kselftest.h"
  29. #else
  30. static inline int ksft_exit_pass(void)
  31. {
  32. exit(0);
  33. }
  34. static inline int ksft_exit_fail(void)
  35. {
  36. exit(1);
  37. }
  38. #endif
  39. #define CLOCK_MONOTONIC_RAW 4
  40. #define NSEC_PER_SEC 1000000000LL
  41. #define shift_right(x, s) ({ \
  42. __typeof__(x) __x = (x); \
  43. __typeof__(s) __s = (s); \
  44. __x < 0 ? -(-__x >> __s) : __x >> __s; \
  45. })
  46. long long llabs(long long val)
  47. {
  48. if (val < 0)
  49. val = -val;
  50. return val;
  51. }
  52. unsigned long long ts_to_nsec(struct timespec ts)
  53. {
  54. return ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec;
  55. }
  56. struct timespec nsec_to_ts(long long ns)
  57. {
  58. struct timespec ts;
  59. ts.tv_sec = ns/NSEC_PER_SEC;
  60. ts.tv_nsec = ns%NSEC_PER_SEC;
  61. return ts;
  62. }
  63. long long diff_timespec(struct timespec start, struct timespec end)
  64. {
  65. long long start_ns, end_ns;
  66. start_ns = ts_to_nsec(start);
  67. end_ns = ts_to_nsec(end);
  68. return end_ns - start_ns;
  69. }
  70. void get_monotonic_and_raw(struct timespec *mon, struct timespec *raw)
  71. {
  72. struct timespec start, mid, end;
  73. long long diff = 0, tmp;
  74. int i;
  75. for (i = 0; i < 3; i++) {
  76. long long newdiff;
  77. clock_gettime(CLOCK_MONOTONIC, &start);
  78. clock_gettime(CLOCK_MONOTONIC_RAW, &mid);
  79. clock_gettime(CLOCK_MONOTONIC, &end);
  80. newdiff = diff_timespec(start, end);
  81. if (diff == 0 || newdiff < diff) {
  82. diff = newdiff;
  83. *raw = mid;
  84. tmp = (ts_to_nsec(start) + ts_to_nsec(end))/2;
  85. *mon = nsec_to_ts(tmp);
  86. }
  87. }
  88. }
  89. int main(int argv, char **argc)
  90. {
  91. struct timespec mon, raw, start, end;
  92. long long delta1, delta2, interval, eppm, ppm;
  93. struct timex tx1, tx2;
  94. setbuf(stdout, NULL);
  95. if (clock_gettime(CLOCK_MONOTONIC_RAW, &raw)) {
  96. printf("ERR: NO CLOCK_MONOTONIC_RAW\n");
  97. return -1;
  98. }
  99. tx1.modes = 0;
  100. adjtimex(&tx1);
  101. get_monotonic_and_raw(&mon, &raw);
  102. start = mon;
  103. delta1 = diff_timespec(mon, raw);
  104. if (tx1.offset)
  105. printf("WARNING: ADJ_OFFSET in progress, this will cause inaccurate results\n");
  106. printf("Estimating clock drift: ");
  107. sleep(120);
  108. get_monotonic_and_raw(&mon, &raw);
  109. end = mon;
  110. tx2.modes = 0;
  111. adjtimex(&tx2);
  112. delta2 = diff_timespec(mon, raw);
  113. interval = diff_timespec(start, end);
  114. /* calculate measured ppm between MONOTONIC and MONOTONIC_RAW */
  115. eppm = ((delta2-delta1)*NSEC_PER_SEC)/interval;
  116. eppm = -eppm;
  117. printf("%lld.%i(est)", eppm/1000, abs((int)(eppm%1000)));
  118. /* Avg the two actual freq samples adjtimex gave us */
  119. ppm = (tx1.freq + tx2.freq) * 1000 / 2;
  120. ppm = (long long)tx1.freq * 1000;
  121. ppm = shift_right(ppm, 16);
  122. printf(" %lld.%i(act)", ppm/1000, abs((int)(ppm%1000)));
  123. if (llabs(eppm - ppm) > 1000) {
  124. printf(" [FAILED]\n");
  125. return ksft_exit_fail();
  126. }
  127. printf(" [OK]\n");
  128. return ksft_exit_pass();
  129. }