Upgrade to Time::HiRes 1.83
[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 "use Time::HiRes qw($symbol)";
37     return 0 unless $@ eq '';
38     eval "my \$a = $symbol";
39     return $@ eq '';
40 }
41
42 printf "# have_gettimeofday  = %d\n", $have_gettimeofday;
43 printf "# have_usleep        = %d\n", $have_usleep;
44 printf "# have_nanosleep     = %d\n", $have_nanosleep;
45 printf "# have_ualarm        = %d\n", $have_ualarm;
46 printf "# have_clock_gettime = %d\n", $have_clock_gettime;
47 printf "# have_clock_getres  = %d\n", $have_clock_getres;
48
49 import Time::HiRes 'gettimeofday'       if $have_gettimeofday;
50 import Time::HiRes 'usleep'             if $have_usleep;
51 import Time::HiRes 'nanosleep'          if $have_nanosleep;
52 import Time::HiRes 'ualarm'             if $have_ualarm;
53 import Time::HiRes 'clock_gettime'      if $have_clock_gettime;
54 import Time::HiRes 'clock_getres'       if $have_clock_getres;
55
56 use Config;
57
58 use Time::HiRes qw(gettimeofday);
59
60 my $have_alarm = $Config{d_alarm};
61 my $have_fork  = $Config{d_fork};
62 my $waitfor = 60; # 10-20 seconds is normal (load affects this).
63 my $timer_pid;
64 my $TheEnd;
65
66 if ($have_fork) {
67     print "# I am the main process $$, starting the timer process...\n";
68     $timer_pid = fork();
69     if (defined $timer_pid) {
70         if ($timer_pid == 0) { # We are the kid, set up the timer.
71             print "# I am the timer process $$, sleeping for $waitfor seconds...\n";
72             sleep($waitfor);
73             warn "\n$0: overall time allowed for tests (${waitfor}s) exceeded!\n";
74             print "# Terminating the main process...\n";
75             kill('TERM', getppid());
76             print "# This is the timer process $$, over and out.\n";
77             exit(0);
78         } else {
79             print "# The timer process $timer_pid launched, continuing testing...\n";
80             $TheEnd = time() + $waitfor;
81         }
82     } else {
83         warn "$0: fork failed: $!\n";
84     }
85 } else {
86     print "# No timer process (need fork)\n";
87 }
88
89 my $xdefine = ''; 
90
91 if (open(XDEFINE, "xdefine")) {
92     chomp($xdefine = <XDEFINE>);
93     close(XDEFINE);
94 }
95
96 # Ideally, we'd like to test that the timers are rather precise.
97 # However, if the system is busy, there are no guarantees on how
98 # quickly we will return.  This limit used to be 10%, but that
99 # was occasionally triggered falsely.  
100 # Try 20%.  
101 # Another possibility might be to print "ok" if the test completes fine
102 # with (say) 10% slosh, "skip - system may have been busy?" if the test
103 # completes fine with (say) 30% slosh, and fail otherwise.  If you do that,
104 # consider changing over to test.pl at the same time.
105 # --A.D., Nov 27, 2001
106 my $limit = 0.20; # 20% is acceptable slosh for testing timers
107
108 sub skip {
109     map { print "ok $_ # skipped\n" } @_;
110 }
111
112 sub ok {
113     my ($n, $result, @info) = @_;
114     if ($result) {
115         print "ok $n\n";
116     }
117     else {
118         print "not ok $n\n";
119         print "# @info\n" if @info;
120     }
121 }
122
123 unless ($have_gettimeofday) {
124     skip 2..6;
125 }
126 else {
127     my @one = gettimeofday();
128     ok 2, @one == 2, 'gettimeofday returned ', 0+@one, ' args';
129     ok 3, $one[0] > 850_000_000, "@one too small";
130
131     sleep 1;
132
133     my @two = gettimeofday();
134     ok 4, ($two[0] > $one[0] || ($two[0] == $one[0] && $two[1] > $one[1])),
135             "@two is not greater than @one";
136
137     my $f = Time::HiRes::time();
138     ok 5, $f > 850_000_000, "$f too small";
139     ok 6, $f - $two[0] < 2, "$f - $two[0] >= 2";
140 }
141
142 unless ($have_usleep) {
143     skip 7..8;
144 }
145 else {
146     use Time::HiRes qw(usleep);
147     my $one = time;
148     usleep(10_000);
149     my $two = time;
150     usleep(10_000);
151     my $three = time;
152     ok 7, $one == $two || $two == $three, "slept too long, $one $two $three";
153
154     unless ($have_gettimeofday) {
155         skip 8;
156     }
157     else {
158         my $f = Time::HiRes::time();
159         usleep(500_000);
160         my $f2 = Time::HiRes::time();
161         my $d = $f2 - $f;
162         ok 8, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
163     }
164 }
165
166 # Two-arg tv_interval() is always available.
167 {
168     my $f = tv_interval [5, 100_000], [10, 500_000];
169     ok 9, abs($f - 5.4) < 0.001, $f;
170 }
171
172 unless ($have_gettimeofday) {
173     skip 10;
174 }
175 else {
176     my $r = [gettimeofday()];
177     my $f = tv_interval $r;
178     ok 10, $f < 2, $f;
179 }
180
181 unless ($have_usleep && $have_gettimeofday) {
182     skip 11;
183 }
184 else {
185     my $r = [ gettimeofday() ];
186     Time::HiRes::sleep( 0.5 );
187     my $f = tv_interval $r;
188     ok 11, $f > 0.4 && $f < 0.9, "slept $f instead of 0.5 secs.";
189 }
190
191 unless ($have_ualarm && $have_alarm) {
192     skip 12..13;
193 }
194 else {
195     my $tick = 0;
196     local $SIG{ ALRM } = sub { $tick++ };
197
198     my $one = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
199     my $two = time; $tick = 0; ualarm(10_000); while ($tick == 0) { }
200     my $three = time;
201     ok 12, $one == $two || $two == $three, "slept too long, $one $two $three";
202     print "# tick = $tick, one = $one, two = $two, three = $three\n";
203
204     $tick = 0; ualarm(10_000, 10_000); while ($tick < 3) { }
205     ok 13, 1;
206     ualarm(0);
207     print "# tick = $tick, one = $one, two = $two, three = $three\n";
208 }
209
210 # Did we even get close?
211
212 unless ($have_gettimeofday) {
213     skip 14;
214 } else {
215  my ($s, $n, $i) = (0);
216  for $i (1 .. 100) {
217      $s += Time::HiRes::time() - time();
218      $n++;
219  }
220  # $s should be, at worst, equal to $n
221  # (time() may be rounding down, up, or closest)
222  ok 14, abs($s) / $n <= 1.0, "Time::HiRes::time() not close to time()";
223  print "# s = $s, n = $n, s/n = ", $s/$n, "\n";
224 }
225
226 my $has_ualarm = $Config{d_ualarm};
227
228 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
229
230 unless (   defined &Time::HiRes::gettimeofday
231         && defined &Time::HiRes::ualarm
232         && defined &Time::HiRes::usleep
233         && $has_ualarm) {
234     for (15..17) {
235         print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
236     }
237 } else {
238     use Time::HiRes qw(time alarm sleep);
239
240     my ($f, $r, $i, $not, $ok);
241
242     $f = time; 
243     print "# time...$f\n";
244     print "ok 15\n";
245
246     $r = [Time::HiRes::gettimeofday()];
247     sleep (0.5);
248     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
249
250     $r = [Time::HiRes::gettimeofday()];
251     $i = 5;
252     $SIG{ALRM} = "tick";
253     while ($i > 0)
254     {
255         alarm(0.3);
256         select (undef, undef, undef, 3);
257         my $ival = Time::HiRes::tv_interval ($r);
258         print "# Select returned! $i $ival\n";
259         print "# ", abs($ival/3 - 1), "\n";
260         # Whether select() gets restarted after signals is
261         # implementation dependent.  If it is restarted, we
262         # will get about 3.3 seconds: 3 from the select, 0.3
263         # from the alarm.  If this happens, let's just skip
264         # this particular test.  --jhi
265         if (abs($ival/3.3 - 1) < $limit) {
266             $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
267             undef $not;
268             last;
269         }
270         my $exp = 0.3 * (5 - $i);
271         # This test is more sensitive, so impose a softer limit.
272         if (abs($ival/$exp - 1) > 3*$limit) {
273             my $ratio = abs($ival/$exp);
274             $not = "while: $exp sleep took $ival ratio $ratio";
275             last;
276         }
277         $ok = $i;
278     }
279
280     sub tick
281     {
282         $i--;
283         my $ival = Time::HiRes::tv_interval ($r);
284         print "# Tick! $i $ival\n";
285         my $exp = 0.3 * (5 - $i);
286         # This test is more sensitive, so impose a softer limit.
287         if (abs($ival/$exp - 1) > 3*$limit) {
288             my $ratio = abs($ival/$exp);
289             $not = "tick: $exp sleep took $ival ratio $ratio";
290             $i = 0;
291         }
292     }
293
294     alarm(0); # can't cancel usig %SIG
295
296     print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
297 }
298
299 unless (   defined &Time::HiRes::setitimer
300         && defined &Time::HiRes::getitimer
301         && has_symbol('ITIMER_VIRTUAL')
302         && $Config{sig_name} =~ m/\bVTALRM\b/) {
303     for (18..19) {
304         print "ok $_ # Skip: no virtual interval timers\n";
305     }
306 } else {
307     use Time::HiRes qw(setitimer getitimer ITIMER_VIRTUAL);
308
309     my $i = 3;
310     my $r = [Time::HiRes::gettimeofday()];
311
312     $SIG{VTALRM} = sub {
313         $i ? $i-- : setitimer(&ITIMER_VIRTUAL, 0);
314         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
315     };  
316
317     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
318
319     # Assume interval timer granularity of $limit * 0.5 seconds.  Too bold?
320     my $virt = getitimer(&ITIMER_VIRTUAL);
321     print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
322     print "ok 18\n";
323
324     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
325
326     while (getitimer(&ITIMER_VIRTUAL)) {
327         my $j;
328         for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
329     }
330
331     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
332
333     $virt = getitimer(&ITIMER_VIRTUAL);
334     print "not " unless defined $virt && $virt == 0;
335     print "ok 19\n";
336
337     $SIG{VTALRM} = 'DEFAULT';
338 }
339
340 if ($have_gettimeofday &&
341     $have_usleep) {
342     use Time::HiRes qw(usleep);
343
344     my ($t0, $td);
345
346     my $sleep = 1.5; # seconds
347     my $msg;
348
349     $t0 = gettimeofday();
350     $a = abs(sleep($sleep)        / $sleep         - 1.0);
351     $td = gettimeofday() - $t0;
352     my $ratio = 1.0 + $a;
353
354     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
355
356     if ($td < $sleep * (1 + $limit)) {
357         print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
358     } else {
359         print "ok 20 # Skip: $msg";
360     }
361
362     $t0 = gettimeofday();
363     $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
364     $td = gettimeofday() - $t0;
365     $ratio = 1.0 + $a;
366
367     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
368
369     if ($td < $sleep * (1 + $limit)) {
370         print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
371     } else {
372         print "ok 21 # Skip: $msg";
373     }
374
375 } else {
376     for (20..21) {
377         print "ok $_ # Skip: no gettimeofday\n";
378     }
379 }
380
381 unless ($have_nanosleep) {
382     skip 22..23;
383 }
384 else {
385     my $one = CORE::time;
386     nanosleep(10_000_000);
387     my $two = CORE::time;
388     nanosleep(10_000_000);
389     my $three = CORE::time;
390     ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
391
392     unless ($have_gettimeofday) {
393         skip 23;
394     }
395     else {
396         my $f = Time::HiRes::time();
397         nanosleep(500_000_000);
398         my $f2 = Time::HiRes::time();
399         my $d = $f2 - $f;
400         ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
401     }
402 }
403
404 eval { sleep(-1) };
405 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
406     "ok 24\n" : "not ok 24\n";
407
408 eval { usleep(-2) };
409 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
410     "ok 25\n" : "not ok 25\n";
411
412 if ($have_ualarm) {
413     eval { alarm(-3) };
414     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
415         "ok 26\n" : "not ok 26\n";
416
417     eval { ualarm(-4) };
418     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
419     "ok 27\n" : "not ok 27\n";
420 } else {
421     skip 26;
422     skip 27;
423 }
424
425 if ($have_nanosleep) {
426     eval { nanosleep(-5) };
427     print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
428         "ok 28\n" : "not ok 28\n";
429 } else {
430     skip 28;
431 }
432
433 if ($have_ualarm && $] >= 5.008001) {
434     # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
435     # Perl changes [18765] and [18770], perl bug [perl #20920]
436
437     # First we will find the loop size N (a for() loop 0..N-1)
438     # that will take more than T seconds.
439
440     my $T = 0.01;
441     use Time::HiRes qw(time);
442     my $N = 1024;
443     my $i;
444     N: {
445         do {
446             my $t0 = time();
447             for ($i = 0; $i < $N; $i++) { }
448             my $t1 = time();
449             my $dt = $t1 - $t0;
450             print "# N = $N, t1 = $t1, t0 = $t0, dt = $dt\n";
451             last N if $dt > $T;
452             $N *= 2;
453         } while (1);
454     }
455
456     # The time-burner which takes at least T seconds.
457     my $F = sub {
458         my $c = @_ ? shift : 1;
459         my $n = $c * $N;
460         my $i;
461         for ($i = 0; $i < $n; $i++) { }
462     };
463
464     # Then we will setup a periodic timer (the two-argument alarm() of
465     # Time::HiRes, behind the curtains the libc ualarm()) which has
466     # a signal handler that takes so much time (on the first initial
467     # invocation) that the first periodic invocation (second invocation)
468     # will happen before the first invocation has finished.  In Perl 5.8.0
469     # the "safe signals" concept was implemented, with unfortunately at least
470     # one bug that caused a core dump on reentering the handler. This bug
471     # was fixed by the time of Perl 5.8.1.
472
473     # Do not try mixing sleep() and alarm() for testing this.
474
475     my $a = 0; # Number of alarms we receive.
476     my $A = 2; # Number of alarms we will handle before disarming.
477                # (We may well get $A + 1 alarms.)
478
479     $SIG{ALRM} = sub {
480         $a++;
481         print "# Alarm $a - ", time(), "\n";
482         alarm(0) if $a >= $A; # Disarm the alarm.
483         $F->(2); # Try burning CPU at least for 2T seconds.
484     }; 
485
486     use Time::HiRes qw(alarm); 
487     alarm($T, $T);  # Arm the alarm.
488
489     $F->(10); # Try burning CPU at least for 10T seconds.
490
491     print "ok 29\n"; # Not core dumping by now is considered to be the success.
492 } else {
493     skip 29;
494 }
495
496 if ($have_clock_gettime &&
497     # All implementations of clock_gettime() 
498     # are SUPPOSED TO support CLOCK_REALTIME.
499     has_symbol('CLOCK_REALTIME')) {
500     my $ok = 0;
501  TRY: {
502         for my $try (1..3) {
503             print "# CLOCK_REALTIME: try = $try\n";
504             my $t0 = clock_gettime(&CLOCK_REALTIME);
505             use Time::HiRes qw(sleep);
506             my $T = 1.5;
507             sleep($T);
508             my $t1 = clock_gettime(&CLOCK_REALTIME);
509             if ($t0 > 0 && $t1 > $t0) {
510                 print "# t1 = $t1, t0 = $t0\n";
511                 my $dt = $t1 - $t0;
512                 my $rt = abs(1 - $dt / $T);
513                 print "# dt = $dt, rt = $rt\n";
514                 if ($rt <= 2 * $limit) {
515                     $ok = 1;
516                     last TRY;
517                 }
518             } else {
519                 print "# Error: t0 = $t0, t1 = $t1\n";
520             }
521             my $r = rand() + rand();
522             printf "# Sleeping for %.6f seconds...\n";
523             sleep($r);
524         }
525     }
526     if ($ok) {
527         print "ok 30\n";
528     } else {
529         print "not ok 30\n";
530     }
531 } else {
532     print "# No clock_gettime\n";
533     skip 30;
534 }
535
536 if ($have_clock_getres) {
537     my $tr = clock_getres();
538    if ($tr > 0) {
539        print "ok 31 # tr = $tr\n";
540    } else {
541        print "not ok 31 # tr = $tr\n";
542    }
543 } else {
544     print "# No clock_getres\n";
545     skip 31;
546 }
547
548 END {
549     if (defined $timer_pid) {
550         my $left = $TheEnd - time();
551         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;
552         kill('TERM', $timer_pid); # We are done, the timer can go.
553         unlink("ktrace.out"); # Used in BSD system call tracing.
554         print "# All done.\n";
555     }
556 }
557