1 /* Tests for getitimer(2)/setitimer(2) - by D.C. van Moolenbroek */
2 /* Warning: this test deals with (real and virtual) time, and, lacking a proper
3 * point of reference, its correctness depends on circumstances like CPU speed
4 * and system load. A succeeding test run says a lot - failure not so much. */
9 #include <minix/config.h>
10 #include <sys/types.h>
13 #include <sys/syslimits.h>
24 /* we have to keep in mind the millisecond values are rounded up */
25 #define UPPERUSEC(us) ((us)+(1000000/system_hz))
27 ((l) <= ((r) + (1000000/system_hz)) && (l) >= ((r) - (1000000/system_hz)))
29 #define FILLITIMER(it, vs, vu, is, iu) \
30 (it).it_value.tv_sec = (vs); \
31 (it).it_value.tv_usec = (vu); \
32 (it).it_interval.tv_sec = (is); \
33 (it).it_interval.tv_usec = (iu);
35 /* these two macros are not fully working for all possible values;
36 * the tests only use values that the macros can deal with, though. */
37 #define EQITIMER(it, vs, vu, is, iu) \
38 ((it).it_value.tv_sec == (vs) && EQUSEC((it).it_value.tv_usec,vu) && \
39 (it).it_interval.tv_sec == (is) && (it).it_interval.tv_usec == (iu))
41 #define LEITIMER(it, vs, vu, is, iu) \
42 ((it).it_value.tv_sec > 0 && ((it).it_value.tv_sec < (vs) || \
43 ((it).it_value.tv_sec == (vs) && (it).it_value.tv_usec <= \
45 (it).it_interval.tv_sec == (is) && EQUSEC((it).it_interval.tv_usec,iu))
47 int main(int argc
, char **argv
);
48 void test(int m
, int t
);
49 void test_which(void);
50 void test_getset(void);
51 void test_neglarge(void);
53 void test_timer(void);
54 void test_alarm(void);
58 void got_alarm(int sig
);
59 void busy_wait(int secs
);
60 #define my_e(n) do { printf("Timer %s, ", names[timer]); e(n); } while(0)
62 static char *executable
;
65 static long system_hz
;
67 static int sigs
[] = { SIGALRM
, SIGVTALRM
, SIGPROF
};
68 static const char *names
[] = { "REAL", "VIRTUAL", "PROF" };
74 int i
, m
= 0xFFFF, n
= 0xF;
75 char cp_cmd
[NAME_MAX
+10];
77 system_hz
= sysconf(_SC_CLK_TCK
);
79 if (strcmp(argv
[0], "DO CHECK") == 0) {
80 timer
= atoi(argv
[1]);
89 snprintf(cp_cmd
, sizeof(cp_cmd
), "cp ../%s .", executable
);
92 if (argc
>= 2) m
= atoi(argv
[1]);
93 if (argc
>= 3) n
= atoi(argv
[2]);
95 for (i
= 0; i
< ITERATIONS
; i
++) {
96 if (n
& 1) test(m
, ITIMER_REAL
);
97 if (n
& 2) test(m
, ITIMER_VIRTUAL
);
98 if (n
& 4) test(m
, ITIMER_PROF
);
102 return(-1); /* impossible */
111 if (m
& 0001) test_which();
112 if (m
& 0002) test_getset();
113 if (m
& 0004) test_neglarge();
114 if (m
& 0010) test_zero();
115 if (m
& 0020) test_timer();
116 if (m
& 0040) test_alarm();
117 if (m
& 0100) test_fork();
118 if (m
& 0200) test_exec();
121 /* test invalid and unsupported 'which' values */
128 errno
= 0; if (!getitimer(-1, &it
) || errno
!= EINVAL
) my_e(1);
129 errno
= 0; if ( getitimer(timer
, &it
) ) my_e(2);
130 errno
= 0; if (!getitimer( 3, &it
) || errno
!= EINVAL
) my_e(3);
133 /* test if we get back what we set */
136 struct itimerval it
, oit
;
140 /* no alarm should be set initially */
141 if (getitimer(timer
, &it
)) my_e(1);
142 if (!EQITIMER(it
, 0, 0, 0, 0)) my_e(2);
144 if (setitimer(timer
, &it
, &oit
)) my_e(3);
145 if (setitimer(timer
, &oit
, NULL
)) my_e(4);
146 if (!EQITIMER(oit
, 0, 0, 0, 0)) my_e(5);
148 FILLITIMER(it
, 123, 0, 456, 0);
149 if (setitimer(timer
, &it
, NULL
)) my_e(6);
151 FILLITIMER(it
, 987, 0, 654, 0);
152 if (setitimer(timer
, &it
, &oit
)) my_e(7);
153 if (!LEITIMER(oit
, 123, 0, 456, 0)) my_e(8);
155 if (getitimer(timer
, &oit
)) my_e(9);
156 if (!LEITIMER(oit
, 987, 0, 654, 0)) my_e(10);
158 FILLITIMER(it
, 0, 0, 0, 0);
159 if (setitimer(timer
, &it
, &oit
)) my_e(11);
160 if (!LEITIMER(oit
, 987, 0, 654, 0)) my_e(12);
162 if (getitimer(timer
, &oit
)) my_e(13);
163 if (!EQITIMER(oit
, 0, 0, 0, 0)) my_e(14);
166 /* test negative/large values */
173 FILLITIMER(it
, 4, 0, 5, 0);
174 if (setitimer(timer
, &it
, NULL
)) my_e(1);
176 FILLITIMER(it
, 1000000000, 0, 0, 0);
177 if (!setitimer(timer
, &it
, NULL
) || errno
!= EINVAL
) my_e(2);
179 FILLITIMER(it
, 0, 1000000, 0, 0);
180 if (!setitimer(timer
, &it
, NULL
) || errno
!= EINVAL
) my_e(3);
182 FILLITIMER(it
, 0, 0, 0, 1000000);
183 if (!setitimer(timer
, &it
, NULL
) || errno
!= EINVAL
) my_e(4);
185 FILLITIMER(it
, -1, 0, 0, 0);
186 if (!setitimer(timer
, &it
, NULL
) || errno
!= EINVAL
) my_e(5);
188 FILLITIMER(it
, 0, -1, 0, 0);
189 if (!setitimer(timer
, &it
, NULL
) || errno
!= EINVAL
) my_e(6);
191 FILLITIMER(it
, 0, 0, -1, 0);
192 if (!setitimer(timer
, &it
, NULL
) || errno
!= EINVAL
) my_e(7);
194 FILLITIMER(it
, 0, 0, 0, -1);
195 if (!setitimer(timer
, &it
, NULL
) || errno
!= EINVAL
) my_e(8);
197 if (getitimer(timer
, &it
)) my_e(9);
198 if (!LEITIMER(it
, 4, 0, 5, 0)) my_e(10);
201 /* setitimer with a zero timer has to set the interval to zero as well */
208 it
.it_value
.tv_sec
= 0;
209 it
.it_value
.tv_usec
= 0;
210 it
.it_interval
.tv_sec
= 1;
211 it
.it_interval
.tv_usec
= 1;
213 if (setitimer(timer
, &it
, NULL
)) my_e(1);
214 if (getitimer(timer
, &it
)) my_e(2);
215 if (!EQITIMER(it
, 0, 0, 0, 0)) my_e(3);
218 /* test actual timer functioning */
225 if (signal(sigs
[timer
], got_alarm
) == SIG_ERR
) my_e(1);
227 FILLITIMER(it
, 0, 100000, 0, 100000);
229 if (setitimer(timer
, &it
, NULL
)) my_e(2);
234 FILLITIMER(it
, 0, 0, 0, 0);
235 if (setitimer(timer
, &it
, NULL
)) my_e(3);
237 /* we don't know how many signals we'll actually get in practice,
238 * so these checks more or less cover the extremes of the acceptable */
239 if (signals
< 2) my_e(4);
240 if (signals
> system_hz
* 2) my_e(5);
242 /* only for REAL timer can we check against the clock */
243 if (timer
== ITIMER_REAL
) {
244 FILLITIMER(it
, 1, 0, 0, 0);
245 if (setitimer(timer
, &it
, NULL
)) my_e(6);
250 FILLITIMER(it
, 0, 0, 0, 0);
251 if (setitimer(timer
, &it
, NULL
)) my_e(7);
253 if (signals
!= 1) my_e(8);
259 if (signals
!= 0) my_e(9);
262 /* test itimer/alarm interaction */
263 void test_alarm(void) {
266 /* only applicable for ITIMER_REAL */
267 if (timer
!= ITIMER_REAL
) return;
271 if (signal(SIGALRM
, got_alarm
) == SIG_ERR
) my_e(1);
273 FILLITIMER(it
, 3, 0, 1, 0);
274 if (setitimer(timer
, &it
, NULL
)) my_e(2);
276 if (alarm(2) != 3) my_e(3);
278 if (getitimer(timer
, &it
)) my_e(4);
279 if (!LEITIMER(it
, 2, 0, 0, 0)) my_e(5);
284 if (signals
!= 1) my_e(6);
286 if (getitimer(timer
, &it
)) my_e(7);
287 if (!EQITIMER(it
, 0, 0, 0, 0)) my_e(8);
290 /* test that the timer is reset on forking */
291 void test_fork(void) {
292 struct itimerval it
, oit
;
298 FILLITIMER(it
, 12, 34, 56, 78);
300 if (setitimer(timer
, &it
, NULL
)) my_e(1);
303 if (pid
< 0) my_e(2);
306 if (getitimer(timer
, &it
)) exit(5);
307 if (!EQITIMER(it
, 0, 0, 0, 0)) exit(6);
312 if (wait(&status
) != pid
) my_e(3);
313 if (!WIFEXITED(status
)) my_e(4);
314 if (WEXITSTATUS(status
) != 0) my_e(WEXITSTATUS(status
));
316 FILLITIMER(it
, 0, 0, 0, 0);
317 if (setitimer(timer
, &it
, &oit
)) my_e(7);
318 if (!LEITIMER(oit
, 12, 34, 56, 78)) my_e(8);
321 /* test if timer is carried over to exec()'ed process */
322 void test_exec(void) {
331 if (pid
< 0) my_e(1);
334 FILLITIMER(it
, 3, 0, 1, 0);
335 if (setitimer(timer
, &it
, NULL
)) exit(2);
337 sprintf(buf
, "%d", timer
);
338 execl(executable
, "DO CHECK", buf
, NULL
);
343 if (wait(&status
) != pid
) my_e(4);
344 if (WIFSIGNALED(status
)) {
345 /* process should have died from corresponding signal */
346 if (WTERMSIG(status
) != sigs
[timer
]) my_e(5);
349 if (WIFEXITED(status
)) my_e(WEXITSTATUS(status
));
354 /* procedure of the exec()'ed process */
359 if (getitimer(timer
, &it
)) return(81);
360 if (!LEITIMER(it
, 3, 0, 1, 0)) return(82);
373 exp
= time(&now
) + secs
+ 1;
376 for (i
= 0; i
< 100000; i
++);
385 if (sig
!= sigs
[timer
]) my_e(1001);