1 /* Test program for process CPU clocks.
2 Copyright (C) 2004-2013 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/>. */
29 /* This function is intended to rack up both user and system time. */
35 static volatile char buf[4096];
36 for (int i = 0; i < 100; ++i)
37 for (size_t j = 0; j < sizeof buf; ++j)
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)
43 write (nullfd, (char *) buf, sizeof buf);
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). */
70 if (wait (&x) != dead_child)
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));
86 /* Now give us a live child eating up CPU time. */
99 e = clock_getcpuclockid (child, &cl);
102 puts ("clock_getcpuclockid does not support other processes");
107 printf ("clock_getcpuclockid on live PID %d => %s\n",
108 child, strerror (e));
113 const clockid_t child_clock = cl;
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));
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));
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");
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));
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)
161 diff.tv_nsec += 1000000000;
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);
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",
181 printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
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));
195 struct timespec d = { .tv_sec = afterns.tv_sec - after.tv_sec,
196 .tv_nsec = afterns.tv_nsec - after.tv_nsec };
200 d.tv_nsec += 1000000000;
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);
213 if (kill (child, SIGKILL) != 0)
220 /* Wait long enough to let the child finish dying. */
222 sleeptime.tv_nsec = 200000000;
223 if (nanosleep (&sleeptime, NULL) != 0)
225 perror ("nanosleep");
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));
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)
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);
256 /* Now reap the child and verify that its clock is no longer valid. */
259 if (waitpid (child, &x, 0) != child)
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);
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);
292 printf ("clock_getres on reaped PID %d clock %lx => %s\n",
293 child, (unsigned long int) child_clock, strerror (errno));
300 if (kill (child, SIGKILL) != 0 && errno != ESRCH)
306 if (waitpid (child, &x, 0) != child && errno != ECHILD)
318 #define TEST_FUNCTION do_test ()
319 #include "../test-skeleton.c"