9410e7df43fdf9c2b01880852f459b6cac8948d6
[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.9704; # 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 = 180; # 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 # So let's try 25%.
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.25; # 25% 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  # but allow 10% of slop.
233  ok 14, abs($s) / $n <= 1.10, "Time::HiRes::time() not close to time()";
234  print "# s = $s, n = $n, s/n = ", abs($s)/$n, "\n";
235 }
236
237 my $has_ualarm = $Config{d_ualarm};
238
239 $has_ualarm ||= $xdefine =~ /-DHAS_UALARM/;
240
241 unless (   defined &Time::HiRes::gettimeofday
242         && defined &Time::HiRes::ualarm
243         && defined &Time::HiRes::usleep
244         && $has_ualarm) {
245     for (15..17) {
246         print "ok $_ # Skip: no gettimeofday or no ualarm or no usleep\n";
247     }
248 } else {
249     use Time::HiRes qw(time alarm sleep);
250     eval { require POSIX };
251     my $use_sigaction =
252         !$@ && defined &POSIX::sigaction && &POSIX::SIGALRM > 0;
253
254     my ($f, $r, $i, $not, $ok);
255
256     $f = time; 
257     print "# time...$f\n";
258     print "ok 15\n";
259
260     $r = [Time::HiRes::gettimeofday()];
261     sleep (0.5);
262     print "# sleep...", Time::HiRes::tv_interval($r), "\nok 16\n";
263
264     $r = [Time::HiRes::gettimeofday()];
265     $i = 5;
266     my $oldaction;
267     if ($use_sigaction) {
268         $oldaction = new POSIX::SigAction;
269         printf "# sigaction tick, ALRM = %d\n", &POSIX::SIGALRM;
270
271         # Perl's deferred signals may be too wimpy to break through
272         # a restartable select(), so use POSIX::sigaction if available.
273
274         sub tick {
275             $i--;
276             my $ival = Time::HiRes::tv_interval ($r);
277             print "# Tick! $i $ival\n";
278             my $exp = 0.3 * (5 - $i);
279             # This test is more sensitive, so impose a softer limit.
280             if (abs($ival/$exp - 1) > 4*$limit) {
281                 my $ratio = abs($ival/$exp);
282                 $not = "tick: $exp sleep took $ival ratio $ratio";
283                 $i = 0;
284             }
285         }
286
287         POSIX::sigaction(&POSIX::SIGALRM,
288                          POSIX::SigAction->new("tick"),
289                          $oldaction)
290             or die "Error setting SIGALRM handler with sigaction: $!\n";
291     } else {
292         print "# SIG tick\n";
293         $SIG{ALRM} = "tick";
294     }
295
296     # On VMS timers can not interrupt select.
297     if ($^O eq 'VMS') {
298         $ok = "Skip: VMS select() does not get interrupted.";
299     } else {
300         while ($i > 0) {
301             alarm(0.3);
302             select (undef, undef, undef, 3);
303             my $ival = Time::HiRes::tv_interval ($r);
304             print "# Select returned! $i $ival\n";
305             print "# ", abs($ival/3 - 1), "\n";
306             # Whether select() gets restarted after signals is
307             # implementation dependent.  If it is restarted, we
308             # will get about 3.3 seconds: 3 from the select, 0.3
309             # from the alarm.  If this happens, let's just skip
310             # this particular test.  --jhi
311             if (abs($ival/3.3 - 1) < $limit) {
312                 $ok = "Skip: your select() may get restarted by your SIGALRM (or just retry test)";
313                 undef $not;
314                 last;
315             }
316             my $exp = 0.3 * (5 - $i);
317             # This test is more sensitive, so impose a softer limit.
318             if (abs($ival/$exp - 1) > 3*$limit) {
319                 my $ratio = abs($ival/$exp);
320                 $not = "while: $exp sleep took $ival ratio $ratio";
321                 last;
322             }
323             $ok = $i;
324         }
325     }
326
327     if ($use_sigaction) {
328         POSIX::sigaction(&POSIX::SIGALRM, $oldaction);
329     } else {
330         alarm(0); # can't cancel usig %SIG
331     }
332
333     print $not ? "not ok 17 # $not\n" : "ok 17 # $ok\n";
334 }
335
336 unless (   defined &Time::HiRes::setitimer
337         && defined &Time::HiRes::getitimer
338         && has_symbol('ITIMER_VIRTUAL')
339         && $Config{sig_name} =~ m/\bVTALRM\b/
340         && $^O !~ /^(nto)$/) { # nto: QNX 6 has the API but no implementation
341     for (18..19) {
342         print "ok $_ # Skip: no virtual interval timers\n";
343     }
344 } else {
345     use Time::HiRes qw(setitimer getitimer ITIMER_VIRTUAL);
346
347     my $i = 3;
348     my $r = [Time::HiRes::gettimeofday()];
349
350     $SIG{VTALRM} = sub {
351         $i ? $i-- : setitimer(&ITIMER_VIRTUAL, 0);
352         print "# Tick! $i ", Time::HiRes::tv_interval($r), "\n";
353     };  
354
355     print "# setitimer: ", join(" ", setitimer(ITIMER_VIRTUAL, 0.5, 0.4)), "\n";
356
357     # Assume interval timer granularity of $limit * 0.5 seconds.  Too bold?
358     my $virt = getitimer(&ITIMER_VIRTUAL);
359     print "not " unless defined $virt && abs($virt / 0.5) - 1 < $limit;
360     print "ok 18\n";
361
362     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
363
364     while (getitimer(&ITIMER_VIRTUAL)) {
365         my $j;
366         for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer().
367     }
368
369     print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n";
370
371     $virt = getitimer(&ITIMER_VIRTUAL);
372     print "not " unless defined $virt && $virt == 0;
373     print "ok 19\n";
374
375     $SIG{VTALRM} = 'DEFAULT';
376 }
377
378 if ($have_gettimeofday &&
379     $have_usleep) {
380     use Time::HiRes qw(usleep);
381
382     my ($t0, $td);
383
384     my $sleep = 1.5; # seconds
385     my $msg;
386
387     $t0 = gettimeofday();
388     $a = abs(sleep($sleep)        / $sleep         - 1.0);
389     $td = gettimeofday() - $t0;
390     my $ratio = 1.0 + $a;
391
392     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
393
394     if ($td < $sleep * (1 + $limit)) {
395         print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
396     } else {
397         print "ok 20 # Skip: $msg";
398     }
399
400     $t0 = gettimeofday();
401     $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
402     $td = gettimeofday() - $t0;
403     $ratio = 1.0 + $a;
404
405     $msg = "$td went by while sleeping $sleep, ratio $ratio.\n";
406
407     if ($td < $sleep * (1 + $limit)) {
408         print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
409     } else {
410         print "ok 21 # Skip: $msg";
411     }
412
413 } else {
414     for (20..21) {
415         print "ok $_ # Skip: no gettimeofday\n";
416     }
417 }
418
419 unless ($have_nanosleep) {
420     skip 22..23;
421 }
422 else {
423     my $one = CORE::time;
424     nanosleep(10_000_000);
425     my $two = CORE::time;
426     nanosleep(10_000_000);
427     my $three = CORE::time;
428     ok 22, $one == $two || $two == $three, "slept too long, $one $two $three";
429
430     unless ($have_gettimeofday) {
431         skip 23;
432     }
433     else {
434         my $f = Time::HiRes::time();
435         nanosleep(500_000_000);
436         my $f2 = Time::HiRes::time();
437         my $d = $f2 - $f;
438         ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2";
439     }
440 }
441
442 eval { sleep(-1) };
443 print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?
444     "ok 24\n" : "not ok 24\n";
445
446 eval { usleep(-2) };
447 print $@ =~ /::usleep\(-2\): negative time not invented yet/ ?
448     "ok 25\n" : "not ok 25\n";
449
450 if ($have_ualarm) {
451     eval { alarm(-3) };
452     print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ?
453         "ok 26\n" : "not ok 26\n";
454
455     eval { ualarm(-4) };
456     print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ?
457     "ok 27\n" : "not ok 27\n";
458 } else {
459     skip 26;
460     skip 27;
461 }
462
463 if ($have_nanosleep) {
464     eval { nanosleep(-5) };
465     print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ?
466         "ok 28\n" : "not ok 28\n";
467 } else {
468     skip 28;
469 }
470
471 # Find the loop size N (a for() loop 0..N-1)
472 # that will take more than T seconds.
473
474 if ($have_ualarm && $] >= 5.008001) {
475     # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3
476     # Perl changes [18765] and [18770], perl bug [perl #20920]
477
478     print "# Finding delay loop...\n";
479
480     my $T = 0.01;
481     use Time::HiRes qw(time);
482     my $DelayN = 1024;
483     my $i;
484  N: {
485      do {
486          my $t0 = time();
487          for ($i = 0; $i < $DelayN; $i++) { }
488          my $t1 = time();
489          my $dt = $t1 - $t0;
490          print "# N = $DelayN, t1 = $t1, t0 = $t0, dt = $dt\n";
491          last N if $dt > $T;
492          $DelayN *= 2;
493      } while (1);
494  }
495
496     # The time-burner which takes at least T (default 1) seconds.
497     my $Delay = sub {
498         my $c = @_ ? shift : 1;
499         my $n = $c * $DelayN;
500         my $i;
501         for ($i = 0; $i < $n; $i++) { }
502     };
503
504     # Next setup a periodic timer (the two-argument alarm() of
505     # Time::HiRes, behind the curtains the libc ualarm()) which has
506     # a signal handler that takes so much time (on the first initial
507     # invocation) that the first periodic invocation (second invocation)
508     # will happen before the first invocation has finished.  In Perl 5.8.0
509     # the "safe signals" concept was implemented, with unfortunately at least
510     # one bug that caused a core dump on reentering the handler. This bug
511     # was fixed by the time of Perl 5.8.1.
512
513     # Do not try mixing sleep() and alarm() for testing this.
514
515     my $a = 0; # Number of alarms we receive.
516     my $A = 2; # Number of alarms we will handle before disarming.
517                # (We may well get $A + 1 alarms.)
518
519     $SIG{ALRM} = sub {
520         $a++;
521         print "# Alarm $a - ", time(), "\n";
522         alarm(0) if $a >= $A; # Disarm the alarm.
523         $Delay->(2); # Try burning CPU at least for 2T seconds.
524     }; 
525
526     use Time::HiRes qw(alarm); 
527     alarm($T, $T);  # Arm the alarm.
528
529     $Delay->(10); # Try burning CPU at least for 10T seconds.
530
531     print "ok 29\n"; # Not core dumping by now is considered to be the success.
532 } else {
533     skip 29;
534 }
535
536 if ($have_clock_gettime &&
537     # All implementations of clock_gettime() 
538     # are SUPPOSED TO support CLOCK_REALTIME.
539     has_symbol('CLOCK_REALTIME')) {
540     my $ok = 0;
541  TRY: {
542         for my $try (1..3) {
543             print "# CLOCK_REALTIME: try = $try\n";
544             my $t0 = clock_gettime(&CLOCK_REALTIME);
545             use Time::HiRes qw(sleep);
546             my $T = 1.5;
547             sleep($T);
548             my $t1 = clock_gettime(&CLOCK_REALTIME);
549             if ($t0 > 0 && $t1 > $t0) {
550                 print "# t1 = $t1, t0 = $t0\n";
551                 my $dt = $t1 - $t0;
552                 my $rt = abs(1 - $dt / $T);
553                 print "# dt = $dt, rt = $rt\n";
554                 if ($rt <= 2 * $limit) {
555                     $ok = 1;
556                     last TRY;
557                 }
558             } else {
559                 print "# Error: t0 = $t0, t1 = $t1\n";
560             }
561             my $r = rand() + rand();
562             printf "# Sleeping for %.6f seconds...\n", $r;
563             sleep($r);
564         }
565     }
566     if ($ok) {
567         print "ok 30\n";
568     } else {
569         print "not ok 30\n";
570     }
571 } else {
572     print "# No clock_gettime\n";
573     skip 30;
574 }
575
576 if ($have_clock_getres) {
577     my $tr = clock_getres();
578     if ($tr > 0) {
579         print "ok 31 # tr = $tr\n";
580     } else {
581         print "not ok 31 # tr = $tr\n";
582     }
583 } else {
584     print "# No clock_getres\n";
585     skip 31;
586 }
587
588 if ($have_clock_nanosleep &&
589     has_symbol('CLOCK_REALTIME')) {
590     my $s = 1.5e9;
591     my $t = clock_nanosleep(&CLOCK_REALTIME, $s);
592     my $r = abs(1 - $t / $s);
593     if ($r < 2 * $limit) {
594         print "ok 32\n";
595     } else {
596         print "not ok 32 # $t = $t, r = $r\n";
597     }
598 } else {
599     print "# No clock_nanosleep\n";
600     skip 32;
601 }
602
603 if ($have_clock) {
604     my @clock = clock();
605     print "# clock = @clock\n";
606     for my $i (1..3) {
607         for (my $j = 0; $j < 1e6; $j++) { }
608         push @clock, clock();
609         print "# clock = @clock\n";
610     }
611     if ($clock[0] >= 0 &&
612         $clock[1] > $clock[0] &&
613         $clock[2] > $clock[1] &&
614         $clock[3] > $clock[2]) {
615         print "ok 33\n";
616     } else {
617         print "not ok 33\n";
618     }
619 } else {
620     skip 33;
621 }
622
623 if ($have_ualarm) {
624     # 1_100_000 sligthly over 1_000_000,
625     # 2_200_000 slightly over 2**31/1000,
626     # 4_300_000 slightly over 2**32/1000.
627     for my $t ([34, 100_000],
628                [35, 1_100_000],
629                [36, 2_200_000],
630                [37, 4_300_000]) {
631         my ($i, $n) = @$t;
632         my $alarmed = 0;
633         local $SIG{ ALRM } = sub { $alarmed++ };
634         my $t0 = Time::HiRes::time();
635         print "# t0 = $t0\n";
636         print "# ualarm($n)\n";
637         ualarm($n); 1 while $alarmed == 0;
638         my $t1 = Time::HiRes::time();
639         print "# t1 = $t1\n";
640         my $dt = $t1 - $t0;
641         print "# dt = $dt\n";
642         my $r = $dt / ($n/1e6);
643         print "# r = $r\n";
644         ok $i,
645         ($n < 1_000_000 || # Too much noise.
646          $r >= 0.8 && $r <= 1.6), "ualarm($n) close enough";
647     }
648 } else {
649     print "# No ualarm\n";
650     skip 34..37;
651 }
652
653 if ($^O =~ /^(cygwin|MSWin)/) {
654     print "# $^O: timestamps may not be good enough\n";
655     skip 38;
656 } elsif (&Time::HiRes::d_hires_stat) {
657     my @stat;
658     my @atime;
659     my @mtime;
660     for (1..5) {
661         Time::HiRes::sleep(rand(0.1) + 0.1);
662         open(X, ">$$");
663         print X $$;
664         close(X);
665         @stat = Time::HiRes::stat($$);
666         push @mtime, $stat[9];
667         Time::HiRes::sleep(rand(0.1) + 0.1);
668         open(X, "<$$");
669         <X>;
670         close(X);
671         @stat = Time::HiRes::stat($$);
672         push @atime, $stat[8];
673     }
674     1 while unlink $$;
675     print "# mtime = @mtime\n";
676     print "# atime = @atime\n";
677     my $ai = 0;
678     my $mi = 0;
679     my $ss = 0;
680     for (my $i = 1; $i < @atime; $i++) {
681         if ($atime[$i] >= $atime[$i-1]) {
682             $ai++;
683         }
684         if ($atime[$i] > int($atime[$i])) {
685             $ss++;
686         }
687     }
688     for (my $i = 1; $i < @mtime; $i++) {
689         if ($mtime[$i] >= $mtime[$i-1]) {
690             $mi++;
691         }
692         if ($mtime[$i] > int($mtime[$i])) {
693             $ss++;
694         }
695     }
696     print "# ai = $ai, mi = $mi, ss = $ss\n";
697     # Need at least 75% of monotonical increase and
698     # 20% of subsecond results. Yes, this is guessing.
699     if ($ss == 0) {
700         print "# No subsecond timestamps detected\n";
701         skip 38;
702     } elsif ($mi/(@mtime-1) >= 0.75 && $ai/(@atime-1) >= 0.75 &&
703              $ss/(@mtime+@atime) >= 0.2) {
704         print "ok 38\n";
705     } else {
706         print "not ok 38\n";
707     }
708 } else {
709     print "# No effectual d_hires_stat\n";
710     skip 38;
711 }
712
713 END {
714     if ($timer_pid) { # Only in the main process.
715         my $left = $TheEnd - time();
716         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;
717         my $kill = kill('TERM', $timer_pid); # We are done, the timer can go.
718         printf "# kill TERM $timer_pid = %d\n", $kill;
719         unlink("ktrace.out"); # Used in BSD system call tracing.
720         print "# All done.\n";
721     }
722 }
723