support: Reformat Makefile.
[glibc.git] / rt / tst-cpuclock2.c
blobe4584d879117a1e908047397c787b322b0cbebb3
1 /* Test program for process and thread CPU clocks.
2 Copyright (C) 2005-2023 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/>. */
19 #include <unistd.h>
20 #include <stdint.h>
22 #if (_POSIX_THREADS - 0) <= 0
24 static int
25 do_test ()
27 return 0;
30 #else
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <pthread.h>
40 static pthread_barrier_t barrier;
42 /* This function is intended to rack up both user and system time. */
43 static void *
44 chew_cpu (void *arg)
46 pthread_barrier_wait (&barrier);
48 while (1)
50 static volatile char buf[4096];
51 for (int i = 0; i < 100; ++i)
52 for (size_t j = 0; j < sizeof buf; ++j)
53 buf[j] = 0xaa;
54 int nullfd = open ("/dev/null", O_WRONLY);
55 for (int i = 0; i < 100; ++i)
56 for (size_t j = 0; j < sizeof buf; ++j)
57 buf[j] = 0xbb;
58 write (nullfd, (char *) buf, sizeof buf);
59 close (nullfd);
62 return NULL;
65 static void
66 test_nanosleep (clockid_t clock, const char *which,
67 int *bad)
69 const struct timespec sleeptime = { .tv_nsec = 100000000 };
70 int e = clock_nanosleep (clock, 0, &sleeptime, NULL);
71 if (e == EINVAL || e == ENOTSUP || e == ENOSYS)
73 printf ("clock_nanosleep not supported for %s CPU clock: %s\n",
74 which, strerror (e));
75 return;
77 if (e != 0)
79 printf ("clock_nanosleep on %s CPU clock: %s\n", which, strerror (e));
80 *bad = 1;
81 return;
84 struct timespec after;
85 if (clock_gettime (clock, &after) < 0)
87 printf ("clock_gettime on %s CPU clock %lx => %s\n",
88 which, (unsigned long int) clock, strerror (errno));
89 *bad = 1;
90 return;
93 struct timespec sleeptimeabs = sleeptime;
94 sleeptimeabs.tv_sec += after.tv_sec;
95 sleeptimeabs.tv_nsec += after.tv_nsec;
96 while (sleeptimeabs.tv_nsec >= 1000000000)
98 ++sleeptimeabs.tv_sec;
99 sleeptimeabs.tv_nsec -= 1000000000;
101 e = clock_nanosleep (clock, TIMER_ABSTIME, &sleeptimeabs, NULL);
102 if (e != 0)
104 printf ("absolute clock_nanosleep on %s CPU clock: %s\n",
105 which, strerror (e));
106 *bad = 1;
107 return;
110 struct timespec afterabs;
111 if (clock_gettime (clock, &afterabs) < 0)
113 printf ("clock_gettime on %s CPU clock %lx => %s\n",
114 which, (unsigned long int) clock, strerror (errno));
115 *bad = 1;
116 return;
119 return;
124 static int
125 do_test (void)
127 int result = 0;
128 clockid_t process_clock, th_clock, my_thread_clock;
129 int e;
130 pthread_t th;
132 e = clock_getcpuclockid (0, &process_clock);
133 if (e != 0)
135 printf ("clock_getcpuclockid on self => %s\n", strerror (e));
136 return 1;
139 e = pthread_getcpuclockid (pthread_self (), &my_thread_clock);
140 if (e != 0)
142 printf ("pthread_getcpuclockid on self => %s\n", strerror (e));
143 return 1;
146 /* This is a kludge. This test fails if the semantics of thread and
147 process clocks are wrong. The old code using hp-timing without kernel
148 support has bogus semantics if there are context switches. We don't
149 fail to report failure when the proper functionality is not available
150 in the kernel. It so happens that Linux kernels without correct CPU
151 clock support also lack CPU timer support, so we use use that to guess
152 that we are using the bogus code and not test it. */
153 timer_t t;
154 if (timer_create (my_thread_clock, NULL, &t) != 0)
156 printf ("timer_create: %m\n");
157 puts ("No support for CPU clocks with good semantics, skipping test");
158 return 0;
160 timer_delete (t);
163 pthread_barrier_init (&barrier, NULL, 2);
165 e = pthread_create (&th, NULL, chew_cpu, NULL);
166 if (e != 0)
168 printf ("pthread_create: %s\n", strerror (e));
169 return 1;
172 e = pthread_getcpuclockid (th, &th_clock);
173 if (e == ENOENT || e == ENOSYS || e == ENOTSUP)
175 puts ("pthread_getcpuclockid does not support other threads");
176 return 1;
179 pthread_barrier_wait (&barrier);
181 struct timespec res;
182 if (clock_getres (th_clock, &res) < 0)
184 printf ("clock_getres on live thread clock %lx => %s\n",
185 (unsigned long int) th_clock, strerror (errno));
186 result = 1;
187 return 1;
189 printf ("live thread clock %lx resolution %ju.%.9ju\n",
190 (unsigned long int) th_clock,
191 (uintmax_t) res.tv_sec, (uintmax_t) res.tv_nsec);
193 struct timespec process_before, process_after;
194 if (clock_gettime (process_clock, &process_before) < 0)
196 printf ("clock_gettime on process clock %lx => %s\n",
197 (unsigned long int) process_clock, strerror (errno));
198 return 1;
201 struct timespec before, after;
202 if (clock_gettime (th_clock, &before) < 0)
204 printf ("clock_gettime on live thread clock %lx => %s\n",
205 (unsigned long int) th_clock, strerror (errno));
206 return 1;
208 printf ("live thread before sleep => %ju.%.9ju\n",
209 (uintmax_t) before.tv_sec, (uintmax_t) before.tv_nsec);
211 struct timespec me_before, me_after;
212 if (clock_gettime (my_thread_clock, &me_before) < 0)
214 printf ("clock_gettime on self thread clock %lx => %s\n",
215 (unsigned long int) my_thread_clock, strerror (errno));
216 return 1;
218 printf ("self thread before sleep => %ju.%.9ju\n",
219 (uintmax_t) me_before.tv_sec, (uintmax_t) me_before.tv_nsec);
221 struct timespec sleeptime = { .tv_nsec = 500000000 };
222 if (nanosleep (&sleeptime, NULL) != 0)
224 perror ("nanosleep");
225 return 1;
228 if (clock_gettime (th_clock, &after) < 0)
230 printf ("clock_gettime on live thread clock %lx => %s\n",
231 (unsigned long int) th_clock, strerror (errno));
232 return 1;
234 printf ("live thread after sleep => %ju.%.9ju\n",
235 (uintmax_t) after.tv_sec, (uintmax_t) after.tv_nsec);
237 if (clock_gettime (process_clock, &process_after) < 0)
239 printf ("clock_gettime on process clock %lx => %s\n",
240 (unsigned long int) process_clock, strerror (errno));
241 return 1;
244 if (clock_gettime (my_thread_clock, &me_after) < 0)
246 printf ("clock_gettime on self thread clock %lx => %s\n",
247 (unsigned long int) my_thread_clock, strerror (errno));
248 return 1;
250 printf ("self thread after sleep => %ju.%.9ju\n",
251 (uintmax_t) me_after.tv_sec, (uintmax_t) me_after.tv_nsec);
253 test_nanosleep (th_clock, "live thread",
254 &result);
255 test_nanosleep (process_clock, "process",
256 &result);
257 test_nanosleep (CLOCK_PROCESS_CPUTIME_ID,
258 "PROCESS_CPUTIME_ID", &result);
260 pthread_cancel (th);
262 e = clock_nanosleep (CLOCK_THREAD_CPUTIME_ID, 0, &sleeptime, NULL);
263 if (e != EINVAL)
265 printf ("clock_nanosleep CLOCK_THREAD_CPUTIME_ID: %s\n",
266 strerror (e));
267 result = 1;
270 return result;
272 #endif
274 #include <support/test-driver.c>