lib/Mojo/IOLoop/ReadWriteProcess/Shared/Memory.pm

filter: Statementstmt 29.2% 38 / 130 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 21.0% 8 / 38 Podpod 7.1% 1 / 14 totaltotal 16.7% 47 / 281 CC 33 SCAR 38.9
CC
33
Cov
18%
save
210.0
_loadsize
30.0
load
20.0
CRAP 48.8
« lib/Mojo/IOLoop/ReadWriteProcess/Shared/Semaphore.pm ↑ Summary lib/Mojo/IOLoop/ReadWriteProcess.pm »
1 package Mojo::IOLoop::ReadWriteProcess::Shared::Memory;
2  
3 39 ▶use Mojo::IOLoop::ReadWriteProcess::Shared::Lock;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 39 ▶use Carp qw(croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use constant DEBUG => $ENV{MOJO_PROCESS_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use IPC::SharedMem;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 use IPC::SysV
11 39 ▶ qw(ftok IPC_PRIVATE IPC_NOWAIT IPC_CREAT IPC_EXCL S_IRUSR S_IWUSR S_IRGRP S_IWGRP S_IROTH S_IWOTH SEM_UNDO S_IRWXU S_IRWXG);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 39 our @EXPORT_OK = qw(shared_memory shared_lock semaphore);
14 39 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶has key => sub { Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore::_genkey() };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17 39 has 'buffer';
18 39 has destroy => 0;
19 39 has flags => S_IRWXU() | S_IRWXG() | IPC_CREAT();
20 39 has lock_flags => IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
21 | S_IROTH | S_IWOTH;
22 39 has _size => 10 * 1024;
23 0 ▶has _shared_memory => sub { $_[0]->_newmem() };
Subroutine __ANON__: not called CC=1 SCAR=1.2
24 has _shared_size =>
25 0 ▶ sub { $_[0]->_newmem((2 * shift->key) - 1, $Config{intsize}) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
26 0 ▶has _lock => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 Mojo::IOLoop::ReadWriteProcess::Shared::Lock->new(
28 flags => $_[0]->lock_flags,
29 key => (2 * shift->key) + 1
30 );
31 39 };
32  
33 39 has dynamic_resize => 1;
34 39 has dynamic_decrement => 1;
35 39 has dynamic_increment => 1;
36  
37 0 ▶sub shared_lock { Mojo::IOLoop::ReadWriteProcess::Shared::Lock->new(@_) }
Subroutine shared_lock: not called CC=1 SCAR=6.9
Pod undocumented
38 0 ▶sub semaphore { Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore->new(@_) }
Subroutine semaphore: not called CC=1 SCAR=6.9
Pod undocumented
39 0 ▶sub shared_memory { __PACKAGE__->new(@_) }
Subroutine shared_memory: not called CC=1 SCAR=6.9
Pod undocumented
40  
41 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
42 0 my $s = shift->SUPER::new(@_);
43 0 ▶ confess 'Could not allocate shared size memory ' . $s->key
Branch: 1 branch
BranchTrueFalseTotal
unless $s->_shared_size 0 0 0
44 unless $s->_shared_size;
45 0 $s->_loadsize;
46 0 ▶ confess 'Could not allocate shared memory with key ' . $s->key
Branch: 1 branch
BranchTrueFalseTotal
unless $s->_shared_memory 0 0 0
47 unless $s->_shared_memory;
48 0 return $s;
49 }
50  
51 0 ▶sub _encode_content { $_[0]->buffer(unpack 'H*', shift->buffer()) }
Subroutine _encode_content: not called CC=1 SCAR=6.9
52 0 ▶sub _decode_content { $_[0]->buffer(pack 'H*', shift->buffer()) }
Subroutine _decode_content: not called CC=1 SCAR=6.9
53  
54 0 ▶sub _writesize {
Subroutine _writesize: not called CC=1 SCAR=6.9
55 0 my $self = shift;
56 0 my $size = shift;
57 0 $self->_shared_size()->write(pack('I', $size), 0, $Config{intsize});
58 }
59  
60 0 ▶sub _readsize {
Subroutine _readsize: not called CC=1 SCAR=6.9
61 0 my $self = shift;
62 0 my $s = $self->_shared_size()->read(0, $Config{intsize});
63 0 return unpack('I', $s);
64 }
65  
66 0 ▶sub _loadsize {
Subroutine _loadsize: not called CC=5 SCAR=34.0
67 0 my $s = $_[0]->_readsize;
68 0 my $cur_size = $_[0]->_size;
69 0 ▶ $s = $_[0]->_size if $s == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $s == 0 0 0 0
70 0 ▶ $_[0]->_size($s =~ /\d/ ? $s : $_[0]->_size);
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /\d/u ? : 0 0 0
71 0 ▶ $_[0]->_writesize($_[0]->_size) and $_[0]->_shared_memory($_[0]->_newmem)
Branch: 1 branch
BranchTrueFalseTotal
if $s != $cur_size 0 0 0
Condition0%0/6
$s != $cur_size && ($_[0]->_writesize($_[0]->_size) && $_[0]->_shared_memory($_[0]->_newmem))
!ll&&!rl&&r
000
$_[0]->_writesize($_[0]->_size) and $_[0]->_shared_memory($_[0]->_newmem)
!ll&&!rl&&r
000
MC/DC0%0/3
$s != $cur_size && ($_[0]->_writesize($_[0]->_size) && $_[0]->_shared_memory($_[0]->_newmem))
$s != $cur_size $_[0]->_writesize($_[0]->_size) $_[0]->_shared_memory($_[0]->_newmem)
Truth table0%0/4
A && B and C
A= $s != $cur_sizeB= $_[0]->_writesize($_[0]->_size)C= $_[0]->_shared_memory($_[0]->_newmem)
ABCresult
0XX0
10X0
1100
1111
72 if $s != $cur_size;
73  
74 0 warn "[debug:$$] Mem size: " . $_[0]->_size if DEBUG;
75 }
76  
77 0 ▶sub _reload {
Subroutine _reload: not called CC=2 SCAR=17.9
78 0 $_[0]->_shared_memory($_[0]->_newmem);
79 0 ▶ $_[0]->_shared_memory($_[0]->_newmem) until defined $_[0]->_shared_memory;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->_shared_memory or do { $_[0]->_shared_memory($_[0]->_newmem) } 0 0 0
80 }
81  
82 # Must be run in a locked section
83 0 ▶sub resize {
Subroutine resize: not called CC=3 SCAR=24.8
Pod undocumented
84 0 my $self = shift;
85 0 $self->_shared_memory->detach();
86 0 ▶ 1 until $self->_safe_remove;
Branch: 1 branch
BranchTrueFalseTotal
$self->_safe_remove or do { '???' } 0 0 0
87 0 ▶ $self->_size($_[0] // length $self->buffer);
Condition0%0/3
$_[0] // length $self->buffer
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] // length $self->buffer
$_[0] length $self->buffer
Truth table0%0/3
A // B
A= $_[0]B= length $self->buffer
ABresult
1X1
011
000
88 0 $self->_reload;
89  
90 # XXX: is faster to re-allocate the shared memory with shmctl, but SHM_SIZE
91 # seems to not be really portable:
92 # shmctl $_[0]->_shared_memory->id, SHM_SIZE, struct
93 # $_[0]->_writesize($_[1] // length $_[0]->buffer ) if $_[0]->_shared_memory;
94 }
95  
96 # Must be run in a locked section
97 0 ▶sub _sync_size {
Subroutine _sync_size: not called CC=1 SCAR=6.9
98 0 warn "[debug:$$] Sync size for content ("
99 . length($_[0]->buffer)
100 . ") vs currently allocated ("
101 . $_[0]->_size . ")"
102 if DEBUG;
103 0 $_[0]->resize;
104 }
105  
106 0 ▶sub save {
Subroutine save: not called CC=14 SCAR=53.5
Pod undocumented
107 0 warn "[debug:$$] Writing data : " . $_[0]->buffer if DEBUG;
108  
109 0 $_[0]->_encode_content;
110  
111 0 eval {
112 # Resize
113 0 ▶ $_[0]->_sync_size
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->dynamic_resize and $_[0]->dynamic_increment && (defined $_[0]->buffer && length $_[0]->buffer > $_[0]->_size) || $_[0]->dynamic_decrement && (defined $_[0]->buffer && $_[0]->_size > length $_[0]->buffer) 0 0 0
Condition0%0/18
$_[0]->dynamic_resize and $_[0]->dynamic_increment && (defined $_[0]->buffer && length $_[0]->buffer > $_[0]->_size) || $_[0]->dynamic_decrement && (defined $_[0]->buffer && $_[0]->_size > length $_[0]->buffer)
!ll&&!rl&&r
000
$_[0]->dynamic_increment and (defined $_[0]->buffer and length $_[0]->buffer > $_[0]->_size) or $_[0]->dynamic_decrement and (defined $_[0]->buffer and $_[0]->_size > length $_[0]->buffer)
l!l&&r!l&&!r
000
$_[0]->dynamic_increment and (defined $_[0]->buffer and length $_[0]->buffer > $_[0]->_size)
!ll&&!rl&&r
000
defined $_[0]->buffer and length $_[0]->buffer > $_[0]->_size
!ll&&!rl&&r
000
$_[0]->dynamic_decrement and (defined $_[0]->buffer and $_[0]->_size > length $_[0]->buffer)
!ll&&!rl&&r
000
defined $_[0]->buffer and $_[0]->_size > length $_[0]->buffer
!ll&&!rl&&r
000
MC/DC0%0/7
$_[0]->dynamic_resize and $_[0]->dynamic_increment && (defined $_[0]->buffer && length $_[0]->buffer > $_[0]->_size) || $_[0]->dynamic_decrement && (defined $_[0]->buffer && $_[0]->_size > length $_[0]->buffer)
$_[0]->dynamic_resize $_[0]->dynamic_increment defined $_[0]->buffer length $_[0]->buffer > $_[0]->_size $_[0]->dynamic_decrement defined $_[0]->buffer $_[0]->_size > length $_[0]->buffer
Truth table0%0/14
A and B and C and D or E and F and G
A= $_[0]->dynamic_resizeB= $_[0]->dynamic_incrementC= defined $_[0]->bufferD= length $_[0]->buffer > $_[0]->_sizeE= $_[0]->dynamic_decrementF= defined $_[0]->bufferG= $_[0]->_size > length $_[0]->buffer
ABCDEFGresult
0XXXXXX0
10XX0XX0
10XX10X0
10XX1100
110X0XX0
110X10X0
110X1100
11100XX0
111010X0
11101100
1111XXX1
10XX1111
110X1111
11101111
114 if (
115 $_[0]->dynamic_resize && (
116 (
117 $_[0]->dynamic_increment
118 && (defined $_[0]->buffer && length $_[0]->buffer > $_[0]->_size)
119 ) # Increment
120 || ($_[0]->dynamic_decrement
121 && (defined $_[0]->buffer && $_[0]->_size > length $_[0]->buffer)
122 ) # Decrement
123 ));
124 0 ▶ $_[0]->_writesize($_[0]->_size) if $_[0]->_shared_memory();
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->_shared_memory 0 0 0
125  
126 # $_[0]->_reload;
127  
128 0 ▶ $_[0]->_shared_memory()->write($_[0]->buffer, 0, $_[0]->_size)
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->_shared_memory 0 0 0
129 if $_[0]->_shared_memory();
130 };
131  
132 0 ▶ warn "[debug:$$] Error Saving data : $@" if $@ && DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and undef 0 0 0
Condition0%0/2
$@ and undef
!ll
00
MC/DC0%0/1
$@ and undef
$@
Truth table0%0/2
A
A= $@
Aresult
00
11
133  
134 0 ▶ $_[0]->_shared_memory->detach() if $_[0]->_shared_memory;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->_shared_memory 0 0 0
135 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
136 0 return 1;
137 }
138  
139 0 ▶sub _newmem {
Subroutine _newmem: not called CC=3 SCAR=24.8
140 0 ▶ IPC::SharedMem->new(
Condition0%0/6
$_[1] // $_[0]->key
l!l&&r!l&&!r
000
$_[2] // $_[0]->_size
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] // $_[0]->key
$_[1] $_[0]->key
MC/DC0%0/2
$_[2] // $_[0]->_size
$_[2] $_[0]->_size
Truth table0%0/3
A // B
A= $_[1]B= $_[0]->key
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $_[2]B= $_[0]->_size
ABresult
1X1
011
000
141 $_[1] // $_[0]->key(),
142 $_[2] // $_[0]->_size,
143 $_[0]->flags
144 );
145 }
146  
147 0 ▶sub load {
Subroutine load: not called CC=4 SCAR=30.0
Pod undocumented
148  
149 0 eval {
150 0 $_[0]->_loadsize;
151 0 warn "[debug:$$] Reading " . $_[0]->_size if DEBUG;
152 0 $_[0]->_reload;
153 0 $_[0]->_shared_memory->attach();
154 0 $_[0]->buffer($_[0]->_shared_memory()->read(0, $_[0]->_size));
155  
156 # XXX: Remove the 0 padding?
157 # substr($_[0]->{buffer}, index($_[0]->{buffer}, "\0")) = "";
158 0 $_[0]->_decode_content;
159 };
160  
161 0 ▶ warn "[debug:$$] Error Loading data : $@" if $@ && DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and undef 0 0 0
Condition0%0/2
$@ and undef
!ll
00
MC/DC0%0/1
$@ and undef
$@
Truth table0%0/2
A
A= $@
Aresult
00
11
162 0 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
163 0 return 1;
164 }
165  
166 0 ▶sub _safe_remove {
Subroutine _safe_remove: not called CC=3 SCAR=24.8
167 0 my $self = shift;
168 0 my $stat = $self->_shared_memory()->stat();
169 0 ▶ if (defined($stat) && ($stat->nattch() == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stat and $stat->nattch == 0) 0 0 0
Condition0%0/3
defined $stat and $stat->nattch == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $stat and $stat->nattch == 0
defined $stat $stat->nattch == 0
Truth table0%0/3
A and B
A= defined $statB= $stat->nattch == 0
ABresult
0X0
100
111
170 0 $self->_shared_memory()->remove();
171 0 return 1;
172 }
173 0 return 0;
174 }
175  
176 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my $self = shift;
178 0 $self->_shared_memory->detach();
179 0 $self->_lock->remove;
180 0 $self->_shared_size()->remove();
181 0 return $self->_safe_remove;
182 }
183  
184 0 ▶sub clean {
Subroutine clean: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my $self = shift;
186 0 ▶ $self->lock_section(sub { $self->buffer(' ')->save });
Subroutine __ANON__: not called CC=1 SCAR=6.9
187 }
188  
189 0 ▶sub unlock {
Subroutine unlock: not called CC=1 SCAR=6.9
Pod undocumented
190 0 eval { $_[0]->save };
191 0 shift->_lock->unlock(@_);
192 }
193  
194 0 ▶sub lock { my $s = shift; my $r = $s->_lock->lock(@_); $s->load; $r }
Subroutine lock: not called CC=1 SCAR=6.9
Pod undocumented
195 0 ▶sub try_lock { $_[0]->_lock->try_lock() }
Subroutine try_lock: not called CC=1 SCAR=6.9
Pod undocumented
196  
197 0 ▶sub lock_section {
Subroutine lock_section: not called CC=1 SCAR=6.9
Pod undocumented
198 0 my ($self, $fn) = @_;
199  
200 return $self->_lock->lock_section(
201 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
202 0 my $r;
203 {
204 0 $self->load;
205 0 local $@;
206 0 $r = eval { $fn->() };
207 0 ▶ warn "[debug:$$] Error inside locked memory section : $@"
Branch: 1 branch
BranchTrueFalseTotal
if $@ and undef 0 0 0
Condition0%0/2
$@ and undef
!ll
00
MC/DC0%0/1
$@ and undef
$@
Truth table0%0/2
A
A= $@
Aresult
00
11
208 if $@ && DEBUG;
209 0 eval { $self->save };
210 };
211 0 return $r;
212 0 });
213 }
214  
215 0 ▶sub stat { shift->_shared_memory->stat }
Subroutine stat: not called CC=1 SCAR=6.9
Pod undocumented
216  
217 0 ▶sub DESTROY { $_[0]->remove if $_[0]->destroy() }
Subroutine DESTROY: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->destroy 0 0 0
218  
219 39 !!42;
« lib/Mojo/IOLoop/ReadWriteProcess/Shared/Semaphore.pm ↑ Summary lib/Mojo/IOLoop/ReadWriteProcess.pm »