tst-cpuclock2.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Test program for process and thread CPU clocks.
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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 <unistd.h>
  17. #if (_POSIX_THREADS - 0) <= 0
  18. # define TEST_FUNCTION 0
  19. #else
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #include <fcntl.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <pthread.h>
  27. static pthread_barrier_t barrier;
  28. /* This function is intended to rack up both user and system time. */
  29. static void *
  30. chew_cpu (void *arg)
  31. {
  32. pthread_barrier_wait (&barrier);
  33. while (1)
  34. {
  35. static volatile char buf[4096];
  36. for (int i = 0; i < 100; ++i)
  37. for (size_t j = 0; j < sizeof buf; ++j)
  38. buf[j] = 0xaa;
  39. int nullfd = open ("/dev/null", O_WRONLY);
  40. for (int i = 0; i < 100; ++i)
  41. for (size_t j = 0; j < sizeof buf; ++j)
  42. buf[j] = 0xbb;
  43. write (nullfd, (char *) buf, sizeof buf);
  44. close (nullfd);
  45. }
  46. return NULL;
  47. }
  48. static unsigned long long int
  49. tsdiff (const struct timespec *before, const struct timespec *after)
  50. {
  51. struct timespec diff = { .tv_sec = after->tv_sec - before->tv_sec,
  52. .tv_nsec = after->tv_nsec - before->tv_nsec };
  53. while (diff.tv_nsec < 0)
  54. {
  55. --diff.tv_sec;
  56. diff.tv_nsec += 1000000000;
  57. }
  58. return diff.tv_sec * 1000000000ULL + diff.tv_nsec;
  59. }
  60. static unsigned long long int
  61. test_nanosleep (clockid_t clock, const char *which,
  62. const struct timespec *before, int *bad)
  63. {
  64. const struct timespec sleeptime = { .tv_nsec = 100000000 };
  65. int e = clock_nanosleep (clock, 0, &sleeptime, NULL);
  66. if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
  67. {
  68. printf ("clock_nanosleep not supported for %s CPU clock: %s\n",
  69. which, strerror (e));
  70. return 0;
  71. }
  72. if (e != 0)
  73. {
  74. printf ("clock_nanosleep on %s CPU clock: %s\n", which, strerror (e));
  75. *bad = 1;
  76. return 0;
  77. }
  78. struct timespec after;
  79. if (clock_gettime (clock, &after) < 0)
  80. {
  81. printf ("clock_gettime on %s CPU clock %lx => %s\n",
  82. which, (unsigned long int) clock, strerror (errno));
  83. *bad = 1;
  84. return 0;
  85. }
  86. unsigned long long int diff = tsdiff (before, &after);
  87. if (diff < sleeptime.tv_nsec || diff > sleeptime.tv_nsec * 2)
  88. {
  89. printf ("clock_nanosleep on %s slept %llu (outside reasonable range)\n",
  90. which, diff);
  91. *bad = 1;
  92. return diff;
  93. }
  94. struct timespec sleeptimeabs = sleeptime;
  95. sleeptimeabs.tv_sec += after.tv_sec;
  96. sleeptimeabs.tv_nsec += after.tv_nsec;
  97. while (sleeptimeabs.tv_nsec > 1000000000)
  98. {
  99. ++sleeptimeabs.tv_sec;
  100. sleeptimeabs.tv_nsec -= 1000000000;
  101. }
  102. e = clock_nanosleep (clock, TIMER_ABSTIME, &sleeptimeabs, NULL);
  103. if (e != 0)
  104. {
  105. printf ("absolute clock_nanosleep on %s CPU clock: %s\n",
  106. which, strerror (e));
  107. *bad = 1;
  108. return diff;
  109. }
  110. struct timespec afterabs;
  111. if (clock_gettime (clock, &afterabs) < 0)
  112. {
  113. printf ("clock_gettime on %s CPU clock %lx => %s\n",
  114. which, (unsigned long int) clock, strerror (errno));
  115. *bad = 1;
  116. return diff;
  117. }
  118. unsigned long long int sleepdiff = tsdiff (&sleeptimeabs, &afterabs);
  119. if (sleepdiff > sleeptime.tv_nsec)
  120. {
  121. printf ("\
  122. absolute clock_nanosleep on %s %llu past target (outside reasonable range)\n",
  123. which, sleepdiff);
  124. *bad = 1;
  125. }
  126. unsigned long long int diffabs = tsdiff (&after, &afterabs);
  127. if (diffabs < sleeptime.tv_nsec || diffabs > sleeptime.tv_nsec * 2)
  128. {
  129. printf ("\
  130. absolute clock_nanosleep on %s slept %llu (outside reasonable range)\n",
  131. which, diffabs);
  132. *bad = 1;
  133. }
  134. return diff + diffabs;
  135. }
  136. static int
  137. do_test (void)
  138. {
  139. int result = 0;
  140. clockid_t process_clock, th_clock, my_thread_clock;
  141. int e;
  142. pthread_t th;
  143. e = clock_getcpuclockid (0, &process_clock);
  144. if (e != 0)
  145. {
  146. printf ("clock_getcpuclockid on self => %s\n", strerror (e));
  147. return 1;
  148. }
  149. e = pthread_getcpuclockid (pthread_self (), &my_thread_clock);
  150. if (e != 0)
  151. {
  152. printf ("pthread_getcpuclockid on self => %s\n", strerror (e));
  153. return 1;
  154. }
  155. /* This is a kludge. This test fails if the semantics of thread and
  156. process clocks are wrong. The old code using hp-timing without kernel
  157. support has bogus semantics if there are context switches. We don't
  158. fail to report failure when the proper functionality is not available
  159. in the kernel. It so happens that Linux kernels without correct CPU
  160. clock support also lack CPU timer support, so we use use that to guess
  161. that we are using the bogus code and not test it. */
  162. timer_t t;
  163. if (timer_create (my_thread_clock, NULL, &t) != 0)
  164. {
  165. printf ("timer_create: %m\n");
  166. puts ("No support for CPU clocks with good semantics, skipping test");
  167. return 0;
  168. }
  169. timer_delete (t);
  170. pthread_barrier_init (&barrier, NULL, 2);
  171. e = pthread_create (&th, NULL, chew_cpu, NULL);
  172. if (e != 0)
  173. {
  174. printf ("pthread_create: %s\n", strerror (e));
  175. return 1;
  176. }
  177. e = pthread_getcpuclockid (th, &th_clock);
  178. if (e == ENOENT || e == ENOSYS || e == ENOTSUP)
  179. {
  180. puts ("pthread_getcpuclockid does not support other threads");
  181. return 1;
  182. }
  183. pthread_barrier_wait (&barrier);
  184. struct timespec res;
  185. if (clock_getres (th_clock, &res) < 0)
  186. {
  187. printf ("clock_getres on thread clock %lx => %s\n",
  188. (unsigned long int) th_clock, strerror (errno));
  189. result = 1;
  190. return 1;
  191. }
  192. printf ("live thread clock %lx resolution %lu.%.9lu\n",
  193. (unsigned long int) th_clock, res.tv_sec, res.tv_nsec);
  194. struct timespec process_before, process_after;
  195. if (clock_gettime (process_clock, &process_before) < 0)
  196. {
  197. printf ("clock_gettime on process clock %lx => %s\n",
  198. (unsigned long int) th_clock, strerror (errno));
  199. return 1;
  200. }
  201. struct timespec before, after;
  202. if (clock_gettime (th_clock, &before) < 0)
  203. {
  204. printf ("clock_gettime on live thread clock %lx => %s\n",
  205. (unsigned long int) th_clock, strerror (errno));
  206. return 1;
  207. }
  208. printf ("live thread before sleep => %lu.%.9lu\n",
  209. before.tv_sec, before.tv_nsec);
  210. struct timespec me_before, me_after;
  211. if (clock_gettime (my_thread_clock, &me_before) < 0)
  212. {
  213. printf ("clock_gettime on live thread clock %lx => %s\n",
  214. (unsigned long int) th_clock, strerror (errno));
  215. return 1;
  216. }
  217. printf ("self thread before sleep => %lu.%.9lu\n",
  218. me_before.tv_sec, me_before.tv_nsec);
  219. struct timespec sleeptime = { .tv_nsec = 500000000 };
  220. nanosleep (&sleeptime, NULL);
  221. if (clock_gettime (th_clock, &after) < 0)
  222. {
  223. printf ("clock_gettime on live thread clock %lx => %s\n",
  224. (unsigned long int) th_clock, strerror (errno));
  225. return 1;
  226. }
  227. printf ("live thread after sleep => %lu.%.9lu\n",
  228. after.tv_sec, after.tv_nsec);
  229. if (clock_gettime (process_clock, &process_after) < 0)
  230. {
  231. printf ("clock_gettime on process clock %lx => %s\n",
  232. (unsigned long int) th_clock, strerror (errno));
  233. return 1;
  234. }
  235. if (clock_gettime (my_thread_clock, &me_after) < 0)
  236. {
  237. printf ("clock_gettime on live thread clock %lx => %s\n",
  238. (unsigned long int) th_clock, strerror (errno));
  239. return 1;
  240. }
  241. printf ("self thread after sleep => %lu.%.9lu\n",
  242. me_after.tv_sec, me_after.tv_nsec);
  243. unsigned long long int th_diff = tsdiff (&before, &after);
  244. unsigned long long int pdiff = tsdiff (&process_before, &process_after);
  245. unsigned long long int my_diff = tsdiff (&me_before, &me_after);
  246. if (th_diff < 100000000 || th_diff > 600000000)
  247. {
  248. printf ("thread before - after %llu outside reasonable range\n",
  249. th_diff);
  250. result = 1;
  251. }
  252. if (my_diff > 100000000)
  253. {
  254. printf ("self thread before - after %llu outside reasonable range\n",
  255. my_diff);
  256. result = 1;
  257. }
  258. if (pdiff < th_diff)
  259. {
  260. printf ("process before - after %llu outside reasonable range (%llu)\n",
  261. pdiff, th_diff);
  262. result = 1;
  263. }
  264. process_after.tv_nsec += test_nanosleep (th_clock, "thread",
  265. &after, &result);
  266. process_after.tv_nsec += test_nanosleep (process_clock, "process",
  267. &process_after, &result);
  268. test_nanosleep (CLOCK_PROCESS_CPUTIME_ID,
  269. "PROCESS_CPUTIME_ID", &process_after, &result);
  270. pthread_cancel (th);
  271. e = clock_nanosleep (CLOCK_THREAD_CPUTIME_ID, 0, &sleeptime, NULL);
  272. if (e != EINVAL)
  273. {
  274. printf ("clock_nanosleep CLOCK_THREAD_CPUTIME_ID: %s\n",
  275. strerror (e));
  276. result = 1;
  277. }
  278. return result;
  279. }
  280. # define TIMEOUT 8
  281. # define TEST_FUNCTION do_test ()
  282. #endif
  283. #include "../test-skeleton.c"