Upgrade to Time-HiRes-1.78
[p5sagit/p5-mst-13.2.git] / ext / Time / HiRes / t / HiRes.t
1 #!./perl -w
2
3 BEGIN {
4     if ($ENV{PERL_CORE}) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
7         require Config; import Config;
8         if (" $Config{'extensions'} " !~ m[ Time/HiRes ]) {
9             print "1..0 # Skip -- Perl configured without Time::HiRes module\n";
10             exit 0;
11         }
12     }
13 }
14
15 BEGIN { $| = 1; print "1..31\n"; }
16
17 END { print "not ok 1\n" unless $loaded }
18
19 use Time::HiRes qw(tv_interval);
20
21 $loaded = 1;
22
23 print "ok 1\n";
24
25 use strict;
26
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
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;
40
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;
47
48 use Config;
49
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).
53 my $timer_pid;
54
55 if ($have_fork) {
56     print "# I am the main process $$, starting the timer process...\n";
57     $timer_pid = fork();
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";
61             sleep($waitfor);
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";
66             exit(0);
67         } else {
68             print "# Timer process $timer_pid launched, continuing testing...\n";
69         }
70     } else {
71         warn "$0: fork failed: $!\n";
72     }
73 } else {
74     print "# No timer process (need fork)\n";
75 }
76
77 my $xdefine = ''; 
78
79 if (open(XDEFINE, "xdefine")) {
80     chomp($xdefine = <XDEFINE>);
81     close(XDEFINE);
82 }
83
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.  
88 # Try 20%.  
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
95
96 sub skip {
97     map { print "ok $_ # skipped\n" } @_;
98 }
99
100 sub ok {
101     my ($n, $result, @info) = @_;
102     if ($result) {
103         print "ok $n\n";
104     }
105     else {
106         print "not ok $n\n";
107         print "# @info\n" if @info;
108     }
109 }
110
111 unless ($have_gettimeofday) {
112     skip 2..6;
113 }
114 else {
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";
118
119     sleep 1;
120
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";
124
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";
128 }
129
130 unless ($have_usleep) {
131     skip 7..8;
132 }
133 else {
134     my $one = time;
135     usleep(10_000);
136     my $two = time;
137     usleep(10_000);
138     my $three = time;
139     ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
140
141     unless ($have_gettimeofday) {
142         skip 8;
143     }
144     else {
145         my $f = Time::HiRes::time();
146         usleep(500_000);
147         my $f2 = Time::HiRes::time();
148         my $d = $f2 - $f;
149         ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
150     }
151 }
152
153 # Two-arg tv_interval() is always available.
154 {
155     my $f = tv_interval [5, 100_000], [10, 500_000];
156     ok 9, abs($f - 5.4) < 0.001, $f;
157 }
158
159 unless ($have_gettimeofday) {
160     skip 10;
161 }
162 else {
163     my $r = [gettimeofday()];
164     my $f = tv_interval $r;
165     ok 10, $f < 2, $f;
166 }
167
168 unless ($have_usleep && $have_gettimeofday) {
169     skip 11;
170 }
171 else {
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.";
176 }
177
178 unless ($have_ualarm && $have_alarm) {
179     skip 12..13;
180 }
181 else {
182     my $tick = 0;
183     local $SIG{ ALRM } = sub { $tick++ };
184
185     my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
186     my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
187     my $three = time;
188     ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
189     print "# tick = $tick, one = $one, two = $two, three = $three\n";
190
191     $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
192     ok 13, 1;
193     ualarm(0);
194     print "# tick = $tick, one = $one, two = $two, three = $three\n";
195 }
196
197 # Did we even get close?
198
199 unless ($have_gettimeofday) {
200     skip 14;
201 } else {
202  my ($s, $n, $i) = (0);
203  for $i (1 .. 100) {
204      $s += Time::HiRes::time() - time();
205      $n++;
206  }
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";
211 }
212
213 my $has_ualarm = $Config{d_ualarm};
214
215 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
216
217 unless (   defined &Time::HiRes::gettimeofday
218         && defined &Time::HiRes::ualarm
219         && defined &Time::HiRes::usleep
220         && $has_ualarm) {
221     for (15..17) {
222         print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
223     }
224 } else {
225     use Time::HiRes qw(time alarm sleep);
226
227     my ($f, $r, $i, $not, $ok);
228
229     $f = time; 
230     print "# time...$f\n";
231     print "ok 15\n";
232
233     $r = [Time::HiRes::gettimeofday()];
234     sleep (0.5);
235     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
236
237     $r = [Time::HiRes::gettimeofday()];
238     $i = 5;
239     $SIG{ALRM} = "tick";
240     while ($i > 0)
241     {
242         alarm(0.3);
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)";
254             undef $not;
255             last;
256         }
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";
262             last;
263         }
264         $ok = $i;
265     }
266
267     sub tick
268     {
269         $i--;
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";
277             $i = 0;
278         }
279     }
280
281     alarm(0); # can't cancel usig %SIG
282
283     print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
284 }
285
286 unless (   defined &Time::HiRes::setitimer
287         && defined &Time::HiRes::getitimer
288         && eval 'use Time::HiRes qw(ITIMER_VIRTUAL); print ITIMER_VIRTUAL'
289         && $Config{d_select}
290         && $Config{d_select}
291         && $Config{sig_name} =~ m/\bVTALRM\b/) {
292     for (18..19) {
293         print "ok $_ # Skip: no virtual interval timers\n";
294     }
295 } else {
296     use Time::HiRes qw (setitimer getitimer ITIMER_VIRTUAL);
297
298     my $i = 3;
299     my $r = [Time::HiRes::gettimeofday()];
300
301     $SIG{VTALRM} = sub {
302         $i ? $i-- : setitimer(ITIMER_VIRTUAL, 0);
303         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
304     };  
305
306     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
307
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;
311     print "ok 18\n";
312
313     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
314
315     while (getitimer(ITIMER_VIRTUAL)) {
316         my $j;
317         for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
318     }
319
320     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
321
322     $virt = getitimer(ITIMER_VIRTUAL);
323     print "not " unless defined $virt && $virt == 0;
324     print "ok 19\n";
325
326     $SIG{VTALRM} = 'DEFAULT';
327 }
328
329 if ($have_gettimeofday) {
330     my ($t0, $td);
331
332     my $sleep = 1.5; # seconds
333     my $msg;
334
335     $t0 = gettimeofday();
336     $a = abs(sleep($sleep)        / $sleep         - 1.0);
337     $td = gettimeofday() - $t0;
338     my $ratio = 1.0 + $a;
339
340     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
341
342     if ($td < $sleep * (1 + $limit)) {
343         print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
344     } else {
345         print "ok 20 # Skip: $msg";
346     }
347
348     $t0 = gettimeofday();
349     $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
350     $td = gettimeofday() - $t0;
351     $ratio = 1.0 + $a;
352
353     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
354
355     if ($td < $sleep * (1 + $limit)) {
356         print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
357     } else {
358         print "ok 21 # Skip: $msg";
359     }
360
361 } else {
362     for (20..21) {
363         print "ok $_ # Skip: no gettimeofday\n";
364     }
365 }
366
367 unless ($have_nanosleep) {
368     skip 22..23;
369 }
370 else {
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";
377
378     unless ($have_gettimeofday) {
379         skip 23;
380     }
381     else {
382         my $f = Time::HiRes::time();
383         nanosleep(500_000_000);
384         my $f2 = Time::HiRes::time();
385         my $d = $f2 - $f;
386         ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
387     }
388 }
389
390 eval { sleep(-1) };
391 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
392     "ok 24\n" : "not ok 24\n";
393
394 eval { usleep(-2) };
395 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
396     "ok 25\n" : "not ok 25\n";
397
398 if ($have_ualarm) {
399     eval { alarm(-3) };
400     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
401         "ok 26\n" : "not ok 26\n";
402
403     eval { ualarm(-4) };
404     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
405     "ok 27\n" : "not ok 27\n";
406 } else {
407     skip 26;
408     skip 27;
409 }
410
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";
415 } else {
416     skip 28;
417 }
418
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]
422
423     # First we will find the loop size N (a for() loop 0..N-1)
424     # that will take more than T seconds.
425
426     my $T = 0.01;
427     use Time::HiRes qw(time);
428     my $N = 1024;
429     my $i;
430     N: {
431         do {
432             my $t0 = time();
433             for ($i = 0; $i < $N; $i++) { }
434             my $t1 = time();
435             my $dt = $t1 - $t0;
436             print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
437             last N if $dt > $T;
438             $N *= 2;
439         } while (1);
440     }
441
442     # The time-burner which takes at least T seconds.
443     my $F = sub {
444         my $c = @_ ? shift : 1;
445         my $n = $c * $N;
446         my $i;
447         for ($i = 0; $i < $n; $i++) { }
448     };
449
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.
458
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.)
462
463     $SIG{ALRM} = sub {
464         $a++;
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.
468     }; 
469
470     use Time::HiRes qw(alarm); 
471     alarm($T, $T);  # Arm the alarm.
472
473     $F->(10); # Try burning CPU at least for 10T seconds.
474
475     print "ok 29\n"; # Not core dumping by now is considered to be the success.
476 } else {
477     skip 29;
478 }
479
480 if ($have_clock_gettime) {
481     # All implementations are SUPPOSED TO support CLOCK_REALTIME...
482     eval 'use Time::HiRes qw(CLOCK_REALTIME)';
483     unless ($@) {
484         my $t0 = clock_gettime(&CLOCK_REALTIME);
485         use Time::HiRes qw(sleep);
486         my $T = 0.1;
487         sleep($T);
488         my $t1 = clock_gettime(&CLOCK_REALTIME);
489         if ($t0 > 0 && $t1) {
490             print "# t1 = $t1, t0 = $t0\n";
491             my $dt = $t1 - $t0;
492             my $rt = abs(1 - $dt / $T);
493             if ($rt <= 0.25) { # Allow 25% jitter.
494                 print "ok 30 # dt = $dt, r = $rt\n";
495             } else {
496                 print "not ok 30 # dt = $dt, rt = $rt\n";
497             }
498         } else {
499             print "# Error '$!'\n";
500             skip 30;
501         }
502     } else {
503         print "# No CLOCK_REALTIME ($@)\n";
504         skip 30;
505     }
506 } else {
507     print "# No clock_gettime\n";
508     skip 30;
509 }
510
511 if ($have_clock_getres) {
512     my $tr = clock_getres();
513    if ($tr > 0) {
514        print "ok 31 # tr = $tr\n";
515    } else {
516        print "not ok 31 # tr = $tr\n";
517    }
518 } else {
519     print "# No clock_getres\n";
520     skip 31;
521 }
522
523 END {
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";
529     }
530 }
531