PowerPC: Suppress unnecessary FPSCR write
[glibc.git] / rt / tst-cpuclock1.c
blobf503bc28bf4a22fde33f89fcc56e986f65eb5b82
1 /* Test program for process CPU clocks.
2 Copyright (C) 2004-2014 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <signal.h>
27 #include <sys/wait.h>
29 /* This function is intended to rack up both user and system time. */
30 static void
31 chew_cpu (void)
33 while (1)
35 static volatile char buf[4096];
36 for (int i = 0; i < 100; ++i)
37 for (size_t j = 0; j < sizeof buf; ++j)
38 buf[j] = 0xaa;
39 int nullfd = open ("/dev/null", O_WRONLY);
40 for (int i = 0; i < 100; ++i)
41 for (size_t j = 0; j < sizeof buf; ++j)
42 buf[j] = 0xbb;
43 write (nullfd, (char *) buf, sizeof buf);
44 close (nullfd);
45 if (getppid () == 1)
46 _exit (2);
50 static int
51 do_test (void)
53 int result = 0;
54 clockid_t cl;
55 int e;
56 pid_t dead_child, child;
58 /* Fork a child and let it die, to give us a PID known not be valid
59 (assuming PIDs don't wrap around during the test). */
61 dead_child = fork ();
62 if (dead_child == 0)
63 _exit (0);
64 if (dead_child < 0)
66 perror ("fork");
67 return 1;
69 int x;
70 if (wait (&x) != dead_child)
72 perror ("wait");
73 return 2;
77 /* POSIX says we should get ESRCH for this. */
78 e = clock_getcpuclockid (dead_child, &cl);
79 if (e != ENOSYS && e != ESRCH && e != EPERM)
81 printf ("clock_getcpuclockid on dead PID %d => %s\n",
82 dead_child, strerror (e));
83 result = 1;
86 /* Now give us a live child eating up CPU time. */
87 child = fork ();
88 if (child == 0)
90 chew_cpu ();
91 _exit (1);
93 if (child < 0)
95 perror ("fork");
96 return 1;
99 e = clock_getcpuclockid (child, &cl);
100 if (e == EPERM)
102 puts ("clock_getcpuclockid does not support other processes");
103 goto done;
105 if (e != 0)
107 printf ("clock_getcpuclockid on live PID %d => %s\n",
108 child, strerror (e));
109 result = 1;
110 goto done;
113 const clockid_t child_clock = cl;
114 struct timespec res;
115 if (clock_getres (child_clock, &res) < 0)
117 printf ("clock_getres on live PID %d clock %lx => %s\n",
118 child, (unsigned long int) child_clock, strerror (errno));
119 result = 1;
120 goto done;
122 printf ("live PID %d clock %lx resolution %lu.%.9lu\n",
123 child, (unsigned long int) child_clock, res.tv_sec, res.tv_nsec);
125 struct timespec before, after;
126 if (clock_gettime (child_clock, &before) < 0)
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;
133 /* Should be close to 0.0. */
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 if (nanosleep (&sleeptime, NULL) != 0)
140 perror ("nanosleep");
141 result = 1;
142 goto done;
145 if (clock_gettime (child_clock, &after) < 0)
147 printf ("clock_gettime on live PID %d clock %lx => %s\n",
148 child, (unsigned long int) child_clock, strerror (errno));
149 result = 1;
150 goto done;
152 /* Should be close to 0.5. */
153 printf ("live PID %d after sleep => %lu.%.9lu\n",
154 child, after.tv_sec, after.tv_nsec);
156 struct timespec diff = { .tv_sec = after.tv_sec - before.tv_sec,
157 .tv_nsec = after.tv_nsec - before.tv_nsec };
158 if (diff.tv_nsec < 0)
160 --diff.tv_sec;
161 diff.tv_nsec += 1000000000;
163 if (diff.tv_sec != 0
164 || diff.tv_nsec > 600000000
165 || diff.tv_nsec < 100000000)
167 printf ("before - after %lu.%.9lu outside reasonable range\n",
168 diff.tv_sec, diff.tv_nsec);
169 result = 1;
172 sleeptime.tv_nsec = 100000000;
173 e = clock_nanosleep (child_clock, 0, &sleeptime, NULL);
174 if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
176 printf ("clock_nanosleep not supported for other process clock: %s\n",
177 strerror (e));
179 else if (e != 0)
181 printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
182 result = 1;
184 else
186 struct timespec afterns;
187 if (clock_gettime (child_clock, &afterns) < 0)
189 printf ("clock_gettime on live PID %d clock %lx => %s\n",
190 child, (unsigned long int) child_clock, strerror (errno));
191 result = 1;
193 else
195 struct timespec d = { .tv_sec = afterns.tv_sec - after.tv_sec,
196 .tv_nsec = afterns.tv_nsec - after.tv_nsec };
197 if (d.tv_nsec < 0)
199 --d.tv_sec;
200 d.tv_nsec += 1000000000;
202 if (d.tv_sec > 0
203 || d.tv_nsec < sleeptime.tv_nsec
204 || d.tv_nsec > sleeptime.tv_nsec * 2)
206 printf ("nanosleep time %lu.%.9lu outside reasonable range\n",
207 d.tv_sec, d.tv_nsec);
208 result = 1;
213 if (kill (child, SIGKILL) != 0)
215 perror ("kill");
216 result = 2;
217 goto done;
220 /* Wait long enough to let the child finish dying. */
222 sleeptime.tv_nsec = 200000000;
223 if (nanosleep (&sleeptime, NULL) != 0)
225 perror ("nanosleep");
226 result = 1;
227 goto done;
230 struct timespec dead;
231 if (clock_gettime (child_clock, &dead) < 0)
233 printf ("clock_gettime on dead PID %d clock %lx => %s\n",
234 child, (unsigned long int) child_clock, strerror (errno));
235 result = 1;
236 goto done;
238 /* Should be close to 0.6. */
239 printf ("dead PID %d => %lu.%.9lu\n",
240 child, dead.tv_sec, dead.tv_nsec);
242 diff.tv_sec = dead.tv_sec - after.tv_sec;
243 diff.tv_nsec = dead.tv_nsec - after.tv_nsec;
244 if (diff.tv_nsec < 0)
246 --diff.tv_sec;
247 diff.tv_nsec += 1000000000;
249 if (diff.tv_sec != 0 || diff.tv_nsec > 200000000)
251 printf ("dead - after %lu.%.9lu outside reasonable range\n",
252 diff.tv_sec, diff.tv_nsec);
253 result = 1;
256 /* Now reap the child and verify that its clock is no longer valid. */
258 int x;
259 if (waitpid (child, &x, 0) != child)
261 perror ("waitpid");
262 result = 1;
266 if (clock_gettime (child_clock, &dead) == 0)
268 printf ("clock_gettime on reaped PID %d clock %lx => %lu%.9lu\n",
269 child, (unsigned long int) child_clock,
270 dead.tv_sec, dead.tv_nsec);
271 result = 1;
273 else
275 if (errno != EINVAL)
276 result = 1;
277 printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
278 child, (unsigned long int) child_clock, strerror (errno));
281 if (clock_getres (child_clock, &dead) == 0)
283 printf ("clock_getres on reaped PID %d clock %lx => %lu%.9lu\n",
284 child, (unsigned long int) child_clock,
285 dead.tv_sec, dead.tv_nsec);
286 result = 1;
288 else
290 if (errno != EINVAL)
291 result = 1;
292 printf ("clock_getres on reaped PID %d clock %lx => %s\n",
293 child, (unsigned long int) child_clock, strerror (errno));
296 return result;
298 done:
300 if (kill (child, SIGKILL) != 0 && errno != ESRCH)
302 perror ("kill");
303 return 2;
305 int x;
306 if (waitpid (child, &x, 0) != child && errno != ECHILD)
308 perror ("waitpid");
309 return 2;
313 return result;
317 #define TIMEOUT 5
318 #define TEST_FUNCTION do_test ()
319 #include "../test-skeleton.c"