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 use Time::HiRes qw(gettimeofday);
59 my $have_alarm = $Config{d_alarm};
60 my $have_fork = $Config{d_fork};
61 my $waitfor = 60; # 10-20 seconds is normal (load affects this).
66 print "# I am the main process $$, starting the timer process...\n";
68 if (defined $timer_pid) {
69 if ($timer_pid == 0) { # We are the kid, set up the timer.
70 print "# I am the timer process $$, sleeping for $waitfor seconds...\n";
72 warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
73 print "# Terminating the main process...\n";
74 kill('TERM', getppid());
75 print "# This is the timer process $$, over and out.\n";
78 print "# The timer process $timer_pid launched, continuing testing...\n";
79 $TheEnd = time() + $waitfor;
82 warn "$0: fork failed: $!\n";
85 print "# No timer process (need fork)\n";
90 if (open(XDEFINE, "xdefine")) {
91 chomp($xdefine = <XDEFINE>);
95 # Ideally, we'd like to test that the timers are rather precise.
96 # However, if the system is busy, there are no guarantees on how
97 # quickly we will return. This limit used to be 10%, but that
98 # was occasionally triggered falsely.
100 # Another possibility might be to print "ok" if the test completes fine
101 # with (say) 10% slosh, "skip - system may have been busy?" if the test
102 # completes fine with (say) 30% slosh, and fail otherwise. If you do that,
103 # consider changing over to test.pl at the same time.
104 # --A.D., Nov 27, 2001
105 my $limit = 0.20; # 20% is acceptable slosh for testing timers
108 map { print "ok $_ # skipped\n" } @_;
112 my ($n, $result, @info) = @_;
118 print "# @info\n" if @info;
122 unless ($have_gettimeofday) {
126 my @one = gettimeofday();
127 ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
128 ok 3, $one[0] > 850_000_000, "@one too small";
132 my @two = gettimeofday();
133 ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
134 "@two is not greater than @one";
136 my $f = Time::HiRes::time();
137 ok 5, $f > 850_000_000, "$f too small";
138 ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
141 unless ($have_usleep) {
145 use Time::HiRes qw(usleep);
151 ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
153 unless ($have_gettimeofday) {
157 my $f = Time::HiRes::time();
159 my $f2 = Time::HiRes::time();
161 ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
165 # Two-arg tv_interval() is always available.
167 my $f = tv_interval [5, 100_000], [10, 500_000];
168 ok 9, abs($f - 5.4) < 0.001, $f;
171 unless ($have_gettimeofday) {
175 my $r = [gettimeofday()];
176 my $f = tv_interval $r;
180 unless ($have_usleep && $have_gettimeofday) {
184 my $r = [ gettimeofday() ];
185 Time::HiRes::sleep( 0.5 );
186 my $f = tv_interval $r;
187 ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
190 unless ($have_ualarm && $have_alarm) {
195 local $SIG{ ALRM } = sub { $tick++ };
197 my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
198 my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
200 ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
201 print "# tick = $tick, one = $one, two = $two, three = $three\n";
203 $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
206 print "# tick = $tick, one = $one, two = $two, three = $three\n";
209 # Did we even get close?
211 unless ($have_gettimeofday) {
214 my ($s, $n, $i) = (0);
216 $s += Time::HiRes::time() - time();
219 # $s should be, at worst, equal to $n
220 # (time() may be rounding down, up, or closest)
221 ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
222 print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
225 my $has_ualarm = $Config{d_ualarm};
227 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
229 unless ( defined &Time::HiRes::gettimeofday
230 && defined &Time::HiRes::ualarm
231 && defined &Time::HiRes::usleep
234 print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
237 use Time::HiRes qw(time alarm sleep);
239 my ($f, $r, $i, $not, $ok);
242 print "# time...$f\n";
245 $r = [Time::HiRes::gettimeofday()];
247 print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
249 $r = [Time::HiRes::gettimeofday()];
255 select (undef, undef, undef, 3);
256 my $ival = Time::HiRes::tv_interval ($r);
257 print "# Select returned! $i $ival\n";
258 print "# ", abs($ival/3 - 1), "\n";
259 # Whether select() gets restarted after signals is
260 # implementation dependent. If it is restarted, we
261 # will get about 3.3 seconds: 3 from the select, 0.3
262 # from the alarm. If this happens, let's just skip
263 # this particular test. --jhi
264 if (abs($ival/3.3 - 1) < $limit) {
265 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
269 my $exp = 0.3 * (5 - $i);
270 # This test is more sensitive, so impose a softer limit.
271 if (abs($ival/$exp - 1) > 3*$limit) {
272 my $ratio = abs($ival/$exp);
273 $not = "while: $exp sleep took $ival ratio $ratio";
282 my $ival = Time::HiRes::tv_interval ($r);
283 print "# Tick! $i $ival\n";
284 my $exp = 0.3 * (5 - $i);
285 # This test is more sensitive, so impose a softer limit.
286 if (abs($ival/$exp - 1) > 3*$limit) {
287 my $ratio = abs($ival/$exp);
288 $not = "tick: $exp sleep took $ival ratio $ratio";
293 alarm(0); # can't cancel usig %SIG
295 print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
298 unless ( defined &Time::HiRes::setitimer
299 && defined &Time::HiRes::getitimer
300 && has_symbol('ITIMER_VIRTUAL')
301 && $Config{sig_name} =~ m/\bVTALRM\b/) {
303 print "ok $_ # Skip: no virtual interval timers\n";
306 use Time::HiRes qw(setitimer getitimer ITIMER_VIRTUAL);
309 my $r = [Time::HiRes::gettimeofday()];
312 $i ? $i-- : setitimer(&ITIMER_VIRTUAL, 0);
313 print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
316 print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
318 # Assume interval timer granularity of $limit * 0.5 seconds. Too bold?
319 my $virt = getitimer(&ITIMER_VIRTUAL);
320 print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
323 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
325 while (getitimer(&ITIMER_VIRTUAL)) {
327 for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
330 print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
332 $virt = getitimer(&ITIMER_VIRTUAL);
333 print "not " unless defined $virt && $virt == 0;
336 $SIG{VTALRM} = 'DEFAULT';
339 if ($have_gettimeofday &&
341 use Time::HiRes qw(usleep);
345 my $sleep = 1.5; # seconds
348 $t0 = gettimeofday();
349 $a = abs(sleep($sleep) / $sleep - 1.0);
350 $td = gettimeofday() - $t0;
351 my $ratio = 1.0 + $a;
353 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
355 if ($td < $sleep * (1 + $limit)) {
356 print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
358 print "ok 20 # Skip: $msg";
361 $t0 = gettimeofday();
362 $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
363 $td = gettimeofday() - $t0;
366 $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
368 if ($td < $sleep * (1 + $limit)) {
369 print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
371 print "ok 21 # Skip: $msg";
376 print "ok $_ # Skip: no gettimeofday\n";
380 unless ($have_nanosleep) {
384 my $one = CORE::time;
385 nanosleep(10_000_000);
386 my $two = CORE::time;
387 nanosleep(10_000_000);
388 my $three = CORE::time;
389 ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
391 unless ($have_gettimeofday) {
395 my $f = Time::HiRes::time();
396 nanosleep(500_000_000);
397 my $f2 = Time::HiRes::time();
399 ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
404 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
405 "ok 24\n" : "not ok 24\n";
408 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
409 "ok 25\n" : "not ok 25\n";
413 print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
414 "ok 26\n" : "not ok 26\n";
417 print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
418 "ok 27\n" : "not ok 27\n";
424 if ($have_nanosleep) {
425 eval { nanosleep(-5) };
426 print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
427 "ok 28\n" : "not ok 28\n";
432 if ($have_ualarm && $] >= 5.008001) {
433 # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
434 # Perl changes [18765] and [18770], perl bug [perl #20920]
436 # First we will find the loop size N (a for() loop 0..N-1)
437 # that will take more than T seconds.
440 use Time::HiRes qw(time);
446 for ($i = 0; $i < $N; $i++) { }
449 print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
455 # The time-burner which takes at least T seconds.
457 my $c = @_ ? shift : 1;
460 for ($i = 0; $i < $n; $i++) { }
463 # Then we will setup a periodic timer (the two-argument alarm() of
464 # Time::HiRes, behind the curtains the libc ualarm()) which has
465 # a signal handler that takes so much time (on the first initial
466 # invocation) that the first periodic invocation (second invocation)
467 # will happen before the first invocation has finished. In Perl 5.8.0
468 # the "safe signals" concept was implemented, with unfortunately at least
469 # one bug that caused a core dump on reentering the handler. This bug
470 # was fixed by the time of Perl 5.8.1.
472 # Do not try mixing sleep() and alarm() for testing this.
474 my $a = 0; # Number of alarms we receive.
475 my $A = 2; # Number of alarms we will handle before disarming.
476 # (We may well get $A + 1 alarms.)
480 print "# Alarm $a - ", time(), "\n";
481 alarm(0) if $a >= $A; # Disarm the alarm.
482 $F->(2); # Try burning CPU at least for 2T seconds.
485 use Time::HiRes qw(alarm);
486 alarm($T, $T); # Arm the alarm.
488 $F->(10); # Try burning CPU at least for 10T seconds.
490 print "ok 29\n"; # Not core dumping by now is considered to be the success.
495 if ($have_clock_gettime &&
496 # All implementations of clock_gettime()
497 # are SUPPOSED TO support CLOCK_REALTIME.
498 has_symbol('CLOCK_REALTIME')) {
499 my $t0 = clock_gettime(&CLOCK_REALTIME);
500 use Time::HiRes qw(sleep);
503 my $t1 = clock_gettime(&CLOCK_REALTIME);
504 if ($t0 > 0 && $t1 > $t0) {
505 print "# t1 = $t1, t0 = $t0\n";
507 my $rt = abs(1 - $dt / $T);
509 print "ok 30 # dt = $dt, r = $rt\n";
511 print "not ok 30 # dt = $dt, rt = $rt\n";
514 print "# Error: t0 = $t0, t1 = $t1\n";
518 print "# No clock_gettime\n";
522 if ($have_clock_getres) {
523 my $tr = clock_getres();
525 print "ok 31 # tr = $tr\n";
527 print "not ok 31 # tr = $tr\n";
530 print "# No clock_getres\n";
535 if (defined $timer_pid) {
536 my $left = $TheEnd - time();
537 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;
538 kill('TERM', $timer_pid); # We are done, the timer can go.
539 unlink("ktrace.out"); # Used in BSD system call tracing.
540 print "# All done.\n";