posix_timers.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Red Hat, Inc., Frederic Weisbecker <[email protected]>
  4. *
  5. * Selftests for a few posix timers interface.
  6. *
  7. * Kernel loop code stolen from Steven Rostedt <[email protected]>
  8. */
  9. #include <sys/time.h>
  10. #include <stdio.h>
  11. #include <signal.h>
  12. #include <unistd.h>
  13. #include <time.h>
  14. #include <pthread.h>
  15. #include "../kselftest.h"
  16. #define DELAY 2
  17. #define USECS_PER_SEC 1000000
  18. static volatile int done;
  19. /* Busy loop in userspace to elapse ITIMER_VIRTUAL */
  20. static void user_loop(void)
  21. {
  22. while (!done);
  23. }
  24. /*
  25. * Try to spend as much time as possible in kernelspace
  26. * to elapse ITIMER_PROF.
  27. */
  28. static void kernel_loop(void)
  29. {
  30. void *addr = sbrk(0);
  31. int err = 0;
  32. while (!done && !err) {
  33. err = brk(addr + 4096);
  34. err |= brk(addr);
  35. }
  36. }
  37. /*
  38. * Sleep until ITIMER_REAL expiration.
  39. */
  40. static void idle_loop(void)
  41. {
  42. pause();
  43. }
  44. static void sig_handler(int nr)
  45. {
  46. done = 1;
  47. }
  48. /*
  49. * Check the expected timer expiration matches the GTOD elapsed delta since
  50. * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
  51. */
  52. static int check_diff(struct timeval start, struct timeval end)
  53. {
  54. long long diff;
  55. diff = end.tv_usec - start.tv_usec;
  56. diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
  57. if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
  58. printf("Diff too high: %lld..", diff);
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static int check_itimer(int which)
  64. {
  65. int err;
  66. struct timeval start, end;
  67. struct itimerval val = {
  68. .it_value.tv_sec = DELAY,
  69. };
  70. printf("Check itimer ");
  71. if (which == ITIMER_VIRTUAL)
  72. printf("virtual... ");
  73. else if (which == ITIMER_PROF)
  74. printf("prof... ");
  75. else if (which == ITIMER_REAL)
  76. printf("real... ");
  77. fflush(stdout);
  78. done = 0;
  79. if (which == ITIMER_VIRTUAL)
  80. signal(SIGVTALRM, sig_handler);
  81. else if (which == ITIMER_PROF)
  82. signal(SIGPROF, sig_handler);
  83. else if (which == ITIMER_REAL)
  84. signal(SIGALRM, sig_handler);
  85. err = gettimeofday(&start, NULL);
  86. if (err < 0) {
  87. perror("Can't call gettimeofday()\n");
  88. return -1;
  89. }
  90. err = setitimer(which, &val, NULL);
  91. if (err < 0) {
  92. perror("Can't set timer\n");
  93. return -1;
  94. }
  95. if (which == ITIMER_VIRTUAL)
  96. user_loop();
  97. else if (which == ITIMER_PROF)
  98. kernel_loop();
  99. else if (which == ITIMER_REAL)
  100. idle_loop();
  101. err = gettimeofday(&end, NULL);
  102. if (err < 0) {
  103. perror("Can't call gettimeofday()\n");
  104. return -1;
  105. }
  106. if (!check_diff(start, end))
  107. printf("[OK]\n");
  108. else
  109. printf("[FAIL]\n");
  110. return 0;
  111. }
  112. static int check_timer_create(int which)
  113. {
  114. int err;
  115. timer_t id;
  116. struct timeval start, end;
  117. struct itimerspec val = {
  118. .it_value.tv_sec = DELAY,
  119. };
  120. printf("Check timer_create() ");
  121. if (which == CLOCK_THREAD_CPUTIME_ID) {
  122. printf("per thread... ");
  123. } else if (which == CLOCK_PROCESS_CPUTIME_ID) {
  124. printf("per process... ");
  125. }
  126. fflush(stdout);
  127. done = 0;
  128. err = timer_create(which, NULL, &id);
  129. if (err < 0) {
  130. perror("Can't create timer\n");
  131. return -1;
  132. }
  133. signal(SIGALRM, sig_handler);
  134. err = gettimeofday(&start, NULL);
  135. if (err < 0) {
  136. perror("Can't call gettimeofday()\n");
  137. return -1;
  138. }
  139. err = timer_settime(id, 0, &val, NULL);
  140. if (err < 0) {
  141. perror("Can't set timer\n");
  142. return -1;
  143. }
  144. user_loop();
  145. err = gettimeofday(&end, NULL);
  146. if (err < 0) {
  147. perror("Can't call gettimeofday()\n");
  148. return -1;
  149. }
  150. if (!check_diff(start, end))
  151. printf("[OK]\n");
  152. else
  153. printf("[FAIL]\n");
  154. return 0;
  155. }
  156. int main(int argc, char **argv)
  157. {
  158. printf("Testing posix timers. False negative may happen on CPU execution \n");
  159. printf("based timers if other threads run on the CPU...\n");
  160. if (check_itimer(ITIMER_VIRTUAL) < 0)
  161. return ksft_exit_fail();
  162. if (check_itimer(ITIMER_PROF) < 0)
  163. return ksft_exit_fail();
  164. if (check_itimer(ITIMER_REAL) < 0)
  165. return ksft_exit_fail();
  166. if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0)
  167. return ksft_exit_fail();
  168. /*
  169. * It's unfortunately hard to reliably test a timer expiration
  170. * on parallel multithread cputime. We could arm it to expire
  171. * on DELAY * nr_threads, with nr_threads busy looping, then wait
  172. * the normal DELAY since the time is elapsing nr_threads faster.
  173. * But for that we need to ensure we have real physical free CPUs
  174. * to ensure true parallelism. So test only one thread until we
  175. * find a better solution.
  176. */
  177. if (check_timer_create(CLOCK_PROCESS_CPUTIME_ID) < 0)
  178. return ksft_exit_fail();
  179. return ksft_exit_pass();
  180. }