set-timer-lat.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* set_timer latency test
  2. * John Stultz ([email protected])
  3. * (C) Copyright Linaro 2014
  4. * Licensed under the GPLv2
  5. *
  6. * This test makes sure the set_timer api is correct
  7. *
  8. * To build:
  9. * $ gcc set-timer-lat.c -o set-timer-lat -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 <errno.h>
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <time.h>
  25. #include <string.h>
  26. #include <signal.h>
  27. #include <stdlib.h>
  28. #include <pthread.h>
  29. #include "../kselftest.h"
  30. #define CLOCK_REALTIME 0
  31. #define CLOCK_MONOTONIC 1
  32. #define CLOCK_PROCESS_CPUTIME_ID 2
  33. #define CLOCK_THREAD_CPUTIME_ID 3
  34. #define CLOCK_MONOTONIC_RAW 4
  35. #define CLOCK_REALTIME_COARSE 5
  36. #define CLOCK_MONOTONIC_COARSE 6
  37. #define CLOCK_BOOTTIME 7
  38. #define CLOCK_REALTIME_ALARM 8
  39. #define CLOCK_BOOTTIME_ALARM 9
  40. #define CLOCK_HWSPECIFIC 10
  41. #define CLOCK_TAI 11
  42. #define NR_CLOCKIDS 12
  43. #define NSEC_PER_SEC 1000000000ULL
  44. #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
  45. #define TIMER_SECS 1
  46. int alarmcount;
  47. int clock_id;
  48. struct timespec start_time;
  49. long long max_latency_ns;
  50. int timer_fired_early;
  51. char *clockstring(int clockid)
  52. {
  53. switch (clockid) {
  54. case CLOCK_REALTIME:
  55. return "CLOCK_REALTIME";
  56. case CLOCK_MONOTONIC:
  57. return "CLOCK_MONOTONIC";
  58. case CLOCK_PROCESS_CPUTIME_ID:
  59. return "CLOCK_PROCESS_CPUTIME_ID";
  60. case CLOCK_THREAD_CPUTIME_ID:
  61. return "CLOCK_THREAD_CPUTIME_ID";
  62. case CLOCK_MONOTONIC_RAW:
  63. return "CLOCK_MONOTONIC_RAW";
  64. case CLOCK_REALTIME_COARSE:
  65. return "CLOCK_REALTIME_COARSE";
  66. case CLOCK_MONOTONIC_COARSE:
  67. return "CLOCK_MONOTONIC_COARSE";
  68. case CLOCK_BOOTTIME:
  69. return "CLOCK_BOOTTIME";
  70. case CLOCK_REALTIME_ALARM:
  71. return "CLOCK_REALTIME_ALARM";
  72. case CLOCK_BOOTTIME_ALARM:
  73. return "CLOCK_BOOTTIME_ALARM";
  74. case CLOCK_TAI:
  75. return "CLOCK_TAI";
  76. };
  77. return "UNKNOWN_CLOCKID";
  78. }
  79. long long timespec_sub(struct timespec a, struct timespec b)
  80. {
  81. long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
  82. ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
  83. return ret;
  84. }
  85. void sigalarm(int signo)
  86. {
  87. long long delta_ns;
  88. struct timespec ts;
  89. clock_gettime(clock_id, &ts);
  90. alarmcount++;
  91. delta_ns = timespec_sub(start_time, ts);
  92. delta_ns -= NSEC_PER_SEC * TIMER_SECS * alarmcount;
  93. if (delta_ns < 0)
  94. timer_fired_early = 1;
  95. if (delta_ns > max_latency_ns)
  96. max_latency_ns = delta_ns;
  97. }
  98. void describe_timer(int flags, int interval)
  99. {
  100. printf("%-22s %s %s ",
  101. clockstring(clock_id),
  102. flags ? "ABSTIME":"RELTIME",
  103. interval ? "PERIODIC":"ONE-SHOT");
  104. }
  105. int setup_timer(int clock_id, int flags, int interval, timer_t *tm1)
  106. {
  107. struct sigevent se;
  108. struct itimerspec its1, its2;
  109. int err;
  110. /* Set up timer: */
  111. memset(&se, 0, sizeof(se));
  112. se.sigev_notify = SIGEV_SIGNAL;
  113. se.sigev_signo = SIGRTMAX;
  114. se.sigev_value.sival_int = 0;
  115. max_latency_ns = 0;
  116. alarmcount = 0;
  117. timer_fired_early = 0;
  118. err = timer_create(clock_id, &se, tm1);
  119. if (err) {
  120. if ((clock_id == CLOCK_REALTIME_ALARM) ||
  121. (clock_id == CLOCK_BOOTTIME_ALARM)) {
  122. printf("%-22s %s missing CAP_WAKE_ALARM? : [UNSUPPORTED]\n",
  123. clockstring(clock_id),
  124. flags ? "ABSTIME":"RELTIME");
  125. /* Indicate timer isn't set, so caller doesn't wait */
  126. return 1;
  127. }
  128. printf("%s - timer_create() failed\n", clockstring(clock_id));
  129. return -1;
  130. }
  131. clock_gettime(clock_id, &start_time);
  132. if (flags) {
  133. its1.it_value = start_time;
  134. its1.it_value.tv_sec += TIMER_SECS;
  135. } else {
  136. its1.it_value.tv_sec = TIMER_SECS;
  137. its1.it_value.tv_nsec = 0;
  138. }
  139. its1.it_interval.tv_sec = interval;
  140. its1.it_interval.tv_nsec = 0;
  141. err = timer_settime(*tm1, flags, &its1, &its2);
  142. if (err) {
  143. printf("%s - timer_settime() failed\n", clockstring(clock_id));
  144. return -1;
  145. }
  146. return 0;
  147. }
  148. int check_timer_latency(int flags, int interval)
  149. {
  150. int err = 0;
  151. describe_timer(flags, interval);
  152. printf("timer fired early: %7d : ", timer_fired_early);
  153. if (!timer_fired_early) {
  154. printf("[OK]\n");
  155. } else {
  156. printf("[FAILED]\n");
  157. err = -1;
  158. }
  159. describe_timer(flags, interval);
  160. printf("max latency: %10lld ns : ", max_latency_ns);
  161. if (max_latency_ns < UNRESONABLE_LATENCY) {
  162. printf("[OK]\n");
  163. } else {
  164. printf("[FAILED]\n");
  165. err = -1;
  166. }
  167. return err;
  168. }
  169. int check_alarmcount(int flags, int interval)
  170. {
  171. describe_timer(flags, interval);
  172. printf("count: %19d : ", alarmcount);
  173. if (alarmcount == 1) {
  174. printf("[OK]\n");
  175. return 0;
  176. }
  177. printf("[FAILED]\n");
  178. return -1;
  179. }
  180. int do_timer(int clock_id, int flags)
  181. {
  182. timer_t tm1;
  183. const int interval = TIMER_SECS;
  184. int err;
  185. err = setup_timer(clock_id, flags, interval, &tm1);
  186. /* Unsupported case - return 0 to not fail the test */
  187. if (err)
  188. return err == 1 ? 0 : err;
  189. while (alarmcount < 5)
  190. sleep(1);
  191. timer_delete(tm1);
  192. return check_timer_latency(flags, interval);
  193. }
  194. int do_timer_oneshot(int clock_id, int flags)
  195. {
  196. timer_t tm1;
  197. const int interval = 0;
  198. struct timeval timeout;
  199. int err;
  200. err = setup_timer(clock_id, flags, interval, &tm1);
  201. /* Unsupported case - return 0 to not fail the test */
  202. if (err)
  203. return err == 1 ? 0 : err;
  204. memset(&timeout, 0, sizeof(timeout));
  205. timeout.tv_sec = 5;
  206. do {
  207. err = select(0, NULL, NULL, NULL, &timeout);
  208. } while (err == -1 && errno == EINTR);
  209. timer_delete(tm1);
  210. err = check_timer_latency(flags, interval);
  211. err |= check_alarmcount(flags, interval);
  212. return err;
  213. }
  214. int main(void)
  215. {
  216. struct sigaction act;
  217. int signum = SIGRTMAX;
  218. int ret = 0;
  219. /* Set up signal handler: */
  220. sigfillset(&act.sa_mask);
  221. act.sa_flags = 0;
  222. act.sa_handler = sigalarm;
  223. sigaction(signum, &act, NULL);
  224. printf("Setting timers for every %i seconds\n", TIMER_SECS);
  225. for (clock_id = 0; clock_id < NR_CLOCKIDS; clock_id++) {
  226. if ((clock_id == CLOCK_PROCESS_CPUTIME_ID) ||
  227. (clock_id == CLOCK_THREAD_CPUTIME_ID) ||
  228. (clock_id == CLOCK_MONOTONIC_RAW) ||
  229. (clock_id == CLOCK_REALTIME_COARSE) ||
  230. (clock_id == CLOCK_MONOTONIC_COARSE) ||
  231. (clock_id == CLOCK_HWSPECIFIC))
  232. continue;
  233. ret |= do_timer(clock_id, TIMER_ABSTIME);
  234. ret |= do_timer(clock_id, 0);
  235. ret |= do_timer_oneshot(clock_id, TIMER_ABSTIME);
  236. ret |= do_timer_oneshot(clock_id, 0);
  237. }
  238. if (ret)
  239. return ksft_exit_fail();
  240. return ksft_exit_pass();
  241. }