tst-cpuclock1.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <signal.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 %lu.%.9lu\n",
  113. child, (unsigned long int) child_clock, res.tv_sec, res.tv_nsec);
  114. struct timespec before, after;
  115. if (clock_gettime (child_clock, &before) < 0)
  116. {
  117. printf ("clock_gettime on live PID %d clock %lx => %s\n",
  118. child, (unsigned long int) child_clock, strerror (errno));
  119. result = 1;
  120. goto done;
  121. }
  122. printf ("live PID %d before sleep => %lu.%.9lu\n",
  123. child, before.tv_sec, before.tv_nsec);
  124. struct timespec sleeptime = { .tv_nsec = 500000000 };
  125. nanosleep (&sleeptime, NULL);
  126. if (clock_gettime (child_clock, &after) < 0)
  127. {
  128. printf ("clock_gettime on live PID %d clock %lx => %s\n",
  129. child, (unsigned long int) child_clock, strerror (errno));
  130. result = 1;
  131. goto done;
  132. }
  133. printf ("live PID %d after sleep => %lu.%.9lu\n",
  134. child, after.tv_sec, after.tv_nsec);
  135. struct timespec diff = { .tv_sec = after.tv_sec - before.tv_sec,
  136. .tv_nsec = after.tv_nsec - before.tv_nsec };
  137. if (diff.tv_nsec < 0)
  138. {
  139. --diff.tv_sec;
  140. diff.tv_nsec += 1000000000;
  141. }
  142. if (diff.tv_sec != 0
  143. || diff.tv_nsec > 600000000
  144. || diff.tv_nsec < 100000000)
  145. {
  146. printf ("before - after %lu.%.9lu outside reasonable range\n",
  147. diff.tv_sec, diff.tv_nsec);
  148. result = 1;
  149. }
  150. sleeptime.tv_nsec = 100000000;
  151. e = clock_nanosleep (child_clock, 0, &sleeptime, NULL);
  152. if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
  153. {
  154. printf ("clock_nanosleep not supported for other process clock: %s\n",
  155. strerror (e));
  156. }
  157. else if (e != 0)
  158. {
  159. printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
  160. result = 1;
  161. }
  162. else
  163. {
  164. struct timespec afterns;
  165. if (clock_gettime (child_clock, &afterns) < 0)
  166. {
  167. printf ("clock_gettime on live PID %d clock %lx => %s\n",
  168. child, (unsigned long int) child_clock, strerror (errno));
  169. result = 1;
  170. }
  171. else
  172. {
  173. struct timespec d = { .tv_sec = afterns.tv_sec - after.tv_sec,
  174. .tv_nsec = afterns.tv_nsec - after.tv_nsec };
  175. if (d.tv_nsec < 0)
  176. {
  177. --d.tv_sec;
  178. d.tv_nsec += 1000000000;
  179. }
  180. if (d.tv_sec > 0
  181. || d.tv_nsec < sleeptime.tv_nsec
  182. || d.tv_nsec > sleeptime.tv_nsec * 2)
  183. {
  184. printf ("nanosleep time %lu.%.9lu outside reasonable range\n",
  185. d.tv_sec, d.tv_nsec);
  186. result = 1;
  187. }
  188. }
  189. }
  190. if (kill (child, SIGKILL) != 0)
  191. {
  192. perror ("kill");
  193. result = 2;
  194. goto done;
  195. }
  196. /* Wait long enough to let the child finish dying. */
  197. sleeptime.tv_nsec = 200000000;
  198. nanosleep (&sleeptime, NULL);
  199. struct timespec dead;
  200. if (clock_gettime (child_clock, &dead) < 0)
  201. {
  202. printf ("clock_gettime on dead PID %d clock %lx => %s\n",
  203. child, (unsigned long int) child_clock, strerror (errno));
  204. result = 1;
  205. goto done;
  206. }
  207. printf ("dead PID %d => %lu.%.9lu\n",
  208. child, dead.tv_sec, dead.tv_nsec);
  209. diff.tv_sec = dead.tv_sec - after.tv_sec;
  210. diff.tv_nsec = dead.tv_nsec - after.tv_nsec;
  211. if (diff.tv_nsec < 0)
  212. {
  213. --diff.tv_sec;
  214. diff.tv_nsec += 1000000000;
  215. }
  216. if (diff.tv_sec != 0 || diff.tv_nsec > 200000000)
  217. {
  218. printf ("dead - after %lu.%.9lu outside reasonable range\n",
  219. diff.tv_sec, diff.tv_nsec);
  220. result = 1;
  221. }
  222. /* Now reap the child and verify that its clock is no longer valid. */
  223. {
  224. int x;
  225. if (waitpid (child, &x, 0) != child)
  226. {
  227. perror ("waitpid");
  228. result = 1;
  229. }
  230. }
  231. if (clock_gettime (child_clock, &dead) == 0)
  232. {
  233. printf ("clock_gettime on reaped PID %d clock %lx => %lu%.9lu\n",
  234. child, (unsigned long int) child_clock,
  235. dead.tv_sec, dead.tv_nsec);
  236. result = 1;
  237. }
  238. else
  239. {
  240. if (errno != EINVAL)
  241. result = 1;
  242. printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
  243. child, (unsigned long int) child_clock, strerror (errno));
  244. }
  245. if (clock_getres (child_clock, &dead) == 0)
  246. {
  247. printf ("clock_getres on reaped PID %d clock %lx => %lu%.9lu\n",
  248. child, (unsigned long int) child_clock,
  249. dead.tv_sec, dead.tv_nsec);
  250. result = 1;
  251. }
  252. else
  253. {
  254. if (errno != EINVAL)
  255. result = 1;
  256. printf ("clock_getres on reaped PID %d clock %lx => %s\n",
  257. child, (unsigned long int) child_clock, strerror (errno));
  258. }
  259. return result;
  260. done:
  261. {
  262. if (kill (child, SIGKILL) != 0 && errno != ESRCH)
  263. {
  264. perror ("kill");
  265. return 2;
  266. }
  267. int x;
  268. if (waitpid (child, &x, 0) != child && errno != ECHILD)
  269. {
  270. perror ("waitpid");
  271. return 2;
  272. }
  273. }
  274. return result;
  275. }
  276. #define TIMEOUT 5
  277. #define TEST_FUNCTION do_test ()
  278. #include "../test-skeleton.c"