blib/lib/POE/Resource/Clock.pm

filter: Statementstmt 30.5% 54 / 177 Branchbran 15.9% 7 / 44 Conditioncond 5.8% 1 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 26.4% 9 / 34 Podpod n/a totaltotal 25.1% 71 / 282 CC 29 SCAR 27.2
CC
29
Cov
26%
_sig_number
42.0
BEGIN
36.8
_rt_read_pipe
30.0
CRAP 15.2
« blib/lib/POE/Wheel/ReadWrite.pm ↑ Summary blib/lib/POE/Component/Client/TCP.pm »
1 # Manage a platonic, monotonic clock to keep the event queue ordered
2  
3 package POE::Resource::Clock;
4  
5 202 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 202 $VERSION = '1.370'; # NOTE - Should be #.### (three decimal places)
7  
8 202 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 202 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 202 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 202 ▶use POE::Pipe::OneWay;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 202 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 202 require Exporter;
16 202 our @EXPORT_OK = qw( monotime sleep walltime wall2mono mono2wall time );
17 202 our @ISA = qw( Exporter );
18  
19 sub DEBUG () { 0 }
20  
21 sub CLK_TIMEOUT () { 0 }
22 sub CLK_SKEW () { 1 }
23  
24 sub CLK_EN_READ () { "rt-lock-read" }
25  
26  
27 # Perform a runtime check for a compile-time flag.
28 #
29 # TODO - Enable compiler optimization of all calls to this function.
30 # The customary way to do this is to migrate the environment variable
31 # value into a constant at compile time, then for all callers to check
32 # the constant directly. This is such a common thing to do that POE
33 # should define a utility library for it.
34  
35 sub _do_X
36 404 ▶{
Subroutine _do_X: called CC=4 SCAR=14.3
37 404 my( $X, $default ) = @_;
38 404 my $m = $X;
39 404 ▶ return POE::Kernel->can( $m )->() if POE::Kernel->can( $m );
Branch: 1 branch
BranchTrueFalseTotal
if "POE::Kernel"->can($m) 0 404 404
40 404 my $k = "POE_$X";
41 404 ▶ return $ENV{$k} if exists $ENV{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{$k} 0 404 404
42 404 ▶ return $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 0 404 404
43 404 return 1;
44 }
45  
46  
47 # Try to get the exact difference between the monotonic clock's epoch
48 # and the system clock's epoch. We do this by comparing the 2 for
49 # 0.25 second or 10 samples. To compensate for delays between calling
50 # time and get_time, we run in both order. Even so, I still see up to
51 # 10 mS divergence in my dev VM between invocations.
52 #
53 # Only called once, at compile time.
54  
55 sub _exact_epoch
56 0 ▶{
Subroutine _exact_epoch: not called CC=3 SCAR=24.8
57 0 my( $monoclock ) = @_;
58  
59 0 my $N=0;
60 0 my $total = 0;
61 0 my $end = $monoclock->get_time() + 0.25;
62 0 ▶ while( $end > $monoclock->get_time() or $N < 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end > $monoclock->get_time or $N < 20) 0 0 0
Condition0%0/3
$end > $monoclock->get_time or $N < 20
l!l&&r!l&&!r
000
MC/DC0%0/2
$end > $monoclock->get_time or $N < 20
$end > $monoclock->get_time $N < 20
Truth table0%0/3
A or B
A= $end > $monoclock->get_timeB= $N < 20
ABresult
1X1
011
000
63 0 my $hr = Time::HiRes::time();
64 0 my $mono = $monoclock->get_time();
65 0 $total += $hr - $mono;
66 0 $N++;
67 0 $mono = $monoclock->get_time();
68 0 $hr = Time::HiRes::time();
69 0 $total += $hr - $mono;
70 0 $N++;
71 }
72 0 DEBUG and POE::Kernel::_warn( "<ck> RT clock samples=$N" );
73 0 return $total/$N;
74 }
75  
76  
77 #########################################
78 sub _get_epoch
79 0 ▶{
Subroutine _get_epoch: not called CC=1 SCAR=6.9
80 0 my( $monoclock, $wallclock ) = @_;
81 0 return $wallclock->get_time() - $monoclock->get_time();
82 }
83  
84  
85 #########################################
86 202 our $FORMAT = 'iF';
87 202 our $LENGTH = length pack $FORMAT, 0, 0;
88 sub _pipe_write
89 0 ▶{
Subroutine _pipe_write: not called CC=1 SCAR=6.9
90 0 my( $write, $op, $skew ) = @_;
91 0 DEBUG and POE::Kernel::_warn( "<ck> write op=$op" );
92 0 my $buffer = pack $FORMAT, $op, $skew;
93 0 syswrite( $write, $buffer, $LENGTH );
94 }
95  
96  
97 #########################################
98 sub _pipe_read
99 0 ▶{
Subroutine _pipe_read: not called CC=2 SCAR=17.9
100 0 my( $read ) = @_;
101 0 my $buffer;
102 0 sysread( $read, $buffer, $LENGTH );
103 0 ▶ return unless length $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless length $buffer 0 0 0
104 0 return unpack $FORMAT, $buffer;
105 }
106  
107  
108 202 our( $SIGACT, $SIGSET );
109 sub _build_sig
110 0 ▶{
Subroutine _build_sig: not called CC=4 SCAR=30.0
111 0 my( $write ) = @_;
112 0 ▶ my $handler = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 0 DEBUG and POE::Kernel::_warn( "<ck> timeout" );
114 0 _pipe_write( $write, CLK_TIMEOUT, 0 );
115 0 };
116 0 my $default = eval { _sig_number( 'RTMIN' ) } ||
117 0 ▶ eval { _sig_number( 'RTALRM' ) } ||
Condition0%0/5
eval { do { _sig_number('RTMIN') } } || eval { do { _sig_number('RTALRM') } } || 14
l!l
00
eval { do { _sig_number("RTMIN") } } or eval { do { _sig_number("RTALRM") } }
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { _sig_number('RTMIN') } } || eval { do { _sig_number('RTALRM') } } || 14
eval { do { _sig_number("RTMIN") } } eval { do { _sig_number("RTALRM") } }
Truth table0%0/3
A or B
A= eval { do { _sig_number("RTMIN") } }B= eval { do { _sig_number("RTALRM") } }
ABresult
1X1
011
000
118 SIGALRM;
119  
120 0 ▶ my $signal = _do_X( 'CLOCK_SIGNAL', $default ) || $default;
Condition0%0/3
_do_X('CLOCK_SIGNAL', $default) || $default
l!l&&r!l&&!r
000
MC/DC0%0/2
_do_X('CLOCK_SIGNAL', $default) || $default
_do_X('CLOCK_SIGNAL', $default) $default
Truth table0%0/3
A || B
A= _do_X('CLOCK_SIGNAL', $default)B= $default
ABresult
1X1
011
000
121 0 $SIGSET = POSIX::SigSet->new( $signal );
122 0 $SIGACT = POSIX::SigAction->new( $handler, $SIGSET, 0 );
123 0 $SIGACT->safe(1);
124 0 POSIX::sigaction( $signal, $SIGACT );
125 0 return $signal;
126 }
127  
128  
129 #########################################
130 sub _rt_setup
131 0 ▶{
Subroutine _rt_setup: not called CC=1 SCAR=6.9
132 0 my( $read, $kernel ) = @_;
133 0 $kernel->loop_pause_time_watcher();
134 0 DEBUG and POE::Kernel::_warn( "<ck> Setup RT pipe" );
135 # Add to the select list
136 0 $kernel->_data_handle_condition( $read );
137 0 $kernel->loop_watch_filehandle( $read, POE::Kernel::MODE_RD() );
138 }
139  
140  
141 202 our $EPSILON = 0.0001;
142 sub _rt_resume
143 0 ▶{
Subroutine _rt_resume: not called CC=2 SCAR=17.9
144 0 my( $what, $timer, $kernel, $pri ) = @_;
145 0 DEBUG and POE::Kernel::_warn( "<ck> $what pri=$pri" );
146 0 $kernel->loop_pause_time_watcher();
147 0 ▶ if( $pri <= monotime() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pri <= &monotime()) { } 0 0 0
148 0 $timer->set_timeout( $EPSILON );
149 }
150 else {
151 0 $timer->set_timeout( $pri, 0, 1 );
152 }
153 }
154  
155  
156 sub _rt_pause
157 0 ▶{
Subroutine _rt_pause: not called CC=1 SCAR=6.9
158 0 my( $timer, $kernel ) = @_;
159 0 DEBUG and POE::Kernel::_warn( "<ck> Pause" );
160 0 $timer->set_timeout( 60 );
161 0 $kernel->loop_pause_time_watcher();
162 }
163  
164  
165 #########################################
166 sub _rt_read_pipe
167 0 ▶{
Subroutine _rt_read_pipe: not called CC=5 SCAR=34.0
168 0 my( $kernel, $read ) = @_;
169 0 my $dispatch_once;
170 0 while( 1 ) {
171 0 my( $op, $skew ) = _pipe_read( $read );
172 0 ▶ return unless defined $op;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op 0 0 0
173 0 DEBUG and POE::Kernel::_warn( "<ck> Read pipe op=$op" );
174 0 ▶ if( $op == CLK_TIMEOUT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op == 0) { } 0 0 0
elsif ($op == 1) { } 0 0 0
175 0 ▶ next unless $dispatch_once;
Branch: 1 branch
BranchTrueFalseTotal
unless $dispatch_once 0 0 0
176 0 $kernel->_data_ev_dispatch_due();
177 0 $dispatch_once = 1;
178 }
179 elsif( $op == CLK_SKEW ) {
180 0 rt_skew( $kernel );
181 0 $dispatch_once = 0;
182 }
183 elsif( DEBUG ) {
184 POE::Kernel::_warn( "<ck> Unknown op=$op" );
185 }
186 }
187 }
188  
189  
190 #########################################
191 sub _rt_ready
192 0 ▶{
Subroutine _rt_ready: not called CC=2 SCAR=17.9
193 0 my( $read, $frd, $kernel, $fileno ) = @_;
194 0 ▶ return 0 unless $frd == $fileno;
Branch: 1 branch
BranchTrueFalseTotal
unless $frd == $fileno 0 0 0
195 0 _rt_read_pipe( $kernel, $read );
196 0 return 1;
197 }
198  
199  
200 #########################################
201 202 my %SIGnames;
202 sub _sig_number
203 0 ▶{
Subroutine _sig_number: not called CC=6 SCAR=37.4
204 0 my( $name ) = @_;
205 0 ▶ return $name if $name =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\d+$/ 0 0 0
206 0 my $X = 0;
207 0 ▶ $X = $1 if $name =~ s/\+(\d+)$//;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ s/\+(\d+)$// 0 0 0
208 0 ▶ unless( %SIGnames ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%SIGnames) 0 0 0
209 # this code is lifted from Config pod
210 die "Config is missing either sig_name or sig_num; You must use a numeric signal"
211 0 ▶ unless $Config{sig_name} and $Config{sig_num};
Branch: 1 branch
BranchTrueFalseTotal
unless $POE::Resource::Clock::Config{"sig_name"} and $POE::Resource::Clock::Config{"sig_num"} 0 0 0
Condition0%0/3
$POE::Resource::Clock::Config{"sig_name"} and $POE::Resource::Clock::Config{"sig_num"}
!ll&&!rl&&r
000
MC/DC0%0/2
$POE::Resource::Clock::Config{"sig_name"} and $POE::Resource::Clock::Config{"sig_num"}
$POE::Resource::Clock::Config{"sig_name"} $POE::Resource::Clock::Config{"sig_num"}
Truth table0%0/3
A and B
A= $POE::Resource::Clock::Config{"sig_name"}B= $POE::Resource::Clock::Config{"sig_num"}
ABresult
0X0
100
111
212 0 my @names = split ' ', $Config{sig_name};
213 0 @SIGnames{@names} = split ' ', $Config{sig_num};
214 }
215 0 return $SIGnames{ $name }+$X;
216 }
217  
218  
219 #########################################
220 202 ▶BEGIN {
Subroutine BEGIN: called CC=9 SCAR=36.1
221 202 my $done;
222 my $have_clock;
223 202 ▶ if( _do_X( 'USE_POSIXRT' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_do_X("USE_POSIXRT")) 202 0 202
224 202 eval {
225 202 require File::Spec->catfile( qw( POSIX RT Clock.pm ) );
226 0 require File::Spec->catfile( qw( POSIX RT Timer.pm ) );
227 0 my $monoclock = POSIX::RT::Clock->new( 'monotonic' );
228 0 my $wallclock = POSIX::RT::Clock->new( 'realtime' );
229 0 ▶ *monotime = sub { return $monoclock->get_time(); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
230 0 ▶ *walltime = sub { return $wallclock->get_time(); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
231 0 ▶ *sleep = sub { $monoclock->sleep_deeply(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
232 0 ▶ if( _do_X( 'USE_STATIC_EPOCH' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_do_X('USE_STATIC_EPOCH')) { } 0 0 0
233 # This is where we cheat: without a static epoch the tests fail
234 # because they expect alarm(), alarm_set() to arrive in order
235 # Calling _get_epoch() each time would preclude this
236 0 my $epoch = 0;
237 0 ▶ if( _do_X( 'USE_EXACT_EPOCH', 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_do_X('USE_EXACT_EPOCH', 0)) { } 0 0 0
238 0 $epoch = _exact_epoch( $monoclock, $wallclock );
239 }
240 else {
241 0 $epoch = _get_epoch( $monoclock, $wallclock );
242 }
243 0 DEBUG and warn( "<ck> epoch=$epoch" );
244 0 ▶ *wall2mono = sub { $_[0] - $epoch };
Subroutine __ANON__: not called CC=1 SCAR=6.9
245 0 ▶ *mono2wall = sub { $_[0] + $epoch };
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 }
247 else {
248 0 ▶ *wall2mono = sub { $_[0] - _get_epoch($monoclock, $wallclock) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 0 ▶ *mono2wall = sub { $_[0] + _get_epoch($monoclock, $wallclock) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
250  
251 0 my ($rd, $wr) = POE::Pipe::OneWay->new();
252 0 ▶ die "Unable to build pipe: $!" unless defined $rd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rd 0 0 0
253  
254 0 my $signal = _build_sig( $wr );
255  
256 0 my $timer = POSIX::RT::Timer->new(
257 value => 0,
258 interval => 0,
259 clock => 'monotonic',
260 signal => $signal
261 );
262  
263 0 $EPSILON = $monoclock->get_resolution();
264 0 DEBUG and warn( "<ck> epsilon=$EPSILON" );
265 #*clock_pause = sub { _rt_pause( $timer, @_ ); };
266 #*clock_reset = sub { _rt_resume( Reset = > $timer, @_ ); };
267 #*clock_resume = sub { _rt_resume( Resume = > $timer, @_ ); };
268 #*clock_setup = sub { _rt_setup( $rd, @_ ) };
269 0 my $frd = fileno( $rd );
270 #*clock_read = sub { _rt_ready( $rd, $frd, @_ ) };
271 0 $have_clock = 1;
272 }
273 0 $done = 1;
274 };
275 202 if( DEBUG ) {
276 warn( "<ck> POSIX::RT::Clock not installed: $@" ) if $@;
277 warn( "<ck> using POSIX::RT::Clock" ) if $done;
278 }
279 }
280 202 ▶ if( !$done and _do_X( 'USE_HIRES' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $done and _do_X("USE_HIRES")) 202 0 202
Condition33%1/3
not $done and _do_X("USE_HIRES")
!ll&&!rl&&r
00202
MC/DC0%0/2
not $done and _do_X("USE_HIRES")
not $done _do_X("USE_HIRES")
Truth table33%1/3
not(A) and B
A= not $doneB= _do_X("USE_HIRES")
ABresult
0X0
100
111
281 202 eval {
282 202 require File::Spec->catfile( qw( Time HiRes.pm ) );
283 202 *monotime = \&Time::HiRes::time;
284 202 *walltime = \&Time::HiRes::time;
285 202 *sleep = \&Time::HiRes::sleep;
286 202 ▶ *wall2mono = sub { return $_[0] };
Subroutine __ANON__: called CC=1 SCAR=0.0
287 0 ▶ *mono2wall = sub { return $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
288 202 $done = 1;
289 };
290 202 if( DEBUG ) {
291 warn( "<ck> Time::HiRes not installed: $@" )if $@;
292 warn( "<ck> using Time::HiRes" ) if $done;
293 }
294 }
295 202 ▶ unless( $done ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 202 202
296 # \&CORE::time fails :-(
297 0 ▶ *monotime = sub { CORE::time };
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 0 ▶ *walltime = sub { CORE::time };
Subroutine __ANON__: not called CC=1 SCAR=6.9
299 0 ▶ *sleep = sub { CORE::sleep(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
300 0 ▶ *wall2mono = sub { return $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
301 0 ▶ *mono2wall = sub { return $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
302 0 warn( "<ck> using CORE::time" )if DEBUG;
303 }
304  
305 202 ▶ unless( $have_clock ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_clock) 202 0 202
306 #*clock_pause = sub { $_[0]->loop_pause_time_watcher() };
307 #*clock_reset = sub { $_[0]->loop_reset_time_watcher(mono2wall($_[1])) };
308 #*clock_resume = sub { $_[0]->loop_resume_time_watcher(mono2wall($_[1])) };
309 #*clock_setup = sub { 0 };
310 #*clock_read = sub { 0 };
311 }
312  
313 # *time = sub { Carp::confess( "This should be monotime" ) };
314 202 *time = \&walltime;
315 }
316  
317 202 1;
318  
319 __END__
« blib/lib/POE/Wheel/ReadWrite.pm ↑ Summary blib/lib/POE/Component/Client/TCP.pm »