if ($ENV{PERL_CORE}) {
chdir 't' if -d 't';
@INC = '../lib';
+ require Config; import Config;
+ if (" $Config{'extensions'} " !~ m[ Time/HiRes ]) {
+ print "1..0 # Skip -- Perl configured without Time::HiRes module\n";
+ exit 0;
+ }
}
}
-BEGIN { $| = 1; print "1..25\n"; }
+BEGIN { $| = 1; print "1..31\n"; }
-END {print "not ok 1\n" unless $loaded;}
+END { print "not ok 1\n" unless $loaded }
use Time::HiRes qw(tv_interval);
use strict;
-my $have_gettimeofday = defined &Time::HiRes::gettimeofday;
-my $have_usleep = defined &Time::HiRes::usleep;
-my $have_ualarm = defined &Time::HiRes::ualarm;
-my $have_time = defined &Time::HiRes::time;
+my $have_gettimeofday = &Time::HiRes::d_gettimeofday;
+my $have_usleep = &Time::HiRes::d_usleep;
+my $have_nanosleep = &Time::HiRes::d_nanosleep;
+my $have_ualarm = &Time::HiRes::d_ualarm;
+my $have_clock_gettime = &Time::HiRes::d_clock_gettime;
+my $have_clock_getres = &Time::HiRes::d_clock_getres;
+
+sub has_symbol {
+ my $symbol = shift;
+ eval "use Time::HiRes qw($symbol)";
+ return 0 unless $@ eq '';
+ eval "my \$a = $symbol";
+ return $@ eq '';
+}
+
+printf "# have_gettimeofday = %d\n", $have_gettimeofday;
+printf "# have_usleep = %d\n", $have_usleep;
+printf "# have_nanosleep = %d\n", $have_nanosleep;
+printf "# have_ualarm = %d\n", $have_ualarm;
+printf "# have_clock_gettime = %d\n", $have_clock_gettime;
+printf "# have_clock_getres = %d\n", $have_clock_getres;
import Time::HiRes 'gettimeofday' if $have_gettimeofday;
import Time::HiRes 'usleep' if $have_usleep;
+import Time::HiRes 'nanosleep' if $have_nanosleep;
import Time::HiRes 'ualarm' if $have_ualarm;
+import Time::HiRes 'clock_gettime' if $have_clock_gettime;
+import Time::HiRes 'clock_getres' if $have_clock_getres;
use Config;
+use Time::HiRes qw(gettimeofday);
+
my $have_alarm = $Config{d_alarm};
my $have_fork = $Config{d_fork};
-my $waitfor = 60; # 10 seconds is normal.
-my $pid;
+my $waitfor = 60; # 10-20 seconds is normal (load affects this).
+my $timer_pid;
+my $TheEnd;
if ($have_fork) {
- print "# Testing process $$\n";
- print "# Starting the timer process\n";
- if (defined ($pid = fork())) {
- if ($pid == 0) { # We are the kid, set up the timer.
- print "# Timer process $$\n";
+ print "# I am the main process $$, starting the timer process...\n";
+ $timer_pid = fork();
+ if (defined $timer_pid) {
+ if ($timer_pid == 0) { # We are the kid, set up the timer.
+ print "# I am the timer process $$, sleeping for $waitfor seconds...\n";
sleep($waitfor);
- warn "$0: Time's up!\n";
- print "# Terminating the testing process\n";
+ warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
+ print "# Terminating the main process...\n";
kill('TERM', getppid());
- print "# Timer process exiting\n";
+ print "# This is the timer process $$, over and out.\n";
exit(0);
+ } else {
+ print "# The timer process $timer_pid launched, continuing testing...\n";
+ $TheEnd = time() + $waitfor;
}
} else {
warn "$0: fork failed: $!\n";
}
} else {
- print "# No timer process\n";
+ print "# No timer process (need fork)\n";
}
my $xdefine = '';
}
}
-if (!$have_gettimeofday) {
+unless ($have_gettimeofday) {
skip 2..6;
}
else {
ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
}
-if (!$have_usleep) {
+unless ($have_usleep) {
skip 7..8;
}
else {
+ use Time::HiRes qw(usleep);
my $one = time;
usleep(10_000);
my $two = time;
my $three = time;
ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
- if (!$have_gettimeofday) {
+ unless ($have_gettimeofday) {
skip 8;
}
else {
ok 9, abs($f - 5.4) < 0.001, $f;
}
-if (!$have_gettimeofday) {
+unless ($have_gettimeofday) {
skip 10;
}
else {
ok 10, $f < 2, $f;
}
-if (!$have_usleep || !$have_gettimeofday) {
+unless ($have_usleep && $have_gettimeofday) {
skip 11;
}
else {
- my $r = [gettimeofday()];
+ my $r = [ gettimeofday() ];
Time::HiRes::sleep( 0.5 );
my $f = tv_interval $r;
ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
}
-if (!$have_ualarm || !$have_alarm) {
+unless ($have_ualarm && $have_alarm) {
skip 12..13;
}
else {
my $tick = 0;
- local $SIG{ALRM} = sub { $tick++ };
+ local $SIG{ ALRM } = sub { $tick++ };
- my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
- my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { sleep }
+ my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
+ my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
my $three = time;
ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
+ print "# tick = $tick, one = $one, two = $two, three = $three\n";
- $tick = 0;
- ualarm(10_000, 10_000);
- while ($tick < 3) { sleep }
+ $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
ok 13, 1;
ualarm(0);
+ print "# tick = $tick, one = $one, two = $two, three = $three\n";
}
-# new test: did we even get close?
+# Did we even get close?
-if (!$have_time) {
- skip 14
+unless ($have_gettimeofday) {
+ skip 14;
} else {
- my ($s, $n);
- for my $i (1 .. 100) {
+ my ($s, $n, $i) = (0);
+ for $i (1 .. 100) {
$s += Time::HiRes::time() - time();
$n++;
}
print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
}
} else {
- use Time::HiRes qw (time alarm sleep);
+ use Time::HiRes qw(time alarm sleep);
my ($f, $r, $i, $not, $ok);
unless ( defined &Time::HiRes::setitimer
&& defined &Time::HiRes::getitimer
- && eval 'Time::HiRes::ITIMER_VIRTUAL'
- && $Config{d_select}
+ && has_symbol('ITIMER_VIRTUAL')
&& $Config{sig_name} =~ m/\bVTALRM\b/) {
for (18..19) {
print "ok $_ # Skip: no virtual interval timers\n";
}
} else {
- use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
+ use Time::HiRes qw(setitimer getitimer ITIMER_VIRTUAL);
my $i = 3;
my $r = [Time::HiRes::gettimeofday()];
$SIG{VTALRM} = sub {
- $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
+ $i ? $i-- : setitimer(&ITIMER_VIRTUAL, 0);
print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
};
print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
# Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
- print "not " unless abs(getitimer(ITIMER_VIRTUAL) / 0.5) - 1 < $limit;
+ my $virt = getitimer(&ITIMER_VIRTUAL);
+ print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
print "ok 18\n";
print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
- while (getitimer(ITIMER_VIRTUAL)) {
+ while (getitimer(&ITIMER_VIRTUAL)) {
my $j;
for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
}
print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
- print "not " unless getitimer(ITIMER_VIRTUAL) == 0;
+ $virt = getitimer(&ITIMER_VIRTUAL);
+ print "not " unless defined $virt && $virt == 0;
print "ok 19\n";
$SIG{VTALRM} = 'DEFAULT';
}
-if ($have_gettimeofday) {
+if ($have_gettimeofday &&
+ $have_usleep) {
+ use Time::HiRes qw(usleep);
+
my ($t0, $td);
my $sleep = 1.5; # seconds
}
}
+unless ($have_nanosleep) {
+ skip 22..23;
+}
+else {
+ my $one = CORE::time;
+ nanosleep(10_000_000);
+ my $two = CORE::time;
+ nanosleep(10_000_000);
+ my $three = CORE::time;
+ ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
+
+ unless ($have_gettimeofday) {
+ skip 23;
+ }
+ else {
+ my $f = Time::HiRes::time();
+ nanosleep(500_000_000);
+ my $f2 = Time::HiRes::time();
+ my $d = $f2 - $f;
+ ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
+ }
+}
+
eval { sleep(-1) };
print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
- "ok 22\n" : "not ok 22\n";
+ "ok 24\n" : "not ok 24\n";
eval { usleep(-2) };
print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
- "ok 23\n" : "not ok 23\n";
+ "ok 25\n" : "not ok 25\n";
if ($have_ualarm) {
eval { alarm(-3) };
print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
- "ok 24\n" : "not ok 24\n";
+ "ok 26\n" : "not ok 26\n";
eval { ualarm(-4) };
print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
- "ok 25\n" : "not ok 25\n";
+ "ok 27\n" : "not ok 27\n";
} else {
- skip 24;
- skip 25;
+ skip 26;
+ skip 27;
}
-if (defined $pid) {
- print "# Terminating the timer process $pid\n";
- kill('TERM', $pid); # We are done, the timer can go.
+if ($have_nanosleep) {
+ eval { nanosleep(-5) };
+ print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
+ "ok 28\n" : "not ok 28\n";
+} else {
+ skip 28;
+}
+
+if ($have_ualarm && $] >= 5.008001) {
+ # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
+ # Perl changes [18765] and [18770], perl bug [perl #20920]
+
+ # First we will find the loop size N (a for() loop 0..N-1)
+ # that will take more than T seconds.
+
+ my $T = 0.01;
+ use Time::HiRes qw(time);
+ my $N = 1024;
+ my $i;
+ N: {
+ do {
+ my $t0 = time();
+ for ($i = 0; $i < $N; $i++) { }
+ my $t1 = time();
+ my $dt = $t1 - $t0;
+ print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
+ last N if $dt > $T;
+ $N *= 2;
+ } while (1);
+ }
+
+ # The time-burner which takes at least T seconds.
+ my $F = sub {
+ my $c = @_ ? shift : 1;
+ my $n = $c * $N;
+ my $i;
+ for ($i = 0; $i < $n; $i++) { }
+ };
+
+ # Then we will setup a periodic timer (the two-argument alarm() of
+ # Time::HiRes, behind the curtains the libc ualarm()) which has
+ # a signal handler that takes so much time (on the first initial
+ # invocation) that the first periodic invocation (second invocation)
+ # will happen before the first invocation has finished. In Perl 5.8.0
+ # the "safe signals" concept was implemented, with unfortunately at least
+ # one bug that caused a core dump on reentering the handler. This bug
+ # was fixed by the time of Perl 5.8.1.
+
+ # Do not try mixing sleep() and alarm() for testing this.
+
+ my $a = 0; # Number of alarms we receive.
+ my $A = 2; # Number of alarms we will handle before disarming.
+ # (We may well get $A + 1 alarms.)
+
+ $SIG{ALRM} = sub {
+ $a++;
+ print "# Alarm $a - ", time(), "\n";
+ alarm(0) if $a >= $A; # Disarm the alarm.
+ $F->(2); # Try burning CPU at least for 2T seconds.
+ };
+
+ use Time::HiRes qw(alarm);
+ alarm($T, $T); # Arm the alarm.
+
+ $F->(10); # Try burning CPU at least for 10T seconds.
+
+ print "ok 29\n"; # Not core dumping by now is considered to be the success.
+} else {
+ skip 29;
+}
+
+if ($have_clock_gettime &&
+ # All implementations of clock_gettime()
+ # are SUPPOSED TO support CLOCK_REALTIME.
+ has_symbol('CLOCK_REALTIME')) {
+ my $ok = 0;
+ TRY: {
+ for my $try (1..3) {
+ print "# CLOCK_REALTIME: try = $try\n";
+ my $t0 = clock_gettime(&CLOCK_REALTIME);
+ use Time::HiRes qw(sleep);
+ my $T = 1.5;
+ sleep($T);
+ my $t1 = clock_gettime(&CLOCK_REALTIME);
+ if ($t0 > 0 && $t1 > $t0) {
+ print "# t1 = $t1, t0 = $t0\n";
+ my $dt = $t1 - $t0;
+ my $rt = abs(1 - $dt / $T);
+ print "# dt = $dt, rt = $rt\n";
+ if ($rt <= 2 * $limit) {
+ $ok = 1;
+ last TRY;
+ }
+ } else {
+ print "# Error: t0 = $t0, t1 = $t1\n";
+ }
+ my $r = rand() + rand();
+ printf "# Sleeping for %.6f seconds...\n";
+ sleep($r);
+ }
+ }
+ if ($ok) {
+ print "ok 30\n";
+ } else {
+ print "not ok 30\n";
+ }
+} else {
+ print "# No clock_gettime\n";
+ skip 30;
+}
+
+if ($have_clock_getres) {
+ my $tr = clock_getres();
+ if ($tr > 0) {
+ print "ok 31 # tr = $tr\n";
+ } else {
+ print "not ok 31 # tr = $tr\n";
+ }
+} else {
+ print "# No clock_getres\n";
+ skip 31;
+}
+
+END {
+ if (defined $timer_pid) {
+ my $left = $TheEnd - time();
+ printf "# I am the main process $$, terminating the timer process $timer_pid\n# before it terminates me in %d seconds (testing took %d seconds).\n", $left, $waitfor - $left;
+ kill('TERM', $timer_pid); # We are done, the timer can go.
+ unlink("ktrace.out"); # Used in BSD system call tracing.
+ print "# All done.\n";
+ }
}