set-timer-lat.c 6.2 KB

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