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