tst-cpuclock1.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Test program for process CPU clocks.
  2. Copyright (C) 2004-2026 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, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <time.h>
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include <stdint.h>
  24. #include <sys/wait.h>
  25. #include <support/xunistd.h>
  26. /* This function is intended to rack up both user and system time. */
  27. static void
  28. chew_cpu (void)
  29. {
  30. while (1)
  31. {
  32. static volatile char buf[4096];
  33. for (int i = 0; i < 100; ++i)
  34. for (size_t j = 0; j < sizeof buf; ++j)
  35. buf[j] = 0xaa;
  36. int nullfd = open ("/dev/null", O_WRONLY);
  37. for (int i = 0; i < 100; ++i)
  38. for (size_t j = 0; j < sizeof buf; ++j)
  39. buf[j] = 0xbb;
  40. xwrite (nullfd, (char *) buf, sizeof buf);
  41. close (nullfd);
  42. if (getppid () == 1)
  43. _exit (2);
  44. }
  45. }
  46. static int
  47. do_test (void)
  48. {
  49. int result = 0;
  50. clockid_t cl;
  51. int e;
  52. pid_t dead_child, child;
  53. /* Fork a child and let it die, to give us a PID known not be valid
  54. (assuming PIDs don't wrap around during the test). */
  55. {
  56. dead_child = fork ();
  57. if (dead_child == 0)
  58. _exit (0);
  59. if (dead_child < 0)
  60. {
  61. perror ("fork");
  62. return 1;
  63. }
  64. int x;
  65. if (wait (&x) != dead_child)
  66. {
  67. perror ("wait");
  68. return 2;
  69. }
  70. }
  71. /* POSIX says we should get ESRCH for this. */
  72. e = clock_getcpuclockid (dead_child, &cl);
  73. if (e != ENOSYS && e != ESRCH && e != EPERM)
  74. {
  75. printf ("clock_getcpuclockid on dead PID %d => %s\n",
  76. dead_child, strerror (e));
  77. result = 1;
  78. }
  79. /* Now give us a live child eating up CPU time. */
  80. child = fork ();
  81. if (child == 0)
  82. {
  83. chew_cpu ();
  84. _exit (1);
  85. }
  86. if (child < 0)
  87. {
  88. perror ("fork");
  89. return 1;
  90. }
  91. e = clock_getcpuclockid (child, &cl);
  92. if (e == EPERM)
  93. {
  94. puts ("clock_getcpuclockid does not support other processes");
  95. goto done;
  96. }
  97. if (e != 0)
  98. {
  99. printf ("clock_getcpuclockid on live PID %d => %s\n",
  100. child, strerror (e));
  101. result = 1;
  102. goto done;
  103. }
  104. const clockid_t child_clock = cl;
  105. struct timespec res;
  106. if (clock_getres (child_clock, &res) < 0)
  107. {
  108. printf ("clock_getres on live PID %d clock %lx => %s\n",
  109. child, (unsigned long int) child_clock, strerror (errno));
  110. result = 1;
  111. goto done;
  112. }
  113. printf ("live PID %d clock %lx resolution %ju.%.9ju\n",
  114. child, (unsigned long int) child_clock,
  115. (uintmax_t) res.tv_sec, (uintmax_t) res.tv_nsec);
  116. struct timespec before;
  117. if (clock_gettime (child_clock, &before) < 0)
  118. {
  119. printf ("clock_gettime on live PID %d clock %lx => %s\n",
  120. child, (unsigned long int) child_clock, strerror (errno));
  121. result = 1;
  122. goto done;
  123. }
  124. /* Should be close to 0.0. */
  125. printf ("live PID %d before sleep => %ju.%.9ju\n",
  126. child, (uintmax_t) before.tv_sec, (uintmax_t) before.tv_nsec);
  127. struct timespec sleeptime = { .tv_nsec = 100000000 };
  128. e = clock_nanosleep (child_clock, 0, &sleeptime, NULL);
  129. if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
  130. {
  131. printf ("clock_nanosleep not supported for other process clock: %s\n",
  132. strerror (e));
  133. }
  134. else if (e != 0)
  135. {
  136. printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
  137. result = 1;
  138. }
  139. else
  140. {
  141. struct timespec afterns;
  142. if (clock_gettime (child_clock, &afterns) < 0)
  143. {
  144. printf ("clock_gettime on live PID %d clock %lx => %s\n",
  145. child, (unsigned long int) child_clock, strerror (errno));
  146. result = 1;
  147. }
  148. else
  149. {
  150. printf ("live PID %d after sleep => %ju.%.9ju\n",
  151. child, (uintmax_t) afterns.tv_sec,
  152. (uintmax_t) afterns.tv_nsec);
  153. }
  154. }
  155. if (kill (child, SIGKILL) != 0)
  156. {
  157. perror ("kill");
  158. result = 2;
  159. goto done;
  160. }
  161. /* Wait long enough to let the child finish dying. */
  162. sleeptime.tv_nsec = 200000000;
  163. if (nanosleep (&sleeptime, NULL) != 0)
  164. {
  165. perror ("nanosleep");
  166. result = 1;
  167. goto done;
  168. }
  169. struct timespec dead;
  170. if (clock_gettime (child_clock, &dead) < 0)
  171. {
  172. printf ("clock_gettime on dead PID %d clock %lx => %s\n",
  173. child, (unsigned long int) child_clock, strerror (errno));
  174. result = 1;
  175. goto done;
  176. }
  177. /* Should be close to 0.1. */
  178. printf ("dead PID %d => %ju.%.9ju\n",
  179. child, (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
  180. /* Now reap the child and verify that its clock is no longer valid. */
  181. {
  182. int x;
  183. if (waitpid (child, &x, 0) != child)
  184. {
  185. perror ("waitpid");
  186. result = 1;
  187. }
  188. }
  189. if (clock_gettime (child_clock, &dead) == 0)
  190. {
  191. printf ("clock_gettime on reaped PID %d clock %lx => %ju%.9ju\n",
  192. child, (unsigned long int) child_clock,
  193. (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
  194. result = 1;
  195. }
  196. else
  197. {
  198. if (errno != EINVAL)
  199. result = 1;
  200. printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
  201. child, (unsigned long int) child_clock, strerror (errno));
  202. }
  203. if (clock_getres (child_clock, &dead) == 0)
  204. {
  205. printf ("clock_getres on reaped PID %d clock %lx => %ju%.9ju\n",
  206. child, (unsigned long int) child_clock,
  207. (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
  208. result = 1;
  209. }
  210. else
  211. {
  212. if (errno != EINVAL)
  213. result = 1;
  214. printf ("clock_getres on reaped PID %d clock %lx => %s\n",
  215. child, (unsigned long int) child_clock, strerror (errno));
  216. }
  217. return result;
  218. done:
  219. {
  220. if (kill (child, SIGKILL) != 0 && errno != ESRCH)
  221. {
  222. perror ("kill");
  223. return 2;
  224. }
  225. int x;
  226. if (waitpid (child, &x, 0) != child && errno != ECHILD)
  227. {
  228. perror ("waitpid");
  229. return 2;
  230. }
  231. }
  232. return result;
  233. }
  234. #define TEST_FUNCTION do_test ()
  235. #include "../test-skeleton.c"