Upgrade to Time-HiRes-1.82
[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 sub has_symbol {
35     my $symbol = shift;
36     eval "import Time::HiRes qw($symbol)";
37     return 0 unless $@ eq '';
38     return exists &{"Time::HiRes::$symbol"};
39 }
40
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;
47
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;
54
55 use Config;
56
57 use Time::HiRes qw(gettimeofday);
58
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).
62 my $timer_pid;
63 my $TheEnd;
64
65 if ($have_fork) {
66     print "# I am the main process $$, starting the timer process...\n";
67     $timer_pid = fork();
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";
71             sleep($waitfor);
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";
76             exit(0);
77         } else {
78             print "# The timer process $timer_pid launched, continuing testing...\n";
79             $TheEnd = time() + $waitfor;
80         }
81     } else {
82         warn "$0: fork failed: $!\n";
83     }
84 } else {
85     print "# No timer process (need fork)\n";
86 }
87
88 my $xdefine = ''; 
89
90 if (open(XDEFINE, "xdefine")) {
91     chomp($xdefine = <XDEFINE>);
92     close(XDEFINE);
93 }
94
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.  
99 # Try 20%.  
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
106
107 sub skip {
108     map { print "ok $_ # skipped\n" } @_;
109 }
110
111 sub ok {
112     my ($n, $result, @info) = @_;
113     if ($result) {
114         print "ok $n\n";
115     }
116     else {
117         print "not ok $n\n";
118         print "# @info\n" if @info;
119     }
120 }
121
122 unless ($have_gettimeofday) {
123     skip 2..6;
124 }
125 else {
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";
129
130     sleep 1;
131
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";
135
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";
139 }
140
141 unless ($have_usleep) {
142     skip 7..8;
143 }
144 else {
145     use Time::HiRes qw(usleep);
146     my $one = time;
147     usleep(10_000);
148     my $two = time;
149     usleep(10_000);
150     my $three = time;
151     ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
152
153     unless ($have_gettimeofday) {
154         skip 8;
155     }
156     else {
157         my $f = Time::HiRes::time();
158         usleep(500_000);
159         my $f2 = Time::HiRes::time();
160         my $d = $f2 - $f;
161         ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
162     }
163 }
164
165 # Two-arg tv_interval() is always available.
166 {
167     my $f = tv_interval [5, 100_000], [10, 500_000];
168     ok 9, abs($f - 5.4) < 0.001, $f;
169 }
170
171 unless ($have_gettimeofday) {
172     skip 10;
173 }
174 else {
175     my $r = [gettimeofday()];
176     my $f = tv_interval $r;
177     ok 10, $f < 2, $f;
178 }
179
180 unless ($have_usleep && $have_gettimeofday) {
181     skip 11;
182 }
183 else {
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.";
188 }
189
190 unless ($have_ualarm && $have_alarm) {
191     skip 12..13;
192 }
193 else {
194     my $tick = 0;
195     local $SIG{ ALRM } = sub { $tick++ };
196
197     my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
198     my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
199     my $three = time;
200     ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
201     print "# tick = $tick, one = $one, two = $two, three = $three\n";
202
203     $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
204     ok 13, 1;
205     ualarm(0);
206     print "# tick = $tick, one = $one, two = $two, three = $three\n";
207 }
208
209 # Did we even get close?
210
211 unless ($have_gettimeofday) {
212     skip 14;
213 } else {
214  my ($s, $n, $i) = (0);
215  for $i (1 .. 100) {
216      $s += Time::HiRes::time() - time();
217      $n++;
218  }
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";
223 }
224
225 my $has_ualarm = $Config{d_ualarm};
226
227 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
228
229 unless (   defined &Time::HiRes::gettimeofday
230         && defined &Time::HiRes::ualarm
231         && defined &Time::HiRes::usleep
232         && $has_ualarm) {
233     for (15..17) {
234         print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
235     }
236 } else {
237     use Time::HiRes qw(time alarm sleep);
238
239     my ($f, $r, $i, $not, $ok);
240
241     $f = time; 
242     print "# time...$f\n";
243     print "ok 15\n";
244
245     $r = [Time::HiRes::gettimeofday()];
246     sleep (0.5);
247     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
248
249     $r = [Time::HiRes::gettimeofday()];
250     $i = 5;
251     $SIG{ALRM} = "tick";
252     while ($i > 0)
253     {
254         alarm(0.3);
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)";
266             undef $not;
267             last;
268         }
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";
274             last;
275         }
276         $ok = $i;
277     }
278
279     sub tick
280     {
281         $i--;
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";
289             $i = 0;
290         }
291     }
292
293     alarm(0); # can't cancel usig %SIG
294
295     print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
296 }
297
298 unless (   defined &Time::HiRes::setitimer
299         && defined &Time::HiRes::getitimer
300         && has_symbol('ITIMER_VIRTUAL')
301         && $Config{sig_name} =~ m/\bVTALRM\b/) {
302     for (18..19) {
303         print "ok $_ # Skip: no virtual interval timers\n";
304     }
305 } else {
306     use Time::HiRes qw(setitimer getitimer ITIMER_VIRTUAL);
307
308     my $i = 3;
309     my $r = [Time::HiRes::gettimeofday()];
310
311     $SIG{VTALRM} = sub {
312         $i ? $i-- : setitimer(&ITIMER_VIRTUAL, 0);
313         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
314     };  
315
316     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
317
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;
321     print "ok 18\n";
322
323     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
324
325     while (getitimer(&ITIMER_VIRTUAL)) {
326         my $j;
327         for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
328     }
329
330     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
331
332     $virt = getitimer(&ITIMER_VIRTUAL);
333     print "not " unless defined $virt && $virt == 0;
334     print "ok 19\n";
335
336     $SIG{VTALRM} = 'DEFAULT';
337 }
338
339 if ($have_gettimeofday &&
340     $have_usleep) {
341     use Time::HiRes qw(usleep);
342
343     my ($t0, $td);
344
345     my $sleep = 1.5; # seconds
346     my $msg;
347
348     $t0 = gettimeofday();
349     $a = abs(sleep($sleep)        / $sleep         - 1.0);
350     $td = gettimeofday() - $t0;
351     my $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 20 # $msg" : "not ok 20 # $msg";
357     } else {
358         print "ok 20 # Skip: $msg";
359     }
360
361     $t0 = gettimeofday();
362     $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
363     $td = gettimeofday() - $t0;
364     $ratio = 1.0 + $a;
365
366     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
367
368     if ($td < $sleep * (1 + $limit)) {
369         print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
370     } else {
371         print "ok 21 # Skip: $msg";
372     }
373
374 } else {
375     for (20..21) {
376         print "ok $_ # Skip: no gettimeofday\n";
377     }
378 }
379
380 unless ($have_nanosleep) {
381     skip 22..23;
382 }
383 else {
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";
390
391     unless ($have_gettimeofday) {
392         skip 23;
393     }
394     else {
395         my $f = Time::HiRes::time();
396         nanosleep(500_000_000);
397         my $f2 = Time::HiRes::time();
398         my $d = $f2 - $f;
399         ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
400     }
401 }
402
403 eval { sleep(-1) };
404 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
405     "ok 24\n" : "not ok 24\n";
406
407 eval { usleep(-2) };
408 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
409     "ok 25\n" : "not ok 25\n";
410
411 if ($have_ualarm) {
412     eval { alarm(-3) };
413     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
414         "ok 26\n" : "not ok 26\n";
415
416     eval { ualarm(-4) };
417     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
418     "ok 27\n" : "not ok 27\n";
419 } else {
420     skip 26;
421     skip 27;
422 }
423
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";
428 } else {
429     skip 28;
430 }
431
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]
435
436     # First we will find the loop size N (a for() loop 0..N-1)
437     # that will take more than T seconds.
438
439     my $T = 0.01;
440     use Time::HiRes qw(time);
441     my $N = 1024;
442     my $i;
443     N: {
444         do {
445             my $t0 = time();
446             for ($i = 0; $i < $N; $i++) { }
447             my $t1 = time();
448             my $dt = $t1 - $t0;
449             print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
450             last N if $dt > $T;
451             $N *= 2;
452         } while (1);
453     }
454
455     # The time-burner which takes at least T seconds.
456     my $F = sub {
457         my $c = @_ ? shift : 1;
458         my $n = $c * $N;
459         my $i;
460         for ($i = 0; $i < $n; $i++) { }
461     };
462
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.
471
472     # Do not try mixing sleep() and alarm() for testing this.
473
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.)
477
478     $SIG{ALRM} = sub {
479         $a++;
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.
483     }; 
484
485     use Time::HiRes qw(alarm); 
486     alarm($T, $T);  # Arm the alarm.
487
488     $F->(10); # Try burning CPU at least for 10T seconds.
489
490     print "ok 29\n"; # Not core dumping by now is considered to be the success.
491 } else {
492     skip 29;
493 }
494
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);
501     my $T = 1.5;
502     sleep($T);
503     my $t1 = clock_gettime(&CLOCK_REALTIME);
504     if ($t0 > 0 && $t1 > $t0) {
505         print "# t1 = $t1, t0 = $t0\n";
506         my $dt = $t1 - $t0;
507         my $rt = abs(1 - $dt / $T);
508         if ($rt <= $limit) {
509             print "ok 30 # dt = $dt, r = $rt\n";
510         } else {
511             print "not ok 30 # dt = $dt, rt = $rt\n";
512         }
513     } else {
514         print "# Error: t0 = $t0, t1 = $t1\n";
515         skip 30;
516     }
517 } else {
518     print "# No clock_gettime\n";
519     skip 30;
520 }
521
522 if ($have_clock_getres) {
523     my $tr = clock_getres();
524    if ($tr > 0) {
525        print "ok 31 # tr = $tr\n";
526    } else {
527        print "not ok 31 # tr = $tr\n";
528    }
529 } else {
530     print "# No clock_getres\n";
531     skip 31;
532 }
533
534 END {
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";
541     }
542 }
543