7 require Config; import Config;
8 if (" $Config{'extensions'} " !~ m[ Time/HiRes ]) {
9 print "1..0 # Skip -- Perl configured without Time::HiRes module\n";
15 BEGIN { $| = 1; print "1..28\n"; }
17 END { print "not ok 1\n" unless $loaded }
19 use Time::HiRes qw(tv_interval);
27 my $have_gettimeofday = defined &Time::HiRes::gettimeofday;
28 my $have_usleep = defined &Time::HiRes::usleep;
29 my $have_nanosleep = defined &Time::HiRes::nanosleep;
30 my $have_ualarm = defined &Time::HiRes::ualarm;
31 my $have_time = defined &Time::HiRes::time;
33 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
34 import Time::HiRes 'usleep' if $have_usleep;
35 import Time::HiRes 'nanosleep' if $have_nanosleep;
36 import Time::HiRes 'ualarm' if $have_ualarm;
40 my $have_alarm = $Config{d_alarm};
41 my $have_fork = $Config{d_fork};
42 my $waitfor = 60; # 10 seconds is normal.
46 print "# I am process $$, starting the timer process\n";
47 if (defined ($pid = fork())) {
48 if ($pid == 0) { # We are the kid, set up the timer.
49 print "# I am timer process $$\n";
51 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded\n";
52 print "# Terminating the testing process\n";
53 kill('TERM', getppid());
54 print "# Timer process exiting\n";
58 warn "$0: fork failed: $!\n";
61 print "# No timer process\n";
66 if (open(XDEFINE, "xdefine")) {
67 chomp($xdefine = <XDEFINE>);
71 # Ideally, we'd like to test that the timers are rather precise.
72 # However, if the system is busy, there are no guarantees on how
73 # quickly we will return. This limit used to be 10%, but that
74 # was occasionally triggered falsely.
76 # Another possibility might be to print "ok" if the test completes fine
77 # with (say) 10% slosh, "skip - system may have been busy?" if the test
78 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
79 # consider changing over to test.pl at the same time.
80 # --A.D., Nov 27, 2001
81 my $limit = 0.20; # 20% is acceptable slosh for testing timers
84 map { print "ok $_ # skipped\n" } @_;
88 my ($n, $result, @info) = @_;
94 print "# @info\n" if @info;
98 if (!$have_gettimeofday) {
102 my @one = gettimeofday();
103 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
104 ok 3, $one[0] > 850_000_000, "@one too small";
108 my @two = gettimeofday();
109 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
110 "@two is not greater than @one";
112 my $f = Time::HiRes::time();
113 ok 5, $f > 850_000_000, "$f too small";
114 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
126 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
128 if (!$have_gettimeofday) {
132 my $f = Time::HiRes::time();
134 my $f2 = Time::HiRes::time();
136 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
140 # Two-arg tv_interval() is always available.
142 my $f = tv_interval [5, 100_000], [10, 500_000];
143 ok 9, abs($f - 5.4) < 0.001, $f;
146 if (!$have_gettimeofday) {
150 my $r = [gettimeofday()];
151 my $f = tv_interval $r;
155 if (!$have_usleep || !$have_gettimeofday) {
159 my $r = [gettimeofday()];
160 Time::HiRes::sleep( 0.5 );
161 my $f = tv_interval $r;
162 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
165 if (!$have_ualarm || !$have_alarm) {
170 local $SIG{ ALRM } = sub { $tick++ };
172 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
173 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
175 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
176 print "# tick = $tick, one = $one, two = $two, three = $three\n";
178 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
181 print "# tick = $tick, one = $one, two = $two, three = $three\n";
184 # Did we even get close?
189 my ($s, $n, $i) = (0);
191 $s += Time::HiRes::time() - time();
194 # $s should be, at worst, equal to $n
195 # (time() may be rounding down, up, or closest)
196 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
197 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
200 my $has_ualarm = $Config{d_ualarm};
202 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
204 unless ( defined &Time::HiRes::gettimeofday
205 && defined &Time::HiRes::ualarm
206 && defined &Time::HiRes::usleep
209 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
212 use Time::HiRes qw (time alarm sleep);
214 my ($f, $r, $i, $not, $ok);
217 print "# time...$f\n";
220 $r = [Time::HiRes::gettimeofday()];
222 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
224 $r = [Time::HiRes::gettimeofday()];
230 select (undef, undef, undef, 3);
231 my $ival = Time::HiRes::tv_interval ($r);
232 print "# Select returned! $i $ival\n";
233 print "# ", abs($ival/3 - 1), "\n";
234 # Whether select() gets restarted after signals is
235 # implementation dependent. If it is restarted, we
236 # will get about 3.3 seconds: 3 from the select, 0.3
237 # from the alarm. If this happens, let's just skip
238 # this particular test. --jhi
239 if (abs($ival/3.3 - 1) < $limit) {
240 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
244 my $exp = 0.3 * (5 - $i);
245 # This test is more sensitive, so impose a softer limit.
246 if (abs($ival/$exp - 1) > 3*$limit) {
247 my $ratio = abs($ival/$exp);
248 $not = "while: $exp sleep took $ival ratio $ratio";
257 my $ival = Time::HiRes::tv_interval ($r);
258 print "# Tick! $i $ival\n";
259 my $exp = 0.3 * (5 - $i);
260 # This test is more sensitive, so impose a softer limit.
261 if (abs($ival/$exp - 1) > 3*$limit) {
262 my $ratio = abs($ival/$exp);
263 $not = "tick: $exp sleep took $ival ratio $ratio";
268 alarm(0); # can't cancel usig %SIG
270 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
273 unless ( defined &Time::HiRes::setitimer
274 && defined &Time::HiRes::getitimer
275 && eval 'Time::HiRes::ITIMER_VIRTUAL'
277 && $Config{sig_name} =~ m/\bVTALRM\b/) {
279 print "ok $_ # Skip: no virtual interval timers\n";
282 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
285 my $r = [Time::HiRes::gettimeofday()];
288 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
289 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
292 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
294 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
295 my $virt = getitimer(ITIMER_VIRTUAL);
296 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
299 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
301 while (getitimer(ITIMER_VIRTUAL)) {
303 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
306 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
308 $virt = getitimer(ITIMER_VIRTUAL);
309 print "not " unless defined $virt && $virt == 0;
312 $SIG{VTALRM} = 'DEFAULT';
315 if ($have_gettimeofday) {
318 my $sleep = 1.5; # seconds
321 $t0 = gettimeofday();
322 $a = abs(sleep($sleep) / $sleep - 1.0);
323 $td = gettimeofday() - $t0;
324 my $ratio = 1.0 + $a;
326 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
328 if ($td < $sleep * (1 + $limit)) {
329 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
331 print "ok 20 # Skip: $msg";
334 $t0 = gettimeofday();
335 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
336 $td = gettimeofday() - $t0;
339 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
341 if ($td < $sleep * (1 + $limit)) {
342 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
344 print "ok 21 # Skip: $msg";
349 print "ok $_ # Skip: no gettimeofday\n";
353 if (!$have_nanosleep) {
357 my $one = CORE::time;
358 nanosleep(10_000_000);
359 my $two = CORE::time;
360 nanosleep(10_000_000);
361 my $three = CORE::time;
362 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
364 if (!$have_gettimeofday) {
368 my $f = Time::HiRes::time();
369 nanosleep(500_000_000);
370 my $f2 = Time::HiRes::time();
372 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
377 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
378 "ok 24\n" : "not ok 24\n";
381 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
382 "ok 25\n" : "not ok 25\n";
386 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
387 "ok 26\n" : "not ok 26\n";
390 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
391 "ok 27\n" : "not ok 27\n";
397 if ($have_nanosleep) {
398 eval { nanosleep(-5) };
399 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
400 "ok 28\n" : "not ok 28\n";
406 print "# I am process $$, terminating the timer process $pid\n";
407 kill('TERM', $pid); # We are done, the timer can go.
408 unlink("ktrace.out");