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..31\n"; }
17 END { print "not ok 1\n" unless $loaded }
19 use Time::HiRes qw(tv_interval);
27 my $have_gettimeofday = &Time::HiRes::d_gettimeofday;
28 my $have_usleep = &Time::HiRes::d_usleep;
29 my $have_nanosleep = &Time::HiRes::d_nanosleep;
30 my $have_ualarm = &Time::HiRes::d_ualarm;
31 my $have_clock_gettime = &Time::HiRes::d_clock_gettime;
32 my $have_clock_getres = &Time::HiRes::d_clock_getres;
34 printf "# have_gettimeofday = %d\n", $have_gettimeofday;
35 printf "# have_usleep = %d\n", $have_usleep;
36 printf "# have_nanosleep = %d\n", $have_nanosleep;
37 printf "# have_ualarm = %d\n", $have_ualarm;
38 printf "# have_clock_gettime = %d\n", $have_clock_gettime;
39 printf "# have_clock_getres = %d\n", $have_clock_getres;
41 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
42 import Time::HiRes 'usleep' if $have_usleep;
43 import Time::HiRes 'nanosleep' if $have_nanosleep;
44 import Time::HiRes 'ualarm' if $have_ualarm;
45 import Time::HiRes 'clock_gettime' if $have_clock_gettime;
46 import Time::HiRes 'clock_getres' if $have_clock_getres;
50 my $have_alarm = $Config{d_alarm};
51 my $have_fork = $Config{d_fork};
52 my $waitfor = 60; # 10-20 seconds is normal (load affects this).
56 print "# I am the main process $$, starting the timer process...\n";
58 if (defined $timer_pid) {
59 if ($timer_pid == 0) { # We are the kid, set up the timer.
60 print "# I am the timer process $$, sleeping for $waitfor seconds...\n";
62 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
63 print "# Terminating the main process...\n";
64 kill('TERM', getppid());
65 print "# This is the timer process $$, over and out.\n";
68 print "# Timer process $timer_pid launched, continuing testing...\n";
71 warn "$0: fork failed: $!\n";
74 print "# No timer process (need fork)\n";
79 if (open(XDEFINE, "xdefine")) {
80 chomp($xdefine = <XDEFINE>);
84 # Ideally, we'd like to test that the timers are rather precise.
85 # However, if the system is busy, there are no guarantees on how
86 # quickly we will return. This limit used to be 10%, but that
87 # was occasionally triggered falsely.
89 # Another possibility might be to print "ok" if the test completes fine
90 # with (say) 10% slosh, "skip - system may have been busy?" if the test
91 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
92 # consider changing over to test.pl at the same time.
93 # --A.D., Nov 27, 2001
94 my $limit = 0.20; # 20% is acceptable slosh for testing timers
97 map { print "ok $_ # skipped\n" } @_;
101 my ($n, $result, @info) = @_;
107 print "# @info\n" if @info;
111 unless ($have_gettimeofday) {
115 my @one = gettimeofday();
116 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
117 ok 3, $one[0] > 850_000_000, "@one too small";
121 my @two = gettimeofday();
122 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
123 "@two is not greater than @one";
125 my $f = Time::HiRes::time();
126 ok 5, $f > 850_000_000, "$f too small";
127 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
130 unless ($have_usleep) {
139 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
141 unless ($have_gettimeofday) {
145 my $f = Time::HiRes::time();
147 my $f2 = Time::HiRes::time();
149 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
153 # Two-arg tv_interval() is always available.
155 my $f = tv_interval [5, 100_000], [10, 500_000];
156 ok 9, abs($f - 5.4) < 0.001, $f;
159 unless ($have_gettimeofday) {
163 my $r = [gettimeofday()];
164 my $f = tv_interval $r;
168 unless ($have_usleep && $have_gettimeofday) {
172 my $r = [gettimeofday()];
173 Time::HiRes::sleep( 0.5 );
174 my $f = tv_interval $r;
175 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
178 unless ($have_ualarm && $have_alarm) {
183 local $SIG{ ALRM } = sub { $tick++ };
185 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
186 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
188 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
189 print "# tick = $tick, one = $one, two = $two, three = $three\n";
191 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
194 print "# tick = $tick, one = $one, two = $two, three = $three\n";
197 # Did we even get close?
199 unless ($have_gettimeofday) {
202 my ($s, $n, $i) = (0);
204 $s += Time::HiRes::time() - time();
207 # $s should be, at worst, equal to $n
208 # (time() may be rounding down, up, or closest)
209 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
210 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
213 my $has_ualarm = $Config{d_ualarm};
215 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
217 unless ( defined &Time::HiRes::gettimeofday
218 && defined &Time::HiRes::ualarm
219 && defined &Time::HiRes::usleep
222 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
225 use Time::HiRes qw(time alarm sleep);
227 my ($f, $r, $i, $not, $ok);
230 print "# time...$f\n";
233 $r = [Time::HiRes::gettimeofday()];
235 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
237 $r = [Time::HiRes::gettimeofday()];
243 select (undef, undef, undef, 3);
244 my $ival = Time::HiRes::tv_interval ($r);
245 print "# Select returned! $i $ival\n";
246 print "# ", abs($ival/3 - 1), "\n";
247 # Whether select() gets restarted after signals is
248 # implementation dependent. If it is restarted, we
249 # will get about 3.3 seconds: 3 from the select, 0.3
250 # from the alarm. If this happens, let's just skip
251 # this particular test. --jhi
252 if (abs($ival/3.3 - 1) < $limit) {
253 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
257 my $exp = 0.3 * (5 - $i);
258 # This test is more sensitive, so impose a softer limit.
259 if (abs($ival/$exp - 1) > 3*$limit) {
260 my $ratio = abs($ival/$exp);
261 $not = "while: $exp sleep took $ival ratio $ratio";
270 my $ival = Time::HiRes::tv_interval ($r);
271 print "# Tick! $i $ival\n";
272 my $exp = 0.3 * (5 - $i);
273 # This test is more sensitive, so impose a softer limit.
274 if (abs($ival/$exp - 1) > 3*$limit) {
275 my $ratio = abs($ival/$exp);
276 $not = "tick: $exp sleep took $ival ratio $ratio";
281 alarm(0); # can't cancel usig %SIG
283 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
286 unless ( defined &Time::HiRes::setitimer
287 && defined &Time::HiRes::getitimer
288 && exists &Time::HiRes::ITIMER_VIRTUAL
290 && $Config{sig_name} =~ m/\bVTALRM\b/) {
292 print "ok $_ # Skip: no virtual interval timers\n";
295 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
298 my $r = [Time::HiRes::gettimeofday()];
301 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
302 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
305 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
307 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
308 my $virt = getitimer(ITIMER_VIRTUAL);
309 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
312 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
314 while (getitimer(ITIMER_VIRTUAL)) {
316 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
319 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
321 $virt = getitimer(ITIMER_VIRTUAL);
322 print "not " unless defined $virt && $virt == 0;
325 $SIG{VTALRM} = 'DEFAULT';
328 if ($have_gettimeofday) {
331 my $sleep = 1.5; # seconds
334 $t0 = gettimeofday();
335 $a = abs(sleep($sleep) / $sleep - 1.0);
336 $td = gettimeofday() - $t0;
337 my $ratio = 1.0 + $a;
339 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
341 if ($td < $sleep * (1 + $limit)) {
342 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
344 print "ok 20 # Skip: $msg";
347 $t0 = gettimeofday();
348 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
349 $td = gettimeofday() - $t0;
352 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
354 if ($td < $sleep * (1 + $limit)) {
355 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
357 print "ok 21 # Skip: $msg";
362 print "ok $_ # Skip: no gettimeofday\n";
366 unless ($have_nanosleep) {
370 my $one = CORE::time;
371 nanosleep(10_000_000);
372 my $two = CORE::time;
373 nanosleep(10_000_000);
374 my $three = CORE::time;
375 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
377 unless ($have_gettimeofday) {
381 my $f = Time::HiRes::time();
382 nanosleep(500_000_000);
383 my $f2 = Time::HiRes::time();
385 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
390 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
391 "ok 24\n" : "not ok 24\n";
394 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
395 "ok 25\n" : "not ok 25\n";
399 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
400 "ok 26\n" : "not ok 26\n";
403 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
404 "ok 27\n" : "not ok 27\n";
410 if ($have_nanosleep) {
411 eval { nanosleep(-5) };
412 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
413 "ok 28\n" : "not ok 28\n";
418 if ($have_ualarm && $] >= 5.008001) {
419 # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
420 # Perl changes [18765] and [18770], perl bug [perl #20920]
422 # First we will find the loop size N (a for() loop 0..N-1)
423 # that will take more than T seconds.
426 use Time::HiRes qw(time);
432 for ($i = 0; $i < $N; $i++) { }
435 print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
441 # The time-burner which takes at least T seconds.
443 my $c = @_ ? shift : 1;
446 for ($i = 0; $i < $n; $i++) { }
449 # Then we will setup a periodic timer (the two-argument alarm() of
450 # Time::HiRes, behind the curtains the libc ualarm()) which has
451 # a signal handler that takes so much time (on the first initial
452 # invocation) that the first periodic invocation (second invocation)
453 # will happen before the first invocation has finished. In Perl 5.8.0
454 # the "safe signals" concept was implemented, with unfortunately at least
455 # one bug that caused a core dump on reentering the handler. This bug
456 # was fixed by the time of Perl 5.8.1.
458 my $a = 0; # Number of alarms we receive.
459 my $A = 2; # Number of alarms we will handle before disarming.
460 # (We may well get $A + 1 alarms.)
464 print "# Alarm $a - ", time(), "\n";
465 alarm(0) if $a >= $A; # Disarm the alarm.
466 $F->(2); # Try burning CPU at least for 2T seconds.
469 use Time::HiRes qw(alarm);
470 alarm($T, $T); # Arm the alarm.
472 $F->(10); # Try burning CPU at least for 10T seconds.
474 print "ok 29\n"; # Not core dumping by now is considered to be the success.
479 if ($have_clock_gettime) {
480 # All implementations are SUPPOSED TO support CLOCK_REALTIME...
481 eval 'use Time::HiRes qw(CLOCK_REALTIME)';
483 my $t0 = clock_gettime(&CLOCK_REALTIME);
484 use Time::HiRes qw(sleep);
487 my $t1 = clock_gettime(&CLOCK_REALTIME);
488 if ($t0 > 0 && $t1) {
489 print "# t1 = $t1, t0 = $t0\n";
491 my $rt = abs(1 - $dt / $T);
492 if ($rt <= 0.25) { # Allow 25% jitter.
493 print "ok 30 # dt = $dt, r = $rt\n";
495 print "not ok 30 # dt = $dt, rt = $rt\n";
498 print "# Error '$!'\n";
502 print "# No CLOCK_REALTIME ($@)\n";
506 print "# No clock_gettime\n";
510 if ($have_clock_getres) {
511 my $tr = clock_getres();
513 print "ok 31 # tr = $tr\n";
515 print "not ok 31 # tr = $tr\n";
518 print "# No clock_getres\n";
523 if (defined $timer_pid) {
524 print "# I am the main process $$, terminating the timer process $timer_pid.\n";
525 kill('TERM', $timer_pid); # We are done, the timer can go.
526 unlink("ktrace.out"); # Used in BSD system call tracing.
527 print "# All done.\n";