posix_timers.c 4.3 KB

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