tst-cpuclock1.c 7.6 KB

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