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..33\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;
33 my $have_clock_nanosleep = &Time::HiRes::d_clock_nanosleep;
34 my $have_clock = &Time::HiRes::d_clock;
38 eval "use Time::HiRes qw($symbol)";
39 return 0 unless $@ eq '';
40 eval "my \$a = $symbol";
44 printf "# have_gettimeofday = %d\n", $have_gettimeofday;
45 printf "# have_usleep = %d\n", $have_usleep;
46 printf "# have_nanosleep = %d\n", $have_nanosleep;
47 printf "# have_ualarm = %d\n", $have_ualarm;
48 printf "# have_clock_gettime = %d\n", $have_clock_gettime;
49 printf "# have_clock_getres = %d\n", $have_clock_getres;
50 printf "# have_clock_nanosleep = %d\n", $have_clock_nanosleep;
51 printf "# have_clock = %d\n", $have_clock;
53 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
54 import Time::HiRes 'usleep' if $have_usleep;
55 import Time::HiRes 'nanosleep' if $have_nanosleep;
56 import Time::HiRes 'ualarm' if $have_ualarm;
57 import Time::HiRes 'clock_gettime' if $have_clock_gettime;
58 import Time::HiRes 'clock_getres' if $have_clock_getres;
59 import Time::HiRes 'clock_nanosleep' if $have_clock_nanosleep;
60 import Time::HiRes 'clock' if $have_clock;
64 use Time::HiRes qw(gettimeofday);
66 my $have_alarm = $Config{d_alarm};
67 my $have_fork = $Config{d_fork};
68 my $waitfor = 60; # 10-20 seconds is normal (load affects this).
73 print "# I am the main process $$, starting the timer process...\n";
75 if (defined $timer_pid) {
76 if ($timer_pid == 0) { # We are the kid, set up the timer.
77 print "# I am the timer process $$, sleeping for $waitfor seconds...\n";
79 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
80 print "# Terminating the main process...\n";
81 kill('TERM', getppid());
82 print "# This is the timer process $$, over and out.\n";
85 print "# The timer process $timer_pid launched, continuing testing...\n";
86 $TheEnd = time() + $waitfor;
89 warn "$0: fork failed: $!\n";
92 print "# No timer process (need fork)\n";
97 if (open(XDEFINE, "xdefine")) {
98 chomp($xdefine = <XDEFINE>);
102 # Ideally, we'd like to test that the timers are rather precise.
103 # However, if the system is busy, there are no guarantees on how
104 # quickly we will return. This limit used to be 10%, but that
105 # was occasionally triggered falsely.
107 # Another possibility might be to print "ok" if the test completes fine
108 # with (say) 10% slosh, "skip - system may have been busy?" if the test
109 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
110 # consider changing over to test.pl at the same time.
111 # --A.D., Nov 27, 2001
112 my $limit = 0.20; # 20% is acceptable slosh for testing timers
115 map { print "ok $_ # skipped\n" } @_;
119 my ($n, $result, @info) = @_;
125 print "# @info\n" if @info;
129 unless ($have_gettimeofday) {
133 my @one = gettimeofday();
134 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
135 ok 3, $one[0] > 850_000_000, "@one too small";
139 my @two = gettimeofday();
140 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
141 "@two is not greater than @one";
143 my $f = Time::HiRes::time();
144 ok 5, $f > 850_000_000, "$f too small";
145 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
148 unless ($have_usleep) {
152 use Time::HiRes qw(usleep);
158 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
160 unless ($have_gettimeofday) {
164 my $f = Time::HiRes::time();
166 my $f2 = Time::HiRes::time();
168 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
172 # Two-arg tv_interval() is always available.
174 my $f = tv_interval [5, 100_000], [10, 500_000];
175 ok 9, abs($f - 5.4) < 0.001, $f;
178 unless ($have_gettimeofday) {
182 my $r = [gettimeofday()];
183 my $f = tv_interval $r;
187 unless ($have_usleep && $have_gettimeofday) {
191 my $r = [ gettimeofday() ];
192 Time::HiRes::sleep( 0.5 );
193 my $f = tv_interval $r;
194 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
197 unless ($have_ualarm && $have_alarm) {
202 local $SIG{ ALRM } = sub { $tick++ };
204 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
205 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
207 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
208 print "# tick = $tick, one = $one, two = $two, three = $three\n";
210 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
213 print "# tick = $tick, one = $one, two = $two, three = $three\n";
216 # Did we even get close?
218 unless ($have_gettimeofday) {
221 my ($s, $n, $i) = (0);
223 $s += Time::HiRes::time() - time();
226 # $s should be, at worst, equal to $n
227 # (time() may be rounding down, up, or closest)
228 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
229 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
232 my $has_ualarm = $Config{d_ualarm};
234 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
236 unless ( defined &Time::HiRes::gettimeofday
237 && defined &Time::HiRes::ualarm
238 && defined &Time::HiRes::usleep
241 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
244 use Time::HiRes qw(time alarm sleep);
246 my ($f, $r, $i, $not, $ok);
249 print "# time...$f\n";
252 $r = [Time::HiRes::gettimeofday()];
254 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
256 $r = [Time::HiRes::gettimeofday()];
262 select (undef, undef, undef, 3);
263 my $ival = Time::HiRes::tv_interval ($r);
264 print "# Select returned! $i $ival\n";
265 print "# ", abs($ival/3 - 1), "\n";
266 # Whether select() gets restarted after signals is
267 # implementation dependent. If it is restarted, we
268 # will get about 3.3 seconds: 3 from the select, 0.3
269 # from the alarm. If this happens, let's just skip
270 # this particular test. --jhi
271 if (abs($ival/3.3 - 1) < $limit) {
272 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
276 my $exp = 0.3 * (5 - $i);
277 # This test is more sensitive, so impose a softer limit.
278 if (abs($ival/$exp - 1) > 3*$limit) {
279 my $ratio = abs($ival/$exp);
280 $not = "while: $exp sleep took $ival ratio $ratio";
289 my $ival = Time::HiRes::tv_interval ($r);
290 print "# Tick! $i $ival\n";
291 my $exp = 0.3 * (5 - $i);
292 # This test is more sensitive, so impose a softer limit.
293 if (abs($ival/$exp - 1) > 3*$limit) {
294 my $ratio = abs($ival/$exp);
295 $not = "tick: $exp sleep took $ival ratio $ratio";
300 alarm(0); # can't cancel usig %SIG
302 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
305 unless ( defined &Time::HiRes::setitimer
306 && defined &Time::HiRes::getitimer
307 && has_symbol('ITIMER_VIRTUAL')
308 && $Config{sig_name} =~ m/\bVTALRM\b/) {
310 print "ok $_ # Skip: no virtual interval timers\n";
313 use Time::HiRes qw(setitimer getitimer ITIMER_VIRTUAL);
316 my $r = [Time::HiRes::gettimeofday()];
319 $i ? $i-- : setitimer(&ITIMER_VIRTUAL, 0);
320 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
323 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
325 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
326 my $virt = getitimer(&ITIMER_VIRTUAL);
327 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
330 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
332 while (getitimer(&ITIMER_VIRTUAL)) {
334 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
337 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
339 $virt = getitimer(&ITIMER_VIRTUAL);
340 print "not " unless defined $virt && $virt == 0;
343 $SIG{VTALRM} = 'DEFAULT';
346 if ($have_gettimeofday &&
348 use Time::HiRes qw(usleep);
352 my $sleep = 1.5; # seconds
355 $t0 = gettimeofday();
356 $a = abs(sleep($sleep) / $sleep - 1.0);
357 $td = gettimeofday() - $t0;
358 my $ratio = 1.0 + $a;
360 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
362 if ($td < $sleep * (1 + $limit)) {
363 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
365 print "ok 20 # Skip: $msg";
368 $t0 = gettimeofday();
369 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
370 $td = gettimeofday() - $t0;
373 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
375 if ($td < $sleep * (1 + $limit)) {
376 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
378 print "ok 21 # Skip: $msg";
383 print "ok $_ # Skip: no gettimeofday\n";
387 unless ($have_nanosleep) {
391 my $one = CORE::time;
392 nanosleep(10_000_000);
393 my $two = CORE::time;
394 nanosleep(10_000_000);
395 my $three = CORE::time;
396 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
398 unless ($have_gettimeofday) {
402 my $f = Time::HiRes::time();
403 nanosleep(500_000_000);
404 my $f2 = Time::HiRes::time();
406 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
411 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
412 "ok 24\n" : "not ok 24\n";
415 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
416 "ok 25\n" : "not ok 25\n";
420 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
421 "ok 26\n" : "not ok 26\n";
424 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
425 "ok 27\n" : "not ok 27\n";
431 if ($have_nanosleep) {
432 eval { nanosleep(-5) };
433 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
434 "ok 28\n" : "not ok 28\n";
439 if ($have_ualarm && $] >= 5.008001) {
440 # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
441 # Perl changes [18765] and [18770], perl bug [perl #20920]
443 # First we will find the loop size N (a for() loop 0..N-1)
444 # that will take more than T seconds.
447 use Time::HiRes qw(time);
453 for ($i = 0; $i < $N; $i++) { }
456 print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
462 # The time-burner which takes at least T seconds.
464 my $c = @_ ? shift : 1;
467 for ($i = 0; $i < $n; $i++) { }
470 # Then we will setup a periodic timer (the two-argument alarm() of
471 # Time::HiRes, behind the curtains the libc ualarm()) which has
472 # a signal handler that takes so much time (on the first initial
473 # invocation) that the first periodic invocation (second invocation)
474 # will happen before the first invocation has finished. In Perl 5.8.0
475 # the "safe signals" concept was implemented, with unfortunately at least
476 # one bug that caused a core dump on reentering the handler. This bug
477 # was fixed by the time of Perl 5.8.1.
479 # Do not try mixing sleep() and alarm() for testing this.
481 my $a = 0; # Number of alarms we receive.
482 my $A = 2; # Number of alarms we will handle before disarming.
483 # (We may well get $A + 1 alarms.)
487 print "# Alarm $a - ", time(), "\n";
488 alarm(0) if $a >= $A; # Disarm the alarm.
489 $F->(2); # Try burning CPU at least for 2T seconds.
492 use Time::HiRes qw(alarm);
493 alarm($T, $T); # Arm the alarm.
495 $F->(10); # Try burning CPU at least for 10T seconds.
497 print "ok 29\n"; # Not core dumping by now is considered to be the success.
502 if ($have_clock_gettime &&
503 # All implementations of clock_gettime()
504 # are SUPPOSED TO support CLOCK_REALTIME.
505 has_symbol('CLOCK_REALTIME')) {
509 print "# CLOCK_REALTIME: try = $try\n";
510 my $t0 = clock_gettime(&CLOCK_REALTIME);
511 use Time::HiRes qw(sleep);
514 my $t1 = clock_gettime(&CLOCK_REALTIME);
515 if ($t0 > 0 && $t1 > $t0) {
516 print "# t1 = $t1, t0 = $t0\n";
518 my $rt = abs(1 - $dt / $T);
519 print "# dt = $dt, rt = $rt\n";
520 if ($rt <= 2 * $limit) {
525 print "# Error: t0 = $t0, t1 = $t1\n";
527 my $r = rand() + rand();
528 printf "# Sleeping for %.6f seconds...\n", $r;
538 print "# No clock_gettime\n";
542 if ($have_clock_getres) {
543 my $tr = clock_getres();
545 print "ok 31 # tr = $tr\n";
547 print "not ok 31 # tr = $tr\n";
550 print "# No clock_getres\n";
554 if ($have_clock_nanosleep &&
555 has_symbol('CLOCK_REALTIME')) {
557 my $t = clock_nanosleep(&CLOCK_REALTIME, $s);
558 my $r = abs(1 - $t / $s);
559 if ($r < 2 * $limit) {
562 print "not ok 32 # $t = $t, r = $r\n";
565 print "# No clock_nanosleep\n";
571 print "# clock = @clock\n";
573 for (my $j = 0; $j < 1e6; $j++) { }
574 push @clock, clock();
575 print "# clock = @clock\n";
577 if ($clock[1] > $clock[0] &&
578 $clock[2] > $clock[1] &&
579 $clock[3] > $clock[2]) {
585 print "# No clock\n";
590 if (defined $timer_pid) {
591 my $left = $TheEnd - time();
592 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;
593 kill('TERM', $timer_pid); # We are done, the timer can go.
594 unlink("ktrace.out"); # Used in BSD system call tracing.
595 print "# All done.\n";