tst-cpuclock1.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /* Test program for process CPU clocks.
  2. Copyright (C) 2004-2019 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. <http://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. /* This function is intended to rack up both user and system time. */
  26. static void
  27. chew_cpu (void)
  28. {
  29. while (1)
  30. {
  31. static volatile char buf[4096];
  32. for (int i = 0; i < 100; ++i)
  33. for (size_t j = 0; j < sizeof buf; ++j)
  34. buf[j] = 0xaa;
  35. int nullfd = open ("/dev/null", O_WRONLY);
  36. for (int i = 0; i < 100; ++i)
  37. for (size_t j = 0; j < sizeof buf; ++j)
  38. buf[j] = 0xbb;
  39. write (nullfd, (char *) buf, sizeof buf);
  40. close (nullfd);
  41. if (getppid () == 1)
  42. _exit (2);
  43. }
  44. }
  45. static int
  46. do_test (void)
  47. {
  48. int result = 0;
  49. clockid_t cl;
  50. int e;
  51. pid_t dead_child, child;
  52. /* Fork a child and let it die, to give us a PID known not be valid
  53. (assuming PIDs don't wrap around during the test). */
  54. {
  55. dead_child = fork ();
  56. if (dead_child == 0)
  57. _exit (0);
  58. if (dead_child < 0)
  59. {
  60. perror ("fork");
  61. return 1;
  62. }
  63. int x;
  64. if (wait (&x) != dead_child)
  65. {
  66. perror ("wait");
  67. return 2;
  68. }
  69. }
  70. /* POSIX says we should get ESRCH for this. */
  71. e = clock_getcpuclockid (dead_child, &cl);
  72. if (e != ENOSYS && e != ESRCH && e != EPERM)
  73. {
  74. printf ("clock_getcpuclockid on dead PID %d => %s\n",
  75. dead_child, strerror (e));
  76. result = 1;
  77. }
  78. /* Now give us a live child eating up CPU time. */
  79. child = fork ();
  80. if (child == 0)
  81. {
  82. chew_cpu ();
  83. _exit (1);
  84. }
  85. if (child < 0)
  86. {
  87. perror ("fork");
  88. return 1;
  89. }
  90. e = clock_getcpuclockid (child, &cl);
  91. if (e == EPERM)
  92. {
  93. puts ("clock_getcpuclockid does not support other processes");
  94. goto done;
  95. }
  96. if (e != 0)
  97. {
  98. printf ("clock_getcpuclockid on live PID %d => %s\n",
  99. child, strerror (e));
  100. result = 1;
  101. goto done;
  102. }
  103. const clockid_t child_clock = cl;
  104. struct timespec res;
  105. if (clock_getres (child_clock, &res) < 0)
  106. {
  107. printf ("clock_getres on live PID %d clock %lx => %s\n",
  108. child, (unsigned long int) child_clock, strerror (errno));
  109. result = 1;
  110. goto done;
  111. }
  112. printf ("live PID %d clock %lx resolution %ju.%.9ju\n",
  113. child, (unsigned long int) child_clock,
  114. (uintmax_t) res.tv_sec, (uintmax_t) res.tv_nsec);
  115. struct timespec before, after;
  116. if (clock_gettime (child_clock, &before) < 0)
  117. {
  118. printf ("clock_gettime on live PID %d clock %lx => %s\n",
  119. child, (unsigned long int) child_clock, strerror (errno));
  120. result = 1;
  121. goto done;
  122. }
  123. /* Should be close to 0.0. */
  124. printf ("live PID %d before sleep => %ju.%.9ju\n",
  125. child, (uintmax_t) before.tv_sec, (uintmax_t) before.tv_nsec);
  126. struct timespec sleeptime = { .tv_nsec = 500000000 };
  127. if (nanosleep (&sleeptime, NULL) != 0)
  128. {
  129. perror ("nanosleep");
  130. result = 1;
  131. goto done;
  132. }
  133. if (clock_gettime (child_clock, &after) < 0)
  134. {
  135. printf ("clock_gettime on live PID %d clock %lx => %s\n",
  136. child, (unsigned long int) child_clock, strerror (errno));
  137. result = 1;
  138. goto done;
  139. }
  140. /* Should be close to 0.5. */
  141. printf ("live PID %d after sleep => %ju.%.9ju\n",
  142. child, (uintmax_t) after.tv_sec, (uintmax_t) after.tv_nsec);
  143. struct timespec diff = { .tv_sec = after.tv_sec - before.tv_sec,
  144. .tv_nsec = after.tv_nsec - before.tv_nsec };
  145. if (diff.tv_nsec < 0)
  146. {
  147. --diff.tv_sec;
  148. diff.tv_nsec += 1000000000;
  149. }
  150. if (diff.tv_sec != 0
  151. || diff.tv_nsec > 600000000
  152. || diff.tv_nsec < 100000000)
  153. {
  154. printf ("before - after %ju.%.9ju outside reasonable range\n",
  155. (uintmax_t) diff.tv_sec, (uintmax_t) diff.tv_nsec);
  156. result = 1;
  157. }
  158. sleeptime.tv_nsec = 100000000;
  159. e = clock_nanosleep (child_clock, 0, &sleeptime, NULL);
  160. if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
  161. {
  162. printf ("clock_nanosleep not supported for other process clock: %s\n",
  163. strerror (e));
  164. }
  165. else if (e != 0)
  166. {
  167. printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
  168. result = 1;
  169. }
  170. else
  171. {
  172. struct timespec afterns;
  173. if (clock_gettime (child_clock, &afterns) < 0)
  174. {
  175. printf ("clock_gettime on live PID %d clock %lx => %s\n",
  176. child, (unsigned long int) child_clock, strerror (errno));
  177. result = 1;
  178. }
  179. else
  180. {
  181. struct timespec d = { .tv_sec = afterns.tv_sec - after.tv_sec,
  182. .tv_nsec = afterns.tv_nsec - after.tv_nsec };
  183. if (d.tv_nsec < 0)
  184. {
  185. --d.tv_sec;
  186. d.tv_nsec += 1000000000;
  187. }
  188. if (d.tv_sec > 0
  189. || d.tv_nsec < sleeptime.tv_nsec
  190. || d.tv_nsec > sleeptime.tv_nsec * 2)
  191. {
  192. printf ("nanosleep time %ju.%.9ju outside reasonable range\n",
  193. (uintmax_t) d.tv_sec, (uintmax_t) d.tv_nsec);
  194. result = 1;
  195. }
  196. }
  197. }
  198. if (kill (child, SIGKILL) != 0)
  199. {
  200. perror ("kill");
  201. result = 2;
  202. goto done;
  203. }
  204. /* Wait long enough to let the child finish dying. */
  205. sleeptime.tv_nsec = 200000000;
  206. if (nanosleep (&sleeptime, NULL) != 0)
  207. {
  208. perror ("nanosleep");
  209. result = 1;
  210. goto done;
  211. }
  212. struct timespec dead;
  213. if (clock_gettime (child_clock, &dead) < 0)
  214. {
  215. printf ("clock_gettime on dead PID %d clock %lx => %s\n",
  216. child, (unsigned long int) child_clock, strerror (errno));
  217. result = 1;
  218. goto done;
  219. }
  220. /* Should be close to 0.6. */
  221. printf ("dead PID %d => %ju.%.9ju\n",
  222. child, (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
  223. diff.tv_sec = dead.tv_sec - after.tv_sec;
  224. diff.tv_nsec = dead.tv_nsec - after.tv_nsec;
  225. if (diff.tv_nsec < 0)
  226. {
  227. --diff.tv_sec;
  228. diff.tv_nsec += 1000000000;
  229. }
  230. if (diff.tv_sec != 0 || diff.tv_nsec > 200000000)
  231. {
  232. printf ("dead - after %ju.%.9ju outside reasonable range\n",
  233. (uintmax_t) diff.tv_sec, (uintmax_t) diff.tv_nsec);
  234. result = 1;
  235. }
  236. /* Now reap the child and verify that its clock is no longer valid. */
  237. {
  238. int x;
  239. if (waitpid (child, &x, 0) != child)
  240. {
  241. perror ("waitpid");
  242. result = 1;
  243. }
  244. }
  245. if (clock_gettime (child_clock, &dead) == 0)
  246. {
  247. printf ("clock_gettime on reaped PID %d clock %lx => %ju%.9ju\n",
  248. child, (unsigned long int) child_clock,
  249. (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
  250. result = 1;
  251. }
  252. else
  253. {
  254. if (errno != EINVAL)
  255. result = 1;
  256. printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
  257. child, (unsigned long int) child_clock, strerror (errno));
  258. }
  259. if (clock_getres (child_clock, &dead) == 0)
  260. {
  261. printf ("clock_getres on reaped PID %d clock %lx => %ju%.9ju\n",
  262. child, (unsigned long int) child_clock,
  263. (uintmax_t) dead.tv_sec, (uintmax_t) dead.tv_nsec);
  264. result = 1;
  265. }
  266. else
  267. {
  268. if (errno != EINVAL)
  269. result = 1;
  270. printf ("clock_getres on reaped PID %d clock %lx => %s\n",
  271. child, (unsigned long int) child_clock, strerror (errno));
  272. }
  273. return result;
  274. done:
  275. {
  276. if (kill (child, SIGKILL) != 0 && errno != ESRCH)
  277. {
  278. perror ("kill");
  279. return 2;
  280. }
  281. int x;
  282. if (waitpid (child, &x, 0) != child && errno != ECHILD)
  283. {
  284. perror ("waitpid");
  285. return 2;
  286. }
  287. }
  288. return result;
  289. }
  290. #define TEST_FUNCTION do_test ()
  291. #include "../test-skeleton.c"