posix_timers.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #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. while (!done) {
  32. brk(addr + 4096);
  33. brk(addr);
  34. }
  35. }
  36. /*
  37. * Sleep until ITIMER_REAL expiration.
  38. */
  39. static void idle_loop(void)
  40. {
  41. pause();
  42. }
  43. static void sig_handler(int nr)
  44. {
  45. done = 1;
  46. }
  47. /*
  48. * Check the expected timer expiration matches the GTOD elapsed delta since
  49. * we armed the timer. Keep a 0.5 sec error margin due to various jitter.
  50. */
  51. static int check_diff(struct timeval start, struct timeval end)
  52. {
  53. long long diff;
  54. diff = end.tv_usec - start.tv_usec;
  55. diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
  56. if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
  57. printf("Diff too high: %lld..", diff);
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. static int check_itimer(int which)
  63. {
  64. int err;
  65. struct timeval start, end;
  66. struct itimerval val = {
  67. .it_value.tv_sec = DELAY,
  68. };
  69. printf("Check itimer ");
  70. if (which == ITIMER_VIRTUAL)
  71. printf("virtual... ");
  72. else if (which == ITIMER_PROF)
  73. printf("prof... ");
  74. else if (which == ITIMER_REAL)
  75. printf("real... ");
  76. fflush(stdout);
  77. done = 0;
  78. if (which == ITIMER_VIRTUAL)
  79. signal(SIGVTALRM, sig_handler);
  80. else if (which == ITIMER_PROF)
  81. signal(SIGPROF, sig_handler);
  82. else if (which == ITIMER_REAL)
  83. signal(SIGALRM, sig_handler);
  84. err = gettimeofday(&start, NULL);
  85. if (err < 0) {
  86. perror("Can't call gettimeofday()\n");
  87. return -1;
  88. }
  89. err = setitimer(which, &val, NULL);
  90. if (err < 0) {
  91. perror("Can't set timer\n");
  92. return -1;
  93. }
  94. if (which == ITIMER_VIRTUAL)
  95. user_loop();
  96. else if (which == ITIMER_PROF)
  97. kernel_loop();
  98. else if (which == ITIMER_REAL)
  99. idle_loop();
  100. gettimeofday(&end, NULL);
  101. if (err < 0) {
  102. perror("Can't call gettimeofday()\n");
  103. return -1;
  104. }
  105. if (!check_diff(start, end))
  106. printf("[OK]\n");
  107. else
  108. printf("[FAIL]\n");
  109. return 0;
  110. }
  111. static int check_timer_create(int which)
  112. {
  113. int err;
  114. timer_t id;
  115. struct timeval start, end;
  116. struct itimerspec val = {
  117. .it_value.tv_sec = DELAY,
  118. };
  119. printf("Check timer_create() ");
  120. if (which == CLOCK_THREAD_CPUTIME_ID) {
  121. printf("per thread... ");
  122. } else if (which == CLOCK_PROCESS_CPUTIME_ID) {
  123. printf("per process... ");
  124. }
  125. fflush(stdout);
  126. done = 0;
  127. timer_create(which, NULL, &id);
  128. if (err < 0) {
  129. perror("Can't create timer\n");
  130. return -1;
  131. }
  132. signal(SIGALRM, sig_handler);
  133. err = gettimeofday(&start, NULL);
  134. if (err < 0) {
  135. perror("Can't call gettimeofday()\n");
  136. return -1;
  137. }
  138. err = timer_settime(id, 0, &val, NULL);
  139. if (err < 0) {
  140. perror("Can't set timer\n");
  141. return -1;
  142. }
  143. user_loop();
  144. gettimeofday(&end, NULL);
  145. if (err < 0) {
  146. perror("Can't call gettimeofday()\n");
  147. return -1;
  148. }
  149. if (!check_diff(start, end))
  150. printf("[OK]\n");
  151. else
  152. printf("[FAIL]\n");
  153. return 0;
  154. }
  155. int main(int argc, char **argv)
  156. {
  157. int err;
  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 -1;
  162. if (check_itimer(ITIMER_PROF) < 0)
  163. return -1;
  164. if (check_itimer(ITIMER_REAL) < 0)
  165. return -1;
  166. if (check_timer_create(CLOCK_THREAD_CPUTIME_ID) < 0)
  167. return -1;
  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 -1;
  179. return 0;
  180. }