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