* malloc/mcheck.c (reallochook): If size==0, free the block.
[glibc/pb-stable.git] / rt / tst-cpuclock1.c
blob024df63141556b3ddcb569100f3cf1623d3c6357
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.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <sys/wait.h>
30 /* This function is intended to rack up both user and system time. */
31 static void
32 chew_cpu (void)
34 while (1)
36 static volatile char buf[4096];
37 for (int i = 0; i < 100; ++i)
38 for (size_t j = 0; j < sizeof buf; ++j)
39 buf[j] = 0xaa;
40 int nullfd = open ("/dev/null", O_WRONLY);
41 for (int i = 0; i < 100; ++i)
42 for (size_t j = 0; j < sizeof buf; ++j)
43 buf[j] = 0xbb;
44 write (nullfd, (char *) buf, sizeof buf);
45 close (nullfd);
46 if (getppid () == 1)
47 _exit (2);
51 static int
52 do_test (void)
54 int result = 0;
55 clockid_t cl;
56 int e;
57 pid_t dead_child, child;
59 /* Fork a child and let it die, to give us a PID known not be valid
60 (assuming PIDs don't wrap around during the test). */
62 dead_child = fork ();
63 if (dead_child == 0)
64 _exit (0);
65 if (dead_child < 0)
67 perror ("fork");
68 return 1;
70 int x;
71 if (wait (&x) != dead_child)
73 perror ("wait");
74 return 2;
78 /* POSIX says we should get ESRCH for this. */
79 e = clock_getcpuclockid (dead_child, &cl);
80 if (e != ENOSYS && e != ESRCH && e != EPERM)
82 printf ("clock_getcpuclockid on dead PID %d => %s\n",
83 dead_child, strerror (e));
84 result = 1;
87 /* Now give us a live child eating up CPU time. */
88 child = fork ();
89 if (child == 0)
91 chew_cpu ();
92 _exit (1);
94 if (child < 0)
96 perror ("fork");
97 return 1;
100 e = clock_getcpuclockid (child, &cl);
101 if (e == EPERM)
103 puts ("clock_getcpuclockid does not support other processes");
104 goto done;
106 if (e != 0)
108 printf ("clock_getcpuclockid on live PID %d => %s\n",
109 child, strerror (e));
110 result = 1;
111 goto done;
114 const clockid_t child_clock = cl;
115 struct timespec res;
116 if (clock_getres (child_clock, &res) < 0)
118 printf ("clock_getres on live PID %d clock %lx => %s\n",
119 child, (unsigned long int) child_clock, strerror (errno));
120 result = 1;
121 goto done;
123 printf ("live PID %d clock %lx resolution %lu.%.9lu\n",
124 child, (unsigned long int) child_clock, res.tv_sec, res.tv_nsec);
126 struct timespec before, after;
127 if (clock_gettime (child_clock, &before) < 0)
129 printf ("clock_gettime on live PID %d clock %lx => %s\n",
130 child, (unsigned long int) child_clock, strerror (errno));
131 result = 1;
132 goto done;
134 printf ("live PID %d before sleep => %lu.%.9lu\n",
135 child, before.tv_sec, before.tv_nsec);
137 struct timespec sleeptime = { .tv_nsec = 500000000 };
138 nanosleep (&sleeptime, NULL);
140 if (clock_gettime (child_clock, &after) < 0)
142 printf ("clock_gettime on live PID %d clock %lx => %s\n",
143 child, (unsigned long int) child_clock, strerror (errno));
144 result = 1;
145 goto done;
147 printf ("live PID %d after sleep => %lu.%.9lu\n",
148 child, after.tv_sec, after.tv_nsec);
150 struct timespec diff = { .tv_sec = after.tv_sec - before.tv_sec,
151 .tv_nsec = after.tv_nsec - before.tv_nsec };
152 if (diff.tv_nsec < 0)
154 --diff.tv_sec;
155 diff.tv_nsec += 1000000000;
157 if (diff.tv_sec != 0
158 || diff.tv_nsec > 600000000
159 || diff.tv_nsec < 100000000)
161 printf ("before - after %lu.%.9lu outside reasonable range\n",
162 diff.tv_sec, diff.tv_nsec);
163 result = 1;
166 sleeptime.tv_nsec = 100000000;
167 e = clock_nanosleep (child_clock, 0, &sleeptime, NULL);
168 if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
170 printf ("clock_nanosleep not supported for other process clock: %s\n",
171 strerror (e));
173 else if (e != 0)
175 printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
176 result = 1;
178 else
180 struct timespec afterns;
181 if (clock_gettime (child_clock, &afterns) < 0)
183 printf ("clock_gettime on live PID %d clock %lx => %s\n",
184 child, (unsigned long int) child_clock, strerror (errno));
185 result = 1;
187 else
189 struct timespec d = { .tv_sec = afterns.tv_sec - after.tv_sec,
190 .tv_nsec = afterns.tv_nsec - after.tv_nsec };
191 if (d.tv_nsec < 0)
193 --d.tv_sec;
194 d.tv_nsec += 1000000000;
196 if (d.tv_sec > 0
197 || d.tv_nsec < sleeptime.tv_nsec
198 || d.tv_nsec > sleeptime.tv_nsec * 2)
200 printf ("nanosleep time %lu.%.9lu outside reasonable range\n",
201 d.tv_sec, d.tv_nsec);
202 result = 1;
207 if (kill (child, SIGKILL) != 0)
209 perror ("kill");
210 result = 2;
211 goto done;
214 /* Wait long enough to let the child finish dying. */
216 sleeptime.tv_nsec = 200000000;
217 nanosleep (&sleeptime, NULL);
219 struct timespec dead;
220 if (clock_gettime (child_clock, &dead) < 0)
222 printf ("clock_gettime on dead PID %d clock %lx => %s\n",
223 child, (unsigned long int) child_clock, strerror (errno));
224 result = 1;
225 goto done;
227 printf ("dead PID %d => %lu.%.9lu\n",
228 child, dead.tv_sec, dead.tv_nsec);
230 diff.tv_sec = dead.tv_sec - after.tv_sec;
231 diff.tv_nsec = dead.tv_nsec - after.tv_nsec;
232 if (diff.tv_nsec < 0)
234 --diff.tv_sec;
235 diff.tv_nsec += 1000000000;
237 if (diff.tv_sec != 0 || diff.tv_nsec > 200000000)
239 printf ("dead - after %lu.%.9lu outside reasonable range\n",
240 diff.tv_sec, diff.tv_nsec);
241 result = 1;
244 /* Now reap the child and verify that its clock is no longer valid. */
246 int x;
247 if (waitpid (child, &x, 0) != child)
249 perror ("waitpid");
250 result = 1;
254 if (clock_gettime (child_clock, &dead) == 0)
256 printf ("clock_gettime on reaped PID %d clock %lx => %lu%.9lu\n",
257 child, (unsigned long int) child_clock,
258 dead.tv_sec, dead.tv_nsec);
259 result = 1;
261 else
263 if (errno != EINVAL)
264 result = 1;
265 printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
266 child, (unsigned long int) child_clock, strerror (errno));
269 if (clock_getres (child_clock, &dead) == 0)
271 printf ("clock_getres on reaped PID %d clock %lx => %lu%.9lu\n",
272 child, (unsigned long int) child_clock,
273 dead.tv_sec, dead.tv_nsec);
274 result = 1;
276 else
278 if (errno != EINVAL)
279 result = 1;
280 printf ("clock_getres on reaped PID %d clock %lx => %s\n",
281 child, (unsigned long int) child_clock, strerror (errno));
284 return result;
286 done:
288 if (kill (child, SIGKILL) != 0 && errno != ESRCH)
290 perror ("kill");
291 return 2;
293 int x;
294 if (waitpid (child, &x, 0) != child && errno != ECHILD)
296 perror ("waitpid");
297 return 2;
301 return result;
305 #define TIMEOUT 5
306 #define TEST_FUNCTION do_test ()
307 #include "../test-skeleton.c"