tst-clock2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #if defined _POSIX_THREAD_CPUTIME && _POSIX_THREAD_CPUTIME >= 0
  23. static pthread_barrier_t b2;
  24. static pthread_barrier_t bN;
  25. static void *
  26. tf (void *arg)
  27. {
  28. int e = pthread_barrier_wait (&b2);
  29. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  30. {
  31. puts ("barrier_wait failed");
  32. exit (1);
  33. }
  34. e = pthread_barrier_wait (&bN);
  35. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  36. {
  37. puts ("barrier_wait failed");
  38. exit (1);
  39. }
  40. return NULL;
  41. }
  42. #endif
  43. int
  44. do_test (void)
  45. {
  46. #if defined _POSIX_THREAD_CPUTIME && _POSIX_THREAD_CPUTIME >= 0
  47. # define N 10
  48. # if _POSIX_THREAD_CPUTIME == 0
  49. if (sysconf (_SC_THREAD_CPUTIME) < 0)
  50. {
  51. puts ("_POSIX_THREAD_CPUTIME option not available");
  52. return 1;
  53. }
  54. # endif
  55. if (pthread_barrier_init (&b2, NULL, 2) != 0
  56. || pthread_barrier_init (&bN, NULL, N + 1) != 0)
  57. {
  58. puts ("barrier_init failed");
  59. return 1;
  60. }
  61. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  62. TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
  63. pthread_t th[N + 1];
  64. clockid_t cl[N + 1];
  65. # ifndef CLOCK_THREAD_CPUTIME_ID
  66. if (pthread_getcpuclockid (pthread_self (), &cl[0]) != 0)
  67. {
  68. puts ("own pthread_getcpuclockid failed");
  69. return 1;
  70. }
  71. # else
  72. cl[0] = CLOCK_THREAD_CPUTIME_ID;
  73. # endif
  74. pthread_attr_t at;
  75. if (pthread_attr_init (&at) != 0)
  76. {
  77. puts ("attr_init failed");
  78. return 1;
  79. }
  80. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  81. {
  82. puts ("attr_setstacksize failed");
  83. return 1;
  84. }
  85. int i;
  86. int e;
  87. for (i = 0; i < N; ++i)
  88. {
  89. if (pthread_create (&th[i], &at, tf, NULL) != 0)
  90. {
  91. puts ("create failed");
  92. return 1;
  93. }
  94. e = pthread_barrier_wait (&b2);
  95. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  96. {
  97. puts ("barrier_wait failed");
  98. return 1;
  99. }
  100. ts.tv_sec = 0;
  101. ts.tv_nsec = 100000000;
  102. TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
  103. if (pthread_getcpuclockid (th[i], &cl[i + 1]) != 0)
  104. {
  105. puts ("pthread_getcpuclockid failed");
  106. return 1;
  107. }
  108. }
  109. if (pthread_attr_destroy (&at) != 0)
  110. {
  111. puts ("attr_destroy failed");
  112. return 1;
  113. }
  114. struct timespec t[N + 1];
  115. for (i = 0; i < N + 1; ++i)
  116. if (clock_gettime (cl[i], &t[i]) != 0)
  117. {
  118. printf ("clock_gettime round %d failed\n", i);
  119. return 1;
  120. }
  121. for (i = 0; i < N; ++i)
  122. {
  123. struct timespec diff;
  124. diff.tv_sec = t[i].tv_sec - t[i + 1].tv_sec;
  125. diff.tv_nsec = t[i].tv_nsec - t[i + 1].tv_nsec;
  126. if (diff.tv_nsec < 0)
  127. {
  128. diff.tv_nsec += 1000000000;
  129. --diff.tv_sec;
  130. }
  131. if (diff.tv_sec < 0 || (diff.tv_sec == 0 && diff.tv_nsec < 100000000))
  132. {
  133. printf ("\
  134. difference between thread %d and %d too small (%ld.%09ld)\n",
  135. i, i + 1, (long int) diff.tv_sec, (long int) diff.tv_nsec);
  136. return 1;
  137. }
  138. printf ("diff %d->%d: %ld.%09ld\n",
  139. i, i + 1, (long int) diff.tv_sec, (long int) diff.tv_nsec);
  140. }
  141. ts.tv_sec = 0;
  142. ts.tv_nsec = 0;
  143. for (i = 0; i < N + 1; ++i)
  144. if (clock_settime (cl[i], &ts) != 0)
  145. {
  146. printf ("clock_settime(%d) round %d failed\n", cl[i], i);
  147. return 1;
  148. }
  149. for (i = 0; i < N + 1; ++i)
  150. {
  151. if (clock_gettime (cl[i], &ts) != 0)
  152. {
  153. puts ("clock_gettime failed");
  154. return 1;
  155. }
  156. if (ts.tv_sec > t[i].tv_sec
  157. || (ts.tv_sec == t[i].tv_sec && ts.tv_nsec > t[i].tv_nsec))
  158. {
  159. puts ("clock_settime didn't reset clock");
  160. return 1;
  161. }
  162. }
  163. #endif
  164. return 0;
  165. }
  166. #define TEST_FUNCTION do_test ()
  167. #include "../test-skeleton.c"