blib/lib/Devel/RingBuffer/Ring.pm

filter: Statementstmt 80.8% 97 / 120 Branchbran 41.6% 10 / 24 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 80.8% 38 / 47 Podpod 0.0% 0 / 25 totaltotal 64.9% 150 / 231 CC 16 SCAR 9.6
CC
16
Cov
73%
clone
12.0
waitForCmdEvent
6.0
DESTROY
5.2
CRAP 2.6
« blib/lib/Devel/RingBuffer.pm ↑ Summary blib/lib/Devel/RingBuffer/ThreadFacade.pm »
1 #/**
2 # A single shared memory ring buffer for diagnosis/debug of Perl scripts.
3 # Uses IPC::Mmap to create/access/manage a memory mapped file (or namespace
4 # on Win32) as a ring buffer structure that can be used by "applications
5 # under test" that use an appropriate debug module (e.g., Devel::STrace)
6 # along with an external monitoring application
7 # (e.g., Devel::STrace::Monitor).
8 # <p>
9 # Note that significant functionality is written in XS/C in order to minimize
10 # tracing/debugging overhead.
11 # <p>
12 # Permission is granted to use this software under the same terms as Perl itself.
13 # Refer to the <a href='http://perldoc.perl.org/perlartistic.html'>Perl Artistic License</a>
14 # for details.
15 #
16 # @author D. Arnold
17 # @since 2006-05-01
18 # @self $self
19 #*/
20 package Devel::RingBuffer::Ring;
21  
22 #use threads;
23 7 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 our @ISA = qw(Exporter);
28 #
29 # consts for member indexes
30 #
31 7 ▶use constant RINGBUF_RING_BUFFER => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 7 ▶use constant RINGBUF_RING_SLOTS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 #
34 # !!!+++!+!+!+!+!+!+!+!+!+!+!+
35 # !!!DON'T CHANGE THIS INDEX UNLESS YOU CHANGE THE XS CODE TOO!!!!
36 # !!!+++!+!+!+!+!+!+!+!+!+!+!+
37 #
38 7 ▶use constant RINGBUF_RING_ADDR => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 7 ▶use constant RINGBUF_RING_PID => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 ▶use constant RINGBUF_RING_TID => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 7 ▶use constant RINGBUF_RING_SLOT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 7 ▶use constant RINGBUF_RING_DEPTH => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 7 ▶use constant RINGBUF_RING_INDEX => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 7 ▶use constant RINGBUF_RING_MSGSZ => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 7 ▶use constant RINGBUF_RING_HDRSZ => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 7 ▶use constant RINGBUF_BASE_ADDR => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 7 ▶use constant RINGBUF_RING_WAIT => 0.3;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 7 our @EXPORT = ();
52 7 our @EXPORT_OK = ();
53 7 our %EXPORT_TAGS = (
54 ring_members => [
55 qw/RINGBUF_RING_BUFFER RINGBUF_RING_SLOTS RINGBUF_RING_ADDR
56 RINGBUF_RING_PID RINGBUF_RING_TID RINGBUF_RING_SLOT RINGBUF_RING_DEPTH
57 RINGBUF_RING_INDEX RINGBUF_RING_MSGSZ RINGBUF_RING_HDRSZ
58 RINGBUF_BASE_ADDR/
59 ],
60 );
61  
62 7 Exporter::export_tags(keys %EXPORT_TAGS);
63  
64 };
65  
66 7 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 7 ▶use Devel::RingBuffer; # to bootstrap
Subroutine BEGIN: called CC=1 SCAR=0.0
68 7 ▶use Devel::RingBuffer qw(:ringbuffer_consts);
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 7 our $hasThreads;
71  
72 7 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=16.5
73 7 ▶ if ($Config{useithreads} && (!$ENV{DEVEL_RINGBUF_NOTHREADS})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config{"useithreads"} and not $ENV{"DEVEL_RINGBUF_NOTHREADS"}) 0 7 7
Condition33%1/3
$Config{"useithreads"} and not $ENV{"DEVEL_RINGBUF_NOTHREADS"}
!ll&&!rl&&r
700
MC/DC0%0/2
$Config{"useithreads"} and not $ENV{"DEVEL_RINGBUF_NOTHREADS"}
$Config{"useithreads"} not $ENV{"DEVEL_RINGBUF_NOTHREADS"}
Truth table33%1/3
A and not(B)
A= $Config{"useithreads"}B= not $ENV{"DEVEL_RINGBUF_NOTHREADS"}
ABresult
0X0
100
111
74 0 require Devel::RingBuffer::ThreadFacade;
75 0 $hasThreads = 1;
76 }
77 }
78  
79 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 7 our $VERSION = '0.31';
83 #/**
84 # Constructor. Allocates a ring buffer, and initializes its header
85 # and control variables.
86 #
87 # @param $ringbuffer the Devel::RingBuffer object
88 # @param $ringaddr the base address of this ring
89 # @param $baseaddr base address of the complete ring buffer structure
90 # @param $ringnum the number (i.e., positional index) of this ring
91 # @param $slots number of slots per ring
92 # @param $msgareasz size of the per-thread message area
93 #
94 # @return Devel::RingBuffer::Ring object on success; undef on failure
95 #*/
96 24 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
97 24 my ($class, $ringbuffer, $ringaddr, $baseaddr, $ringnum, $slots, $msgareasz) = @_;
98  
99 24 ▶ my $tid = ($hasThreads ? Devel::RingBuffer::ThreadFacade->tid() : 0);
Branch: 1 branch
BranchTrueFalseTotal
$Devel::RingBuffer::Ring::hasThreads ? : 0 24 24
100 24 _init_ring($ringaddr, $$, $tid, $baseaddr);
101  
102 24 return bless [
103 $ringbuffer,
104 $slots,
105 $ringaddr,
106 $$,
107 $tid,
108 -1,
109 0,
110 $ringnum,
111 $msgareasz,
112 RINGBUF_BUFHDR_SZ + $msgareasz,
113 $baseaddr
114 ], $class;
115 }
116 #/**
117 # Constructor. Allocates a ring buffer, and initializes its header
118 # and control variables. Called when the AUT object (e.g., DB)
119 # is CLONE'd, so that a new ring can be assigned to the new thread
120 #
121 # @return the Devel::RingBuffer::Ring object
122 #*/
123 0 ▶sub clone {
Subroutine clone: not called CC=3 SCAR=24.8
Pod undocumented
124 0 my $self = shift;
125  
126 0 ▶ my $tid = ($hasThreads ? Devel::RingBuffer::ThreadFacade->tid() : 0);
Branch: 1 branch
BranchTrueFalseTotal
$Devel::RingBuffer::Ring::hasThreads ? : 0 0 0
127 0 my ($ringnum, $ringaddr) = $self->[RINGBUF_RING_BUFFER]->reallocate();
128 0 ▶ return undef unless defined($ringnum);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ringnum 0 0 0
129 0 $self->[RINGBUF_RING_ADDR] = $ringaddr;
130 0 $self->[RINGBUF_RING_INDEX] = $ringnum;
131 0 _init_ring($ringaddr, $$, $tid, $self->[RINGBUF_BASE_ADDR]);
132 0 return $self;
133 }
134 #/**
135 # Constructor. Opens an existing ring buffer for read-only access.
136 #
137 # @param $ringbuffer the Devel::RingBuffer object
138 # @param $ringaddr the base address of this ring
139 # @param $baseaddr base address of the complete ring buffer structure
140 # @param $ringnum the number (i.e., positional index) of this ring
141 # @param $slots number of slots per ring
142 # @param $msgareasz size of the per-thread message area
143 #
144 # @return Devel::RingBuffer::Ring object on success; undef on failure
145 #*/
146 19 ▶sub open {
Subroutine open: called CC=1 SCAR=0.0
Pod undocumented
147 19 my ($class, $ringbuffer, $ringaddr, $baseaddr, $ringnum, $slots, $msgareasz) = @_;
148  
149 19 my ($pid, $tid, $slot, $depth) = _get_header($ringaddr);
150  
151 19 return bless [
152 $ringbuffer,
153 $slots,
154 $ringaddr,
155 $pid,
156 $tid,
157 $slot,
158 $depth,
159 $ringnum,
160 $msgareasz,
161 RINGBUF_BUFHDR_SZ + $msgareasz,
162 $baseaddr
163 ], $class;
164 }
165 #/**
166 # Update the current slot. Only updates linenumber and timestamp.
167 # May be called as either object or class method; in the latter case,
168 # caller must supply the ring's base address <i>(used within DB::DB()
169 # to optimize access speed)</i>
170 #
171 # @param $address <b><i>class method calls only</i></b>: base address of the ring
172 # @param $linenumber linenumber of current statement
173 #
174 # @return the Devel::RingBuffer::Ring object
175 #*/
176 # @xs updateSlot
177  
178 #/**
179 # @xs nextSlot
180 # Allocate and initialize the next slot. If the stack depth is
181 # greater than the configured number of slots, the oldest
182 # in-use slot is used, overwriting its current contents.
183 # May be called as either object or class method; in the latter case,
184 # caller must supply the ring's base address <i>(used within DB::sub()
185 # to optimize access speed)</i>
186 # <p>
187 # <i>Note: In future, this should return prior contents so we can restore
188 # on de-wrapping.</i>
189 #
190 # @param $address <b><i>class method calls only</i></b>: base address of the ring
191 # @param $entry subroutine name (from $DB::sub)
192 #
193 # @return the stack depth after the slot is allocated.
194 #*/
195 # @xs nextSlot
196  
197 #/**
198 # @xs freeSlot
199 # Free the current slot and invalidates its contents.
200 # May be called as either object or class method; in the latter case,
201 # caller must supply the ring's base address <i>(used within DB::sub()
202 # to optimize access speed)</i>
203 #
204 # @param $address <b><i>class method calls only</i></b>: base address of the ring
205 #
206 # @return the stack depth after the slot is freed.
207 #*/
208 # @xs freeSlot
209  
210 #/**
211 # Get the ring header values. Header fields returned are
212 # <p>
213 # <ol>
214 # <li>pid - PID of the ring owner
215 # <li>tid - TID of the ring owner
216 # <li>currSlot - current top slot
217 # <li>depth - current stack depth
218 # </ol>
219 #
220 # @return list of header values
221 #*/
222 16 ▶sub getHeader {
Subroutine getHeader: called CC=1 SCAR=0.0
Pod undocumented
223 16 return _get_header($_[0]->[RINGBUF_RING_ADDR]);
224 }
225  
226 #/**
227 # Get the ring number (i.e., positional index)
228 #
229 # @return the ring number
230 #*/
231 99 ▶sub getIndex { return $_[0]->[RINGBUF_RING_INDEX]; }
Subroutine getIndex: called CC=1 SCAR=0.0
Pod undocumented
232  
233 #/**
234 # Get the ring base address
235 #
236 # @return the ring base address
237 #*/
238 0 ▶sub getAddress { return $_[0]->[RINGBUF_RING_ADDR]; }
Subroutine getAddress: not called CC=1 SCAR=6.9
Pod undocumented
239  
240 #/**
241 # Get the contents of the specified slot.
242 #
243 # @param $slot the number of the slot to return
244 #
245 # @return the line number, timestamp, and subroutine name from the slot
246 #*/
247 39 ▶sub getSlot {
Subroutine getSlot: called CC=3 SCAR=12.5
Pod undocumented
248 39 my ($self, $slot) = @_;
249  
250 39 ▶ return (-1, 0, '(Invalid slot; ring has been wrapped)')
Branch: 1 branch
BranchTrueFalseTotal
if $slot < 0 or $slot > $self->[1] 0 39 39
Condition33%1/3
$slot < 0 or $slot > $self->[1]
l!l&&r!l&&!r
0039
MC/DC0%0/2
$slot < 0 or $slot > $self->[1]
$slot < 0 $slot > $self->[1]
Truth table33%1/3
A or B
A= $slot < 0B= $slot > $self->[1]
ABresult
1X1
011
000
251 if ($slot < 0) || ($slot > $self->[RINGBUF_RING_SLOTS]);
252  
253 39 return _get_slot($self->[RINGBUF_RING_ADDR], $slot);
254 }
255 #/**
256 # Get the ring's trace flag
257 #
258 # @return the ring's trace flag
259 #*/
260 0 ▶sub getTrace {
Subroutine getTrace: not called CC=1 SCAR=6.9
Pod undocumented
261 0 return _get_trace($_[0]->[RINGBUF_RING_ADDR]);
262 }
263  
264 #/**
265 # Set the ring's trace flag
266 #
267 # @param $trace the value to set
268 #
269 # @return the prior value of the ring's trace flag
270 #*/
271 0 ▶sub setTrace {
Subroutine setTrace: not called CC=1 SCAR=6.9
Pod undocumented
272 0 return _set_trace($_[0]->[RINGBUF_RING_ADDR], $_[1]);
273 }
274  
275 #/**
276 # Get the ring's signal flag
277 #
278 # @return the ring's signal flag
279 #*/
280 0 ▶sub getSignal {
Subroutine getSignal: not called CC=1 SCAR=6.9
Pod undocumented
281 0 return _get_single($_[0]->[RINGBUF_RING_ADDR]);
282 }
283  
284 #/**
285 # Set the ring's signal flag
286 #
287 # @param $signal the value to set
288 #
289 # @return the prior value of the ring's signal flag
290 #*/
291 0 ▶sub setSignal {
Subroutine setSignal: not called CC=1 SCAR=6.9
Pod undocumented
292 0 return _set_signal($_[0]->[RINGBUF_RING_ADDR], $_[1]);
293 }
294  
295 #/**
296 # Post a command to the ring's command/message area
297 #
298 # @param $command the command value to set; must be no more than 3 bytes
299 # @param $msg an optional message associated with the command; max length
300 # is determined by configuration settings
301 #
302 # @return the ring object
303 #*/
304 4 ▶sub postCommand { return postCmdEvent(@_, 1); }
Subroutine postCommand: called CC=1 SCAR=0.0
Pod undocumented
305  
306 #/**
307 # Post a response to the ring's command/message area
308 #
309 # @param $response the response value to set; must be no more than 3 bytes
310 # @param $msg an optional message associated with the response; max length
311 # is determined by configuration settings
312 #
313 # @return the ring object
314 #*/
315 4 ▶sub postResponse { return postCmdEvent(@_, 0); }
Subroutine postResponse: called CC=1 SCAR=0.0
Pod undocumented
316  
317 8 ▶sub postCmdEvent {
Subroutine postCmdEvent: called CC=1 SCAR=0.0
Pod undocumented
318 8 my ($self, $cmd, $msg, $state) = @_;
319 8 _post_cmd_msg($self->[RINGBUF_RING_ADDR], $cmd, $msg, $state);
320  
321 8 return $self;
322 }
323  
324 #/**
325 # Wait indefinitely for a command to be posted to the ring's command/message area.
326 #
327 # @return the posted command and message
328 #*/
329 0 ▶sub waitForCommand {
Subroutine waitForCommand: not called CC=1 SCAR=6.9
Pod undocumented
330 0 return waitForCmdEvent(@_, 1);
331 }
332  
333 #/**
334 # Wait indefinitely for a response to be posted to the ring's command/message area.
335 #
336 # @return the posted response and message
337 #*/
338 0 ▶sub waitForResponse {
Subroutine waitForResponse: not called CC=1 SCAR=6.9
Pod undocumented
339 0 return waitForCmdEvent(@_, 0);
340 }
341  
342 0 ▶sub waitForCmdEvent {
Subroutine waitForCmdEvent: not called CC=2 SCAR=17.9
Pod undocumented
343 0 my ($cmd, $msg);
344 0 while (1) {
345 0 ($cmd, $msg) = _check_for_cmd_msg($_[0]->[RINGBUF_RING_ADDR], $_[1]);
346 0 ▶ last if defined($cmd);
Branch: 1 branch
BranchTrueFalseTotal
if defined $cmd 0 0 0
347 0 sleep RINGBUF_RING_WAIT;
348 }
349 0 return ($cmd, $msg);
350 }
351  
352 #/**
353 # Test if a command is available in the ring's command/message area.
354 #
355 # @return if available, the posted command and message; otherwise an empty list
356 #*/
357 4 ▶sub checkCommand {
Subroutine checkCommand: called CC=1 SCAR=0.0
Pod undocumented
358 4 return checkCmdEvent(@_, 1);
359 }
360  
361 #/**
362 # Test if a response is available in the ring's command/message area.
363 #
364 # @return if available, the posted response and message; otherwise an empty list
365 #*/
366 4 ▶sub checkResponse {
Subroutine checkResponse: called CC=1 SCAR=0.0
Pod undocumented
367 4 return checkCmdEvent(@_, 0);
368 }
369  
370 8 ▶sub checkCmdEvent {
Subroutine checkCmdEvent: called CC=1 SCAR=0.0
Pod undocumented
371 8 return _check_for_cmd_msg($_[0]->[RINGBUF_RING_ADDR], $_[1]);
372 }
373 #/**
374 # Allocate and initialize a watchlist entry. Sets the watch expression.
375 #
376 # @param $expr expression to set
377 #
378 # @return allocated watchlist entry number on success; undef on failure
379 #*/
380 4 ▶sub addWatch {
Subroutine addWatch: called CC=1 SCAR=0.0
Pod undocumented
381 4 return _add_watch_expr($_[0]->[RINGBUF_RING_ADDR], $_[1]);
382 }
383  
384 #/**
385 # Free a watchlist entry.
386 #
387 # @param $watch the watchlist entry number to free
388 #
389 #*/
390 4 ▶sub freeWatch {
Subroutine freeWatch: called CC=1 SCAR=0.0
Pod undocumented
391 4 return _free_watch_expr($_[0]->[RINGBUF_RING_ADDR], $_[1]);
392 }
393  
394 #/**
395 # Get a watchlist expression entry.
396 #
397 # @param $watch the watchlist entry number to get
398 #
399 # @return the expression in the watchlist entry, if any; undef otherwise
400 #*/
401 4 ▶sub getWatchExpr {
Subroutine getWatchExpr: called CC=2 SCAR=7.6
Pod undocumented
402 4 ▶ return $_[0]->[RINGBUF_RING_BUFFER] ?
Branch: 1 branch
BranchTrueFalseTotal
$_[0][0] ? : 4 0 4
403 _get_watch_expr($_[0]->[RINGBUF_RING_ADDR], $_[1]) :
404 undef;
405 }
406  
407 #/**
408 # Set a watchlist result entry.
409 #
410 # @param $watch the watchlist entry number to set
411 # @param $result the result of the expression evaluation
412 # @param $error error string if expression evaluation fails
413 #*/
414 4 ▶sub setWatchResult {
Subroutine setWatchResult: called CC=2 SCAR=7.2
Pod undocumented
415 4 my ($self, $watch, $result, $error) = @_;
416  
417 4 ▶ return $self->[RINGBUF_RING_BUFFER] ?
Branch: 1 branch
BranchTrueFalseTotal
$self->[0] ? : 4 0 4
418 _set_watch_result($self->[RINGBUF_RING_ADDR], $watch, $result, $error) :
419 undef;
420 }
421 #/**
422 # Get a watchlist expression entry. If the length of the result exceeds
423 # the configured message size, the result is truncated. If the result is
424 # undef, the length will zero, and both the result and error will be undef.
425 # If the evaluation caused a failure, the length indicates the length of
426 # the error string, and result will be undef.
427 #
428 # @param $watch the watchlist entry number to get
429 #
430 # @return the complete length of the result, the (possibly truncated) result value,
431 # and the (possibly truncated) error message (if the evaluation failed).
432 #*/
433 4 ▶sub getWatchResult {
Subroutine getWatchResult: called CC=2 SCAR=7.6
Pod undocumented
434 4 ▶ return $_[0]->[RINGBUF_RING_BUFFER] ?
Branch: 1 branch
BranchTrueFalseTotal
$_[0][0] ? : 4 0 4
435 _get_watch_result($_[0]->[RINGBUF_RING_ADDR], $_[1]) :
436 (undef, undef, undef);
437 }
438 #/**
439 # Destructor. Updates the Devel::RingBuffer container object's free ring map,
440 # <i>but only if executed in the same process/thread that it was allocated'd in.</i>
441 # (Note that due to threads CLONE, a ring object may be cloned with PID/TID
442 # of another thread, and thus DESTROY() could cause an invalid destruction)
443 # <p>
444 # A future enhancement will add a flag to indicate to preserve
445 # the ring on exit for post-mortem analysis
446 #*/
447 43 ▶sub DESTROY {
Subroutine DESTROY: called CC=5 SCAR=16.6
448 #
449 # for some reason we're getting leakage of ring objects into
450 # the root thread, so only destroy in the thread its created
451 #
452 # return unless defined($_[0]->[RINGBUF_RING_BUFFER]) &&
453 # ($_[0]->[RINGBUF_RING_PID] == $$) &&
454 # ($_[0]->[RINGBUF_RING_TID] == threads->self()->tid());
455 43 ▶ return unless defined($_[0]->[RINGBUF_RING_BUFFER]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0][0] 0 43 43
456 43 my @hdr = _get_header($_[0]->[RINGBUF_RING_ADDR]);
457 43 ▶ my $tid = ($hasThreads ? Devel::RingBuffer::ThreadFacade->tid() : 0);
Branch: 1 branch
BranchTrueFalseTotal
$Devel::RingBuffer::Ring::hasThreads ? : 0 43 43
458  
459 return
460 43 ▶ unless ($hdr[0] == $$) && ($hdr[1] == $tid);
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr[0] == $$ and $hdr[1] == $tid 20 23 43
Condition66%2/3
$hdr[0] == $$ and $hdr[1] == $tid
!ll&&!rl&&r
20023
MC/DC50%1/2
$hdr[0] == $$ and $hdr[1] == $tid
$hdr[0] == $$ $hdr[1] == $tid
Truth table66%2/3
A and B
A= $hdr[0] == $$B= $hdr[1] == $tid
ABresult
0X0
100
111
461 23 $_[0]->[RINGBUF_RING_BUFFER]->free($_[0]->[RINGBUF_RING_INDEX]);
462 }
463  
464 7 1;
« blib/lib/Devel/RingBuffer.pm ↑ Summary blib/lib/Devel/RingBuffer/ThreadFacade.pm »