Commit | Line | Data |
88cb3de1 |
1 | #!./perl -w |
2 | |
dcf686c9 |
3 | BEGIN { |
3f2ee006 |
4 | if ($ENV{PERL_CORE}) { |
5 | chdir 't' if -d 't'; |
6 | @INC = '../lib'; |
df2f6354 |
7 | require Config; import Config; |
8 | if (" $Config{'extensions'} " !~ m[ Time/HiRes ]) { |
9 | print "1..0 # Skip -- Perl configured without Time::HiRes module\n"; |
10 | exit 0; |
11 | } |
3f2ee006 |
12 | } |
dcf686c9 |
13 | } |
14 | |
f7916ddb |
15 | BEGIN { $| = 1; print "1..25\n"; } |
dcf686c9 |
16 | |
17 | END {print "not ok 1\n" unless $loaded;} |
18 | |
19 | use Time::HiRes qw(tv_interval); |
20 | |
21 | $loaded = 1; |
22 | |
23 | print "ok 1\n"; |
24 | |
25 | use strict; |
26 | |
27 | my $have_gettimeofday = defined &Time::HiRes::gettimeofday; |
28 | my $have_usleep = defined &Time::HiRes::usleep; |
29 | my $have_ualarm = defined &Time::HiRes::ualarm; |
c9ff6e92 |
30 | my $have_time = defined &Time::HiRes::time; |
dcf686c9 |
31 | |
32 | import Time::HiRes 'gettimeofday' if $have_gettimeofday; |
33 | import Time::HiRes 'usleep' if $have_usleep; |
34 | import Time::HiRes 'ualarm' if $have_ualarm; |
35 | |
3c72ec00 |
36 | use Config; |
37 | |
690f7c5f |
38 | my $have_alarm = $Config{d_alarm}; |
39 | my $have_fork = $Config{d_fork}; |
40 | my $waitfor = 60; # 10 seconds is normal. |
41 | my $pid; |
42 | |
43 | if ($have_fork) { |
44 | print "# Testing process $$\n"; |
45 | print "# Starting the timer process\n"; |
46 | if (defined ($pid = fork())) { |
47 | if ($pid == 0) { # We are the kid, set up the timer. |
48 | print "# Timer process $$\n"; |
49 | sleep($waitfor); |
b4c5c611 |
50 | warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded\n"; |
690f7c5f |
51 | print "# Terminating the testing process\n"; |
52 | kill('TERM', getppid()); |
53 | print "# Timer process exiting\n"; |
54 | exit(0); |
55 | } |
56 | } else { |
57 | warn "$0: fork failed: $!\n"; |
58 | } |
59 | } else { |
60 | print "# No timer process\n"; |
61 | } |
62 | |
12724655 |
63 | my $xdefine = ''; |
3f2ee006 |
64 | |
65 | if (open(XDEFINE, "xdefine")) { |
66 | chomp($xdefine = <XDEFINE>); |
67 | close(XDEFINE); |
68 | } |
69 | |
0740bb5b |
70 | # Ideally, we'd like to test that the timers are rather precise. |
71 | # However, if the system is busy, there are no guarantees on how |
72 | # quickly we will return. This limit used to be 10%, but that |
73 | # was occasionally triggered falsely. |
74 | # Try 20%. |
75 | # Another possibility might be to print "ok" if the test completes fine |
76 | # with (say) 10% slosh, "skip - system may have been busy?" if the test |
77 | # completes fine with (say) 30% slosh, and fail otherwise. If you do that, |
78 | # consider changing over to test.pl at the same time. |
79 | # --A.D., Nov 27, 2001 |
80 | my $limit = 0.20; # 20% is acceptable slosh for testing timers |
81 | |
dcf686c9 |
82 | sub skip { |
3f2ee006 |
83 | map { print "ok $_ # skipped\n" } @_; |
dcf686c9 |
84 | } |
85 | |
86 | sub ok { |
87 | my ($n, $result, @info) = @_; |
88 | if ($result) { |
89 | print "ok $n\n"; |
90 | } |
91 | else { |
92 | print "not ok $n\n"; |
93 | print "# @info\n" if @info; |
94 | } |
95 | } |
96 | |
97 | if (!$have_gettimeofday) { |
98 | skip 2..6; |
99 | } |
100 | else { |
101 | my @one = gettimeofday(); |
102 | ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args'; |
103 | ok 3, $one[0] > 850_000_000, "@one too small"; |
104 | |
105 | sleep 1; |
106 | |
107 | my @two = gettimeofday(); |
108 | ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])), |
109 | "@two is not greater than @one"; |
110 | |
c9ff6e92 |
111 | my $f = Time::HiRes::time(); |
dcf686c9 |
112 | ok 5, $f > 850_000_000, "$f too small"; |
0740bb5b |
113 | ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2"; |
dcf686c9 |
114 | } |
115 | |
116 | if (!$have_usleep) { |
117 | skip 7..8; |
118 | } |
119 | else { |
120 | my $one = time; |
121 | usleep(10_000); |
122 | my $two = time; |
123 | usleep(10_000); |
124 | my $three = time; |
125 | ok 7, $one == $two || $two == $three, "slept too long, $one $two $three"; |
126 | |
127 | if (!$have_gettimeofday) { |
128 | skip 8; |
129 | } |
130 | else { |
c9ff6e92 |
131 | my $f = Time::HiRes::time(); |
dcf686c9 |
132 | usleep(500_000); |
c9ff6e92 |
133 | my $f2 = Time::HiRes::time(); |
dcf686c9 |
134 | my $d = $f2 - $f; |
0740bb5b |
135 | ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2"; |
dcf686c9 |
136 | } |
137 | } |
138 | |
139 | # Two-arg tv_interval() is always available. |
140 | { |
141 | my $f = tv_interval [5, 100_000], [10, 500_000]; |
6cf89afa |
142 | ok 9, abs($f - 5.4) < 0.001, $f; |
dcf686c9 |
143 | } |
144 | |
145 | if (!$have_gettimeofday) { |
146 | skip 10; |
147 | } |
148 | else { |
149 | my $r = [gettimeofday()]; |
150 | my $f = tv_interval $r; |
151 | ok 10, $f < 2, $f; |
152 | } |
153 | |
c9ff6e92 |
154 | if (!$have_usleep || !$have_gettimeofday) { |
dcf686c9 |
155 | skip 11; |
156 | } |
157 | else { |
158 | my $r = [gettimeofday()]; |
dcf686c9 |
159 | Time::HiRes::sleep( 0.5 ); |
160 | my $f = tv_interval $r; |
0740bb5b |
161 | ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs."; |
dcf686c9 |
162 | } |
163 | |
690f7c5f |
164 | if (!$have_ualarm || !$have_alarm) { |
dcf686c9 |
165 | skip 12..13; |
166 | } |
167 | else { |
168 | my $tick = 0; |
64a7a97c |
169 | local $SIG{ ALRM } = sub { $tick++ }; |
dcf686c9 |
170 | |
64a7a97c |
171 | my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { } |
172 | my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { } |
dcf686c9 |
173 | my $three = time; |
174 | ok 12, $one == $two || $two == $three, "slept too long, $one $two $three"; |
64a7a97c |
175 | print "# tick = $tick, one = $one, two = $two, three = $three\n"; |
dcf686c9 |
176 | |
64a7a97c |
177 | $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { } |
dcf686c9 |
178 | ok 13, 1; |
179 | ualarm(0); |
64a7a97c |
180 | print "# tick = $tick, one = $one, two = $two, three = $three\n"; |
dcf686c9 |
181 | } |
182 | |
64a7a97c |
183 | # Did we even get close? |
dcf686c9 |
184 | |
c9ff6e92 |
185 | if (!$have_time) { |
64a7a97c |
186 | skip 14; |
c9ff6e92 |
187 | } else { |
1fbb4de4 |
188 | my ($s, $n, $i) = (0); |
189 | for $i (1 .. 100) { |
456811e8 |
190 | $s += Time::HiRes::time() - time(); |
191 | $n++; |
64cc9c1c |
192 | } |
456811e8 |
193 | # $s should be, at worst, equal to $n |
194 | # (time() may be rounding down, up, or closest) |
195 | ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()"; |
196 | print "# s = $s, n = $n, s/n = ", $s/$n, "\n"; |
dcf686c9 |
197 | } |
198 | |
3f2ee006 |
199 | my $has_ualarm = $Config{d_ualarm}; |
200 | |
201 | $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/; |
202 | |
203 | unless ( defined &Time::HiRes::gettimeofday |
dcf686c9 |
204 | && defined &Time::HiRes::ualarm |
c7f7de79 |
205 | && defined &Time::HiRes::usleep |
3f2ee006 |
206 | && $has_ualarm) { |
dcf686c9 |
207 | for (15..17) { |
3f2ee006 |
208 | print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n"; |
dcf686c9 |
209 | } |
210 | } else { |
211 | use Time::HiRes qw (time alarm sleep); |
212 | |
cb203acc |
213 | my ($f, $r, $i, $not, $ok); |
dcf686c9 |
214 | |
dcf686c9 |
215 | $f = time; |
add58217 |
216 | print "# time...$f\n"; |
217 | print "ok 15\n"; |
dcf686c9 |
218 | |
c9ff6e92 |
219 | $r = [Time::HiRes::gettimeofday()]; |
dcf686c9 |
220 | sleep (0.5); |
add58217 |
221 | print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n"; |
dcf686c9 |
222 | |
c9ff6e92 |
223 | $r = [Time::HiRes::gettimeofday()]; |
dcf686c9 |
224 | $i = 5; |
225 | $SIG{ALRM} = "tick"; |
6ea166ef |
226 | while ($i > 0) |
dcf686c9 |
227 | { |
0e172911 |
228 | alarm(0.3); |
c7f7de79 |
229 | select (undef, undef, undef, 3); |
230 | my $ival = Time::HiRes::tv_interval ($r); |
231 | print "# Select returned! $i $ival\n"; |
cb203acc |
232 | print "# ", abs($ival/3 - 1), "\n"; |
233 | # Whether select() gets restarted after signals is |
234 | # implementation dependent. If it is restarted, we |
235 | # will get about 3.3 seconds: 3 from the select, 0.3 |
236 | # from the alarm. If this happens, let's just skip |
237 | # this particular test. --jhi |
238 | if (abs($ival/3.3 - 1) < $limit) { |
3f2ee006 |
239 | $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)"; |
cb203acc |
240 | undef $not; |
241 | last; |
242 | } |
c7f7de79 |
243 | my $exp = 0.3 * (5 - $i); |
0740bb5b |
244 | # This test is more sensitive, so impose a softer limit. |
cb203acc |
245 | if (abs($ival/$exp - 1) > 3*$limit) { |
0740bb5b |
246 | my $ratio = abs($ival/$exp); |
247 | $not = "while: $exp sleep took $ival ratio $ratio"; |
c7f7de79 |
248 | last; |
249 | } |
cb203acc |
250 | $ok = $i; |
dcf686c9 |
251 | } |
252 | |
253 | sub tick |
254 | { |
dcf686c9 |
255 | $i--; |
c7f7de79 |
256 | my $ival = Time::HiRes::tv_interval ($r); |
257 | print "# Tick! $i $ival\n"; |
258 | my $exp = 0.3 * (5 - $i); |
0740bb5b |
259 | # This test is more sensitive, so impose a softer limit. |
cb203acc |
260 | if (abs($ival/$exp - 1) > 3*$limit) { |
0740bb5b |
261 | my $ratio = abs($ival/$exp); |
262 | $not = "tick: $exp sleep took $ival ratio $ratio"; |
c7f7de79 |
263 | $i = 0; |
264 | } |
dcf686c9 |
265 | } |
c7f7de79 |
266 | |
d0b83b2b |
267 | alarm(0); # can't cancel usig %SIG |
dcf686c9 |
268 | |
cb203acc |
269 | print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n"; |
dcf686c9 |
270 | } |
271 | |
3f2ee006 |
272 | unless ( defined &Time::HiRes::setitimer |
3c72ec00 |
273 | && defined &Time::HiRes::getitimer |
3f2ee006 |
274 | && eval 'Time::HiRes::ITIMER_VIRTUAL' |
d0b83b2b |
275 | && $Config{d_select} |
276 | && $Config{sig_name} =~ m/\bVTALRM\b/) { |
3c72ec00 |
277 | for (18..19) { |
0e172911 |
278 | print "ok $_ # Skip: no virtual interval timers\n"; |
3c72ec00 |
279 | } |
280 | } else { |
281 | use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL); |
282 | |
283 | my $i = 3; |
c9ff6e92 |
284 | my $r = [Time::HiRes::gettimeofday()]; |
3c72ec00 |
285 | |
286 | $SIG{VTALRM} = sub { |
287 | $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0); |
288 | print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n"; |
289 | }; |
290 | |
0e172911 |
291 | print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n"; |
292 | |
0740bb5b |
293 | # Assume interval timer granularity of $limit * 0.5 seconds. Too bold? |
4ed0e2d4 |
294 | my $virt = getitimer(ITIMER_VIRTUAL); |
295 | print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit; |
0e172911 |
296 | print "ok 18\n"; |
3c72ec00 |
297 | |
298 | print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n"; |
299 | |
1c41c9bf |
300 | while (getitimer(ITIMER_VIRTUAL)) { |
3f2ee006 |
301 | my $j; |
302 | for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer(). |
3c72ec00 |
303 | } |
304 | |
305 | print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n"; |
306 | |
4ed0e2d4 |
307 | $virt = getitimer(ITIMER_VIRTUAL); |
308 | print "not " unless defined $virt && $virt == 0; |
0e172911 |
309 | print "ok 19\n"; |
310 | |
3c72ec00 |
311 | $SIG{VTALRM} = 'DEFAULT'; |
312 | } |
313 | |
9c735d59 |
314 | if ($have_gettimeofday) { |
315 | my ($t0, $td); |
52d72fba |
316 | |
9c735d59 |
317 | my $sleep = 1.5; # seconds |
9c735d59 |
318 | my $msg; |
319 | |
320 | $t0 = gettimeofday(); |
321 | $a = abs(sleep($sleep) / $sleep - 1.0); |
322 | $td = gettimeofday() - $t0; |
0740bb5b |
323 | my $ratio = 1.0 + $a; |
9c735d59 |
324 | |
0740bb5b |
325 | $msg = "$td went by while sleeping $sleep, ratio $ratio.\n"; |
9c735d59 |
326 | |
327 | if ($td < $sleep * (1 + $limit)) { |
328 | print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg"; |
329 | } else { |
330 | print "ok 20 # Skip: $msg"; |
331 | } |
332 | |
333 | $t0 = gettimeofday(); |
334 | $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0); |
335 | $td = gettimeofday() - $t0; |
0740bb5b |
336 | $ratio = 1.0 + $a; |
9c735d59 |
337 | |
0740bb5b |
338 | $msg = "$td went by while sleeping $sleep, ratio $ratio.\n"; |
9c735d59 |
339 | |
340 | if ($td < $sleep * (1 + $limit)) { |
341 | print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg"; |
342 | } else { |
343 | print "ok 21 # Skip: $msg"; |
344 | } |
345 | |
346 | } else { |
347 | for (20..21) { |
348 | print "ok $_ # Skip: no gettimeofday\n"; |
349 | } |
350 | } |
52d72fba |
351 | |
f7916ddb |
352 | eval { sleep(-1) }; |
353 | print $@ =~ /::sleep\(-1\): negative time not invented yet/ ? |
354 | "ok 22\n" : "not ok 22\n"; |
355 | |
356 | eval { usleep(-2) }; |
357 | print $@ =~ /::usleep\(-2\): negative time not invented yet/ ? |
358 | "ok 23\n" : "not ok 23\n"; |
359 | |
ec77158c |
360 | if ($have_ualarm) { |
c46d5038 |
361 | eval { alarm(-3) }; |
362 | print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ? |
363 | "ok 24\n" : "not ok 24\n"; |
364 | |
ec77158c |
365 | eval { ualarm(-4) }; |
366 | print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ? |
f7916ddb |
367 | "ok 25\n" : "not ok 25\n"; |
ec77158c |
368 | } else { |
c46d5038 |
369 | skip 24; |
ec77158c |
370 | skip 25; |
371 | } |
690f7c5f |
372 | |
373 | if (defined $pid) { |
374 | print "# Terminating the timer process $pid\n"; |
375 | kill('TERM', $pid); # We are done, the timer can go. |
64a7a97c |
376 | unlink("ktrace.out"); |
690f7c5f |
377 | } |
378 | |