blib/lib/Devel/RingBuffer.pm

filter: Statementstmt 77.3% 273 / 353 Branchbran 25.8% 16 / 62 Conditioncond 46.3% 19 / 41 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 82.2% 74 / 90 Podpod 0.0% 0 / 25 totaltotal 65.2% 384 / 589 CC 49 SCAR 28.3
CC
49
Cov
68%
new
58.3
_lcl_open
30.0
setGlobalMsg
30.0
CRAP 16.9
↑ Summary blib/lib/Devel/RingBuffer/Ring.pm »
1 #/**
2 # Shared memory ring buffers 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 (e.g., Devel::STrace::Monitor).
7 # <p>
8 # Note that significant functionality is written in XS/C in order to minimize
9 # tracing/debugging overhead.
10 # <p>
11 # Permission is granted to use this software under the same terms as Perl itself.
12 # Refer to the <a href='http://perldoc.perl.org/perlartistic.html'>Perl Artistic License</a>
13 # for details.
14 #
15 # @author D. Arnold
16 # @since 2006-05-01
17 # @self $self
18 #*/
19 package Devel::RingBuffer;
20  
21 7 ▶use Carp qw(cluck carp confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 #use threads;
23 #use threads::shared;
24 7 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 7 ▶use IPC::Mmap;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 7 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 our @ISA = qw(Exporter DynaLoader);
31  
32 #
33 # offset of global fields
34 #
35 7 ▶use constant RINGBUF_SINGLE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use constant RINGBUF_MSGAREA_SZ => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 7 ▶use constant RINGBUF_BUFFERS => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 7 ▶use constant RINGBUF_SLOTS => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 7 ▶use constant RINGBUF_SLOT_SZ => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 7 ▶use constant RINGBUF_CREATE_STOP => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 ▶use constant RINGBUF_CREATE_TRACE => 24;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 7 ▶use constant RINGBUF_GLOBAL_SZ => 28;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 7 ▶use constant RINGBUF_TOTALMSG_SZ => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 7 ▶use constant RINGBUF_GLOBMSG_SZ => 36;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 7 ▶use constant RINGBUF_GLOBAL_MSG => 40;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 7 ▶use constant RINGBUF_RINGHDR_SZ => 40;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 #
48 # offsets of watchlist members
49 #
50 7 ▶use constant RINGBUF_WATCH_INUSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 7 ▶use constant RINGBUF_WATCH_EXPRLEN => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 7 ▶use constant RINGBUF_WATCH_EXPR => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶use constant RINGBUF_WATCH_READY => 264;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 7 ▶use constant RINGBUF_WATCH_RESLEN => 268;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 7 ▶use constant RINGBUF_WATCH_RESULT => 272;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 7 ▶use constant RINGBUF_WATCH_SZ => 784;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 7 ▶use constant RINGBUF_WATCH_CNT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 7 ▶use constant RINGBUF_WATCH_EXPRSZ => 256;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 7 ▶use constant RINGBUF_WATCH_RESSZ => 512;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 #
61 # offsets of ring buffer members
62 #
63 7 ▶use constant RINGBUF_PID => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 7 ▶use constant RINGBUF_TID => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 7 ▶use constant RINGBUF_CURRSLOT => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 7 ▶use constant RINGBUF_DEPTH => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 7 ▶use constant RINGBUF_TRACE => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 7 ▶use constant RINGBUF_SIGNAL => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 7 ▶use constant RINGBUF_BASEADDR => 24;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 7 ▶use constant RINGBUF_WATCH_OFFSET => 28;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 7 ▶use constant RINGBUF_BUFHDR_SZ => 28;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 7 ▶use constant RINGBUF_DFLT_SLOTSZ => 214;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 7 ▶use constant RINGBUF_ENTRY_SZ => 200;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 7 ▶use constant RINGBUF_SLOT_PACKSTR => 'l d S/a*';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 #
77 # consts for member indexes
78 #
79 7 ▶use constant RINGBUF_FILENAME => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 7 ▶use constant RINGBUF_SIZE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 7 ▶use constant RINGBUF_COUNT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 7 ▶use constant RINGBUF_BUFSIZE => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 7 ▶use constant RINGBUF_SLOT_CNT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 7 ▶use constant RINGBUF_FLD_TID => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 7 ▶use constant RINGBUF_FLD_PID => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 7 ▶use constant RINGBUF_RING => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 7 ▶use constant RINGBUF_FH => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 7 ▶use constant RINGBUF_FLD_MSGAREA_SZ => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 7 ▶use constant RINGBUF_FLD_GLOBAL_SZ => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 7 ▶use constant RINGBUF_MAP_OFFSET => 11;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 7 ▶use constant RINGBUF_RINGS_OFFSET => 12;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 7 ▶use constant RINGBUF_MAP_ADDR => 13;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 7 ▶use constant RINGBUF_RINGS_ADDR => 14;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 7 ▶use constant RINGBUF_ADDRESS => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 7 ▶use constant RINGBUF_SLOT_SIZE => 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 7 ▶use constant RINGBUF_NEXT_IDX => 17;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 7 ▶use constant RINGBUF_RING_WAIT => 0.3;
Subroutine BEGIN: called CC=1 SCAR=0.0
99  
100 7 our @EXPORT = ();
101 7 our @EXPORT_OK = ();
102 7 our %EXPORT_TAGS = (
103 ringbuffer_consts => [
104 qw/RINGBUF_SINGLE RINGBUF_MSGAREA_SZ RINGBUF_BUFFERS RINGBUF_SLOTS
105 RINGBUF_SLOT_SZ RINGBUF_CREATE_STOP RINGBUF_CREATE_TRACE RINGBUF_GLOBAL_SZ
106 RINGBUF_TOTALMSG_SZ RINGBUF_GLOBMSG_SZ
107 RINGBUF_GLOBAL_MSG RINGBUF_RINGHDR_SZ RINGBUF_WATCH_INUSE
108 RINGBUF_WATCH_EXPRLEN RINGBUF_WATCH_EXPR RINGBUF_WATCH_READY
109 RINGBUF_WATCH_RESLEN RINGBUF_WATCH_RESULT RINGBUF_WATCH_SZ
110 RINGBUF_WATCH_CNT RINGBUF_PID RINGBUF_TID RINGBUF_CURRSLOT
111 RINGBUF_DEPTH RINGBUF_TRACE RINGBUF_SIGNAL RINGBUF_WATCH_OFFSET
112 RINGBUF_BUFHDR_SZ RINGBUF_DFLT_SLOTSZ RINGBUF_ENTRY_SZ RINGBUF_SLOT_PACKSTR/
113 ],
114  
115 ringbuffer_members => [
116 qw/RINGBUF_FILENAME RINGBUF_SIZE RINGBUF_COUNT RINGBUF_BUFSIZE RINGBUF_SLOT_CNT
117 RINGBUF_FLD_TID RINGBUF_FLD_PID RINGBUF_RING RINGBUF_FH
118 RINGBUF_FLD_MSGAREA_SZ RINGBUF_FLD_GLOBAL_SZ RINGBUF_MAP_OFFSET
119 RINGBUF_RINGS_OFFSET RINGBUF_MAP_ADDR RINGBUF_RINGS_ADDR RINGBUF_ADDRESS
120 RINGBUF_SLOT_SIZE RINGBUF_NEXT_IDX/
121 ],
122 );
123  
124 7 Exporter::export_tags(keys %EXPORT_TAGS);
125  
126 };
127  
128 7 our $VERSION = '0.31';
129 7 our $hasThreads;
130  
131 7 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=16.5
132 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
133 0 require Devel::RingBuffer::ThreadFacade;
134 0 $hasThreads = 1;
135 }
136 }
137  
138 7 ▶use threads::shared;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 7 bootstrap Devel::RingBuffer $VERSION;
144  
145 7 ▶use Devel::RingBuffer::Ring;
Subroutine BEGIN: called CC=1 SCAR=0.0
146  
147 7 our $thrdlock = undef;
148  
149 #/**
150 # Constructor. Using a combination of the optional C<%args> and
151 # various environment variables, creates and initializes a
152 # mmap'ed file in read/write mode with the ring buffer structures.
153 #
154 # @param File name of the file to be created for memory mapping.
155 # @param GlobalSize size of global monitor <=> AUT message buffer.
156 # @param MessageSize size of per-thread monitor <=> AUT message buffer.
157 # @param Rings Number of rings to create in the ring buffer.
158 # @param Slots Number of slots per ring.
159 # @param SlotSize Slot size in bytes.
160 # @param StopOnCreate Initial value for stop_on_create flag.
161 # @param TraceOnCreate Initial value for trace_on_create flag.
162 #
163 # @return Devel::RingBuffer object on success; undef on failure
164 #*/
165 7 ▶sub new {
Subroutine new: called CC=30 SCAR=40.7
Pod undocumented
166 7 my $class = shift;
167  
168 7 my %args = @_;
169  
170 7 ▶ my $file = $args{File} || $ENV{DEVEL_RINGBUF_FILE};
Condition66%2/3
$args{'File'} || $ENV{'DEVEL_RINGBUF_FILE'}
l!l&&r!l&&!r
016
MC/DC50%1/2
$args{'File'} || $ENV{'DEVEL_RINGBUF_FILE'}
$args{'File'} $ENV{'DEVEL_RINGBUF_FILE'}
Truth table66%2/3
A || B
A= $args{'File'}B= $ENV{'DEVEL_RINGBUF_FILE'}
ABresult
1X1
011
000
171 7 my $anon;
172 7 ▶ unless (defined($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 6 1 7
173 6 my @paths = split(/[\/\\]/, $0);
174 6 $file = pop @paths;
175 6 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 6 6
176 0 $anon = 1;
177 }
178 else {
179 6 ▶ $file = defined($ENV{TEMP}) ? "$ENV{TEMP}/$file" : "/tmp/$file";
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'TEMP'} ? : 0 6 6
180 }
181 6 $file=~s/^(.+)\..+/$1/;
182 #
183 # use timestamp sans weekday and year
184 #
185 6 my @pieces = split(/\s+/, scalar localtime);
186 6 pop @pieces; # get rid of year
187 6 $pieces[0] = $$; # replace weekday w/ PID
188 6 $pieces[-1]=~tr/:/_/; # Win32 can't handle colons in filenames
189 6 $file .= '.' . join('_', @pieces);
190 }
191  
192 #print STDERR "RingBuffer new: args:", join(', ', keys %args), "\n";
193  
194 7 ▶ my $ringslots = $args{Slots} || $ENV{DEVEL_RINGBUF_SLOTS} || 10;
Condition40%2/5
$args{'Slots'} || $ENV{'DEVEL_RINGBUF_SLOTS'} || 10
l!l
07
$args{"Slots"} or $ENV{"DEVEL_RINGBUF_SLOTS"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$args{'Slots'} || $ENV{'DEVEL_RINGBUF_SLOTS'} || 10
$args{"Slots"} $ENV{"DEVEL_RINGBUF_SLOTS"}
Truth table33%1/3
A or B
A= $args{"Slots"}B= $ENV{"DEVEL_RINGBUF_SLOTS"}
ABresult
1X1
011
000
195 7 ▶ my $slotsz = $args{SlotSize} || $ENV{DEVEL_RINGBUF_SLOTSZ} || 200;
Condition40%2/5
$args{'SlotSize'} || $ENV{'DEVEL_RINGBUF_SLOTSZ'} || 200
l!l
07
$args{"SlotSize"} or $ENV{"DEVEL_RINGBUF_SLOTSZ"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$args{'SlotSize'} || $ENV{'DEVEL_RINGBUF_SLOTSZ'} || 200
$args{"SlotSize"} $ENV{"DEVEL_RINGBUF_SLOTSZ"}
Truth table33%1/3
A or B
A= $args{"SlotSize"}B= $ENV{"DEVEL_RINGBUF_SLOTSZ"}
ABresult
1X1
011
000
196 7 ▶ my $ringcount = $args{Rings} || $ENV{DEVEL_RINGBUF_BUFFERS} || 20;
Condition40%2/5
$args{'Rings'} || $ENV{'DEVEL_RINGBUF_BUFFERS'} || 20
l!l
07
$args{"Rings"} or $ENV{"DEVEL_RINGBUF_BUFFERS"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$args{'Rings'} || $ENV{'DEVEL_RINGBUF_BUFFERS'} || 20
$args{"Rings"} $ENV{"DEVEL_RINGBUF_BUFFERS"}
Truth table33%1/3
A or B
A= $args{"Rings"}B= $ENV{"DEVEL_RINGBUF_BUFFERS"}
ABresult
1X1
011
000
197 7 ▶ my $ringmsgsz = $args{MessageSize} || $ENV{DEVEL_RINGBUF_MSGSZ} || 256;
Condition40%2/5
$args{'MessageSize'} || $ENV{'DEVEL_RINGBUF_MSGSZ'} || 256
l!l
07
$args{"MessageSize"} or $ENV{"DEVEL_RINGBUF_MSGSZ"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$args{'MessageSize'} || $ENV{'DEVEL_RINGBUF_MSGSZ'} || 256
$args{"MessageSize"} $ENV{"DEVEL_RINGBUF_MSGSZ"}
Truth table33%1/3
A or B
A= $args{"MessageSize"}B= $ENV{"DEVEL_RINGBUF_MSGSZ"}
ABresult
1X1
011
000
198 7 ▶ my $globmsgsz = $args{GlobalSize} || $ENV{DEVEL_RINGBUF_GLOBALSZ} || (16 * 1024);
Condition40%2/5
$args{'GlobalSize'} || $ENV{'DEVEL_RINGBUF_GLOBALSZ'} || 16384
l!l
07
$args{"GlobalSize"} or $ENV{"DEVEL_RINGBUF_GLOBALSZ"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$args{'GlobalSize'} || $ENV{'DEVEL_RINGBUF_GLOBALSZ'} || 16384
$args{"GlobalSize"} $ENV{"DEVEL_RINGBUF_GLOBALSZ"}
Truth table33%1/3
A or B
A= $args{"GlobalSize"}B= $ENV{"DEVEL_RINGBUF_GLOBALSZ"}
ABresult
1X1
011
000
199 7 ▶ my $create_stop = $args{StopOnCreate} || $ENV{DEVEL_RINGBUF_SOC} || 0;
Condition80%4/5
$args{'StopOnCreate'} || $ENV{'DEVEL_RINGBUF_SOC'} || 0
l!l
16
$args{"StopOnCreate"} or $ENV{"DEVEL_RINGBUF_SOC"}
l!l&&r!l&&!r
106
MC/DC50%1/2
$args{'StopOnCreate'} || $ENV{'DEVEL_RINGBUF_SOC'} || 0
$args{"StopOnCreate"} $ENV{"DEVEL_RINGBUF_SOC"}
Truth table66%2/3
A or B
A= $args{"StopOnCreate"}B= $ENV{"DEVEL_RINGBUF_SOC"}
ABresult
1X1
011
000
200 7 ▶ my $create_trace = $args{TraceOnCreate} || $ENV{DEVEL_RINGBUF_TOC} || 0;
Condition40%2/5
$args{'TraceOnCreate'} || $ENV{'DEVEL_RINGBUF_TOC'} || 0
l!l
70
$args{"TraceOnCreate"} or $ENV{"DEVEL_RINGBUF_TOC"}
l!l&&r!l&&!r
700
MC/DC0%0/2
$args{'TraceOnCreate'} || $ENV{'DEVEL_RINGBUF_TOC'} || 0
$args{"TraceOnCreate"} $ENV{"DEVEL_RINGBUF_TOC"}
Truth table33%1/3
A or B
A= $args{"TraceOnCreate"}B= $ENV{"DEVEL_RINGBUF_TOC"}
ABresult
1X1
011
000
201 #
202 # in order to avoid issues with word alignment, we'll always
203 # force slotsz, msg size, and global size to be word aligned
204 # (who knows, we may need to be 8 byte aligned on some platforms)
205 #
206 7 ▶ $slotsz += (4 - ($slotsz & 3)) if ($slotsz & 3);
Branch: 1 branch
BranchTrueFalseTotal
if $slotsz & 3 0 7 7
207 7 ▶ $ringmsgsz += (4 - ($ringmsgsz & 3)) if ($ringmsgsz & 3);
Branch: 1 branch
BranchTrueFalseTotal
if $ringmsgsz & 3 0 7 7
208 7 ▶ $globmsgsz += (4 - ($globmsgsz & 3)) if ($globmsgsz & 3);
Branch: 1 branch
BranchTrueFalseTotal
if $globmsgsz & 3 0 7 7
209  
210 7 my $freemap_offs = RINGBUF_RINGHDR_SZ + $globmsgsz;
211  
212 7 my $ringbufsz = _get_ring_size($ringslots, $slotsz, $ringmsgsz);
213  
214 7 my $ringsize = _get_total_size($ringcount, $ringslots, $slotsz, $ringmsgsz, $globmsgsz) +
215 1024; # Win32 needs some extra room
216  
217 7 ▶ my $self = bless [
Branch: 1 branch
BranchTrueFalseTotal
$Devel::RingBuffer::hasThreads ? : 0 7 7
218 $file,
219 $ringsize,
220 $ringcount,
221 $ringbufsz,
222 $ringslots,
223 ($hasThreads ? Devel::RingBuffer::ThreadFacade->tid() : 0),
224 $$,
225 undef,
226 undef,
227 $ringmsgsz,
228 $globmsgsz,
229 $freemap_offs,
230 _get_rings_addr(0, $ringcount, $globmsgsz),
231 $freemap_offs,
232 _get_rings_addr(0, $ringcount, $globmsgsz),
233 0,
234 $slotsz
235 ], $class;
236 #
237 # create the mmap'ed ring
238 #
239 #cluck "file is $file\n";
240 7 ▶ if ($anon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anon) { } 0 7 7
241 #
242 # on Win32 only...anonymous mmap is useless to us on POSIX
243 #
244 0 ▶ $self->[RINGBUF_RING] = IPC::Mmap->new($file, $ringsize,
Branch: 1 branch
BranchTrueFalseTotal
$self->[7] = "IPC::Mmap"->new($file, $ringsize, PROT_READ() | PROT_WRITE(), MAP_SHARED() | MAP_ANON()) or die "Can't open mmap file ${file}: $!" 0 0 0
245 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON)
246 or die "Can't open mmap file $file: $!";
247 }
248 else {
249 7 ▶ open(FH, ">$file") ||
Branch: 1 branch
BranchTrueFalseTotal
CORE::open FH, ">$file" or confess("Can't open mmap file ${file}: $!") 7 0 7
250 confess "Can't open mmap file $file: $!";
251 7 print FH "\0" x $ringsize;
252 7 close FH;
253  
254 7 ▶ $self->[RINGBUF_RING] = IPC::Mmap->new($file, $ringsize,
Branch: 1 branch
BranchTrueFalseTotal
$self->[7] = "IPC::Mmap"->new($file, $ringsize, PROT_READ() | PROT_WRITE(), MAP_SHARED() | MAP_FILE()) or die "Can't open mmap file ${file}: $!" 7 0 7
255 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FILE)
256 or die "Can't open mmap file $file: $!";
257 }
258 #
259 # share the thrdlock
260 #
261 7 ▶ if ($hasThreads) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Devel::RingBuffer::hasThreads) 0 7 7
262 # print STDERR "we're shared\n";
263 0 share($thrdlock);
264 }
265 #
266 # clear the ringbuffer (Win32 needs this)
267 #
268 7 my $ringbuffer = $self->[RINGBUF_RING] ;
269 7 my $var = "\0" x ($ringsize - 1024);
270 7 $ringbuffer->write($var, 0, $ringsize - 1024);
271 7 my $ringslotsz = $ringslots * $slotsz;
272 #
273 # then init it
274 #
275 return undef
276 7 ▶ unless $ringbuffer->pack(0, 'l l l l l l l l l',
Branch: 1 branch
BranchTrueFalseTotal
unless $ringbuffer->pack(0, "l l l l l l l l l", 0, $ringmsgsz, $ringcount, $ringslots, $slotsz, $create_stop, $create_trace, $globmsgsz, 0) 0 7 7
277 0, $ringmsgsz, $ringcount, $ringslots, $slotsz, $create_stop, $create_trace, $globmsgsz, 0);
278  
279 7 my $addr = $self->[RINGBUF_ADDRESS] = $self->[RINGBUF_RING]->getAddress();
280  
281 7 $self->[RINGBUF_MAP_ADDR] += $addr;
282 7 $self->[RINGBUF_RINGS_ADDR] += $addr;
283  
284 7 my $mapaddr = $self->[RINGBUF_MAP_ADDR];
285 7 my $ringsaddr = $self->[RINGBUF_RINGS_ADDR];
286 #
287 # let XS do init
288 #
289 _free_ring($mapaddr, $ringsaddr, $ringbufsz, $_)
290 7 foreach (0..$ringcount-1);
291 #
292 # for unknown reasons, the first map doesn't take ... so remap
293 #
294 # $self->remmap();
295  
296 7 return $self;
297 }
298  
299 #/**
300 # Get the name of the mmap'ed file.
301 #
302 # @return the name of the mmap'ed file
303 #*/
304 6 ▶sub getName { return $_[0]->[RINGBUF_FILENAME]; }
Subroutine getName: called CC=1 SCAR=0.0
Pod undocumented
305  
306 #/**
307 # Get base address of the mmap'ed file.
308 #
309 # @return the address of the mmap'ed file
310 #*/
311 0 ▶sub getAddress { return $_[0]->[RINGBUF_ADDRESS]; }
Subroutine getAddress: not called CC=1 SCAR=6.9
Pod undocumented
312  
313 #/**
314 # Allocate a ring buffer. Should only be used on ringbuffers created with new().
315 #
316 # @return a Devel::RingBuffer::Ring object on success.
317 # If no rings are available, returns undef.
318 #*/
319 24 ▶sub allocate {
Subroutine allocate: called CC=2 SCAR=6.9
Pod undocumented
320 24 my $self = shift;
321 #
322 # allocate a ring buffer and init it
323 #
324 # unless (($self->[RINGBUF_FLD_TID] == threads->self()->tid()) ||
325 # ($self->[RINGBUF_FLD_PID] == $$)) {
326 # On Win32, the fork() emulation means we shouldn't remap!!!
327 #
328 24 if (0) {
329 unless ($self->[RINGBUF_FLD_PID] == $$) {
330 #
331 # this probably isn't needed anymore for threads, but may be for
332 # processes...
333 #
334 my $file = $self->[RINGBUF_FILENAME];
335 my $ringsize = $self->[RINGBUF_SIZE];
336 $self->[RINGBUF_RING] = IPC::Mmap->new($file, $ringsize,
337 PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FILE) ||
338 die "Can't mmap file $file: $!";
339 $self->[RINGBUF_FLD_TID] = ($hasThreads ? Devel::RingBuffer::ThreadFacade->tid() : 0);
340 $self->[RINGBUF_FLD_PID] = $$;
341 }
342 }
343  
344 24 my $ring = 0;
345 24 my $ringbuffer = $self->[RINGBUF_RING];
346 24 $ringbuffer->lock();
347 {
348 24 lock($thrdlock);
349 #
350 # use XS to find free ring (for performance reasons)
351 #
352 24 $ring = _alloc_ring($self->[RINGBUF_MAP_ADDR], $self->[RINGBUF_COUNT]);
353 }
354 24 $ringbuffer->unlock();
355  
356 24 my $ringsaddr = $self->[RINGBUF_RINGS_ADDR];
357  
358 24 ▶ return defined($ring) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $ring ? : 24 0 24
359 Devel::RingBuffer::Ring->new(
360 $self,
361 _get_ring_addr($self->[RINGBUF_RINGS_ADDR],
362 $ring,
363 $self->[RINGBUF_SLOT_CNT],
364 $self->[RINGBUF_SLOT_SIZE],
365 $self->[RINGBUF_FLD_MSGAREA_SZ]),
366 $self->[RINGBUF_ADDRESS],
367 $ring,
368 $self->[RINGBUF_SLOT_CNT],
369 $self->[RINGBUF_FLD_MSGAREA_SZ],
370 ) :
371 undef;
372 }
373  
374 #/**
375 # Re-allocates a ring buffer. Required to handle threads' CLONE()
376 # of the existing ring buffer object when a new thread is created.
377 # C<reallocate()> simply allocates a ring buffer and returns its
378 # ring number, and its base address; the caller than updates
379 # an existing ring object with the returned values.
380 #
381 # @return the allocated ring index and address
382 #*/
383 0 ▶sub reallocate {
Subroutine reallocate: not called CC=2 SCAR=17.9
Pod undocumented
384 0 my $self = shift;
385  
386 0 my $newring = 0;
387 0 my $ringbuffer = $self->[RINGBUF_RING];
388 0 $ringbuffer->lock();
389 {
390 0 lock($thrdlock);
391 #
392 # use XS to find free ring (for performance reasons)
393 #
394 0 $newring = _alloc_ring($self->[RINGBUF_MAP_ADDR], $self->[RINGBUF_COUNT]);
395 }
396 0 $ringbuffer->unlock();
397  
398 0 ▶ return defined($newring) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $newring ? : 0 0 0
399 ($newring,
400 _get_ring_addr(
401 $self->[RINGBUF_RINGS_ADDR],
402 $newring,
403 $self->[RINGBUF_SLOT_CNT],
404 $self->[RINGBUF_SLOT_SIZE],
405 $self->[RINGBUF_FLD_MSGAREA_SZ])) :
406 ();
407 }
408  
409 #/**
410 # Constructor. Opens an existing mmap'd file for read/write
411 # access (for interactive debuggers)
412 #
413 # @param $file optional name of mmap'ed file (or namespace for Win32)
414 #
415 # @return Devel::RingBuffer object on success; undef on failure
416 #*/
417 0 ▶sub open {
Subroutine open: not called CC=1 SCAR=6.9
Pod undocumented
418 0 return _lcl_open(@_, PROT_READ|PROT_WRITE);
419 }
420  
421 #/**
422 # Constructor. Opens an existing mmap'd file for read-only
423 # access (for simple monitor applications)
424 #
425 # @param $file optional name of mmap'ed file (or namespace for Win32)
426 #
427 # @return Devel::RingBuffer object on success; undef on failure
428 #*/
429 0 ▶sub monitor {
Subroutine monitor: not called CC=1 SCAR=6.9
Pod undocumented
430 0 return _lcl_open(@_, PROT_READ);
431 }
432  
433 0 ▶sub _lcl_open {
Subroutine _lcl_open: not called CC=5 SCAR=34.0
434 0 my ($class, $file, $mode) = @_;
435 #
436 # open twice: first to get config params, then
437 # to map the whole file
438 #
439 # use anonymous open for Win32
440 #
441 0 ▶ my $flags = ($^O eq 'MSWin32') ?
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
442 MAP_SHARED | MAP_ANON :
443 MAP_SHARED | MAP_FILE;
444  
445 0 ▶ my $ringbuffer =
Branch: 1 branch
BranchTrueFalseTotal
my $ringbuffer = "IPC::Mmap"->new($file, 40, PROT_READ(), $flags) or die "Can't mmap file ${file}: $!" 0 0 0
446 IPC::Mmap->new($file, RINGBUF_RINGHDR_SZ, PROT_READ, $flags) or
447 die "Can't mmap file $file: $!";
448  
449 0 my ($msgareasz, $count, $slots, $slotsz, $stop, $trace, $globmsgsz) =
450 $ringbuffer->unpack(4, 28, 'l7');
451  
452 0 my $freemap_offs = RINGBUF_RINGHDR_SZ + $globmsgsz;
453  
454 0 my $ringbufsz = _get_ring_size($slots, $slotsz, $msgareasz);
455  
456 0 my $ringsize = _get_total_size($count, $slots, $slotsz, $msgareasz, $globmsgsz) +
457 1024; # Win32 needs some extra room
458  
459 0 $ringbuffer->close();
460  
461 0 ▶ $ringbuffer = IPC::Mmap->new($file, $ringsize, $mode, $flags)
Branch: 1 branch
BranchTrueFalseTotal
$ringbuffer = "IPC::Mmap"->new($file, $ringsize, $mode, $flags) or die "Can't mmap file ${file}: $!" 0 0 0
462 or die "Can't mmap file $file: $!";
463  
464 0 ▶ return bless [
Branch: 1 branch
BranchTrueFalseTotal
$Devel::RingBuffer::hasThreads ? : 0 0 0
465 $file,
466 $ringsize,
467 $count,
468 $ringbufsz,
469 $slots,
470 ($hasThreads ? Devel::RingBuffer::ThreadFacade->tid() : 0),
471 $$,
472 $ringbuffer,
473 undef,
474 $msgareasz,
475 $globmsgsz,
476 $freemap_offs,
477 _get_rings_addr(0, $count, $globmsgsz),
478 $ringbuffer->getAddress() + $freemap_offs,
479 _get_rings_addr($ringbuffer->getAddress(), $count, $globmsgsz),
480 $ringbuffer->getAddress(),
481 $slotsz
482 ], $class;
483 }
484  
485 #/**
486 # Get the free buffer map
487 #
488 # @return list of bytes, one per ring; if and element is 'true', the associated
489 # ring is free; otherwise the ring is in use.
490 #*/
491 1 ▶sub getMap {
Subroutine getMap: called CC=1 SCAR=0.0
Pod undocumented
492 1 return $_[0]->[RINGBUF_RING]->unpack(
493 $_[0]->[RINGBUF_MAP_OFFSET],
494 $_[0]->[RINGBUF_COUNT],
495 'C' . $_[0]->[RINGBUF_COUNT] );
496 }
497  
498 #/**
499 # Get the RingBuffer global header fields. The fields
500 # returned include:
501 # <p>
502 # <ol>
503 # <li>single - global control variable
504 # <li>msgarea_sz - size of per-thread message area
505 # <li>max_buffer - number of configured rings
506 # <li>slots - number of slots per ring
507 # <li>slot_sz - size of each slot (excluding linenumber and timestamp header)
508 # <li>stop_on_create - 1 => new threads created with signal = 1
509 # <li>trace_on_create - 1 => new threads created with trace = 1
510 # <li>global_sz - size of global message buffer
511 # <li>globmsg_total - size of complete global message contents
512 # <li>globmsg_sz - size of current global message fragment
513 # </ol>
514 #
515 # @return list of the specified header values
516 #*/
517 0 ▶sub getHeader {
Subroutine getHeader: not called CC=1 SCAR=6.9
Pod undocumented
518 0 return $_[0]->[RINGBUF_RING]->unpack(0, 40, 'l10');
519 }
520 #/**
521 # Open and return a Devel::RingBuffer::Ring object
522 # for the specified ring number.
523 #
524 # @param $ringnum number of ring to be opened
525 #
526 # @return Devel::RingBuffer::Ring object
527 #*/
528 19 ▶sub getRing {
Subroutine getRing: called CC=1 SCAR=0.0
Pod undocumented
529 19 my ($self, $ringnum) = @_;
530 19 return Devel::RingBuffer::Ring->open(
531 $self,
532 _get_ring_addr(
533 $self->[RINGBUF_RINGS_ADDR],
534 $ringnum,
535 $self->[RINGBUF_SLOT_CNT],
536 $self->[RINGBUF_SLOT_SIZE],
537 $self->[RINGBUF_FLD_MSGAREA_SZ]),
538 $self->[RINGBUF_ADDRESS],
539 $ringnum,
540 $self->[RINGBUF_SLOT_CNT],
541 $self->[RINGBUF_FLD_MSGAREA_SZ]
542 );
543 }
544  
545 #/**
546 # Get the configured number of slots per ring.
547 #
548 # @return the number of slots configured for the ring buffer.
549 #*/
550 0 ▶sub getSlots { return $_[0]->[RINGBUF_SLOT_CNT]; }
Subroutine getSlots: not called CC=1 SCAR=6.9
Pod undocumented
551 #/**
552 # Get the configured size of slots.
553 #
554 # @return the slot size
555 #*/
556 0 ▶sub getSlotSize { return $_[0]->[RINGBUF_SLOT_SIZE]; }
Subroutine getSlotSize: not called CC=1 SCAR=6.9
Pod undocumented
557 #/**
558 # Get the number of configured rings.
559 #
560 # @return the count of rings
561 #*/
562 6 ▶sub getCount { return $_[0]->[RINGBUF_COUNT]; }
Subroutine getCount: called CC=1 SCAR=0.0
Pod undocumented
563 #/**
564 # Close the ring buffer.
565 #
566 # @deprecated
567 #*/
568 1 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod undocumented
569 1 my $self = shift;
570 1 my $ring = delete $self->[RINGBUF_RING];
571 1 return 1;
572 }
573 #/**
574 # Free a ring. Returns a ring to the free list
575 #
576 # @param $ring the ring object to be freed
577 #*/
578 23 ▶sub free {
Subroutine free: called CC=2 SCAR=7.0
Pod undocumented
579 23 my ($self, $ring) = @_;
580 #print STDERR "freeing ring $ring\n";
581 23 ▶ return 1 unless $self->[RINGBUF_RING];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[7] 0 23 23
582  
583 23 my $ringbuffer = $self->[RINGBUF_RING];
584 23 $ringbuffer->lock();
585 {
586 23 lock($thrdlock);
587 #
588 # XS handles everything but the locks
589 #
590 23 _free_ring($self->[RINGBUF_MAP_ADDR],
591 $self->[RINGBUF_RINGS_ADDR],
592 $self->[RINGBUF_BUFSIZE],
593 $ring);
594 }
595  
596 23 $ringbuffer->unlock();
597 }
598 #/**
599 # Get the IPC::Mmap object used to store the ringbuffer.
600 #
601 # @return the IPC::Mmap object
602 #*/
603 0 ▶sub getMmap { return $_[0]->[RINGBUF_RING]; }
Subroutine getMmap: not called CC=1 SCAR=6.9
Pod undocumented
604 #
605 # just check for the current thread/process's ring instance;
606 # note this can be a lengthy process, since we must
607 # scan the mmap'd ring buffer headers for matching PID/TID,
608 # and then free it
609 #
610 # !!!DPERECATED!!! We can't permit DESTROY if cloned versions
611 # might destroy things; just let process run down deal with
612 # closing the file
613 #
614 0 ▶sub OLDDESTROY {
Subroutine OLDDESTROY: not called CC=4 SCAR=30.0
Pod undocumented
615 0 my $self = shift;
616 0 ▶ my $tid = ($hasThreads ? Devel::RingBuffer::ThreadFacade->tid() : 0);
Branch: 1 branch
BranchTrueFalseTotal
$Devel::RingBuffer::hasThreads ? : 0 0 0
617  
618 0 ▶ return unless $self->[RINGBUF_RING];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[7] 0 0 0
619  
620 0 print STDERR "RingBuffer DESTROYING in thread $tid\n";
621  
622 0 my $ringbuffer = $self->[RINGBUF_RING];
623 0 $ringbuffer->lock();
624 {
625 0 lock($thrdlock);
626 #
627 # XS handles everything but the locks
628 #
629 0 my $ring = _find_ring($self->[RINGBUF_RINGS_ADDR],
630 $self->[RINGBUF_BUFSIZE], $self->[RINGBUF_COUNT], $$, $tid);
631 0 ▶ _free_ring($self->[RINGBUF_MAP_ADDR],
Branch: 1 branch
BranchTrueFalseTotal
if defined $ring 0 0 0
632 $self->[RINGBUF_RINGS_ADDR],
633 $self->[RINGBUF_BUFSIZE],
634 $ring)
635 if defined($ring);
636 }
637 0 $ringbuffer->unlock();
638 }
639  
640 #/**
641 # Sets the value of the global single field.
642 #
643 # @param value to set
644 #
645 # @return the prior value of the field.
646 #*/
647 0 ▶sub setSingle {
Subroutine setSingle: not called CC=1 SCAR=6.9
Pod undocumented
648 0 return $_[0]->[RINGBUF_RING]->pack(0, 'l', $_[1]);
649 }
650  
651 #/**
652 # Gets the value of the global single field.
653 #
654 # @return the value of the field.
655 #*/
656 0 ▶sub getSingle {
Subroutine getSingle: not called CC=1 SCAR=6.9
Pod undocumented
657 0 return $_[0]->[RINGBUF_RING]->unpack(0, 4, 'l');
658 }
659  
660 #/**
661 # Sets the value of the stop_on_create field.
662 #
663 # @return the prior value of the field.
664 #*/
665 0 ▶sub setStopOnCreate {
Subroutine setStopOnCreate: not called CC=1 SCAR=6.9
Pod undocumented
666 0 return $_[0]->[RINGBUF_RING]->pack(RINGBUF_CREATE_STOP, 'l', $_[1]);
667 }
668  
669 #/**
670 # Get the value of the stop_on_create field.
671 #
672 # @return the current value of the field.
673 #*/
674 1 ▶sub getStopOnCreate {
Subroutine getStopOnCreate: called CC=1 SCAR=0.0
Pod undocumented
675 1 return $_[0]->[RINGBUF_RING]->unpack(RINGBUF_CREATE_STOP, 4, 'l');
676 }
677  
678 #/**
679 # Sets the value of the trace_on_create field.
680 #
681 # @param $trace_on_create value to set
682 # @return the prior value of the field
683 #*/
684 0 ▶sub setTraceOnCreate {
Subroutine setTraceOnCreate: not called CC=1 SCAR=6.9
Pod undocumented
685 0 return $_[0]->[RINGBUF_RING]->pack(RINGBUF_CREATE_TRACE, 'l', $_[1]);
686 }
687  
688 #/**
689 # Get the value of the trace_on_create field.
690 #
691 # @return the value of the field
692 #*/
693 1 ▶sub getTraceOnCreate {
Subroutine getTraceOnCreate: called CC=1 SCAR=0.0
Pod undocumented
694 1 return $_[0]->[RINGBUF_RING]->unpack(RINGBUF_CREATE_TRACE, 4, 'l');
695 }
696  
697 #/**
698 # Sets a message into the global message area. Note that
699 # this operation requires locking the entire ring buffer
700 # header until the message is completely transfered.
701 # Messages larger than the configured global message size
702 # will be transfered in chunks; each chunk must back ACK'd by
703 # the message receiver.
704 #
705 # @param $msg the message to send
706 #
707 # @return the RingBuffer object
708 #*/
709 0 ▶sub setGlobalMsg {
Subroutine setGlobalMsg: not called CC=5 SCAR=34.0
Pod undocumented
710 0 my $self = shift;
711 0 my $ringbuffer = $self->[RINGBUF_RING];
712 0 my $globsz = $self->[RINGBUF_FLD_GLOBAL_SZ];
713 0 my $first = 1;
714 0 $ringbuffer->lock();
715 {
716 0 lock($thrdlock);
717 0 my ($t, $frag) = (0,0);
718 0 my $len = length($_[0]);
719 0 ▶ while ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 0 0 0
720 #
721 # may need to fragment
722 #
723 0 ▶ $t = ($len > $globsz) ? $globsz : $len;
Branch: 1 branch
BranchTrueFalseTotal
$len > $globsz ? : 0 0 0
724 0 $ringbuffer->write(substr($_[0], $frag, $t), RINGBUF_GLOBAL_MSG, $t);
725 0 $ringbuffer->pack(RINGBUF_GLOBMSG_SZ, 'l', $t);
726 #
727 # set this last so reader doesn't read to soon
728 #
729 0 ▶ $ringbuffer->pack(RINGBUF_TOTALMSG_SZ, 'l', $len),
Branch: 1 branch
BranchTrueFalseTotal
if $first 0 0 0
730 $first = undef
731 if $first;
732  
733 0 $len -= $t;
734 0 $frag += $t;
735 #
736 # wait for ACK that its been read
737 #
738 0 ▶ sleep RINGBUF_RING_WAIT,
Branch: 1 branch
BranchTrueFalseTotal
$t and do { sleep "0.3", $t = $ringbuffer->unpack(36, 4, "l") } 0 0 0
739 $t = $ringbuffer->unpack(RINGBUF_GLOBMSG_SZ, 4, 'l')
740 while $t;
741 }
742 0 $ringbuffer->pack(RINGBUF_TOTALMSG_SZ, 'l', 0);
743 }
744 0 $ringbuffer->unlock();
745 0 return $self;
746 }
747  
748 #/**
749 # Gets a message from the global message area. Note that
750 # this operation B<does not> lock the entire ring buffer
751 # header, but instead relies on signalling of the message
752 # chunk lengths.
753 # Messages larger than the configured global message size
754 # will be received in chunks; each chunk must back ACK'd by
755 # the message receiver.
756 #
757 # @return the re-assembled global message buffer contents
758 #
759 #*/
760 0 ▶sub getGlobalMsg {
Subroutine getGlobalMsg: not called CC=4 SCAR=30.0
Pod undocumented
761 0 my $self = shift;
762 0 my $ringbuffer = $self->[RINGBUF_RING];
763 0 my $globsz = $self->[RINGBUF_FLD_GLOBAL_SZ];
764 0 my $result = '';
765 0 my $frag;
766 my $t;
767 #
768 # wait for indication that msg is available
769 #
770 0 my $len = $ringbuffer->unpack(RINGBUF_TOTALMSG_SZ, 4, 'l');
771  
772 0 ▶ sleep RINGBUF_RING_WAIT,
Branch: 1 branch
BranchTrueFalseTotal
$len or do { sleep "0.3", $len = $ringbuffer->unpack(32, 4, "l") } 0 0 0
773 $len = $ringbuffer->unpack(RINGBUF_TOTALMSG_SZ, 4, 'l')
774 until $len;
775  
776 0 ▶ while ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 0 0 0
777 #
778 # may be fragmented
779 # wait for length field
780 #
781 0 ▶ sleep RINGBUF_RING_WAIT,
Branch: 1 branch
BranchTrueFalseTotal
$t or do { sleep "0.3", $t = $ringbuffer->unpack(36, 4, "l") } 0 0 0
782 $t = $ringbuffer->unpack(RINGBUF_GLOBMSG_SZ, 4, 'l')
783 until $t;
784  
785 0 $ringbuffer->read($frag, RINGBUF_GLOBAL_MSG, $t);
786 0 $len -= $t;
787 0 $result .= $frag;
788 #
789 # ACK it
790 #
791 0 $ringbuffer->pack(RINGBUF_GLOBMSG_SZ, 'l', 0);
792 }
793 0 return $result;
794 }
795  
796 7 1;
↑ Summary blib/lib/Devel/RingBuffer/Ring.pm »