Match/PctMem.pm

filter: Statementstmt 74.0% 57 / 77 Branchbran 58.0% 29 / 50 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 66.6% 100 / 150 CC 30 SCAR 33.5
CC
30
Cov
66%
match
40.7
new
18.0
BEGIN
1.0
CRAP 28.4
« Match/UID.pm ↑ Summary Match/Time.pm »
1 package Proc::ProcessTable::Match::PctMem;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Proc::ProcessTable::Match::PctMem - Check if the memory usage by percent of a process matches.
11  
12 =head1 VERSION
13  
14 Version 0.1.0
15  
16 =cut
17  
18 2 our $VERSION = '0.1.0';
19  
20  
21 =head1 SYNOPSIS
22  
23 use Proc::ProcessTable::Match::PctMem;
24
25 my %args=(
26 PctMems=>[
27 0,
28 '>1000',
29 ],
30 );
31
32 my $checker=Proc::ProcessTable::Match::PctMem->new( \%args );
33
34 if ( $checker->match( $proc ) ){
35 print "It matches.\n";
36 }
37  
38 =head1 METHODS
39  
40 =head2 new
41  
42 This intiates the object.
43  
44 It takes a hash reference with one key. One key is required and
45 that is 'PctMems', which is a array of PctMem values to match.
46  
47 The PctMem values can be prefixed with the equalities below for doing
48 additional comparisons.
49  
50 <
51 <=
52 >
53 >=
54 !
55  
56 Atleast one value must be specified.
57  
58 If the new method fails, it dies.
59  
60 my %args=(
61 pctmems=>[
62 0,
63 '>1000',
64 ],
65 );
66
67 my $checker=Proc::ProcessTable::Match::PctMem->new( \%args );
68  
69 =cut
70  
71 6 ▶sub new{
Subroutine new: called CC=12 SCAR=28.9
Pod documented
72 6 my %args;
73 6 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 6 0 6
74 6 %args= %{$_[1]};
75 };
76  
77 # run some basic checks to make sure we have the minimum stuff required to work
78 6 ▶ if ( ! defined( $args{pctmems} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"pctmems"}) 1 5 6
79 1 die ('No pctmems key specified in the argument hash');
80 }
81 5 ▶ if ( ref( $args{pctmems} ) ne 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"pctmems"} ne "ARRAY") 0 5 5
82 0 die ('The pctmems key is not a array');
83 }
84 5 ▶ if ( ! defined $args{pctmems}[0] ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"pctmems"}[0]) 1 4 5
85 1 die ('Nothing defined in the pctmems array');
86 }
87  
88 4 my $class=$_[0];
89 my $self = {
90 pctmems=>$args{pctmems},
91 4 physmem=>undef,
92 };
93 4 bless $self, $class;
94  
95 # Figure out how much physical memory this machine has, which is
96 # needed on platforms where Proc::ProcessTable does not provide
97 # pctmem. First try POSIX::sysconf, which requires no external
98 # programs, but not every POSIX module exposes _SC_PHYS_PAGES.
99 4 eval{
100 4 my $phys_pages=POSIX::sysconf( POSIX::_SC_PHYS_PAGES() );
101 0 my $pagesize=POSIX::sysconf( POSIX::_SC_PAGESIZE() );
102 0 ▶ if ( $phys_pages && $pagesize ){
Branch: 1 branch
BranchTrueFalseTotal
if ($phys_pages and $pagesize) 0 0 0
Condition0%0/3
$phys_pages and $pagesize
!ll&&!rl&&r
000
MC/DC0%0/2
$phys_pages and $pagesize
$phys_pages $pagesize
Truth table0%0/3
A and B
A= $phys_pagesB= $pagesize
ABresult
0X0
100
111
103 0 $self->{physmem}=$phys_pages * $pagesize;
104 }
105 };
106  
107 # Fall back on sysctl, checking the various places it may live.
108 4 ▶ if ( ! defined( $self->{physmem} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"physmem"}) 4 0 4
109 4 foreach my $sysctl_bin ( '/sbin/sysctl', '/usr/sbin/sysctl' ){
110 8 ▶ if ( -x $sysctl_bin ){
Branch: 1 branch
BranchTrueFalseTotal
if (-x $sysctl_bin) 8 0 8
111 8 my $physmem=`$sysctl_bin -n hw.physmem 2> /dev/null`;
112 8 ▶ if ( defined( $physmem ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $physmem) 8 0 8
113 8 chomp( $physmem );
114 8 ▶ if ( $physmem =~ /^[0-9]+$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($physmem =~ /^[0-9]+$/) 0 8 8
115 0 $self->{physmem}=$physmem;
116 0 last;
117 }
118 }
119 }
120 }
121 }
122  
123 # If physmem could not be found, match falls back on requiring
124 # the pctmem method, so failing here is not fatal.
125  
126 4 return $self;
127 }
128  
129 =head2 match
130  
131 Checks if a single Proc::ProcessTable::Process object matches the stack.
132  
133 One argument is taken and that is a Proc::ProcessTable::Process object.
134  
135 The returned value is a boolean.
136  
137 if ( $checker->match( $proc ) ){
138 print "The process matches.\n";
139 }
140  
141 =cut
142  
143 7 ▶sub match{
Subroutine match: called CC=19 SCAR=37.1
Pod documented
144 7 my $self=$_[0];
145 7 my $object=$_[1];
146  
147 7 ▶ if ( !defined( $object ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 0 7 7
148 0 return 0;
149 }
150  
151 7 ▶ if ( ref( $object ) ne 'Proc::ProcessTable::Process' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object ne "Proc::ProcessTable::Process") 0 7 7
152 0 return 0;
153 }
154  
155 7 my $proc_pctmem;
156 7 eval{
157 7 $proc_pctmem=$object->pctmem;
158 };
159  
160 # Not all platforms provide pctmem, so on those compute it from
161 # the RSS, which Proc::ProcessTable provides in bytes everywhere.
162 7 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $proc_pctmem and defined $self->{"physmem"}) 0 7 7
Condition33%1/3
not defined $proc_pctmem and defined $self->{"physmem"}
!ll&&!rl&&r
700
MC/DC0%0/2
not defined $proc_pctmem and defined $self->{"physmem"}
not defined $proc_pctmem defined $self->{"physmem"}
Truth table33%1/3
not(A) and B
A= not defined $proc_pctmemB= defined $self->{"physmem"}
ABresult
0X0
100
111
163 ( ! defined( $proc_pctmem ) ) &&
164 ( defined( $self->{physmem} ) )
165 ){
166 0 eval{
167 0 $proc_pctmem=( $object->rss / $self->{physmem} ) * 100;
168 };
169 }
170  
171 # don't bother proceeding, the object won't match ever
172 # as it does not have a PctMem
173 7 ▶ if ( ! defined( $proc_pctmem ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proc_pctmem) 0 7 7
174 0 return 0;
175 }
176  
177 # use while as foreach will reference the value
178 7 my $pctmem_int=0;
179 7 ▶ while (defined( $self->{pctmems}[$pctmem_int] )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pctmems"}[$pctmem_int]) 7 3 10
180 7 my $pctmem=$self->{pctmems}[$pctmem_int];
181 7 ▶ if (
Branch: 6 branches
BranchTrueFalseTotal
if ($pctmem =~ /^[0-9]+(?:\.[0-9]+)?$/ and $pctmem == $proc_pctmem) { } 1 6 7
elsif ($pctmem =~ /^\<\=[0-9]+(?:\.[0-9]+)?$/) { } 0 6 6
elsif ($pctmem =~ /^\<[0-9]+(?:\.[0-9]+)?$/) { } 2 4 6
elsif ($pctmem =~ /^\>\=[0-9]+(?:\.[0-9]+)?$/) { } 3 1 4
elsif ($pctmem =~ /^\>[0-9]+(?:\.[0-9]+)?$/) { } 0 1 1
elsif ($pctmem =~ /^\![0-9]+(?:\.[0-9]+)?$/) { } 0 1 1
Condition100%3/3
$pctmem =~ /^[0-9]+(?:\.[0-9]+)?$/ and $pctmem == $proc_pctmem
!ll&&!rl&&r
511
MC/DC100%2/2
$pctmem =~ /^[0-9]+(?:\.[0-9]+)?$/ and $pctmem == $proc_pctmem
$pctmem =~ /^[0-9]+(?:\.[0-9]+)?$/ $pctmem == $proc_pctmem
Truth table100%3/3
A and B
A= $pctmem =~ /^[0-9]+(?:\.[0-9]+)?$/B= $pctmem == $proc_pctmem
ABresult
0X0
100
111
182 ( $pctmem =~ /^[0-9]+(?:\.[0-9]+)?$/ ) &&
183 ( $pctmem == $proc_pctmem )
184 ){
185 1 return 1;
186 }elsif( $pctmem =~ /^\<\=[0-9]+(?:\.[0-9]+)?$/ ){
187 0 $pctmem=~s/^\<\=//;
188 0 ▶ if ( $proc_pctmem <= $pctmem ){
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_pctmem <= $pctmem) 0 0 0
189 0 return 1;
190 }
191 }elsif( $pctmem =~ /^\<[0-9]+(?:\.[0-9]+)?$/ ){
192 2 $pctmem=~s/^\<//;
193 2 ▶ if ( $proc_pctmem < $pctmem ){
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_pctmem < $pctmem) 1 1 2
194 1 return 1;
195 }
196 }elsif( $pctmem =~ /^\>\=[0-9]+(?:\.[0-9]+)?$/ ){
197 3 $pctmem=~s/^\>\=//;
198 3 ▶ if ( $proc_pctmem >= $pctmem ){
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_pctmem >= $pctmem) 2 1 3
199 2 return 1;
200 }
201 }elsif( $pctmem =~ /^\>[0-9]+(?:\.[0-9]+)?$/ ){
202 0 $pctmem=~s/^\>//;
203 0 ▶ if ( $proc_pctmem > $pctmem ){
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_pctmem > $pctmem) 0 0 0
204 0 return 1;
205 }
206 }elsif( $pctmem =~ /^\![0-9]+(?:\.[0-9]+)?$/ ){
207 0 $pctmem=~s/^\!//;
208 0 ▶ if ( $proc_pctmem != $pctmem ){
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_pctmem != $pctmem) 0 0 0
209 0 return 1;
210 }
211 }
212 3 $pctmem_int++;
213 }
214  
215 3 return 0;
216 }
217  
218 =head1 AUTHOR
219  
220 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
221  
222 =head1 BUGS
223  
224 Please report any bugs or feature requests to C<bug-Proc-ProcessTable-Match at rt.cpan.org>, or through
225 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Proc-ProcessTable-Match>. I will be notified, and then you'll
226 automatically be notified of progress on your bug as I make changes.
227  
228  
229  
230  
231 =head1 SUPPORT
232  
233 You can find documentation for this module with the perldoc command.
234  
235 perldoc Proc::ProcessTable::Match
236  
237  
238 You can also look for information at:
239  
240 =over 4
241  
242 =item * RT: CPAN's request tracker (report bugs here)
243  
244 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Proc-ProcessTable-Match>
245  
246 =item * Search CPAN
247  
248 L<https://metacpan.org/release/Proc-ProcessTable-Match>
249  
250 =back
251  
252  
253 =head1 ACKNOWLEDGEMENTS
254  
255  
256 =head1 LICENSE AND COPYRIGHT
257  
258 Copyright 2019 Zane C. Bowers-Hadley.
259  
260 This program is free software; you can redistribute it and/or modify it
261 under the terms of the the Artistic License (2.0). You may obtain a
262 copy of the full license at:
263  
264 L<http://www.perlfoundation.org/artistic_license_2_0>
265  
266 Any use, modification, and distribution of the Standard or Modified
267 Versions is governed by this Artistic License. By using, modifying or
268 distributing the Package, you accept this license. Do not use, modify,
269 or distribute the Package, if you do not accept this license.
270  
271 If your Modified Version has been derived from a Modified Version made
272 by someone other than you, you are nevertheless required to ensure that
273 your Modified Version complies with the requirements of this license.
274  
275 This license does not grant you the right to use any trademark, service
276 mark, tradename, or logo of the Copyright Holder.
277  
278 This license includes the non-exclusive, worldwide, free-of-charge
279 patent license to make, have made, use, offer to sell, sell, import and
280 otherwise transfer the Package with respect to any patent claims
281 licensable by the Copyright Holder that are necessarily infringed by the
282 Package. If you institute patent litigation (including a cross-claim or
283 counterclaim) against any party alleging that the Package constitutes
284 direct or contributory patent infringement, then this Artistic License
285 to you shall terminate on the date that such litigation is filed.
286  
287 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
288 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
289 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
290 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
291 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
292 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
293 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
294 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295  
296  
297 =cut
298  
299 2 1; # End of Proc::ProcessTable::Match
« Match/UID.pm ↑ Summary Match/Time.pm »