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.
4 
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.
9 
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.
14 
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/>.  */
18 
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>
28 
29 /* This function is intended to rack up both user and system time.  */
30 static void
chew_cpu(void)31 chew_cpu (void)
32 {
33   while (1)
34     {
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);
47     }
48 }
49 
50 static int
do_test(void)51 do_test (void)
52 {
53   int result = 0;
54   clockid_t cl;
55   int e;
56   pid_t dead_child, child;
57 
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).  */
60   {
61     dead_child = fork ();
62     if (dead_child == 0)
63       _exit (0);
64     if (dead_child < 0)
65       {
66 	perror ("fork");
67 	return 1;
68       }
69     int x;
70     if (wait (&x) != dead_child)
71       {
72 	perror ("wait");
73 	return 2;
74       }
75   }
76 
77   /* POSIX says we should get ESRCH for this.  */
78   e = clock_getcpuclockid (dead_child, &cl);
79   if (e != ENOSYS && e != ESRCH && e != EPERM)
80     {
81       printf ("clock_getcpuclockid on dead PID %d => %s\n",
82 	      dead_child, strerror (e));
83       result = 1;
84     }
85 
86   /* Now give us a live child eating up CPU time.  */
87   child = fork ();
88   if (child == 0)
89     {
90       chew_cpu ();
91       _exit (1);
92     }
93   if (child < 0)
94     {
95       perror ("fork");
96       return 1;
97     }
98 
99   e = clock_getcpuclockid (child, &cl);
100   if (e == EPERM)
101     {
102       puts ("clock_getcpuclockid does not support other processes");
103       goto done;
104     }
105   if (e != 0)
106     {
107       printf ("clock_getcpuclockid on live PID %d => %s\n",
108 	      child, strerror (e));
109       result = 1;
110       goto done;
111     }
112 
113   const clockid_t child_clock = cl;
114   struct timespec res;
115   if (clock_getres (child_clock, &res) < 0)
116     {
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;
121     }
122   printf ("live PID %d clock %lx resolution %lu.%.9lu\n",
123 	  child, (unsigned long int) child_clock, res.tv_sec, res.tv_nsec);
124 
125   struct timespec before, after;
126   if (clock_gettime (child_clock, &before) < 0)
127     {
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;
132     }
133   printf ("live PID %d before sleep => %lu.%.9lu\n",
134 	  child, before.tv_sec, before.tv_nsec);
135 
136   struct timespec sleeptime = { .tv_nsec = 500000000 };
137   nanosleep (&sleeptime, NULL);
138 
139   if (clock_gettime (child_clock, &after) < 0)
140     {
141       printf ("clock_gettime on live PID %d clock %lx => %s\n",
142 	      child, (unsigned long int) child_clock, strerror (errno));
143       result = 1;
144       goto done;
145     }
146   printf ("live PID %d after sleep => %lu.%.9lu\n",
147 	  child, after.tv_sec, after.tv_nsec);
148 
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)
152     {
153       --diff.tv_sec;
154       diff.tv_nsec += 1000000000;
155     }
156   if (diff.tv_sec != 0
157       || diff.tv_nsec > 600000000
158       || diff.tv_nsec < 100000000)
159     {
160       printf ("before - after %lu.%.9lu outside reasonable range\n",
161 	      diff.tv_sec, diff.tv_nsec);
162       result = 1;
163     }
164 
165   sleeptime.tv_nsec = 100000000;
166   e = clock_nanosleep (child_clock, 0, &sleeptime, NULL);
167   if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
168     {
169       printf ("clock_nanosleep not supported for other process clock: %s\n",
170 	      strerror (e));
171     }
172   else if (e != 0)
173     {
174       printf ("clock_nanosleep on other process clock: %s\n", strerror (e));
175       result = 1;
176     }
177   else
178     {
179       struct timespec afterns;
180       if (clock_gettime (child_clock, &afterns) < 0)
181 	{
182 	  printf ("clock_gettime on live PID %d clock %lx => %s\n",
183 		  child, (unsigned long int) child_clock, strerror (errno));
184 	  result = 1;
185 	}
186       else
187 	{
188 	  struct timespec d = { .tv_sec = afterns.tv_sec - after.tv_sec,
189 				.tv_nsec = afterns.tv_nsec - after.tv_nsec };
190 	  if (d.tv_nsec < 0)
191 	    {
192 	      --d.tv_sec;
193 	      d.tv_nsec += 1000000000;
194 	    }
195 	  if (d.tv_sec > 0
196 	      || d.tv_nsec < sleeptime.tv_nsec
197 	      || d.tv_nsec > sleeptime.tv_nsec * 2)
198 	    {
199 	      printf ("nanosleep time %lu.%.9lu outside reasonable range\n",
200 		      d.tv_sec, d.tv_nsec);
201 	      result = 1;
202 	    }
203 	}
204     }
205 
206   if (kill (child, SIGKILL) != 0)
207     {
208       perror ("kill");
209       result = 2;
210       goto done;
211     }
212 
213   /* Wait long enough to let the child finish dying.  */
214 
215   sleeptime.tv_nsec = 200000000;
216   nanosleep (&sleeptime, NULL);
217 
218   struct timespec dead;
219   if (clock_gettime (child_clock, &dead) < 0)
220     {
221       printf ("clock_gettime on dead PID %d clock %lx => %s\n",
222 	      child, (unsigned long int) child_clock, strerror (errno));
223       result = 1;
224       goto done;
225     }
226   printf ("dead PID %d => %lu.%.9lu\n",
227 	  child, dead.tv_sec, dead.tv_nsec);
228 
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)
232     {
233       --diff.tv_sec;
234       diff.tv_nsec += 1000000000;
235     }
236   if (diff.tv_sec != 0 || diff.tv_nsec > 200000000)
237     {
238       printf ("dead - after %lu.%.9lu outside reasonable range\n",
239 	      diff.tv_sec, diff.tv_nsec);
240       result = 1;
241     }
242 
243   /* Now reap the child and verify that its clock is no longer valid.  */
244   {
245     int x;
246     if (waitpid (child, &x, 0) != child)
247       {
248 	perror ("waitpid");
249 	result = 1;
250       }
251   }
252 
253   if (clock_gettime (child_clock, &dead) == 0)
254     {
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);
258       result = 1;
259     }
260   else
261     {
262       if (errno != EINVAL)
263 	result = 1;
264       printf ("clock_gettime on reaped PID %d clock %lx => %s\n",
265 	      child, (unsigned long int) child_clock, strerror (errno));
266     }
267 
268   if (clock_getres (child_clock, &dead) == 0)
269     {
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);
273       result = 1;
274     }
275   else
276     {
277       if (errno != EINVAL)
278 	result = 1;
279       printf ("clock_getres on reaped PID %d clock %lx => %s\n",
280 	      child, (unsigned long int) child_clock, strerror (errno));
281     }
282 
283   return result;
284 
285  done:
286   {
287     if (kill (child, SIGKILL) != 0 && errno != ESRCH)
288       {
289 	perror ("kill");
290 	return 2;
291       }
292     int x;
293     if (waitpid (child, &x, 0) != child && errno != ECHILD)
294       {
295 	perror ("waitpid");
296 	return 2;
297       }
298   }
299 
300   return result;
301 }
302 
303 
304 #define TIMEOUT 5
305 #define TEST_FUNCTION do_test ()
306 #include "../test-skeleton.c"
307