nsleep-lat.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Measure nanosleep timer latency
  2. * by: john stultz (john.stultz@linaro.org)
  3. * (C) Copyright Linaro 2013
  4. * Licensed under the GPLv2
  5. *
  6. * To build:
  7. * $ gcc nsleep-lat.c -o nsleep-lat -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 <stdlib.h>
  21. #include <time.h>
  22. #include <sys/time.h>
  23. #include <sys/timex.h>
  24. #include <string.h>
  25. #include <signal.h>
  26. #ifdef KTEST
  27. #include "../kselftest.h"
  28. #else
  29. static inline int ksft_exit_pass(void)
  30. {
  31. exit(0);
  32. }
  33. static inline int ksft_exit_fail(void)
  34. {
  35. exit(1);
  36. }
  37. #endif
  38. #define NSEC_PER_SEC 1000000000ULL
  39. #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
  40. #define CLOCK_REALTIME 0
  41. #define CLOCK_MONOTONIC 1
  42. #define CLOCK_PROCESS_CPUTIME_ID 2
  43. #define CLOCK_THREAD_CPUTIME_ID 3
  44. #define CLOCK_MONOTONIC_RAW 4
  45. #define CLOCK_REALTIME_COARSE 5
  46. #define CLOCK_MONOTONIC_COARSE 6
  47. #define CLOCK_BOOTTIME 7
  48. #define CLOCK_REALTIME_ALARM 8
  49. #define CLOCK_BOOTTIME_ALARM 9
  50. #define CLOCK_HWSPECIFIC 10
  51. #define CLOCK_TAI 11
  52. #define NR_CLOCKIDS 12
  53. #define UNSUPPORTED 0xf00f
  54. char *clockstring(int clockid)
  55. {
  56. switch (clockid) {
  57. case CLOCK_REALTIME:
  58. return "CLOCK_REALTIME";
  59. case CLOCK_MONOTONIC:
  60. return "CLOCK_MONOTONIC";
  61. case CLOCK_PROCESS_CPUTIME_ID:
  62. return "CLOCK_PROCESS_CPUTIME_ID";
  63. case CLOCK_THREAD_CPUTIME_ID:
  64. return "CLOCK_THREAD_CPUTIME_ID";
  65. case CLOCK_MONOTONIC_RAW:
  66. return "CLOCK_MONOTONIC_RAW";
  67. case CLOCK_REALTIME_COARSE:
  68. return "CLOCK_REALTIME_COARSE";
  69. case CLOCK_MONOTONIC_COARSE:
  70. return "CLOCK_MONOTONIC_COARSE";
  71. case CLOCK_BOOTTIME:
  72. return "CLOCK_BOOTTIME";
  73. case CLOCK_REALTIME_ALARM:
  74. return "CLOCK_REALTIME_ALARM";
  75. case CLOCK_BOOTTIME_ALARM:
  76. return "CLOCK_BOOTTIME_ALARM";
  77. case CLOCK_TAI:
  78. return "CLOCK_TAI";
  79. };
  80. return "UNKNOWN_CLOCKID";
  81. }
  82. struct timespec timespec_add(struct timespec ts, unsigned long long ns)
  83. {
  84. ts.tv_nsec += ns;
  85. while (ts.tv_nsec >= NSEC_PER_SEC) {
  86. ts.tv_nsec -= NSEC_PER_SEC;
  87. ts.tv_sec++;
  88. }
  89. return ts;
  90. }
  91. long long timespec_sub(struct timespec a, struct timespec b)
  92. {
  93. long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
  94. ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
  95. return ret;
  96. }
  97. int nanosleep_lat_test(int clockid, long long ns)
  98. {
  99. struct timespec start, end, target;
  100. long long latency = 0;
  101. int i, count;
  102. target.tv_sec = ns/NSEC_PER_SEC;
  103. target.tv_nsec = ns%NSEC_PER_SEC;
  104. if (clock_gettime(clockid, &start))
  105. return UNSUPPORTED;
  106. if (clock_nanosleep(clockid, 0, &target, NULL))
  107. return UNSUPPORTED;
  108. count = 10;
  109. /* First check relative latency */
  110. clock_gettime(clockid, &start);
  111. for (i = 0; i < count; i++)
  112. clock_nanosleep(clockid, 0, &target, NULL);
  113. clock_gettime(clockid, &end);
  114. if (((timespec_sub(start, end)/count)-ns) > UNRESONABLE_LATENCY) {
  115. printf("Large rel latency: %lld ns :", (timespec_sub(start, end)/count)-ns);
  116. return -1;
  117. }
  118. /* Next check absolute latency */
  119. for (i = 0; i < count; i++) {
  120. clock_gettime(clockid, &start);
  121. target = timespec_add(start, ns);
  122. clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL);
  123. clock_gettime(clockid, &end);
  124. latency += timespec_sub(target, end);
  125. }
  126. if (latency/count > UNRESONABLE_LATENCY) {
  127. printf("Large abs latency: %lld ns :", latency/count);
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. int main(int argc, char **argv)
  133. {
  134. long long length;
  135. int clockid, ret;
  136. for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
  137. /* Skip cputime clockids since nanosleep won't increment cputime */
  138. if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
  139. clockid == CLOCK_THREAD_CPUTIME_ID ||
  140. clockid == CLOCK_HWSPECIFIC)
  141. continue;
  142. printf("nsleep latency %-26s ", clockstring(clockid));
  143. length = 10;
  144. while (length <= (NSEC_PER_SEC * 10)) {
  145. ret = nanosleep_lat_test(clockid, length);
  146. if (ret)
  147. break;
  148. length *= 100;
  149. }
  150. if (ret == UNSUPPORTED) {
  151. printf("[UNSUPPORTED]\n");
  152. continue;
  153. }
  154. if (ret < 0) {
  155. printf("[FAILED]\n");
  156. return ksft_exit_fail();
  157. }
  158. printf("[OK]\n");
  159. }
  160. return ksft_exit_pass();
  161. }