blib/lib/Proc/ProcessTable/ncps.pm

filter: Statementstmt 78.5% 376 / 479 Branchbran 56.6% 119 / 210 Conditioncond 50.7% 32 / 63 MC/DCmcdc 28.9% 11 / 38 Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 69.0% 563 / 815 CC 135 SCAR 59.7
CC
135
Cov
70%
run
578.0
physmem
25.0
timeString
8.0
CRAP 391.0
↑ Summary src_bin/ncps »
1 package Proc::ProcessTable::ncps;
2  
3 6 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Proc::ProcessTable::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Proc::ProcessTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Text::ANSITable;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Statistics::Basic qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use List::Util qw( min max sum );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Proc::ProcessTable::InfoString;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Proc::ProcessTable::ncps - New Colorized(optional) PS, an enhanced version of PS with advanced searching capabilities
18  
19 =head1 VERSION
20  
21 Version 0.2.2
22  
23 =cut
24  
25 6 our $VERSION = '0.2.2';
26  
27 =head1 SYNOPSIS
28  
29 use Proc::ProcessTable::ncps;
30  
31 my $args={
32 cmajor_faults=>0,
33 cminor_faults=>0,
34 major_faults=>1,
35 minor_faults=>0,
36 numthr=>0,
37 tty=>0,
38 jid=>0,
39 stats=>1,
40 match=>{
41 checks=>\@filters,
42 }
43 };
44  
45 my $ncps = Proc::ProcessTable::ncps->new( \%args );
46  
47 print $ncps->run
48  
49 The info column is provided by L<Proc::ProcessTable::InfoString>. That
50 POD has the information on what they all mean.
51  
52 =head1 METHODS
53  
54 =head2 new
55  
56 This initiates the object.
57  
58 =head3 args hash ref
59  
60 =head4 match
61  
62 This is a hash to pass to L<Proc::ProcessTable::Match>. If not specified,
63 it will not be used and all processes will be displayed.
64  
65 =head4 cmajor_faults
66  
67 Boolean for if the children major faults column should be shown.
68  
69 Default: 0
70  
71 =head4 cminor_faults
72  
73 Boolean for if the children minor faults column should be shown.
74  
75 Default: 0
76  
77 =head4 major_faults
78  
79 Boolean for if the major faults column should be shown.
80  
81 Default: 0
82  
83 =head4 minor_faults
84  
85 Boolean for if the minor faults column should be shown.
86  
87 Default: 0
88  
89 =head4 jid
90  
91 Boolean for if the JIDs column should be shown.
92  
93 Default: 0
94  
95 =head4 numthr
96  
97 Boolean for if the number of threads column should be shown.
98  
99 Default: 0
100  
101 =head4 stats
102  
103 Boolean for if stats for PctCPU, PctMem, VSZ, RSS
104 and time should be shown at the end.
105  
106 Default: 0
107  
108 =head4 tty
109  
110 Boolean for if the TTY column should be shown.
111  
112 Default: 0
113  
114 =cut
115  
116 6 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
117 6 my %args;
118 6 ▶ if ( defined( $_[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 4 2 6
119 4 %args = %{ $_[1] };
120 }
121  
122 6 my $self = {
123 match => undef,
124 minor_faults => 0,
125 major_faults => 0,
126 cminor_faults => 0,
127 cmajor_faults => 0,
128 colors => [ 'BRIGHT_YELLOW', 'BRIGHT_CYAN', 'BRIGHT_MAGENTA', 'BRIGHT_BLUE' ],
129 timeColors => [ 'GREEN', 'BRIGHT_GREEN', 'RED', 'BRIGHT_RED' ],
130 vszColors => [ 'GREEN', 'YELLOW', 'RED', 'BRIGHT_BLUE' ],
131 rssColors => [ 'BRIGHT_GREEN', 'BRIGHT_YELLOW', 'BRIGHT_RED', 'BRIGHT_BLUE' ],
132 processColor => 'BRIGHT_RED',
133 nextColor => 0,
134 stats => 0,
135 jid => 0,
136 tty => 0,
137 numthr => 0,
138 };
139 6 bless $self;
140  
141 6 ▶ if ( defined( $args{match} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"match"} and defined $args{"match"}{"checks"} and defined $args{"match"}{"checks"}[0]) 2 4 6
Condition66%4/6
defined $args{"match"} and defined $args{"match"}{"checks"} and defined $args{"match"}{"checks"}[0]
!ll&&!rl&&r
402
defined $args{"match"} and defined $args{"match"}{"checks"}
!ll&&!rl&&r
402
MC/DC33%1/3
defined $args{"match"} and defined $args{"match"}{"checks"} and defined $args{"match"}{"checks"}[0]
defined $args{"match"} defined $args{"match"}{"checks"} defined $args{"match"}{"checks"}[0]
Truth table50%2/4
A and B and C
A= defined $args{"match"}B= defined $args{"match"}{"checks"}C= defined $args{"match"}{"checks"}[0]
ABCresult
0XX0
10X0
1100
1111
142 && defined( $args{match}{checks} )
143 && defined( $args{match}{checks}[0] ) )
144 {
145 2 $self->{match} = Proc::ProcessTable::Match->new( $args{match} );
146 }
147  
148 my @bool_feed
149 6 = ( 'major_faults', 'minor_faults', 'cmajor_faults', 'cminor_faults', 'numthr', 'tty', 'jid', 'stats' );
150  
151 6 foreach my $feed (@bool_feed) {
152 48 ▶ if ( defined( $args{$feed} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$feed}) 4 44 48
153 4 $self->{$feed} = $args{$feed};
154 }
155 }
156  
157 6 return $self;
158 } ## end sub new
159  
160 =head2 run
161  
162 This runs it.
163  
164 The return value is a string.
165  
166 print $ncps->run
167  
168 =cut
169  
170 3 ▶sub run {
Subroutine run: called CC=102 SCAR=63.6
Pod documented
171 3 my $self = $_[0];
172  
173 # cache_ttys is not used as Proc::ProcessTable keeps that cache in a
174 # Storable file under /tmp that is shared by every perl on the machine
175 # with the same byte order, so a cache written by a perl built with a
176 # differing double size makes Storable::retrieve die with
177 # 'Double size is not compatible'
178 3 my $ppt = Proc::ProcessTable->new;
179 3 my $pt = $ppt->table;
180  
181 # if this platform does not provide the rss field, compute it from
182 # rssize, which is in pages, so it is usable for both matching and
183 # display... no such fallback is possible for the size field as
184 # tsize+dsize+ssize misses shared libraries and other mappings
185 3 ▶ if ( defined( $pt->[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pt->[0] and not defined $pt->[0]{"rss"} and defined $pt->[0]{"rssize"}) 0 3 3
Condition33%2/6
defined $pt->[0] and not defined $pt->[0]{"rss"} and defined $pt->[0]{"rssize"}
!ll&&!rl&&r
300
defined $pt->[0] and not defined $pt->[0]{"rss"}
!ll&&!rl&&r
030
MC/DC0%0/3
defined $pt->[0] and not defined $pt->[0]{"rss"} and defined $pt->[0]{"rssize"}
defined $pt->[0] not defined $pt->[0]{"rss"} defined $pt->[0]{"rssize"}
Truth table25%1/4
A and not(B) and C
A= defined $pt->[0]B= not defined $pt->[0]{"rss"}C= defined $pt->[0]{"rssize"}
ABCresult
0XX0
10X0
1100
1111
186 && ( !defined( $pt->[0]->{rss} ) )
187 && defined( $pt->[0]->{rssize} ) )
188 {
189 0 my $pagesize;
190 0 eval { $pagesize = POSIX::sysconf( POSIX::_SC_PAGESIZE() ); };
191 0 ▶ if ($pagesize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pagesize) 0 0 0
192 0 foreach my $proc ( @{$pt} ) {
193 0 my $rssize = $proc->{rssize};
194 0 ▶ if ( !defined($rssize) ) { $rssize = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rssize) 0 0 0
195 0 $proc->{rss} = $rssize * $pagesize;
196 }
197 }
198 } ## end if ( defined( $pt->[0] ) && ( !defined( $pt...)))
199  
200 # if this platform does not provide the pctmem field, compute it
201 # if possible so it is usable for both matching and display,
202 # using rss as Proc::ProcessTable provides it in bytes everywhere
203 3 ▶ if ( defined( $pt->[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pt->[0] and not defined $pt->[0]{"pctmem"} and defined $pt->[0]{"rss"}) 0 3 3
Condition33%2/6
defined $pt->[0] and not defined $pt->[0]{"pctmem"} and defined $pt->[0]{"rss"}
!ll&&!rl&&r
300
defined $pt->[0] and not defined $pt->[0]{"pctmem"}
!ll&&!rl&&r
030
MC/DC0%0/3
defined $pt->[0] and not defined $pt->[0]{"pctmem"} and defined $pt->[0]{"rss"}
defined $pt->[0] not defined $pt->[0]{"pctmem"} defined $pt->[0]{"rss"}
Truth table25%1/4
A and not(B) and C
A= defined $pt->[0]B= not defined $pt->[0]{"pctmem"}C= defined $pt->[0]{"rss"}
ABCresult
0XX0
10X0
1100
1111
204 && ( !defined( $pt->[0]->{pctmem} ) )
205 && defined( $pt->[0]->{rss} ) )
206 {
207 0 my $physmem = $self->physmem;
208 0 ▶ if ( defined($physmem) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $physmem) 0 0 0
209 0 foreach my $proc ( @{$pt} ) {
210 0 my $rss = $proc->{rss};
211 0 ▶ if ( !defined($rss) ) { $rss = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rss) 0 0 0
212 0 $proc->{pctmem} = ( $rss / $physmem ) * 100;
213 }
214 }
215 } ## end if ( defined( $pt->[0] ) && ( !defined( $pt...)))
216  
217 3 my $procs;
218 3 ▶ if ( defined( $self->{match} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'match'}) { } 2 1 3
219 2 $procs = [];
220 2 my $match_error;
221 2 foreach my $proc ( @{$pt} ) {
222 18 eval {
223 18 ▶ if ( $self->{match}->match($proc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"match"}->match($proc)) 0 9 9
224 0 push( @{$procs}, $proc );
225 }
226 };
227 18 ▶ if ( $@ && ( !defined($match_error) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not defined $match_error) 1 17 18
Condition100%3/3
$@ and not defined $match_error
!ll&&!rl&&r
981
MC/DC100%2/2
$@ and not defined $match_error
$@ not defined $match_error
Truth table100%3/3
A and not(B)
A= $@B= not defined $match_error
ABresult
0X0
100
111
228 1 $match_error = $@;
229 }
230 } ## end foreach my $proc ( @{$pt} )
231 2 ▶ if ( defined($match_error) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match_error) 1 1 2
232 1 warn( 'One or more processes errored during matching... first error... ' . $match_error );
233 }
234 } else {
235 1 $procs = $pt;
236 }
237  
238 # figures out if this systems reports nice or not
239 3 my $have_nice = 0;
240 3 ▶ if ( defined( $procs->[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $procs->[0] and defined $procs->[0]{"nice"}) 0 3 3
Condition66%2/3
defined $procs->[0] and defined $procs->[0]{"nice"}
!ll&&!rl&&r
210
MC/DC0%0/2
defined $procs->[0] and defined $procs->[0]{"nice"}
defined $procs->[0] defined $procs->[0]{"nice"}
Truth table66%2/3
A and B
A= defined $procs->[0]B= defined $procs->[0]{"nice"}
ABresult
0X0
100
111
241 && defined( $procs->[0]->{nice} ) )
242 {
243 0 $have_nice = 1;
244 }
245  
246 # figures out if this systems reports priority or not
247 3 my $have_pri = 0;
248 3 ▶ if ( defined( $procs->[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $procs->[0] and defined $procs->[0]{"priority"}) 1 2 3
Condition66%2/3
defined $procs->[0] and defined $procs->[0]{"priority"}
!ll&&!rl&&r
201
MC/DC50%1/2
defined $procs->[0] and defined $procs->[0]{"priority"}
defined $procs->[0] defined $procs->[0]{"priority"}
Truth table66%2/3
A and B
A= defined $procs->[0]B= defined $procs->[0]{"priority"}
ABresult
0X0
100
111
249 && defined( $procs->[0]->{priority} ) )
250 {
251 1 $have_pri = 1;
252 }
253  
254 # disable optional columns for fields this platform does not support
255 3 my %optional_column_fields = (
256 jid => 'jid',
257 numthr => 'numthr',
258 tty => 'ttydev',
259 major_faults => 'majflt',
260 minor_faults => 'minflt',
261 cmajor_faults => 'cmajflt',
262 cminor_faults => 'cminflt',
263 );
264 3 foreach my $optional_column ( keys(%optional_column_fields) ) {
265 21 my $required_field = $optional_column_fields{$optional_column};
266 21 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$optional_column} and not defined $procs->[0] && defined $procs->[0]{$required_field}) 0 21 21
Condition16%1/6
$self->{$optional_column} and not defined $procs->[0] && defined $procs->[0]{$required_field}
!ll&&!rl&&r
2100
defined $procs->[0] && defined $procs->[0]{$required_field}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{$optional_column} and not defined $procs->[0] && defined $procs->[0]{$required_field}
$self->{$optional_column} defined $procs->[0] defined $procs->[0]{$required_field}
Truth table25%1/4
A and not(B && C)
A= $self->{$optional_column}B= defined $procs->[0]C= defined $procs->[0]{$required_field}
ABCresult
0XX0
1110
10X1
1101
267 $self->{$optional_column}
268 && ( !defined( $procs->[0] )
269 || !defined( $procs->[0]->{$required_field} ) )
270 )
271 {
272 0 $self->{$optional_column} = 0;
273 }
274 } ## end foreach my $optional_column ( keys(%optional_column_fields...))
275  
276 # figures out which of the standard columns this platform supports
277 3 my %have_field;
278 3 foreach my $standard_field ( 'pctcpu', 'pctmem', 'size', 'rss', 'time', 'start' ) {
279 18 $have_field{$standard_field} = 0;
280 18 ▶ if ( defined( $procs->[0] )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $procs->[0] and defined $procs->[0]{$standard_field}) 6 12 18
Condition66%2/3
defined $procs->[0] and defined $procs->[0]{$standard_field}
!ll&&!rl&&r
1206
MC/DC50%1/2
defined $procs->[0] and defined $procs->[0]{$standard_field}
defined $procs->[0] defined $procs->[0]{$standard_field}
Truth table66%2/3
A and B
A= defined $procs->[0]B= defined $procs->[0]{$standard_field}
ABresult
0X0
100
111
281 && defined( $procs->[0]->{$standard_field} ) )
282 {
283 6 $have_field{$standard_field} = 1;
284 }
285 }
286  
287 3 my $tb = Text::ANSITable->new;
288 3 $tb->border_style('ASCII::None');
289 3 $tb->color_theme('NoColor');
290  
291 #
292 # assemble the headers
293 #
294 3 my @headers;
295 3 my $header_int = 0;
296 3 my $padding = 0;
297 3 push( @headers, 'User' );
298 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 3 3
299 3 else { $padding = 0; }
300 3 $tb->set_column_style( $header_int, pad => $padding );
301 3 $header_int++;
302 3 push( @headers, 'PID' );
303 3 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 3 0 3
304 0 else { $padding = 0; }
305 3 $tb->set_column_style( $header_int, pad => $padding );
306 3 $header_int++;
307 # add CPU percent if needed
308 3 ▶ if ( $have_field{pctcpu} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"pctcpu"}) 1 2 3
309 1 push( @headers, 'CPU' );
310 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 1 1
311 1 else { $padding = 0; }
312 1 $tb->set_column_style( $header_int, pad => $padding );
313 1 $header_int++;
314 }
315 # add memory percent if needed
316 3 ▶ if ( $have_field{pctmem} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"pctmem"}) 1 2 3
317 1 push( @headers, 'MEM' );
318 1 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 1 0 1
319 0 else { $padding = 0; }
320 1 $tb->set_column_style( $header_int, pad => $padding );
321 1 $header_int++;
322 }
323 # add VSZ if needed
324 3 ▶ if ( $have_field{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"size"}) 1 2 3
325 1 push( @headers, 'VSZ' );
326 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 1 1
327 1 else { $padding = 0; }
328 1 $tb->set_column_style( $header_int, pad => $padding );
329 1 $header_int++;
330 }
331 # add RSS if needed
332 3 ▶ if ( $have_field{rss} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"rss"}) 1 2 3
333 1 push( @headers, 'RSS' );
334 1 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 1 0 1
335 0 else { $padding = 0; }
336 1 $tb->set_column_style( $header_int, pad => $padding );
337 1 $header_int++;
338 }
339 3 push( @headers, 'Info' );
340 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 3 3
341 3 else { $padding = 0; }
342 3 $tb->set_column_style( $header_int, pad => $padding );
343 3 $header_int++;
344 # add nice if needed
345 3 ▶ if ($have_nice) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_nice) 0 3 3
346 0 push( @headers, 'Nic' );
347 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
348 0 else { $padding = 0; }
349 0 $tb->set_column_style( $header_int, pad => $padding );
350 0 $header_int++;
351 }
352 # add priority if needed
353 3 ▶ if ($have_pri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_pri) 1 2 3
354 1 push( @headers, 'Pri' );
355 1 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 1 0 1
356 0 else { $padding = 0; }
357 1 $tb->set_column_style( $header_int, pad => $padding );
358 1 $header_int++;
359 }
360 # add major faults if needed
361 3 ▶ if ( $self->{major_faults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"major_faults"}) 0 3 3
362 0 push( @headers, 'MajF' );
363 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
364 0 else { $padding = 0; }
365 0 $tb->set_column_style( $header_int, pad => $padding );
366 0 $header_int++;
367 }
368 # add minor faults if needed
369 3 ▶ if ( $self->{minor_faults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"minor_faults"}) 0 3 3
370 0 push( @headers, 'minF' );
371 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
372 0 else { $padding = 0; }
373 0 $tb->set_column_style( $header_int, pad => $padding );
374 0 $header_int++;
375 }
376 # add children major faults if needed
377 3 ▶ if ( $self->{cmajor_faults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cmajor_faults"}) 0 3 3
378 0 push( @headers, 'cMajF' );
379 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
380 0 else { $padding = 0; }
381 0 $tb->set_column_style( $header_int, pad => $padding );
382 0 $header_int++;
383 }
384 # add children minor faults if needed
385 3 ▶ if ( $self->{cminor_faults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cminor_faults"}) 0 3 3
386 0 push( @headers, 'cminF' );
387 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
388 0 else { $padding = 0; }
389 0 $tb->set_column_style( $header_int, pad => $padding );
390 0 $header_int++;
391 }
392 # add number of threads if needed
393 3 ▶ if ( $self->{numthr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"numthr"}) 0 3 3
394 0 push( @headers, 'Thr' );
395 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
396 0 else { $padding = 0; }
397 0 $tb->set_column_style( $header_int, pad => $padding );
398 0 $header_int++;
399 }
400 # add TTY if needed
401 3 ▶ if ( $self->{tty} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tty"}) 0 3 3
402 0 push( @headers, 'TTY' );
403 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
404 0 else { $padding = 0; }
405 0 $tb->set_column_style( $header_int, pad => $padding );
406 0 $header_int++;
407 }
408 # add jail ID if needed
409 3 ▶ if ( $self->{jid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"jid"}) 0 3 3
410 0 push( @headers, 'JID' );
411 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 0 0
412 0 else { $padding = 0; }
413 0 $tb->set_column_style( $header_int, pad => $padding );
414 0 $header_int++;
415 }
416 # add start time if needed
417 3 ▶ if ( $have_field{start} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"start"}) 1 2 3
418 1 push( @headers, 'Start' );
419 0 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 0 1 1
420 1 else { $padding = 0; }
421 1 $tb->set_column_style( $header_int, pad => $padding );
422 1 $header_int++;
423 }
424 # add CPU time if needed
425 3 ▶ if ( $have_field{time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"time"}) 1 2 3
426 1 push( @headers, 'Time' );
427 1 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 1 0 1
428 0 else { $padding = 0; }
429 1 $tb->set_column_style( $header_int, pad => $padding );
430 1 $header_int++;
431 }
432 3 push( @headers, 'Command' );
433 3 ▶ if ( ( $header_int % 2 ) != 0 ) { $padding = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_int % 2 != 0) { } 2 1 3
434 1 else { $padding = 0; }
435 3 $tb->set_column_style( $header_int, pad => $padding, formats => [ [ wrap => { ansi => 1, mb => 1 } ] ] );
436  
437 3 $tb->columns( \@headers );
438  
439 3 my @stats_rss;
440 my @stats_vsz;
441 3 my @stats_time;
442 3 my @stats_pctcpu;
443 3 my @stats_pctmem;
444  
445 3 my @td;
446 3 foreach my $proc ( @{$procs} ) {
447 9 my @new_line;
448  
449 #
450 # handle username column
451 #
452 9 my $user = getpwuid( $proc->{uid} );
453 9 ▶ if ( !defined($user) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user) 0 9 9
454 0 $user = $proc->{uid};
455 }
456 9 $user = color( $self->nextColor ) . $user . color('reset');
457 9 push( @new_line, $user );
458  
459 #
460 # handles the PID
461 #
462 9 push( @new_line, color( $self->nextColor ) . $proc->{pid} . color('reset') );
463  
464 #
465 # handles the %CPU
466 #
467 9 ▶ if ( $have_field{pctcpu} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"pctcpu"}) 9 0 9
468 9 my $pctcpu = $proc->{pctcpu};
469 # Proc::ProcessTable reports inf/nan for freshly started processes
470 0 ▶ if ( ( !defined($pctcpu) ) || ( $pctcpu !~ /^\s*[0-9]*\.?[0-9]+\s*$/ ) ) { $pctcpu = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pctcpu and $pctcpu =~ /^\s*[0-9]*\.?[0-9]+\s*$/) 0 9 9
Condition33%1/3
defined $pctcpu and $pctcpu =~ /^\s*[0-9]*\.?[0-9]+\s*$/
!ll&&!rl&&r
009
MC/DC0%0/2
defined $pctcpu and $pctcpu =~ /^\s*[0-9]*\.?[0-9]+\s*$/
defined $pctcpu $pctcpu =~ /^\s*[0-9]*\.?[0-9]+\s*$/
Truth table33%1/3
A and B
A= defined $pctcpuB= $pctcpu =~ /^\s*[0-9]*\.?[0-9]+\s*$/
ABresult
0X0
100
111
471 9 push( @new_line, color( $self->nextColor ) . sprintf( '%.2f', $pctcpu ) . color('reset') );
472 9 ▶ if ( $self->{stats} ) { push( @stats_pctcpu, $pctcpu ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stats"}) 9 0 9
473 }
474  
475 #
476 # handles the %MEM
477 #
478 9 ▶ if ( $have_field{pctmem} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"pctmem"}) 9 0 9
479 9 my $pctmem = $proc->{pctmem};
480 0 ▶ if ( !defined($pctmem) ) { $pctmem = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pctmem) 0 9 9
481 9 push( @new_line, color( $self->nextColor ) . sprintf( '%.2f', $pctmem ) . color('reset') );
482 9 ▶ if ( $self->{stats} ) { push( @stats_pctmem, $pctmem ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stats"}) 9 0 9
483 }
484  
485 #
486 # handles VSZ
487 #
488 9 ▶ if ( $have_field{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"size"}) 9 0 9
489 9 my $size = $proc->{size};
490 0 ▶ if ( !defined($size) ) { $size = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size) 0 9 9
491 9 push( @new_line, $self->memString( $size, 'vsz' ) );
492 9 ▶ if ( $self->{stats} ) { push( @stats_vsz, $size ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stats"}) 9 0 9
493 }
494  
495 #
496 # handles the rss
497 #
498 9 ▶ if ( $have_field{rss} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"rss"}) 9 0 9
499 9 my $rss = $proc->{rss};
500 0 ▶ if ( !defined($rss) ) { $rss = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rss) 0 9 9
501 9 push( @new_line, $self->memString( $rss, 'rss' ) );
502 9 ▶ if ( $self->{stats} ) { push( @stats_rss, $rss ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stats"}) 9 0 9
503 }
504  
505 #
506 # handles the info
507 #
508 9 my $is = Proc::ProcessTable::InfoString->new(
509 {
510 flags_color => $self->nextColor,
511 wchan_color => $self->nextColor,
512 }
513 );
514 9 push( @new_line, $is->info($proc) );
515  
516 #
517 # handle the nice column
518 #
519 9 ▶ if ($have_nice) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_nice) 0 9 9
520 0 push( @new_line, color( $self->nextColor ) . $proc->{nice} . color('reset') );
521 }
522  
523 #
524 # handle the priority column
525 #
526 9 ▶ if ($have_pri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_pri) 9 0 9
527 9 push( @new_line, color( $self->nextColor ) . $proc->{priority} . color('reset') );
528 }
529  
530 #
531 # major faults
532 #
533 9 ▶ if ( $self->{major_faults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"major_faults"}) 0 9 9
534 0 push( @new_line, color( $self->nextColor ) . $proc->{majflt} . color('reset') );
535 }
536  
537 #
538 # minor faults
539 #
540 9 ▶ if ( $self->{minor_faults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"minor_faults"}) 0 9 9
541 0 push( @new_line, color( $self->nextColor ) . $proc->{minflt} . color('reset') );
542 }
543  
544 #
545 # children major faults
546 #
547 9 ▶ if ( $self->{cmajor_faults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cmajor_faults"}) 0 9 9
548 0 push( @new_line, color( $self->nextColor ) . $proc->{cmajflt} . color('reset') );
549 }
550  
551 #
552 # children minor faults
553 #
554 9 ▶ if ( $self->{cminor_faults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cminor_faults"}) 0 9 9
555 0 push( @new_line, color( $self->nextColor ) . $proc->{cminflt} . color('reset') );
556 }
557  
558 #
559 # number of threads
560 #
561 9 ▶ if ( $self->{numthr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"numthr"}) 0 9 9
562 0 push( @new_line, color( $self->nextColor ) . $proc->{numthr} . color('reset') );
563 }
564  
565 #
566 # TTY
567 #
568 9 ▶ if ( $self->{tty} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tty"}) 0 9 9
569 0 push( @new_line, color( $self->nextColor ) . $proc->{ttydev} . color('reset') );
570 }
571  
572 #
573 # jail ID
574 #
575 9 ▶ if ( $self->{jid} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"jid"}) 0 9 9
576 0 push( @new_line, color( $self->nextColor ) . $proc->{jid} . color('reset') );
577 }
578  
579 #
580 # handles the start column
581 #
582 9 ▶ if ( $have_field{start} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"start"}) 9 0 9
583 9 my $start = $proc->{start};
584 0 ▶ if ( !defined($start) ) { $start = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start) 0 9 9
585 9 push( @new_line, color( $self->nextColor ) . $self->startString($start) . color('reset') );
586 }
587  
588 #
589 # handles the time column
590 #
591 9 ▶ if ( $have_field{time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"time"}) 9 0 9
592 9 my $proc_time = $proc->{time};
593 0 ▶ if ( !defined($proc_time) ) { $proc_time = 0; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $proc_time) 0 9 9
594 9 push( @new_line, $self->timeString($proc_time) );
595 9 ▶ if ( $self->{stats} ) { push( @stats_time, $proc_time ); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stats"}) 9 0 9
596 }
597  
598 #
599 # handle the command
600 #
601 9 my $command = color( $self->{processColor} );
602 9 ▶ if ( ( !defined( $proc->{cmndline} ) ) || ( $proc->{cmndline} =~ /^$/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $proc->{'cmndline'} or $proc->{'cmndline'} =~ /^$/) { } 0 9 9
Condition33%1/3
not defined $proc->{"cmndline"} or $proc->{"cmndline"} =~ /^$/
l!l&&r!l&&!r
009
MC/DC0%0/2
not defined $proc->{"cmndline"} or $proc->{"cmndline"} =~ /^$/
not defined $proc->{"cmndline"} $proc->{"cmndline"} =~ /^$/
Truth table33%1/3
not(A) or B
A= not defined $proc->{"cmndline"}B= $proc->{"cmndline"} =~ /^$/
ABresult
1X1
011
000
603 0 $command = $command . '[' . $proc->{fname} . ']';
604 } else {
605 9 $command = $command . $proc->{cmndline};
606 }
607 9 push( @new_line, $command . color('reset') );
608  
609 9 push( @td, \@new_line );
610 9 $self->{nextColor} = 0;
611 } ## end foreach my $proc ( @{$procs} )
612  
613 3 $tb->add_rows( \@td );
614  
615 3 my $stats = '';
616 3 ▶ if ( $self->{stats} && defined( $procs->[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stats"} and defined $procs->[0]) 1 2 3
Condition100%3/3
$self->{"stats"} and defined $procs->[0]
!ll&&!rl&&r
111
MC/DC100%2/2
$self->{"stats"} and defined $procs->[0]
$self->{"stats"} defined $procs->[0]
Truth table100%3/3
A and B
A= $self->{"stats"}B= defined $procs->[0]
ABresult
0X0
100
111
617 1 my $stb = Text::ANSITable->new;
618 #$stb->border_style('Default::none_ascii');
619 1 $stb->border_style('ASCII::None');
620 1 $stb->color_theme('NoColor');
621  
622 #
623 # assemble the headers
624 #
625 1 my @stats_headers;
626 1 my $stats_header_int = 0;
627 1 push( @stats_headers, ' ' );
628 1 $stb->set_column_style( $stats_header_int, pad => 0 );
629 1 $stats_header_int++;
630 1 push( @stats_headers, 'Min' );
631 1 $stb->set_column_style( $stats_header_int, pad => 1 );
632 1 $stats_header_int++;
633 1 push( @stats_headers, 'Avg' );
634 1 $stb->set_column_style( $stats_header_int, pad => 0 );
635 1 $stats_header_int++;
636 1 push( @stats_headers, 'Med' );
637 1 $stb->set_column_style( $stats_header_int, pad => 1 );
638 1 $stats_header_int++;
639 1 push( @stats_headers, 'Max' );
640 1 $stb->set_column_style( $stats_header_int, pad => 0 );
641 1 $stats_header_int++;
642 1 push( @stats_headers, 'StdDev' );
643 1 $stb->set_column_style( $stats_header_int, pad => 1 );
644 1 $stats_header_int++;
645 1 push( @stats_headers, 'Sum' );
646 1 $stb->set_column_style( $stats_header_int, pad => 0 );
647  
648 1 $stb->columns( \@stats_headers );
649  
650 1 my @std;
651  
652 my $stats_avg;
653 1 my $stats_median;
654 1 my $stats_stddev;
655  
656 1 ▶ if ( $have_field{pctcpu} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"pctcpu"}) 1 0 1
657 1 $stats_avg = avg(@stats_pctcpu);
658 1 $stats_avg =~ s/\,//g;
659 1 $stats_median = median(@stats_pctcpu);
660 1 $stats_median =~ s/\,//g;
661 1 $stats_stddev = stddev(@stats_pctcpu);
662 1 $stats_stddev =~ s/\,//g;
663 1 push(
664 @std,
665 [
666 'CPU%', sprintf( '%.2f', min(@stats_pctcpu) ),
667 $stats_avg, $stats_median,
668 sprintf( '%.2f', max(@stats_pctcpu) ), $stats_stddev,
669 sprintf( '%.2f', sum(@stats_pctcpu) ),
670 ]
671 );
672 } ## end if ( $have_field{pctcpu} )
673  
674 1 ▶ if ( $have_field{pctmem} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"pctmem"}) 1 0 1
675 1 $stats_avg = avg(@stats_pctmem);
676 1 $stats_avg =~ s/\,//g;
677 1 $stats_median = median(@stats_pctmem);
678 1 $stats_median =~ s/\,//g;
679 1 $stats_stddev = stddev(@stats_pctmem);
680 1 $stats_stddev =~ s/\,//g;
681 1 push(
682 @std,
683 [
684 'Mem%', sprintf( '%.2f', min(@stats_pctmem) ),
685 $stats_avg, $stats_median,
686 sprintf( '%.2f', max(@stats_pctmem) ), $stats_stddev,
687 sprintf( '%.2f', sum(@stats_pctmem) ),
688 ]
689 );
690 } ## end if ( $have_field{pctmem} )
691  
692 1 ▶ if ( $have_field{size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"size"}) 1 0 1
693 1 $stats_avg = avg(@stats_vsz);
694 1 $stats_avg =~ s/\,//g;
695 1 $stats_median = median(@stats_vsz);
696 1 $stats_median =~ s/\,//g;
697 1 $stats_stddev = stddev(@stats_vsz);
698 1 $stats_stddev =~ s/\,//g;
699 1 push(
700 @std,
701 [
702 'VSZ',
703 $self->memString( min(@stats_vsz), 'vsz' ),
704 $self->memString( $stats_avg, 'vsz' ),
705 $self->memString( $stats_median, 'vsz' ),
706 $self->memString( max(@stats_vsz), 'vsz' ),
707 $self->memString( $stats_stddev, 'vsz' ),
708 $self->memString( sum(@stats_vsz), 'vsz' ),
709 ]
710 );
711 } ## end if ( $have_field{size} )
712  
713 1 ▶ if ( $have_field{rss} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"rss"}) 1 0 1
714 1 $stats_avg = avg(@stats_rss);
715 1 $stats_avg =~ s/\,//g;
716 1 $stats_median = median(@stats_rss);
717 1 $stats_median =~ s/\,//g;
718 1 $stats_stddev = stddev(@stats_rss);
719 1 $stats_stddev =~ s/\,//g;
720 1 push(
721 @std,
722 [
723 'RSS',
724 $self->memString( min(@stats_rss), 'rss' ),
725 $self->memString( $stats_avg, 'rss' ),
726 $self->memString( $stats_median, 'rss' ),
727 $self->memString( max(@stats_rss), 'rss' ),
728 $self->memString( $stats_stddev, 'rss' ),
729 $self->memString( sum(@stats_rss), 'rss' ),
730 ]
731 );
732 } ## end if ( $have_field{rss} )
733  
734 1 ▶ if ( $have_field{time} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($have_field{"time"}) 1 0 1
735 1 $stats_avg = avg(@stats_time);
736 1 $stats_avg =~ s/\,//g;
737 1 $stats_median = median(@stats_time);
738 1 $stats_median =~ s/\,//g;
739 1 $stats_stddev = stddev(@stats_time);
740 1 $stats_stddev =~ s/\,//g;
741 1 push(
742 @std,
743 [
744 'Time', $self->timeString( min(@stats_time) ),
745 $self->timeString($stats_avg), $self->timeString($stats_median),
746 $self->timeString( max(@stats_time) ), $self->timeString($stats_stddev),
747 $self->timeString( sum(@stats_time) ),
748 ]
749 );
750 } ## end if ( $have_field{time} )
751  
752 1 $stb->add_rows( \@std );
753  
754 1 $stats = "\n" . $stb->draw;
755 } ## end if ( $self->{stats} && defined( $procs->[0...]))
756  
757 3 return $tb->draw . $stats;
758 } ## end sub run
759  
760 =head2 startString
761  
762 Generates a short time string based on the supplied unix time.
763  
764 =cut
765  
766 11 ▶sub startString {
Subroutine startString: called CC=4 SCAR=14.1
Pod documented
767 11 my $self = $_[0];
768 11 my $startTime = $_[1];
769  
770 11 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime($startTime);
771 11 my ( $csec, $cmin, $chour, $cmday, $cmon, $cyear, $cwday, $cyday, $cisdst ) = localtime(time);
772  
773 #add the required stuff to make this sane
774 11 $year += 1900;
775 11 $cyear += 1900;
776 11 $mon += 1;
777 11 $cmon += 1;
778  
779 #find the most common one and return it
780 11 ▶ if ( $year ne $cyear ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($year ne $cyear) 1 10 11
781 return
782 1 $year
783 . sprintf( '%02d', $mon )
784 . sprintf( '%02d', $mday ) . '-'
785 . sprintf( '%02d', $hour ) . ':'
786 . sprintf( '%02d', $min );
787 }
788 10 ▶ if ( $mon ne $cmon ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mon ne $cmon) 0 10 10
789 return
790 0 sprintf( '%02d', $mon )
791 . sprintf( '%02d', $mday ) . '-'
792 . sprintf( '%02d', $hour ) . ':'
793 . sprintf( '%02d', $min );
794 }
795 10 ▶ if ( $mday ne $cmday ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mday ne $cmday) 0 10 10
796 0 return sprintf( '%02d', $mday ) . '-' . sprintf( '%02d', $hour ) . ':' . sprintf( '%02d', $min );
797 }
798  
799 #just return this for anything less
800 10 return sprintf( '%02d', $hour ) . ':' . sprintf( '%02d', $min );
801 } ## end sub startString
802  
803 =head2 timeString
804  
805 Turns the raw run string into something usable.
806  
807 =cut
808  
809 21 ▶sub timeString {
Subroutine timeString: called CC=8 SCAR=20.8
Pod documented
810 21 my $self = $_[0];
811 21 my $time = $_[1];
812  
813 21 ▶ if ( $^O =~ /^linux$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^linux$/) 21 0 21
814 21 $time = $time / 1000000;
815 }
816  
817 21 my $hours = 0;
818 21 ▶ if ( $time >= 3600 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time >= 3600) 2 19 21
819 2 $hours = int( $time / 3600 );
820 }
821 21 my $loSeconds = $time % 3600;
822 21 my $minutes = 0;
823 21 ▶ if ( $time >= 60 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time >= 60) 3 18 21
824 3 $minutes = int( $loSeconds / 60 );
825 }
826 21 my $seconds = ( $loSeconds % 60 ) + ( $time - int($time) );
827  
828 #nicely format it, rounding seconds to two optional decimal places
829 21 $seconds = sprintf( '%.2f', $seconds );
830 21 $seconds =~ s/0+$//;
831 21 $seconds =~ s/\.$//;
832  
833 #this will be returned
834 21 my $toReturn = '';
835  
836 #process the hours bit
837 21 ▶ if ( $hours == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hours == 0) { } 19 2 21
elsif ($hours >= 10) { } 1 1 2
838 #don't do anything if time is 0
839 } elsif ( $hours >= 10 ) {
840 1 $toReturn = color( $self->{timeColors}->[3] ) . $hours . ':';
841 } else {
842 1 $toReturn = color( $self->{timeColors}->[2] ) . $hours . ':';
843 }
844  
845 #process the minutes bit
846 21 ▶ if ( ( $hours > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($hours > 0 or $minutes > 0) 3 18 21
Condition100%3/3
$hours > 0 or $minutes > 0
l!l&&r!l&&!r
2118
MC/DC100%2/2
$hours > 0 or $minutes > 0
$hours > 0 $minutes > 0
Truth table100%3/3
A or B
A= $hours > 0B= $minutes > 0
ABresult
1X1
011
000
847 || ( $minutes > 0 ) )
848 {
849 3 $toReturn = $toReturn . color( $self->{timeColors}->[1] ) . $minutes . ':';
850 }
851  
852 21 $toReturn = $toReturn . color( $self->{timeColors}->[0] ) . $seconds . color('reset');
853  
854 21 return $toReturn;
855 } ## end sub timeString
856  
857 =head2 memString
858  
859 Turns the raw run string into something usable.
860  
861 =cut
862  
863 37 ▶sub memString {
Subroutine memString: called CC=7 SCAR=19.5
Pod documented
864 37 my $self = $_[0];
865 37 my $mem = $_[1];
866 37 my $type = $_[2];
867  
868 37 my $toReturn = '';
869  
870 37 ▶ if ( $mem < 10000 ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($mem < 10000) { } 2 35 37
elsif ($mem >= 10000 and $mem < 1000000) { } 3 32 35
elsif ($mem >= 1000000 and $mem < 1000000000) { } 31 1 32
elsif ($mem >= 1000000000) { } 1 0 1
871 2 $mem = sprintf( '%.2f', $mem );
872 2 $mem =~ s/0+$//;
873 2 $mem =~ s/\.$//;
874 2 $toReturn = color( $self->{ $type . 'Colors' }[0] ) . $mem;
875 ▶ } elsif ( ( $mem >= 10000 )
Condition66%2/3
$mem >= 10000 and $mem < 1000000
!ll&&!rl&&r
0323
MC/DC50%1/2
$mem >= 10000 and $mem < 1000000
$mem >= 10000 $mem < 1000000
Truth table66%2/3
A and B
A= $mem >= 10000B= $mem < 1000000
ABresult
0X0
100
111
876 && ( $mem < 1000000 ) )
877 {
878 3 $mem = sprintf( '%.3f', $mem / 1000 );
879 3 $mem =~ s/0+$//;
880 3 $mem =~ s/\.$//;
881  
882 3 $toReturn = color( $self->{ $type . 'Colors' }[0] ) . $mem . color( $self->{ $type . 'Colors' }[3] ) . 'k';
883 ▶ } elsif ( ( $mem >= 1000000 )
Condition66%2/3
$mem >= 1000000 and $mem < 1000000000
!ll&&!rl&&r
0131
MC/DC50%1/2
$mem >= 1000000 and $mem < 1000000000
$mem >= 1000000 $mem < 1000000000
Truth table66%2/3
A and B
A= $mem >= 1000000B= $mem < 1000000000
ABresult
0X0
100
111
884 && ( $mem < 1000000000 ) )
885 {
886 31 $mem = ( $mem / 1000 ) / 1000;
887 31 $mem = sprintf( '%.3f', $mem );
888 31 my @mem_split = split( /\./, $mem );
889  
890 $toReturn
891 = color( $self->{ $type . 'Colors' }[1] )
892 . $mem_split[0] . '.'
893 . color( $self->{ $type . 'Colors' }[0] )
894 . $mem_split[1]
895 31 . color( $self->{ $type . 'Colors' }[3] ) . 'M';
896 } elsif ( $mem >= 1000000000 ) {
897 1 $mem = ( ( $mem / 1000 ) / 1000 ) / 1000;
898 1 $mem = sprintf( '%.3f', $mem );
899 1 my @mem_split = split( /\./, $mem );
900  
901 $toReturn
902 = color( $self->{ $type . 'Colors' }[2] )
903 . $mem_split[0] . '.'
904 . color( $self->{ $type . 'Colors' }[1] )
905 . $mem_split[1]
906 1 . color( $self->{ $type . 'Colors' }[3] ) . 'G';
907 } ## end elsif ( $mem >= 1000000000 )
908  
909 37 return $toReturn . color('reset');
910 } ## end sub memString
911  
912 =head2 physmem
913  
914 Returns the physical memory size in bytes on platforms it is known
915 how to fetch it on. Otherwise undef is returned.
916  
917 POSIX::sysconf is tried first and failing that sysctl is used.
918  
919 my $physmem=$ncps->physmem;
920  
921 =cut
922  
923 1 ▶sub physmem {
Subroutine physmem: called CC=11 SCAR=32.2
Pod documented
924 1 my $physmem;
925  
926 # first try POSIX::sysconf, which requires no external programs,
927 # but not every POSIX module exposes _SC_PHYS_PAGES
928 1 eval {
929 1 my $phys_pages = POSIX::sysconf( POSIX::_SC_PHYS_PAGES() );
930 0 my $pagesize = POSIX::sysconf( POSIX::_SC_PAGESIZE() );
931 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
932 0 $physmem = $phys_pages * $pagesize;
933 }
934 };
935  
936 # fall back on sysctl, checking the various places it may live
937 1 ▶ if ( !defined($physmem) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $physmem) 1 0 1
938 # hw.physmem on OpenBSD is truncated to 32 bits, so use hw.physmem64 there
939 1 my $physmem_variable = 'hw.physmem';
940 1 ▶ if ( $^O eq 'openbsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "openbsd") 0 1 1
941 0 $physmem_variable = 'hw.physmem64';
942 }
943 1 foreach my $sysctl_bin ( '/sbin/sysctl', '/usr/sbin/sysctl' ) {
944 2 ▶ if ( ( !defined($physmem) ) && ( -x $sysctl_bin ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $physmem and -x $sysctl_bin) 2 0 2
Condition33%1/3
not defined $physmem and -x $sysctl_bin
!ll&&!rl&&r
002
MC/DC0%0/2
not defined $physmem and -x $sysctl_bin
not defined $physmem -x $sysctl_bin
Truth table33%1/3
not(A) and B
A= not defined $physmemB= -x $sysctl_bin
ABresult
0X0
100
111
945 2 my $sysctl_output = `$sysctl_bin -n $physmem_variable 2> /dev/null`;
946 2 ▶ if ( defined($sysctl_output) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sysctl_output) 2 0 2
947 2 chomp($sysctl_output);
948 2 ▶ if ( ( $sysctl_output =~ /^[0-9]+$/ ) && ( $sysctl_output > 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sysctl_output =~ /^[0-9]+$/ and $sysctl_output > 0) 0 2 2
Condition33%1/3
$sysctl_output =~ /^[0-9]+$/ and $sysctl_output > 0
!ll&&!rl&&r
200
MC/DC0%0/2
$sysctl_output =~ /^[0-9]+$/ and $sysctl_output > 0
$sysctl_output =~ /^[0-9]+$/ $sysctl_output > 0
Truth table33%1/3
A and B
A= $sysctl_output =~ /^[0-9]+$/B= $sysctl_output > 0
ABresult
0X0
100
111
949 0 $physmem = $sysctl_output;
950 }
951 }
952 }
953 } ## end foreach my $sysctl_bin ( '/sbin/sysctl', '/usr/sbin/sysctl')
954 } ## end if ( !defined($physmem) )
955  
956 1 return $physmem;
957 } ## end sub physmem
958  
959 =head2 nextColor
960  
961 Returns the next color.
962  
963 =cut
964  
965 77 ▶sub nextColor {
Subroutine nextColor: called CC=2 SCAR=6.9
Pod documented
966 77 my $self = $_[0];
967  
968 77 my $color;
969  
970 77 ▶ if ( defined( $self->{colors}[ $self->{nextColor} ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'colors'}[$self->{'nextColor'}]) { } 67 10 77
971 67 $color = $self->{colors}[ $self->{nextColor} ];
972 67 $self->{nextColor}++;
973 } else {
974 10 $self->{nextColor} = 0;
975 10 $color = $self->{colors}[ $self->{nextColor} ];
976 10 $self->{nextColor}++;
977 }
978  
979 77 return $color;
980 } ## end sub nextColor
981  
982 =head1 AUTHOR
983  
984 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
985  
986 =head1 BUGS
987  
988 Please report any bugs or feature requests to C<bug-proc-processtable-ncps at rt.cpan.org>, or through
989 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Proc-ProcessTable-ncps>. I will be notified, and then you'll
990 automatically be notified of progress on your bug as I make changes.
991  
992  
993  
994  
995 =head1 SUPPORT
996  
997 You can find documentation for this module with the perldoc command.
998  
999 perldoc Proc::ProcessTable::ncps
1000  
1001  
1002 You can also look for information at:
1003  
1004 =over 4
1005  
1006 =item * RT: CPAN's request tracker (report bugs here)
1007  
1008 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Proc-ProcessTable-ncps>
1009  
1010 =item * Search CPAN
1011  
1012 L<https://metacpan.org/release/Proc-ProcessTable-ncps>
1013  
1014 =item * Repository
1015  
1016 L<https://github.com/VVelox/Proc-ProcessTable-ncps>
1017  
1018 =back
1019  
1020  
1021 =head1 LICENSE AND COPYRIGHT
1022  
1023 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
1024  
1025 This is free software, licensed under:
1026  
1027 The Artistic License 2.0 (GPL Compatible)
1028  
1029  
1030 =cut
1031  
1032 6 1; # End of Proc::ProcessTable::ncps
↑ Summary src_bin/ncps »