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 "import Time::HiRes qw($symbol)";
37 return 0 unless $@ eq '';
38 return exists &{"Time::HiRes::$symbol"};
41 printf "# have_gettimeofday = %d\n", $have_gettimeofday;
42 printf "# have_usleep = %d\n", $have_usleep;
43 printf "# have_nanosleep = %d\n", $have_nanosleep;
44 printf "# have_ualarm = %d\n", $have_ualarm;
45 printf "# have_clock_gettime = %d\n", $have_clock_gettime;
46 printf "# have_clock_getres = %d\n", $have_clock_getres;
48 import Time::HiRes 'gettimeofday' if $have_gettimeofday;
49 import Time::HiRes 'usleep' if $have_usleep;
50 import Time::HiRes 'nanosleep' if $have_nanosleep;
51 import Time::HiRes 'ualarm' if $have_ualarm;
52 import Time::HiRes 'clock_gettime' if $have_clock_gettime;
53 import Time::HiRes 'clock_getres' if $have_clock_getres;
57 my $have_alarm = $Config{d_alarm};
58 my $have_fork = $Config{d_fork};
59 my $waitfor = 60; # 10-20 seconds is normal (load affects this).
64 print "# I am the main process $$, starting the timer process...\n";
66 if (defined $timer_pid) {
67 if ($timer_pid == 0) { # We are the kid, set up the timer.
68 print "# I am the timer process $$, sleeping for $waitfor seconds...\n";
70 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
71 print "# Terminating the main process...\n";
72 kill('TERM', getppid());
73 print "# This is the timer process $$, over and out.\n";
76 print "# The timer process $timer_pid launched, continuing testing...\n";
77 $TheEnd = time() + $waitfor;
80 warn "$0: fork failed: $!\n";
83 print "# No timer process (need fork)\n";
88 if (open(XDEFINE, "xdefine")) {
89 chomp($xdefine = <XDEFINE>);
93 # Ideally, we'd like to test that the timers are rather precise.
94 # However, if the system is busy, there are no guarantees on how
95 # quickly we will return. This limit used to be 10%, but that
96 # was occasionally triggered falsely.
98 # Another possibility might be to print "ok" if the test completes fine
99 # with (say) 10% slosh, "skip - system may have been busy?" if the test
100 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
101 # consider changing over to test.pl at the same time.
102 # --A.D., Nov 27, 2001
103 my $limit = 0.20; # 20% is acceptable slosh for testing timers
106 map { print "ok $_ # skipped\n" } @_;
110 my ($n, $result, @info) = @_;
116 print "# @info\n" if @info;
120 unless ($have_gettimeofday) {
124 my @one = gettimeofday();
125 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
126 ok 3, $one[0] > 850_000_000, "@one too small";
130 my @two = gettimeofday();
131 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
132 "@two is not greater than @one";
134 my $f = Time::HiRes::time();
135 ok 5, $f > 850_000_000, "$f too small";
136 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
139 unless ($have_usleep) {
148 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
150 unless ($have_gettimeofday) {
154 my $f = Time::HiRes::time();
156 my $f2 = Time::HiRes::time();
158 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
162 # Two-arg tv_interval() is always available.
164 my $f = tv_interval [5, 100_000], [10, 500_000];
165 ok 9, abs($f - 5.4) < 0.001, $f;
168 unless ($have_gettimeofday) {
172 my $r = [gettimeofday()];
173 my $f = tv_interval $r;
177 unless ($have_usleep && $have_gettimeofday) {
181 my $r = [gettimeofday()];
182 Time::HiRes::sleep( 0.5 );
183 my $f = tv_interval $r;
184 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
187 unless ($have_ualarm && $have_alarm) {
192 local $SIG{ ALRM } = sub { $tick++ };
194 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
195 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
197 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
198 print "# tick = $tick, one = $one, two = $two, three = $three\n";
200 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
203 print "# tick = $tick, one = $one, two = $two, three = $three\n";
206 # Did we even get close?
208 unless ($have_gettimeofday) {
211 my ($s, $n, $i) = (0);
213 $s += Time::HiRes::time() - time();
216 # $s should be, at worst, equal to $n
217 # (time() may be rounding down, up, or closest)
218 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
219 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
222 my $has_ualarm = $Config{d_ualarm};
224 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
226 unless ( defined &Time::HiRes::gettimeofday
227 && defined &Time::HiRes::ualarm
228 && defined &Time::HiRes::usleep
231 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
234 use Time::HiRes qw(time alarm sleep);
236 my ($f, $r, $i, $not, $ok);
239 print "# time...$f\n";
242 $r = [Time::HiRes::gettimeofday()];
244 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
246 $r = [Time::HiRes::gettimeofday()];
252 select (undef, undef, undef, 3);
253 my $ival = Time::HiRes::tv_interval ($r);
254 print "# Select returned! $i $ival\n";
255 print "# ", abs($ival/3 - 1), "\n";
256 # Whether select() gets restarted after signals is
257 # implementation dependent. If it is restarted, we
258 # will get about 3.3 seconds: 3 from the select, 0.3
259 # from the alarm. If this happens, let's just skip
260 # this particular test. --jhi
261 if (abs($ival/3.3 - 1) < $limit) {
262 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
266 my $exp = 0.3 * (5 - $i);
267 # This test is more sensitive, so impose a softer limit.
268 if (abs($ival/$exp - 1) > 3*$limit) {
269 my $ratio = abs($ival/$exp);
270 $not = "while: $exp sleep took $ival ratio $ratio";
279 my $ival = Time::HiRes::tv_interval ($r);
280 print "# Tick! $i $ival\n";
281 my $exp = 0.3 * (5 - $i);
282 # This test is more sensitive, so impose a softer limit.
283 if (abs($ival/$exp - 1) > 3*$limit) {
284 my $ratio = abs($ival/$exp);
285 $not = "tick: $exp sleep took $ival ratio $ratio";
290 alarm(0); # can't cancel usig %SIG
292 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
295 unless ( defined &Time::HiRes::setitimer
296 && defined &Time::HiRes::getitimer
297 && has_symbol('ITIMER_VIRTUAL')
298 && $Config{sig_name} =~ m/\bVTALRM\b/) {
300 print "ok $_ # Skip: no virtual interval timers\n";
303 use Time::HiRes qw(setitimer getitimer ITIMER_VIRTUAL);
306 my $r = [Time::HiRes::gettimeofday()];
309 $i ? $i-- : setitimer(&ITIMER_VIRTUAL, 0);
310 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
313 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
315 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
316 my $virt = getitimer(&ITIMER_VIRTUAL);
317 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
320 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
322 while (getitimer(&ITIMER_VIRTUAL)) {
324 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
327 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
329 $virt = getitimer(&ITIMER_VIRTUAL);
330 print "not " unless defined $virt && $virt == 0;
333 $SIG{VTALRM} = 'DEFAULT';
336 if ($have_gettimeofday) {
339 my $sleep = 1.5; # seconds
342 $t0 = gettimeofday();
343 $a = abs(sleep($sleep) / $sleep - 1.0);
344 $td = gettimeofday() - $t0;
345 my $ratio = 1.0 + $a;
347 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
349 if ($td < $sleep * (1 + $limit)) {
350 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
352 print "ok 20 # Skip: $msg";
355 $t0 = gettimeofday();
356 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
357 $td = gettimeofday() - $t0;
360 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
362 if ($td < $sleep * (1 + $limit)) {
363 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
365 print "ok 21 # Skip: $msg";
370 print "ok $_ # Skip: no gettimeofday\n";
374 unless ($have_nanosleep) {
378 my $one = CORE::time;
379 nanosleep(10_000_000);
380 my $two = CORE::time;
381 nanosleep(10_000_000);
382 my $three = CORE::time;
383 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
385 unless ($have_gettimeofday) {
389 my $f = Time::HiRes::time();
390 nanosleep(500_000_000);
391 my $f2 = Time::HiRes::time();
393 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
398 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
399 "ok 24\n" : "not ok 24\n";
402 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
403 "ok 25\n" : "not ok 25\n";
407 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
408 "ok 26\n" : "not ok 26\n";
411 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
412 "ok 27\n" : "not ok 27\n";
418 if ($have_nanosleep) {
419 eval { nanosleep(-5) };
420 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
421 "ok 28\n" : "not ok 28\n";
426 if ($have_ualarm && $] >= 5.008001) {
427 # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
428 # Perl changes [18765] and [18770], perl bug [perl #20920]
430 # First we will find the loop size N (a for() loop 0..N-1)
431 # that will take more than T seconds.
434 use Time::HiRes qw(time);
440 for ($i = 0; $i < $N; $i++) { }
443 print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
449 # The time-burner which takes at least T seconds.
451 my $c = @_ ? shift : 1;
454 for ($i = 0; $i < $n; $i++) { }
457 # Then we will setup a periodic timer (the two-argument alarm() of
458 # Time::HiRes, behind the curtains the libc ualarm()) which has
459 # a signal handler that takes so much time (on the first initial
460 # invocation) that the first periodic invocation (second invocation)
461 # will happen before the first invocation has finished. In Perl 5.8.0
462 # the "safe signals" concept was implemented, with unfortunately at least
463 # one bug that caused a core dump on reentering the handler. This bug
464 # was fixed by the time of Perl 5.8.1.
466 # Do not try mixing sleep() and alarm() for testing this.
468 my $a = 0; # Number of alarms we receive.
469 my $A = 2; # Number of alarms we will handle before disarming.
470 # (We may well get $A + 1 alarms.)
474 print "# Alarm $a - ", time(), "\n";
475 alarm(0) if $a >= $A; # Disarm the alarm.
476 $F->(2); # Try burning CPU at least for 2T seconds.
479 use Time::HiRes qw(alarm);
480 alarm($T, $T); # Arm the alarm.
482 $F->(10); # Try burning CPU at least for 10T seconds.
484 print "ok 29\n"; # Not core dumping by now is considered to be the success.
489 if ($have_clock_gettime &&
490 # All implementations of clock_gettime()
491 # are SUPPOSED TO support CLOCK_REALTIME.
492 has_symbol('CLOCK_REALTIME')) {
493 my $t0 = clock_gettime(&CLOCK_REALTIME);
494 use Time::HiRes qw(sleep);
497 my $t1 = clock_gettime(&CLOCK_REALTIME);
498 if ($t0 > 0 && $t1 > $t0) {
499 print "# t1 = $t1, t0 = $t0\n";
501 my $rt = abs(1 - $dt / $T);
503 print "ok 30 # dt = $dt, r = $rt\n";
505 print "not ok 30 # dt = $dt, rt = $rt\n";
508 print "# Error: t0 = $t0, t1 = $t1\n";
512 print "# No clock_gettime\n";
516 if ($have_clock_getres) {
517 my $tr = clock_getres();
519 print "ok 31 # tr = $tr\n";
521 print "not ok 31 # tr = $tr\n";
524 print "# No clock_getres\n";
529 if (defined $timer_pid) {
530 my $left = $TheEnd - time();
531 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;
532 kill('TERM', $timer_pid); # We are done, the timer can go.
533 unlink("ktrace.out"); # Used in BSD system call tracing.
534 print "# All done.\n";