tst-clock2.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <unistd.h>
  21. #if defined _POSIX_THREAD_CPUTIME && _POSIX_THREAD_CPUTIME >= 0
  22. static pthread_barrier_t b2;
  23. static pthread_barrier_t bN;
  24. static void *
  25. tf (void *arg)
  26. {
  27. int e = pthread_barrier_wait (&b2);
  28. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  29. {
  30. puts ("barrier_wait failed");
  31. exit (1);
  32. }
  33. e = pthread_barrier_wait (&bN);
  34. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  35. {
  36. puts ("barrier_wait failed");
  37. exit (1);
  38. }
  39. return NULL;
  40. }
  41. #endif
  42. int
  43. do_test (void)
  44. {
  45. #if defined _POSIX_THREAD_CPUTIME && _POSIX_THREAD_CPUTIME >= 0
  46. # define N 10
  47. # if _POSIX_THREAD_CPUTIME == 0
  48. if (sysconf (_SC_THREAD_CPUTIME) < 0)
  49. {
  50. puts ("_POSIX_THREAD_CPUTIME option not available");
  51. return 0;
  52. }
  53. # endif
  54. if (pthread_barrier_init (&b2, NULL, 2) != 0
  55. || pthread_barrier_init (&bN, NULL, N + 1) != 0)
  56. {
  57. puts ("barrier_init failed");
  58. return 1;
  59. }
  60. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  61. TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
  62. pthread_t th[N + 1];
  63. clockid_t cl[N + 1];
  64. # ifndef CLOCK_THREAD_CPUTIME_ID
  65. if (pthread_getcpuclockid (pthread_self (), &cl[0]) != 0)
  66. {
  67. puts ("own pthread_getcpuclockid failed");
  68. return 1;
  69. }
  70. # else
  71. cl[0] = CLOCK_THREAD_CPUTIME_ID;
  72. # endif
  73. pthread_attr_t at;
  74. if (pthread_attr_init (&at) != 0)
  75. {
  76. puts ("attr_init failed");
  77. return 1;
  78. }
  79. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  80. {
  81. puts ("attr_setstacksize failed");
  82. return 1;
  83. }
  84. int i;
  85. int e;
  86. for (i = 0; i < N; ++i)
  87. {
  88. if (pthread_create (&th[i], &at, tf, NULL) != 0)
  89. {
  90. puts ("create failed");
  91. return 1;
  92. }
  93. e = pthread_barrier_wait (&b2);
  94. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  95. {
  96. puts ("barrier_wait failed");
  97. return 1;
  98. }
  99. ts.tv_sec = 0;
  100. ts.tv_nsec = 100000000;
  101. TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
  102. if (pthread_getcpuclockid (th[i], &cl[i + 1]) != 0)
  103. {
  104. puts ("pthread_getcpuclockid failed");
  105. return 1;
  106. }
  107. }
  108. if (pthread_attr_destroy (&at) != 0)
  109. {
  110. puts ("attr_destroy failed");
  111. return 1;
  112. }
  113. struct timespec t[N + 1];
  114. for (i = 0; i < N + 1; ++i)
  115. if (clock_gettime (cl[i], &t[i]) != 0)
  116. {
  117. printf ("clock_gettime round %d failed\n", i);
  118. return 1;
  119. }
  120. for (i = 0; i < N; ++i)
  121. {
  122. struct timespec diff;
  123. diff.tv_sec = t[i].tv_sec - t[i + 1].tv_sec;
  124. diff.tv_nsec = t[i].tv_nsec - t[i + 1].tv_nsec;
  125. if (diff.tv_nsec < 0)
  126. {
  127. diff.tv_nsec += 1000000000;
  128. --diff.tv_sec;
  129. }
  130. if (diff.tv_sec < 0 || (diff.tv_sec == 0 && diff.tv_nsec < 100000000))
  131. {
  132. printf ("\
  133. difference between thread %d and %d too small (%ld.%09ld)\n",
  134. i, i + 1, (long int) diff.tv_sec, (long int) diff.tv_nsec);
  135. return 1;
  136. }
  137. printf ("diff %d->%d: %ld.%09ld\n",
  138. i, i + 1, (long int) diff.tv_sec, (long int) diff.tv_nsec);
  139. }
  140. ts.tv_sec = 0;
  141. ts.tv_nsec = 0;
  142. for (i = 0; i < N + 1; ++i)
  143. if (clock_settime (cl[i], &ts) != 0)
  144. {
  145. printf ("clock_settime(%d) round %d failed\n", cl[i], i);
  146. return 1;
  147. }
  148. for (i = 0; i < N + 1; ++i)
  149. {
  150. if (clock_gettime (cl[i], &ts) != 0)
  151. {
  152. puts ("clock_gettime failed");
  153. return 1;
  154. }
  155. if (ts.tv_sec > t[i].tv_sec
  156. || (ts.tv_sec == t[i].tv_sec && ts.tv_nsec > t[i].tv_nsec))
  157. {
  158. puts ("clock_settime didn't reset clock");
  159. return 1;
  160. }
  161. }
  162. #endif
  163. return 0;
  164. }
  165. #define TEST_FUNCTION do_test ()
  166. #include "../test-skeleton.c"