📄 hires.t
字号:
while (getitimer(&ITIMER_VIRTUAL)) { my $j; for (1..1000) { $j++ } # Can't be unbreakable, must test getitimer(). } print "# getitimer: ", join(" ", getitimer(ITIMER_VIRTUAL)), "\n"; $virt = getitimer(&ITIMER_VIRTUAL); print "not " unless defined $virt && $virt == 0; print "ok 19\n"; $SIG{VTALRM} = 'DEFAULT';}if ($have_gettimeofday && $have_usleep) { use Time::HiRes qw(usleep); my ($t0, $td); my $sleep = 1.5; # seconds my $msg; $t0 = gettimeofday(); $a = abs(sleep($sleep) / $sleep - 1.0); $td = gettimeofday() - $t0; my $ratio = 1.0 + $a; $msg = "$td went by while sleeping $sleep, ratio $ratio.\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; $ratio = 1.0 + $a; $msg = "$td went by while sleeping $sleep, ratio $ratio.\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"; }}unless ($have_nanosleep) { skip 22..23;}else { my $one = CORE::time; nanosleep(10_000_000); my $two = CORE::time; nanosleep(10_000_000); my $three = CORE::time; ok 22, $one == $two || $two == $three, "slept too long, $one $two $three"; unless ($have_gettimeofday) { skip 23; } else { my $f = Time::HiRes::time(); nanosleep(500_000_000); my $f2 = Time::HiRes::time(); my $d = $f2 - $f; ok 23, $d > 0.4 && $d < 0.9, "slept $d secs $f to $f2"; }}eval { sleep(-1) };print $@ =~ /::sleep\(-1\): negative time not invented yet/ ? "ok 24\n" : "not ok 24\n";eval { usleep(-2) };print $@ =~ /::usleep\(-2\): negative time not invented yet/ ? "ok 25\n" : "not ok 25\n";if ($have_ualarm) { eval { alarm(-3) }; print $@ =~ /::alarm\(-3, 0\): negative time not invented yet/ ? "ok 26\n" : "not ok 26\n"; eval { ualarm(-4) }; print $@ =~ /::ualarm\(-4, 0\): negative time not invented yet/ ? "ok 27\n" : "not ok 27\n";} else { skip 26; skip 27;}if ($have_nanosleep) { eval { nanosleep(-5) }; print $@ =~ /::nanosleep\(-5\): negative time not invented yet/ ? "ok 28\n" : "not ok 28\n";} else { skip 28;}# Find the loop size N (a for() loop 0..N-1)# that will take more than T seconds.if ($have_ualarm && $] >= 5.008001) { # http://groups.google.com/group/perl.perl5.porters/browse_thread/thread/adaffaaf939b042e/20dafc298df737f0%2320dafc298df737f0?sa=X&oi=groupsr&start=0&num=3 # Perl changes [18765] and [18770], perl bug [perl #20920] print "# Finding delay loop...\n"; my $T = 0.01; use Time::HiRes qw(time); my $DelayN = 1024; my $i; N: { do { my $t0 = time(); for ($i = 0; $i < $DelayN; $i++) { } my $t1 = time(); my $dt = $t1 - $t0; print "# N = $DelayN, t1 = $t1, t0 = $t0, dt = $dt\n"; last N if $dt > $T; $DelayN *= 2; } while (1); } # The time-burner which takes at least T (default 1) seconds. my $Delay = sub { my $c = @_ ? shift : 1; my $n = $c * $DelayN; my $i; for ($i = 0; $i < $n; $i++) { } }; # Next setup a periodic timer (the two-argument alarm() of # Time::HiRes, behind the curtains the libc ualarm()) which has # a signal handler that takes so much time (on the first initial # invocation) that the first periodic invocation (second invocation) # will happen before the first invocation has finished. In Perl 5.8.0 # the "safe signals" concept was implemented, with unfortunately at least # one bug that caused a core dump on reentering the handler. This bug # was fixed by the time of Perl 5.8.1. # Do not try mixing sleep() and alarm() for testing this. my $a = 0; # Number of alarms we receive. my $A = 2; # Number of alarms we will handle before disarming. # (We may well get $A + 1 alarms.) $SIG{ALRM} = sub { $a++; print "# Alarm $a - ", time(), "\n"; alarm(0) if $a >= $A; # Disarm the alarm. $Delay->(2); # Try burning CPU at least for 2T seconds. }; use Time::HiRes qw(alarm); alarm($T, $T); # Arm the alarm. $Delay->(10); # Try burning CPU at least for 10T seconds. print "ok 29\n"; # Not core dumping by now is considered to be the success.} else { skip 29;}if ($have_clock_gettime && # All implementations of clock_gettime() # are SUPPOSED TO support CLOCK_REALTIME. has_symbol('CLOCK_REALTIME')) { my $ok = 0; TRY: { for my $try (1..3) { print "# CLOCK_REALTIME: try = $try\n"; my $t0 = clock_gettime(&CLOCK_REALTIME); use Time::HiRes qw(sleep); my $T = 1.5; sleep($T); my $t1 = clock_gettime(&CLOCK_REALTIME); if ($t0 > 0 && $t1 > $t0) { print "# t1 = $t1, t0 = $t0\n"; my $dt = $t1 - $t0; my $rt = abs(1 - $dt / $T); print "# dt = $dt, rt = $rt\n"; if ($rt <= 2 * $limit) { $ok = 1; last TRY; } } else { print "# Error: t0 = $t0, t1 = $t1\n"; } my $r = rand() + rand(); printf "# Sleeping for %.6f seconds...\n", $r; sleep($r); } } if ($ok) { print "ok 30\n"; } else { print "not ok 30\n"; }} else { print "# No clock_gettime\n"; skip 30;}if ($have_clock_getres) { my $tr = clock_getres(); if ($tr > 0) { print "ok 31 # tr = $tr\n"; } else { print "not ok 31 # tr = $tr\n"; }} else { print "# No clock_getres\n"; skip 31;}if ($have_clock_nanosleep && has_symbol('CLOCK_REALTIME')) { my $s = 1.5e9; my $t = clock_nanosleep(&CLOCK_REALTIME, $s); my $r = abs(1 - $t / $s); if ($r < 2 * $limit) { print "ok 32\n"; } else { print "not ok 32 # $t = $t, r = $r\n"; }} else { print "# No clock_nanosleep\n"; skip 32;}if ($have_clock) { my @clock = clock(); print "# clock = @clock\n"; for my $i (1..3) { for (my $j = 0; $j < 1e6; $j++) { } push @clock, clock(); print "# clock = @clock\n"; } if ($clock[0] >= 0 && $clock[1] > $clock[0] && $clock[2] > $clock[1] && $clock[3] > $clock[2]) { print "ok 33\n"; } else { print "not ok 33\n"; }} else { skip 33;}if ($have_ualarm) { # 1_100_000 sligthly over 1_000_000, # 2_200_000 slightly over 2**31/1000, # 4_300_000 slightly over 2**32/1000. for my $t ([34, 100_000], [35, 1_100_000], [36, 2_200_000], [37, 4_300_000]) { my ($i, $n) = @$t; my $alarmed = 0; local $SIG{ ALRM } = sub { $alarmed++ }; my $t0 = Time::HiRes::time(); print "# t0 = $t0\n"; print "# ualarm($n)\n"; ualarm($n); 1 while $alarmed == 0; my $t1 = Time::HiRes::time(); print "# t1 = $t1\n"; my $dt = $t1 - $t0; print "# dt = $dt\n"; my $r = $dt / ($n/1e6); print "# r = $r\n"; ok $i, ($n < 1_000_000 || # Too much noise. $r >= 0.8 && $r <= 1.6), "ualarm($n) close enough"; }} else { print "# No ualarm\n"; skip 34..37;}if ($^O =~ /^(cygwin|MSWin)/) { print "# $^O: timestamps may not be good enough\n"; skip 38;} elsif (&Time::HiRes::d_hires_stat) { my @stat; my @atime; my @mtime; for (1..5) { Time::HiRes::sleep(rand(0.1) + 0.1); open(X, ">$$"); print X $$; close(X); @stat = Time::HiRes::stat($$); push @mtime, $stat[9]; Time::HiRes::sleep(rand(0.1) + 0.1); open(X, "<$$"); <X>; close(X); @stat = Time::HiRes::stat($$); push @atime, $stat[8]; } 1 while unlink $$; print "# mtime = @mtime\n"; print "# atime = @atime\n"; my $ai = 0; my $mi = 0; my $ss = 0; for (my $i = 1; $i < @atime; $i++) { if ($atime[$i] >= $atime[$i-1]) { $ai++; } if ($atime[$i] > int($atime[$i])) { $ss++; } } for (my $i = 1; $i < @mtime; $i++) { if ($mtime[$i] >= $mtime[$i-1]) { $mi++; } if ($mtime[$i] > int($mtime[$i])) { $ss++; } } print "# ai = $ai, mi = $mi, ss = $ss\n"; # Need at least 75% of monotonical increase and # 20% of subsecond results. Yes, this is guessing. if ($ss == 0) { print "# No subsecond timestamps detected\n"; skip 38; } elsif ($mi/(@mtime-1) >= 0.75 && $ai/(@atime-1) >= 0.75 && $ss/(@mtime+@atime) >= 0.2) { print "ok 38\n"; } else { print "not ok 38\n"; }} else { print "# No effectual d_hires_stat\n"; skip 38;}END { if ($timer_pid) { # Only in the main process. my $left = $TheEnd - time(); 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; my $kill = kill('TERM', $timer_pid); # We are done, the timer can go. printf "# kill TERM $timer_pid = %d\n", $kill; unlink("ktrace.out"); # Used in BSD system call tracing. print "# All done.\n"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -