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, 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 printf ("live PID %d before sleep => %lu.%.9lu\n",
134 child, before.tv_sec, before.tv_nsec);
136 struct timespec sleeptime = { .tv_nsec = 500000000 };
137 nanosleep (&sleeptime, NULL);
139 if (clock_gettime (child_clock, &after) < 0)
141 printf ("clock_gettime on live PID %d clock %lx => %s\n",
142 child, (unsigned long int) child_clock, strerror (errno));
146 printf ("live PID %d after sleep => %lu.%.9lu\n",
147 child, after.tv_sec, after.tv_nsec);
149 struct timespec diff = { .tv_sec = after.tv_sec - before.tv_sec,
150 .tv_nsec = after.tv_nsec - before.tv_nsec };
151 if (diff.tv_nsec < 0)
154 diff.tv_nsec += 1000000000;
157 || diff.tv_nsec > 600000000
158 || diff.tv_nsec < 100000000)
160 printf ("before - after %lu.%.9lu outside reasonable range\n",
161 diff.tv_sec, diff.tv_nsec);
165 sleeptime.tv_nsec = 100000000;
166 e = clock_nanosleep (child_clock, 0, &sleeptime, NULL);
167 if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
169 printf ("clock_nanosleep not supported for other process clock: %s\n",
174 printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
179 struct timespec afterns;
180 if (clock_gettime (child_clock, &afterns) < 0)
182 printf ("clock_gettime on live PID %d clock %lx => %s\n",
183 child, (unsigned long int) child_clock, strerror (errno));
188 struct timespec d = { .tv_sec = afterns.tv_sec - after.tv_sec,
189 .tv_nsec = afterns.tv_nsec - after.tv_nsec };
193 d.tv_nsec += 1000000000;
196 || d.tv_nsec < sleeptime.tv_nsec
197 || d.tv_nsec > sleeptime.tv_nsec * 2)
199 printf ("nanosleep time %lu.%.9lu outside reasonable range\n",
200 d.tv_sec, d.tv_nsec);
206 if (kill (child, SIGKILL) != 0)
213 /* Wait long enough to let the child finish dying. */
215 sleeptime.tv_nsec = 200000000;
216 nanosleep (&sleeptime, NULL);
218 struct timespec dead;
219 if (clock_gettime (child_clock, &dead) < 0)
221 printf ("clock_gettime on dead PID %d clock %lx => %s\n",
222 child, (unsigned long int) child_clock, strerror (errno));
226 printf ("dead PID %d => %lu.%.9lu\n",
227 child, dead.tv_sec, dead.tv_nsec);
229 diff.tv_sec = dead.tv_sec - after.tv_sec;
230 diff.tv_nsec = dead.tv_nsec - after.tv_nsec;
231 if (diff.tv_nsec < 0)
234 diff.tv_nsec += 1000000000;
236 if (diff.tv_sec != 0 || diff.tv_nsec > 200000000)
238 printf ("dead - after %lu.%.9lu outside reasonable range\n",
239 diff.tv_sec, diff.tv_nsec);
243 /* Now reap the child and verify that its clock is no longer valid. */
246 if (waitpid (child, &x, 0) != child)
253 if (clock_gettime (child_clock, &dead) == 0)
255 printf ("clock_gettime on reaped PID %d clock %lx => %lu%.9lu\n",
256 child, (unsigned long int) child_clock,
257 dead.tv_sec, dead.tv_nsec);
264 printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
265 child, (unsigned long int) child_clock, strerror (errno));
268 if (clock_getres (child_clock, &dead) == 0)
270 printf ("clock_getres on reaped PID %d clock %lx => %lu%.9lu\n",
271 child, (unsigned long int) child_clock,
272 dead.tv_sec, dead.tv_nsec);
279 printf ("clock_getres on reaped PID %d clock %lx => %s\n",
280 child, (unsigned long int) child_clock, strerror (errno));
287 if (kill (child, SIGKILL) != 0 && errno != ESRCH)
293 if (waitpid (child, &x, 0) != child && errno != ECHILD)
305 #define TEST_FUNCTION do_test ()
306 #include "../test-skeleton.c"