nsleep-lat.c 4.3 KB

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