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