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;
36 eval "use Time::HiRes qw($symbol)";
37 return 0 unless $@ eq '';
38 eval "my \$a = $symbol";
42 printf "# have_gettimeofday = %d\n", $have_gettimeofday;
43 printf "# have_usleep = %d\n", $have_usleep;
44 printf "# have_nanosleep = %d\n", $have_nanosleep;
45 printf "# have_ualarm = %d\n", $have_ualarm;
46 printf "# have_clock_gettime = %d\n", $have_clock_gettime;
47 printf "# have_clock_getres = %d\n", $have_clock_getres;
49 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
50 import Time::HiRes 'usleep' if $have_usleep;
51 import Time::HiRes 'nanosleep' if $have_nanosleep;
52 import Time::HiRes 'ualarm' if $have_ualarm;
53 import Time::HiRes 'clock_gettime' if $have_clock_gettime;
54 import Time::HiRes 'clock_getres' if $have_clock_getres;
58 use Time::HiRes qw(gettimeofday);
60 my $have_alarm = $Config{d_alarm};
61 my $have_fork = $Config{d_fork};
62 my $waitfor = 60; # 10-20 seconds is normal (load affects this).
67 print "# I am the main process $$, starting the timer process...\n";
69 if (defined $timer_pid) {
70 if ($timer_pid == 0) { # We are the kid, set up the timer.
71 print "# I am the timer process $$, sleeping for $waitfor seconds...\n";
73 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
74 print "# Terminating the main process...\n";
75 kill('TERM', getppid());
76 print "# This is the timer process $$, over and out.\n";
79 print "# The timer process $timer_pid launched, continuing testing...\n";
80 $TheEnd = time() + $waitfor;
83 warn "$0: fork failed: $!\n";
86 print "# No timer process (need fork)\n";
91 if (open(XDEFINE, "xdefine")) {
92 chomp($xdefine = <XDEFINE>);
96 # Ideally, we'd like to test that the timers are rather precise.
97 # However, if the system is busy, there are no guarantees on how
98 # quickly we will return. This limit used to be 10%, but that
99 # was occasionally triggered falsely.
101 # Another possibility might be to print "ok" if the test completes fine
102 # with (say) 10% slosh, "skip - system may have been busy?" if the test
103 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
104 # consider changing over to test.pl at the same time.
105 # --A.D., Nov 27, 2001
106 my $limit = 0.20; # 20% is acceptable slosh for testing timers
109 map { print "ok $_ # skipped\n" } @_;
113 my ($n, $result, @info) = @_;
119 print "# @info\n" if @info;
123 unless ($have_gettimeofday) {
127 my @one = gettimeofday();
128 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
129 ok 3, $one[0] > 850_000_000, "@one too small";
133 my @two = gettimeofday();
134 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
135 "@two is not greater than @one";
137 my $f = Time::HiRes::time();
138 ok 5, $f > 850_000_000, "$f too small";
139 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
142 unless ($have_usleep) {
146 use Time::HiRes qw(usleep);
152 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
154 unless ($have_gettimeofday) {
158 my $f = Time::HiRes::time();
160 my $f2 = Time::HiRes::time();
162 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
166 # Two-arg tv_interval() is always available.
168 my $f = tv_interval [5, 100_000], [10, 500_000];
169 ok 9, abs($f - 5.4) < 0.001, $f;
172 unless ($have_gettimeofday) {
176 my $r = [gettimeofday()];
177 my $f = tv_interval $r;
181 unless ($have_usleep && $have_gettimeofday) {
185 my $r = [ gettimeofday() ];
186 Time::HiRes::sleep( 0.5 );
187 my $f = tv_interval $r;
188 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
191 unless ($have_ualarm && $have_alarm) {
196 local $SIG{ ALRM } = sub { $tick++ };
198 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
199 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
201 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
202 print "# tick = $tick, one = $one, two = $two, three = $three\n";
204 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
207 print "# tick = $tick, one = $one, two = $two, three = $three\n";
210 # Did we even get close?
212 unless ($have_gettimeofday) {
215 my ($s, $n, $i) = (0);
217 $s += Time::HiRes::time() - time();
220 # $s should be, at worst, equal to $n
221 # (time() may be rounding down, up, or closest)
222 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
223 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
226 my $has_ualarm = $Config{d_ualarm};
228 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
230 unless ( defined &Time::HiRes::gettimeofday
231 && defined &Time::HiRes::ualarm
232 && defined &Time::HiRes::usleep
235 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
238 use Time::HiRes qw(time alarm sleep);
240 my ($f, $r, $i, $not, $ok);
243 print "# time...$f\n";
246 $r = [Time::HiRes::gettimeofday()];
248 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
250 $r = [Time::HiRes::gettimeofday()];
256 select (undef, undef, undef, 3);
257 my $ival = Time::HiRes::tv_interval ($r);
258 print "# Select returned! $i $ival\n";
259 print "# ", abs($ival/3 - 1), "\n";
260 # Whether select() gets restarted after signals is
261 # implementation dependent. If it is restarted, we
262 # will get about 3.3 seconds: 3 from the select, 0.3
263 # from the alarm. If this happens, let's just skip
264 # this particular test. --jhi
265 if (abs($ival/3.3 - 1) < $limit) {
266 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
270 my $exp = 0.3 * (5 - $i);
271 # This test is more sensitive, so impose a softer limit.
272 if (abs($ival/$exp - 1) > 3*$limit) {
273 my $ratio = abs($ival/$exp);
274 $not = "while: $exp sleep took $ival ratio $ratio";
283 my $ival = Time::HiRes::tv_interval ($r);
284 print "# Tick! $i $ival\n";
285 my $exp = 0.3 * (5 - $i);
286 # This test is more sensitive, so impose a softer limit.
287 if (abs($ival/$exp - 1) > 3*$limit) {
288 my $ratio = abs($ival/$exp);
289 $not = "tick: $exp sleep took $ival ratio $ratio";
294 alarm(0); # can't cancel usig %SIG
296 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
299 unless ( defined &Time::HiRes::setitimer
300 && defined &Time::HiRes::getitimer
301 && has_symbol('ITIMER_VIRTUAL')
302 && $Config{sig_name} =~ m/\bVTALRM\b/) {
304 print "ok $_ # Skip: no virtual interval timers\n";
307 use Time::HiRes qw(setitimer getitimer ITIMER_VIRTUAL);
310 my $r = [Time::HiRes::gettimeofday()];
313 $i ? $i-- : setitimer(&ITIMER_VIRTUAL, 0);
314 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
317 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
319 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
320 my $virt = getitimer(&ITIMER_VIRTUAL);
321 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
324 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
326 while (getitimer(&ITIMER_VIRTUAL)) {
328 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
331 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
333 $virt = getitimer(&ITIMER_VIRTUAL);
334 print "not " unless defined $virt && $virt == 0;
337 $SIG{VTALRM} = 'DEFAULT';
340 if ($have_gettimeofday &&
342 use Time::HiRes qw(usleep);
346 my $sleep = 1.5; # seconds
349 $t0 = gettimeofday();
350 $a = abs(sleep($sleep) / $sleep - 1.0);
351 $td = gettimeofday() - $t0;
352 my $ratio = 1.0 + $a;
354 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
356 if ($td < $sleep * (1 + $limit)) {
357 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
359 print "ok 20 # Skip: $msg";
362 $t0 = gettimeofday();
363 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
364 $td = gettimeofday() - $t0;
367 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
369 if ($td < $sleep * (1 + $limit)) {
370 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
372 print "ok 21 # Skip: $msg";
377 print "ok $_ # Skip: no gettimeofday\n";
381 unless ($have_nanosleep) {
385 my $one = CORE::time;
386 nanosleep(10_000_000);
387 my $two = CORE::time;
388 nanosleep(10_000_000);
389 my $three = CORE::time;
390 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
392 unless ($have_gettimeofday) {
396 my $f = Time::HiRes::time();
397 nanosleep(500_000_000);
398 my $f2 = Time::HiRes::time();
400 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
405 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
406 "ok 24\n" : "not ok 24\n";
409 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
410 "ok 25\n" : "not ok 25\n";
414 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
415 "ok 26\n" : "not ok 26\n";
418 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
419 "ok 27\n" : "not ok 27\n";
425 if ($have_nanosleep) {
426 eval { nanosleep(-5) };
427 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
428 "ok 28\n" : "not ok 28\n";
433 if ($have_ualarm && $] >= 5.008001) {
434 # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
435 # Perl changes [18765] and [18770], perl bug [perl #20920]
437 # First we will find the loop size N (a for() loop 0..N-1)
438 # that will take more than T seconds.
441 use Time::HiRes qw(time);
447 for ($i = 0; $i < $N; $i++) { }
450 print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
456 # The time-burner which takes at least T seconds.
458 my $c = @_ ? shift : 1;
461 for ($i = 0; $i < $n; $i++) { }
464 # Then we will setup a periodic timer (the two-argument alarm() of
465 # Time::HiRes, behind the curtains the libc ualarm()) which has
466 # a signal handler that takes so much time (on the first initial
467 # invocation) that the first periodic invocation (second invocation)
468 # will happen before the first invocation has finished. In Perl 5.8.0
469 # the "safe signals" concept was implemented, with unfortunately at least
470 # one bug that caused a core dump on reentering the handler. This bug
471 # was fixed by the time of Perl 5.8.1.
473 # Do not try mixing sleep() and alarm() for testing this.
475 my $a = 0; # Number of alarms we receive.
476 my $A = 2; # Number of alarms we will handle before disarming.
477 # (We may well get $A + 1 alarms.)
481 print "# Alarm $a - ", time(), "\n";
482 alarm(0) if $a >= $A; # Disarm the alarm.
483 $F->(2); # Try burning CPU at least for 2T seconds.
486 use Time::HiRes qw(alarm);
487 alarm($T, $T); # Arm the alarm.
489 $F->(10); # Try burning CPU at least for 10T seconds.
491 print "ok 29\n"; # Not core dumping by now is considered to be the success.
496 if ($have_clock_gettime &&
497 # All implementations of clock_gettime()
498 # are SUPPOSED TO support CLOCK_REALTIME.
499 has_symbol('CLOCK_REALTIME')) {
503 print "# CLOCK_REALTIME: try = $try\n";
504 my $t0 = clock_gettime(&CLOCK_REALTIME);
505 use Time::HiRes qw(sleep);
508 my $t1 = clock_gettime(&CLOCK_REALTIME);
509 if ($t0 > 0 && $t1 > $t0) {
510 print "# t1 = $t1, t0 = $t0\n";
512 my $rt = abs(1 - $dt / $T);
513 print "# dt = $dt, rt = $rt\n";
514 if ($rt <= 2 * $limit) {
519 print "# Error: t0 = $t0, t1 = $t1\n";
521 my $r = rand() + rand();
522 printf "# Sleeping for %.6f seconds...\n";
532 print "# No clock_gettime\n";
536 if ($have_clock_getres) {
537 my $tr = clock_getres();
539 print "ok 31 # tr = $tr\n";
541 print "not ok 31 # tr = $tr\n";
544 print "# No clock_getres\n";
549 if (defined $timer_pid) {
550 my $left = $TheEnd - time();
551 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;
552 kill('TERM', $timer_pid); # We are done, the timer can go.
553 unlink("ktrace.out"); # Used in BSD system call tracing.
554 print "# All done.\n";