summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorJarkko Hietaniemi <jhi@iki.fi>2001-11-04 17:43:10 +0000
committerJarkko Hietaniemi <jhi@iki.fi>2001-11-04 17:43:10 +0000
commit9c735d5936d2e28562f488b19be586dbf4c6d6d2 (patch)
treee61c2ba2423720e4d1569cd5f478fbc3fce7ddab /ext
parent48b581a2b1cd70fdaddb7e065134902493db91a0 (diff)
downloadperl-9c735d5936d2e28562f488b19be586dbf4c6d6d2.tar.gz
Protect against high load: measure how much wall
clock time went by while sleeping. Avoids some false negatives, while may introduce some false positives. Life is hard. p4raw-id: //depot/perl@12846
Diffstat (limited to 'ext')
-rw-r--r--ext/Time/HiRes/HiRes.t39
1 files changed, 35 insertions, 4 deletions
diff --git a/ext/Time/HiRes/HiRes.t b/ext/Time/HiRes/HiRes.t
index f92fba27f4..bee325742e 100644
--- a/ext/Time/HiRes/HiRes.t
+++ b/ext/Time/HiRes/HiRes.t
@@ -223,11 +223,42 @@ unless (defined &Time::HiRes::setitimer
$SIG{VTALRM} = 'DEFAULT';
}
-$a = abs(sleep(1.5) / 1.5 - 1.0);
-print $a < 0.1 ? "ok 20 # $a\n" : "not ok 20 # $a\n";
+if ($have_gettimeofday) {
+ my ($t0, $td);
-$a = abs(usleep(1_500_000) / 1_500_000 - 1.0);
-print $a < 0.1 ? "ok 21 # $a\n" : "not ok 21 # $a\n";
+ my $sleep = 1.5; # seconds
+ my $limit = 0.1; # 10% is acceptable slosh for timers
+ my $msg;
+
+ $t0 = gettimeofday();
+ $a = abs(sleep($sleep) / $sleep - 1.0);
+ $td = gettimeofday() - $t0;
+
+ $msg = "$td went by while sleeping $sleep, ratio $a\n";
+
+ if ($td < $sleep * (1 + $limit)) {
+ print $a < $limit ? "ok 20 # $msg" : "not ok 20 # $msg";
+ } else {
+ print "ok 20 # Skip: $msg";
+ }
+
+ $t0 = gettimeofday();
+ $a = abs(usleep($sleep * 1E6) / ($sleep * 1E6) - 1.0);
+ $td = gettimeofday() - $t0;
+
+ $msg = "$td went by while sleeping $sleep, ratio $a\n";
+
+ if ($td < $sleep * (1 + $limit)) {
+ print $a < $limit ? "ok 21 # $msg" : "not ok 21 # $msg";
+ } else {
+ print "ok 21 # Skip: $msg";
+ }
+
+} else {
+ for (20..21) {
+ print "ok $_ # Skip: no gettimeofday\n";
+ }
+}
eval { sleep(-1) };
print $@ =~ /::sleep\(-1\): negative time not invented yet/ ?