1 /* Test program for process CPU clocks.
2 Copyright (C) 2004-2022 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 <https://www.gnu.org/licenses/>. */
30 /* This function is intended to rack up both user and system time. */
36 static volatile char buf
[4096];
37 for (int i
= 0; i
< 100; ++i
)
38 for (size_t j
= 0; j
< sizeof buf
; ++j
)
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
)
44 write (nullfd
, (char *) buf
, sizeof buf
);
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). */
71 if (wait (&x
) != dead_child
)
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
));
87 /* Now give us a live child eating up CPU time. */
100 e
= clock_getcpuclockid (child
, &cl
);
103 puts ("clock_getcpuclockid does not support other processes");
108 printf ("clock_getcpuclockid on live PID %d => %s\n",
109 child
, strerror (e
));
114 const clockid_t child_clock
= cl
;
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
));
123 printf ("live PID %d clock %lx resolution %ju.%.9ju\n",
124 child
, (unsigned long int) child_clock
,
125 (uintmax_t) res
.tv_sec
, (uintmax_t) res
.tv_nsec
);
127 struct timespec before
;
128 if (clock_gettime (child_clock
, &before
) < 0)
130 printf ("clock_gettime on live PID %d clock %lx => %s\n",
131 child
, (unsigned long int) child_clock
, strerror (errno
));
135 /* Should be close to 0.0. */
136 printf ("live PID %d before sleep => %ju.%.9ju\n",
137 child
, (uintmax_t) before
.tv_sec
, (uintmax_t) before
.tv_nsec
);
139 struct timespec sleeptime
= { .tv_nsec
= 100000000 };
140 e
= clock_nanosleep (child_clock
, 0, &sleeptime
, NULL
);
141 if (e
== EINVAL
|| e
== ENOTSUP
|| e
== ENOSYS
)
143 printf ("clock_nanosleep not supported for other process clock: %s\n",
148 printf ("clock_nanosleep on other process clock: %s\n", strerror (e
));
153 struct timespec afterns
;
154 if (clock_gettime (child_clock
, &afterns
) < 0)
156 printf ("clock_gettime on live PID %d clock %lx => %s\n",
157 child
, (unsigned long int) child_clock
, strerror (errno
));
162 printf ("live PID %d after sleep => %ju.%.9ju\n",
163 child
, (uintmax_t) afterns
.tv_sec
,
164 (uintmax_t) afterns
.tv_nsec
);
168 if (kill (child
, SIGKILL
) != 0)
175 /* Wait long enough to let the child finish dying. */
177 sleeptime
.tv_nsec
= 200000000;
178 if (nanosleep (&sleeptime
, NULL
) != 0)
180 perror ("nanosleep");
185 struct timespec dead
;
186 if (clock_gettime (child_clock
, &dead
) < 0)
188 printf ("clock_gettime on dead PID %d clock %lx => %s\n",
189 child
, (unsigned long int) child_clock
, strerror (errno
));
193 /* Should be close to 0.1. */
194 printf ("dead PID %d => %ju.%.9ju\n",
195 child
, (uintmax_t) dead
.tv_sec
, (uintmax_t) dead
.tv_nsec
);
197 /* Now reap the child and verify that its clock is no longer valid. */
200 if (waitpid (child
, &x
, 0) != child
)
207 if (clock_gettime (child_clock
, &dead
) == 0)
209 printf ("clock_gettime on reaped PID %d clock %lx => %ju%.9ju\n",
210 child
, (unsigned long int) child_clock
,
211 (uintmax_t) dead
.tv_sec
, (uintmax_t) dead
.tv_nsec
);
218 printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
219 child
, (unsigned long int) child_clock
, strerror (errno
));
222 if (clock_getres (child_clock
, &dead
) == 0)
224 printf ("clock_getres on reaped PID %d clock %lx => %ju%.9ju\n",
225 child
, (unsigned long int) child_clock
,
226 (uintmax_t) dead
.tv_sec
, (uintmax_t) dead
.tv_nsec
);
233 printf ("clock_getres on reaped PID %d clock %lx => %s\n",
234 child
, (unsigned long int) child_clock
, strerror (errno
));
241 if (kill (child
, SIGKILL
) != 0 && errno
!= ESRCH
)
247 if (waitpid (child
, &x
, 0) != child
&& errno
!= ECHILD
)
258 #define TEST_FUNCTION do_test ()
259 #include "../test-skeleton.c"