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