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 && eval 'use Time::HiRes qw(ITIMER_VIRTUAL); print ITIMER_VIRTUAL'
291 && $Config{sig_name} =~ m/\bVTALRM\b/) {
293 print "ok $_ # Skip: no virtual interval timers\n";
296 use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
299 my $r = [Time::HiRes::gettimeofday()];
302 $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
303 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
306 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
308 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
309 my $virt = getitimer(ITIMER_VIRTUAL);
310 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
313 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
315 while (getitimer(ITIMER_VIRTUAL)) {
317 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
320 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
322 $virt = getitimer(ITIMER_VIRTUAL);
323 print "not " unless defined $virt && $virt == 0;
326 $SIG{VTALRM} = 'DEFAULT';
329 if ($have_gettimeofday) {
332 my $sleep = 1.5; # seconds
335 $t0 = gettimeofday();
336 $a = abs(sleep($sleep) / $sleep - 1.0);
337 $td = gettimeofday() - $t0;
338 my $ratio = 1.0 + $a;
340 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
342 if ($td < $sleep * (1 + $limit)) {
343 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
345 print "ok 20 # Skip: $msg";
348 $t0 = gettimeofday();
349 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
350 $td = gettimeofday() - $t0;
353 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
355 if ($td < $sleep * (1 + $limit)) {
356 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
358 print "ok 21 # Skip: $msg";
363 print "ok $_ # Skip: no gettimeofday\n";
367 unless ($have_nanosleep) {
371 my $one = CORE::time;
372 nanosleep(10_000_000);
373 my $two = CORE::time;
374 nanosleep(10_000_000);
375 my $three = CORE::time;
376 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
378 unless ($have_gettimeofday) {
382 my $f = Time::HiRes::time();
383 nanosleep(500_000_000);
384 my $f2 = Time::HiRes::time();
386 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
391 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
392 "ok 24\n" : "not ok 24\n";
395 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
396 "ok 25\n" : "not ok 25\n";
400 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
401 "ok 26\n" : "not ok 26\n";
404 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
405 "ok 27\n" : "not ok 27\n";
411 if ($have_nanosleep) {
412 eval { nanosleep(-5) };
413 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
414 "ok 28\n" : "not ok 28\n";
419 if ($have_ualarm && $] >= 5.008001) {
420 # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
421 # Perl changes [18765] and [18770], perl bug [perl #20920]
423 # First we will find the loop size N (a for() loop 0..N-1)
424 # that will take more than T seconds.
427 use Time::HiRes qw(time);
433 for ($i = 0; $i < $N; $i++) { }
436 print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
442 # The time-burner which takes at least T seconds.
444 my $c = @_ ? shift : 1;
447 for ($i = 0; $i < $n; $i++) { }
450 # Then we will setup a periodic timer (the two-argument alarm() of
451 # Time::HiRes, behind the curtains the libc ualarm()) which has
452 # a signal handler that takes so much time (on the first initial
453 # invocation) that the first periodic invocation (second invocation)
454 # will happen before the first invocation has finished. In Perl 5.8.0
455 # the "safe signals" concept was implemented, with unfortunately at least
456 # one bug that caused a core dump on reentering the handler. This bug
457 # was fixed by the time of Perl 5.8.1.
459 my $a = 0; # Number of alarms we receive.
460 my $A = 2; # Number of alarms we will handle before disarming.
461 # (We may well get $A + 1 alarms.)
465 print "# Alarm $a - ", time(), "\n";
466 alarm(0) if $a >= $A; # Disarm the alarm.
467 $F->(2); # Try burning CPU at least for 2T seconds.
470 use Time::HiRes qw(alarm);
471 alarm($T, $T); # Arm the alarm.
473 $F->(10); # Try burning CPU at least for 10T seconds.
475 print "ok 29\n"; # Not core dumping by now is considered to be the success.
480 if ($have_clock_gettime) {
481 # All implementations are SUPPOSED TO support CLOCK_REALTIME...
482 eval 'use Time::HiRes qw(CLOCK_REALTIME)';
484 my $t0 = clock_gettime(&CLOCK_REALTIME);
485 use Time::HiRes qw(sleep);
488 my $t1 = clock_gettime(&CLOCK_REALTIME);
489 if ($t0 > 0 && $t1) {
490 print "# t1 = $t1, t0 = $t0\n";
492 my $rt = abs(1 - $dt / $T);
493 if ($rt <= 0.25) { # Allow 25% jitter.
494 print "ok 30 # dt = $dt, r = $rt\n";
496 print "not ok 30 # dt = $dt, rt = $rt\n";
499 print "# Error '$!'\n";
503 print "# No CLOCK_REALTIME ($@)\n";
507 print "# No clock_gettime\n";
511 if ($have_clock_getres) {
512 my $tr = clock_getres();
514 print "ok 31 # tr = $tr\n";
516 print "not ok 31 # tr = $tr\n";
519 print "# No clock_getres\n";
524 if (defined $timer_pid) {
525 print "# I am the main process $$, terminating the timer process $timer_pid.\n";
526 kill('TERM', $timer_pid); # We are done, the timer can go.
527 unlink("ktrace.out"); # Used in BSD system call tracing.
528 print "# All done.\n";