blib/lib/Proc/ProcessTable/InfoString.pm

filter: Statementstmt 97.9% 190 / 194 Branchbran 96.4% 137 / 142 Conditioncond 88.8% 64 / 72 MC/DCmcdc 83.7% 36 / 43 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 94.8% 438 / 462 CC 99 SCAR 42.0
CC
99
Cov
96%
info
83.1
proc_infostring_describe
16.0
new
3.0
CRAP 66.8
↑ Summary
1 package Proc::ProcessTable::InfoString;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our @EXPORT_OK = ( 'proc_infostring_describe' );
10  
11 =head1 NAME
12  
13 Proc::ProcessTable::InfoString - Creates a PS like stat string showing a symbolic representation of various flags/state as well as the wchan.
14  
15 =head1 VERSION
16  
17 Version 0.2.0
18  
19 =cut
20  
21 3 our $VERSION = '0.2.0';
22  
23  
24 =head1 SYNOPSIS
25  
26 use Proc::ProcessTable::InfoString;
27 use Proc::ProcessTable;
28  
29 my $is = Proc::ProcessTable::InfoString->new();
30  
31 my $p = Proc::ProcessTable->new( 'cache_ttys' => 1 );
32 my $pt = $p->table;
33  
34 foreach my $proc ( @{ $pt } ){
35 print $proc->pid.' '.$is->info( $proc )."\n";
36 }
37  
38 The mapping for the left side of the output is as below.
39  
40 States Description
41 Z Zombie
42 S Sleep
43 W Wait
44 R Run
45 T Stopped
46 t Tracing stop
47 I Idle
48 L Blocked on a kernel lock
49 D Uninterruptible sleep
50 X Dead
51 K Wakekill
52 P Parked
53 ? Unknown state
54  
55 Flags Description
56 O Swapped Output
57 E Exiting
58 s Session Leader
59 L POSIX lock advisory
60 + has controlling terminal
61 c controlling tty is active
62 X traced by a debugger
63 F being forked
64  
65 Flag support varies by OS, being limited by what L<Proc::ProcessTable>
66 reports there.
67  
68 OS Flags
69 FreeBSD, MidnightBSD O, E, s, L, +, c, F, X
70 Linux, GNU/kFreeBSD O, E, s, +, F, X
71 OpenBSD O, s, +
72 NetBSD, DragonFly s, +
73 anything else O
74  
75 On NetBSD and DragonFly, both of which use a procfs based
76 implementation, L<Proc::ProcessTable> does not report a state, the
77 state field holding the wait channel instead, so the state always
78 shows as ?. Those two also do not report resident memory, so there is
79 no swapped out flag. OpenBSD for its part reports no wait channel.
80  
81 L</proc_infostring_describe> returns the above as a printable
82 description tailored to the OS in question, meant for use in help
83 output.
84  
85 use Proc::ProcessTable::InfoString qw(proc_infostring_describe);
86  
87 print proc_infostring_describe();
88  
89 =head1 METHODS
90  
91 =head2 new
92  
93 This initiates the object.
94  
95 One argument is taken and that is an optional hash reference.
96  
97 =head3 args hash
98  
99 The values below are color names to be passed to the color function
100 of L<Term::ANSIColor>.
101  
102 If a color is not specified, no ANSI color codes are used for that
103 section. When a color is used, that section of the string is
104 terminated by an ANSI color reset.
105  
106 =head4 flags_color
107  
108 The color to use for the flags section of the string.
109  
110 =head4 wchan_color
111  
112 The color to use for the wait channel section of the string.
113  
114 =cut
115  
116 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
117 2 my %args;
118 2 ▶ if (defined($_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 1 1 2
119 1 %args= %{$_[1]};
120 }
121  
122 2 my $self = {
123 };
124 2 bless $self;
125  
126 2 my @args_feed=(
127 'flags_color',
128 'wchan_color',
129 );
130  
131 2 foreach my $feed ( @args_feed ){
132 4 $self->{$feed}=$args{$feed};
133 }
134  
135 2 return $self;
136 }
137  
138 =head2 info
139  
140 Creates the info string for a process.
141  
142 One argument is taken and that is a L<Proc::ProcessTable::Process>
143 object, as found in the table returned by L<Proc::ProcessTable>.
144  
145 If the passed value is undefined or not a Proc::ProcessTable::Process
146 object, an empty string is returned.
147  
148 print $proc->pid.' '.$is->info( $proc )."\n";
149  
150 =cut
151  
152 70 ▶sub info {
Subroutine info: called CC=82 SCAR=44.2
Pod documented
153 70 my $self=$_[0];
154 70 my $proc=$_[1];
155  
156 # make sure we got the required bits for proceeding
157 70 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $proc or ref $proc ne "Proc::ProcessTable::Process") 3 67 70
Condition100%3/3
not defined $proc or ref $proc ne "Proc::ProcessTable::Process"
l!l&&r!l&&!r
1267
MC/DC100%2/2
not defined $proc or ref $proc ne "Proc::ProcessTable::Process"
not defined $proc ref $proc ne "Proc::ProcessTable::Process"
Truth table100%3/3
not(A) or B
A= not defined $procB= ref $proc ne "Proc::ProcessTable::Process"
ABresult
1X1
011
000
158 ( ! defined( $proc ) ) ||
159 ( ref( $proc ) ne 'Proc::ProcessTable::Process' )
160 ){
161 3 return '';
162 }
163 67 my %flags;
164 67 $flags{is_session_leader}=0;
165 67 $flags{is_being_forked}=0;
166 67 $flags{working_on_exiting}=0;
167 67 $flags{has_controlling_terminal}=0;
168 67 $flags{controlling_tty_active}=0;
169 67 $flags{traced_by_debugger}=0;
170 67 $flags{is_stopped}=0;
171 67 $flags{is_kern_proc}=0;
172 67 $flags{posix_advisory_lock}=0;
173  
174 # the procfs based implementations, used on NetBSD, DragonFly, and FreeBSD
175 # prior to 5, report the flags as a comma separated list of words instead
176 # of as a number... those implementations also store the wait channel in
177 # the state field, meaning there is no state to display for them
178 67 my $flags_are_words=0;
179 67 ▶ if ( defined( $proc->{flags} ) && ( $proc->{flags} =~ /[^0-9a-fA-FxX]/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc->{"flags"} and $proc->{"flags"} =~ /[^0-9a-fA-FxX]/) 14 53 67
Condition100%3/3
defined $proc->{"flags"} and $proc->{"flags"} =~ /[^0-9a-fA-FxX]/
!ll&&!rl&&r
114214
MC/DC100%2/2
defined $proc->{"flags"} and $proc->{"flags"} =~ /[^0-9a-fA-FxX]/
defined $proc->{"flags"} $proc->{"flags"} =~ /[^0-9a-fA-FxX]/
Truth table100%3/3
A and B
A= defined $proc->{"flags"}B= $proc->{"flags"} =~ /[^0-9a-fA-FxX]/
ABresult
0X0
100
111
180 14 $flags_are_words=1;
181 }
182 67 my $state_is_wchan=0;
183  
184 # exact matches as gnukfreebsd would otherwise match freebsd, while
185 # Proc::ProcessTable uses its Linux implementation there
186 67 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "freebsd" or $^O eq "midnightbsd") 12 55 67
Condition100%3/3
$^O eq "freebsd" or $^O eq "midnightbsd"
l!l&&r!l&&!r
10255
MC/DC100%2/2
$^O eq "freebsd" or $^O eq "midnightbsd"
$^O eq "freebsd" $^O eq "midnightbsd"
Truth table100%3/3
A or B
A= $^O eq "freebsd"B= $^O eq "midnightbsd"
ABresult
1X1
011
000
187 ( $^O eq 'freebsd' ) ||
188 ( $^O eq 'midnightbsd' )
189 ) {
190 12 ▶ if ( $flags_are_words ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags_are_words) { } 2 10 12
191 # ancient FreeBSD, using the procfs implementation, which only
192 # knows about these two
193 2 $state_is_wchan=1;
194 2 ▶ if ( $proc->{flags} =~ /ctty/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->{"flags"} =~ /ctty/) 1 1 2
195 1 $flags{has_controlling_terminal}=1;
196 }
197 2 ▶ if ( $proc->{flags} =~ /sldr/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->{"flags"} =~ /sldr/) 1 1 2
198 1 $flags{is_session_leader}=1;
199 }
200 } else {
201 10 my $proc_flags=0;
202 10 ▶ if ( defined( $proc->{flags} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc->{"flags"}) 10 0 10
203 10 $proc_flags=hex( $proc->{flags} );
204 }
205 10 ▶ if ( ( $proc_flags & 0x00002 ) && ( $proc->ttynum != -1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 2 and $proc->ttynum != -1) 1 9 10
Condition100%3/3
$proc_flags & 2 and $proc->ttynum != -1
!ll&&!rl&&r
721
MC/DC100%2/2
$proc_flags & 2 and $proc->ttynum != -1
$proc_flags & 2 $proc->ttynum != -1
Truth table100%3/3
A and B
A= $proc_flags & 2B= $proc->ttynum != -1
ABresult
0X0
100
111
206 1 $flags{controlling_tty_active}=1;
207 }
208 10 ▶ if ( defined( $proc->sess ) && ( $proc->sess == $proc->pid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc->sess and $proc->sess == $proc->pid) 1 9 10
Condition66%2/3
defined $proc->sess and $proc->sess == $proc->pid
!ll&&!rl&&r
091
MC/DC50%1/2
defined $proc->sess and $proc->sess == $proc->pid
defined $proc->sess $proc->sess == $proc->pid
Truth table66%2/3
A and B
A= defined $proc->sessB= $proc->sess == $proc->pid
ABresult
0X0
100
111
209 1 $flags{is_session_leader}=1;
210 }
211 10 ▶ if ( $proc_flags & 0x00010 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 16) 1 9 10
212 1 $flags{is_being_forked}=1;
213 }
214 10 ▶ if ( $proc_flags & 0x02000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 8192) 1 9 10
215 1 $flags{working_on_exiting}=1;
216 }
217 10 ▶ if ( $proc_flags & 0x00002 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 2) 3 7 10
218 3 $flags{has_controlling_terminal}=1;
219 }
220 10 ▶ if ( $proc_flags & 0x00004 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 4) 1 9 10
221 1 $flags{is_kern_proc}=1;
222 }
223 10 ▶ if ( $proc_flags & 0x00800 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 2048) 2 8 10
224 2 $flags{traced_by_debugger}=1;
225 }
226 10 ▶ if ( $proc_flags & 0x00001 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 1) 1 9 10
227 1 $flags{posix_advisory_lock}=1;
228 }
229 }
230 }
231  
232 # NetBSD and DragonFly both use a procfs based implementation, which
233 # reports the flags as a comma separated list of words, of which only
234 # ctty and sldr exist
235 67 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "netbsd" or $^O eq "dragonfly") 12 55 67
Condition100%3/3
$^O eq "netbsd" or $^O eq "dragonfly"
l!l&&r!l&&!r
6655
MC/DC100%2/2
$^O eq "netbsd" or $^O eq "dragonfly"
$^O eq "netbsd" $^O eq "dragonfly"
Truth table100%3/3
A or B
A= $^O eq "netbsd"B= $^O eq "dragonfly"
ABresult
1X1
011
000
236 ( $^O eq 'netbsd' ) ||
237 ( $^O eq 'dragonfly' )
238 ) {
239 12 $state_is_wchan=1;
240 12 ▶ if ( defined( $proc->{flags} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc->{"flags"}) 12 0 12
241 12 ▶ if ( $proc->{flags} =~ /ctty/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->{"flags"} =~ /ctty/) 4 8 12
242 4 $flags{has_controlling_terminal}=1;
243 }
244 12 ▶ if ( $proc->{flags} =~ /sldr/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->{"flags"} =~ /sldr/) 4 8 12
245 4 $flags{is_session_leader}=1;
246 }
247 }
248 }
249  
250 # OpenBSD does not provide a flags field at all, so session leader and
251 # having a controlling terminal are all that may be figured out
252 67 ▶ if ( $^O eq 'openbsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "openbsd") 11 56 67
253 11 ▶ if ( defined( $proc->{sess} ) && ( $proc->{sess} == $proc->pid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc->{"sess"} and $proc->{"sess"} == $proc->pid) 1 10 11
Condition66%2/3
defined $proc->{"sess"} and $proc->{"sess"} == $proc->pid
!ll&&!rl&&r
0101
MC/DC50%1/2
defined $proc->{"sess"} and $proc->{"sess"} == $proc->pid
defined $proc->{"sess"} $proc->{"sess"} == $proc->pid
Truth table66%2/3
A and B
A= defined $proc->{"sess"}B= $proc->{"sess"} == $proc->pid
ABresult
0X0
100
111
254 1 $flags{is_session_leader}=1;
255 }
256 # no tty is NODEV, which may show up as either -1 or as the same bits
257 # read unsigned, depending on platform
258 11 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc->{"ttynum"} and $proc->{"ttynum"} != -1 and $proc->{"ttynum"} != 4294967295) 1 10 11
Condition83%5/6
defined $proc->{"ttynum"} and $proc->{"ttynum"} != -1 and $proc->{"ttynum"} != 4294967295
!ll&&!rl&&r
911
defined $proc->{"ttynum"} and $proc->{"ttynum"} != -1
!ll&&!rl&&r
092
MC/DC66%2/3
defined $proc->{"ttynum"} and $proc->{"ttynum"} != -1 and $proc->{"ttynum"} != 4294967295
defined $proc->{"ttynum"} $proc->{"ttynum"} != -1 $proc->{"ttynum"} != 4294967295
Truth table75%3/4
A and B and C
A= defined $proc->{"ttynum"}B= $proc->{"ttynum"} != -1C= $proc->{"ttynum"} != 4294967295
ABCresult
0XX0
10X0
1100
1111
259 defined( $proc->{ttynum} ) &&
260 ( $proc->{ttynum} != -1 ) &&
261 ( $proc->{ttynum} != 0xffffffff )
262 ) {
263 1 $flags{has_controlling_terminal}=1;
264 }
265 }
266  
267 # gnukfreebsd is Debian GNU/kFreeBSD, where Proc::ProcessTable uses its
268 # Linux implementation
269 67 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "linux" or $^O eq "gnukfreebsd") 10 57 67
Condition100%3/3
$^O eq "linux" or $^O eq "gnukfreebsd"
l!l&&r!l&&!r
8257
MC/DC100%2/2
$^O eq "linux" or $^O eq "gnukfreebsd"
$^O eq "linux" $^O eq "gnukfreebsd"
Truth table100%3/3
A or B
A= $^O eq "linux"B= $^O eq "gnukfreebsd"
ABresult
1X1
011
000
270 ( $^O eq 'linux' ) ||
271 ( $^O eq 'gnukfreebsd' )
272 ) {
273 # on Linux the flags field is a plain decimal number, not a hex string
274 10 my $proc_flags=$proc->flags;
275 10 ▶ if ( defined( $proc->sess ) && ( $proc->sess == $proc->pid ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc->sess and $proc->sess == $proc->pid) 2 8 10
Condition66%2/3
defined $proc->sess and $proc->sess == $proc->pid
!ll&&!rl&&r
082
MC/DC50%1/2
defined $proc->sess and $proc->sess == $proc->pid
defined $proc->sess $proc->sess == $proc->pid
Truth table66%2/3
A and B
A= defined $proc->sessB= $proc->sess == $proc->pid
ABresult
0X0
100
111
276 2 $flags{is_session_leader}=1;
277 }
278 10 ▶ if ( $proc->ttynum != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc->ttynum != 0) 1 9 10
279 1 $flags{has_controlling_terminal}=1;
280 }
281 # PF_EXITING
282 10 ▶ if ( $proc_flags & 0x00000004 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 4) 2 8 10
283 2 $flags{working_on_exiting}=1;
284 }
285 # PF_KTHREAD
286 10 ▶ if ( $proc_flags & 0x00200000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 2097152) 1 9 10
287 1 $flags{is_kern_proc}=1;
288 }
289 # PF_FORKNOEXEC, forked but has not yet called exec...
290 # kernel threads never exec so it is meaningless noise for them
291 10 ▶ if ( ( $proc_flags & 0x00000040 ) && ( !$flags{is_kern_proc} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proc_flags & 64 and not $flags{"is_kern_proc"}) 1 9 10
Condition100%3/3
$proc_flags & 64 and not $flags{"is_kern_proc"}
!ll&&!rl&&r
811
MC/DC100%2/2
$proc_flags & 64 and not $flags{"is_kern_proc"}
$proc_flags & 64 not $flags{"is_kern_proc"}
Truth table100%3/3
A and not(B)
A= $proc_flags & 64B= not $flags{"is_kern_proc"}
ABresult
0X0
100
111
292 1 $flags{is_being_forked}=1;
293 }
294 # hash access as the tracer field is not present in all Proc::ProcessTable versions
295 10 ▶ if ( exists( $proc->{tracer} ) && defined( $proc->{tracer} ) && ( $proc->{tracer} != 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $proc->{"tracer"} and defined $proc->{"tracer"} and $proc->{"tracer"} != 0) 1 9 10
Condition83%5/6
exists $proc->{"tracer"} and defined $proc->{"tracer"} and $proc->{"tracer"} != 0
!ll&&!rl&&r
811
exists $proc->{"tracer"} and defined $proc->{"tracer"}
!ll&&!rl&&r
802
MC/DC66%2/3
exists $proc->{"tracer"} and defined $proc->{"tracer"} and $proc->{"tracer"} != 0
exists $proc->{"tracer"} defined $proc->{"tracer"} $proc->{"tracer"} != 0
Truth table75%3/4
A and B and C
A= exists $proc->{"tracer"}B= defined $proc->{"tracer"}C= $proc->{"tracer"} != 0
ABCresult
0XX0
10X0
1100
1111
296 1 $flags{traced_by_debugger}=1;
297 }
298 }
299  
300 # hash access as Linux does not set the state field at all when it runs
301 # into a state it does not recognize... lowercased as OpenBSD reports the
302 # state in upper case
303 67 my $state=$proc->{state};
304 67 ▶ if ( !defined( $state ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $state) 1 66 67
305 1 $state='';
306 }
307 67 $state=lc( $state );
308 67 my $info=$state;
309 67 ▶ if ( $state_is_wchan ) {
Branch: 14 branches
BranchTrueFalseTotal
if ($state_is_wchan) { } 14 53 67
elsif ($info eq 'sleep') { } 31 22 53
elsif ($info eq 'zombie') { } 3 19 22
elsif ($info eq 'wait') { } 1 18 19
elsif ($info eq 'run') { } 2 16 18
elsif ($info eq 'stop') { } 2 14 16
elsif ($info eq 'idle') { } 3 11 14
elsif ($info eq 'lock') { } 1 10 11
elsif ($info eq 'defunct') { } 2 8 10
elsif ($info eq 'uwait') { } 1 7 8
elsif ($info eq 'tracingstop') { } 1 6 7
elsif ($info eq 'dead') { } 1 5 6
elsif ($info eq 'wakekill') { } 1 4 5
elsif ($info eq 'parked') { } 1 3 4
310 # no state to map, the field holds the wait channel
311 14 $info='?';
312 } elsif (
313 $info eq 'sleep'
314 ) {
315 31 $info='S';
316 } elsif (
317 $info eq 'zombie'
318 ) {
319 3 $info='Z';
320 } elsif (
321 $info eq 'wait'
322 ) {
323 1 $info='W';
324 } elsif (
325 $info eq 'run'
326 ) {
327 2 $info='R';
328 } elsif (
329 $info eq 'stop'
330 ) {
331 2 $flags{is_stopped}=1;
332 2 $info='T';
333 } elsif (
334 $info eq 'idle'
335 ) {
336 3 $info='I';
337 } elsif (
338 $info eq 'lock'
339 ) {
340 1 $info='L';
341 } elsif (
342 $info eq 'defunct'
343 ) {
344 2 $info='Z';
345 } elsif (
346 $info eq 'uwait'
347 ) {
348 1 $info='D';
349 } elsif (
350 $info eq 'tracingstop'
351 ) {
352 1 $flags{is_stopped}=1;
353 1 $info='t';
354 } elsif (
355 $info eq 'dead'
356 ) {
357 1 $info='X';
358 } elsif (
359 $info eq 'wakekill'
360 ) {
361 1 $info='K';
362 } elsif (
363 $info eq 'parked'
364 ) {
365 1 $info='P';
366 } else {
367 3 $info='?';
368 }
369  
370 #add initial color if needed
371 67 ▶ if ( defined( $self->{flags_color} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"flags_color"}) 1 66 67
372 1 $info=color( $self->{flags_color} ).$info;
373 }
374  
375 #checks if it is swapped out... hash access as the procfs based
376 #implementations, NetBSD and DragonFly, do not provide a rss field
377 67 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($state ne "zombie" and $state ne "defunct" and defined $proc->{"rss"} and $proc->{"rss"} == 0 and $flags{"is_kern_proc"} == 0) 2 65 67
Condition100%12/12
$state ne "zombie" and $state ne "defunct" and defined $proc->{"rss"} and $proc->{"rss"} == 0 and $flags{"is_kern_proc"} == 0
!ll&&!rl&&r
6322
$state ne "zombie" and $state ne "defunct" and defined $proc->{"rss"} and $proc->{"rss"} == 0
!ll&&!rl&&r
17464
$state ne "zombie" and $state ne "defunct" and defined $proc->{"rss"}
!ll&&!rl&&r
51250
$state ne "zombie" and $state ne "defunct"
!ll&&!rl&&r
3262
MC/DC100%5/5
$state ne "zombie" and $state ne "defunct" and defined $proc->{"rss"} and $proc->{"rss"} == 0 and $flags{"is_kern_proc"} == 0
$state ne "zombie" $state ne "defunct" defined $proc->{"rss"} $proc->{"rss"} == 0 $flags{"is_kern_proc"} == 0
Truth table100%6/6
A and B and C and D and E
A= $state ne "zombie"B= $state ne "defunct"C= defined $proc->{"rss"}D= $proc->{"rss"} == 0E= $flags{"is_kern_proc"} == 0
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
378 ( $state ne 'zombie' ) &&
379 ( $state ne 'defunct' ) &&
380 ( defined( $proc->{rss} ) ) &&
381 ( $proc->{rss} == 0 ) &&
382 ( $flags{is_kern_proc} == 0 )
383 ) {
384 2 $info=$info.'O';
385 }
386  
387 #handles the various flags
388 67 ▶ if ( $flags{working_on_exiting} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{"working_on_exiting"}) 3 64 67
389 3 $info=$info.'E';
390 }
391 67 ▶ if ( $flags{is_session_leader} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{"is_session_leader"}) 9 58 67
392 9 $info=$info.'s';
393 }
394 67 ▶ if ( $flags{posix_advisory_lock} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{"posix_advisory_lock"}) 1 66 67
395 1 $info=$info.'L';
396 }
397 67 ▶ if ( $flags{has_controlling_terminal} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{"has_controlling_terminal"}) 10 57 67
398 10 $info=$info.'+';
399 }
400 67 ▶ if ( $flags{controlling_tty_active} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{"controlling_tty_active"}) 1 66 67
401 1 $info=$info.'c';
402 }
403 67 ▶ if ( $flags{is_being_forked} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{"is_being_forked"}) 2 65 67
404 2 $info=$info.'F';
405 }
406 67 ▶ if ( $flags{traced_by_debugger} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{"traced_by_debugger"}) 3 64 67
407 3 $info=$info.'X';
408 }
409  
410 # adds the initial color reset if needed
411 67 ▶ if ( defined( $self->{flags_color} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"flags_color"}) 1 66 67
412 1 $info=$info.color( 'reset' );
413 }
414 67 $info=$info.' ';
415  
416  
417 # adds the second color if needed
418 67 ▶ if ( defined( $self->{wchan_color} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"wchan_color"}) 1 66 67
419 1 $info=$info.color( $self->{wchan_color} );
420 }
421  
422 # adds the wait channel
423 67 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'linux' or $^O eq 'gnukfreebsd') { } 10 57 67
Condition100%3/3
$^O eq "linux" or $^O eq "gnukfreebsd"
l!l&&r!l&&!r
8257
MC/DC100%2/2
$^O eq "linux" or $^O eq "gnukfreebsd"
$^O eq "linux" $^O eq "gnukfreebsd"
Truth table100%3/3
A or B
A= $^O eq "linux"B= $^O eq "gnukfreebsd"
ABresult
1X1
011
000
424 ( $^O eq 'linux' ) ||
425 ( $^O eq 'gnukfreebsd' )
426 ) {
427 10 my $wchan='';
428 10 ▶ if ( open( my $wchan_fh, '<', '/proc/'.$proc->pid.'/wchan' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $wchan_fh, "<", '/proc/' . $proc->pid . '/wchan') 0 10 10
429 0 $wchan=readline( $wchan_fh );
430 # a bare 0 means no wait channel
431 0 ▶ if ( ( ! defined( $wchan ) ) || ( $wchan eq '0' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $wchan or $wchan eq 0) 0 0 0
Condition0%0/3
not defined $wchan or $wchan eq 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $wchan or $wchan eq 0
not defined $wchan $wchan eq 0
Truth table0%0/3
not(A) or B
A= not defined $wchanB= $wchan eq 0
ABresult
1X1
011
000
432 0 $wchan='';
433 }
434 0 close( $wchan_fh );
435 }
436 10 $info=$info.$wchan;
437 } else {
438 # hash access as the wchan accessor croaks on platforms lacking the
439 # field, such as OpenBSD... the procfs based implementations, NetBSD
440 # and DragonFly, use nochan for a process that is not blocked
441 57 ▶ if ( defined( $proc->{wchan} ) && ( $proc->{wchan} ne 'nochan' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proc->{"wchan"} and $proc->{"wchan"} ne "nochan") 7 50 57
Condition100%3/3
defined $proc->{"wchan"} and $proc->{"wchan"} ne "nochan"
!ll&&!rl&&r
4287
MC/DC100%2/2
defined $proc->{"wchan"} and $proc->{"wchan"} ne "nochan"
defined $proc->{"wchan"} $proc->{"wchan"} ne "nochan"
Truth table100%3/3
A and B
A= defined $proc->{"wchan"}B= $proc->{"wchan"} ne "nochan"
ABresult
0X0
100
111
442 7 $info=$info.$proc->{wchan};
443 }
444 }
445  
446 # adds the second color reset if needed
447 67 ▶ if ( defined( $self->{wchan_color} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"wchan_color"}) 1 66 67
448 1 $info=$info.color( 'reset' );
449 }
450  
451 67 return $info;
452 }
453  
454 =head1 FUNCTIONS
455  
456 =head2 proc_infostring_describe
457  
458 Returns a human readable description of what the various parts of the
459 info string mean. Meant for use by programs wanting to print a
460 explanation of the info string as part of their help output.
461  
462 This is a plain function and does not require calling L</new> first. It
463 may be optionally imported.
464  
465 use Proc::ProcessTable::InfoString qw(proc_infostring_describe);
466  
467 print proc_infostring_describe();
468  
469 Or called without importing it.
470  
471 print Proc::ProcessTable::InfoString::proc_infostring_describe();
472  
473 The states and flags described are the ones this module is able to
474 figure out for the OS in question, so what is printed for FreeBSD
475 differs from what is printed for Linux, OpenBSD, NetBSD, or DragonFly.
476  
477 =head3 args
478  
479 Args are taken as a plain hash.
480  
481 =head4 os
482  
483 The OS to describe the info string for. If not specified, the OS perl
484 is currently running on, $^O, is used.
485  
486 print proc_infostring_describe( os => 'linux' );
487  
488 =cut
489  
490 16 ▶sub proc_infostring_describe {
Subroutine proc_infostring_describe: called CC=16 SCAR=27.7
Pod documented
491 16 my %args= @_;
492  
493 16 my $os=$args{os};
494 16 ▶ if ( ! defined( $os ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $os) 2 14 16
495 2 $os=$^O;
496 }
497  
498 # every state and flag this module knows how to produce, in the order
499 # they appear in the info string
500 16 my @all_states=(
501 [ 'S', 'sleep' ],
502 [ 'R', 'run' ],
503 [ 'I', 'idle' ],
504 [ 'W', 'wait' ],
505 [ 'D', 'uninterruptible sleep' ],
506 [ 'L', 'blocked on a kernel lock' ],
507 [ 'T', 'stopped, such as via SIGSTOP' ],
508 [ 't', 'stopped by a tracer, such as a debugger' ],
509 [ 'Z', 'zombie, has exited and is waiting on its parent to reap it' ],
510 [ 'X', 'dead' ],
511 [ 'K', 'wakekill' ],
512 [ 'P', 'parked' ],
513 [ '?', 'unknown state' ],
514 );
515 16 my @all_flags=(
516 [ 'O', 'swapped out, has no resident memory' ],
517 [ 'E', 'exiting' ],
518 [ 's', 'session leader' ],
519 [ 'L', 'holds a POSIX advisory lock' ],
520 [ '+', 'has a controlling terminal' ],
521 [ 'c', 'the controlling tty is active' ],
522 [ 'F', 'forked, but has not called exec yet' ],
523 [ 'X', 'traced by a debugger' ],
524 );
525  
526 # which of those are actually usable is decided by what
527 # L<Proc::ProcessTable> is able to report for the OS in question
528 # exact matches as gnukfreebsd would otherwise match freebsd, while it is
529 # Debian GNU/kFreeBSD, where Proc::ProcessTable uses its Linux
530 # implementation
531 16 my @wanted_states;
532 my @wanted_flags;
533 16 ▶ if (
Branch: 4 branches
BranchTrueFalseTotal
if ($os eq 'freebsd' or $os eq 'midnightbsd') { } 3 13 16
elsif ($os eq 'linux' or $os eq 'gnukfreebsd') { } 5 8 13
elsif ($os eq 'openbsd') { } 2 6 8
elsif ($os eq 'netbsd' or $os eq 'dragonfly') { } 4 2 6
Condition100%3/3
$os eq "freebsd" or $os eq "midnightbsd"
l!l&&r!l&&!r
2113
MC/DC100%2/2
$os eq "freebsd" or $os eq "midnightbsd"
$os eq "freebsd" $os eq "midnightbsd"
Truth table100%3/3
A or B
A= $os eq "freebsd"B= $os eq "midnightbsd"
ABresult
1X1
011
000
534 ( $os eq 'freebsd' ) ||
535 ( $os eq 'midnightbsd' )
536 ) {
537 3 @wanted_states=( 'S', 'R', 'I', 'W', 'L', 'T', 'Z', '?' );
538 3 @wanted_flags=( 'O', 'E', 's', 'L', '+', 'c', 'F', 'X' );
539 ▶ } elsif (
Condition100%3/3
$os eq "linux" or $os eq "gnukfreebsd"
l!l&&r!l&&!r
418
MC/DC100%2/2
$os eq "linux" or $os eq "gnukfreebsd"
$os eq "linux" $os eq "gnukfreebsd"
Truth table100%3/3
A or B
A= $os eq "linux"B= $os eq "gnukfreebsd"
ABresult
1X1
011
000
540 ( $os eq 'linux' ) ||
541 ( $os eq 'gnukfreebsd' )
542 ) {
543 5 @wanted_states=( 'S', 'R', 'I', 'W', 'D', 'T', 't', 'Z', 'X', 'K', 'P', '?' );
544 5 @wanted_flags=( 'O', 'E', 's', '+', 'F', 'X' );
545 } elsif ( $os eq 'openbsd' ) {
546 2 @wanted_states=( 'S', 'R', 'I', 'T', 'Z', '?' );
547 2 @wanted_flags=( 'O', 's', '+' );
548 ▶ } elsif (
Condition100%3/3
$os eq "netbsd" or $os eq "dragonfly"
l!l&&r!l&&!r
222
MC/DC100%2/2
$os eq "netbsd" or $os eq "dragonfly"
$os eq "netbsd" $os eq "dragonfly"
Truth table100%3/3
A or B
A= $os eq "netbsd"B= $os eq "dragonfly"
ABresult
1X1
011
000
549 ( $os eq 'netbsd' ) ||
550 ( $os eq 'dragonfly' )
551 ) {
552 4 @wanted_states=( '?' );
553 4 @wanted_flags=( 's', '+' );
554 } else {
555 # a OS with no flag handling of its own, so everything is listed
556 # as there is no telling what it reports
557 2 @wanted_states=map( { $_->[0] } @all_states );
558 2 @wanted_flags=( 'O' );
559 }
560  
561 # OpenBSD is the one OS here with no wait channel to show
562 16 my $has_wchan=1;
563 16 ▶ if ( $os eq 'openbsd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os eq "openbsd") 2 14 16
564 2 $has_wchan=0;
565 }
566  
567 16 my $described="Info string for ".$os.", formed as below.\n"
568 ."\n"
569 ." <state>[flags]";
570 16 ▶ if ( $has_wchan ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_wchan) 14 2 16
571 14 $described=$described." [wait channel]";
572 }
573 16 $described=$described."\n"
574 ."\n"
575 ."States\n";
576 16 foreach my $state ( @all_states ) {
577 208 ▶ if ( grep( { $_ eq $state->[0] } @wanted_states ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $state->[0];} @wanted_states) 126 82 208
578 126 $described=$described.sprintf( " %-4s%s\n", $state->[0], $state->[1] );
579 }
580 }
581  
582 16 $described=$described."\n"
583 ."Flags\n";
584 16 foreach my $flag ( @all_flags ) {
585 128 ▶ if ( grep( { $_ eq $flag->[0] } @wanted_flags ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $flag->[0];} @wanted_flags) 70 58 128
586 70 $described=$described.sprintf( " %-4s%s\n", $flag->[0], $flag->[1] );
587 }
588 }
589  
590 16 ▶ if ( $has_wchan ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_wchan) 14 2 16
591 14 $described=$described."\n"
592 ."Wait Channel\n"
593 ." The kernel function or resource the process is currently blocked on,\n"
594 ." blank when it is not blocked.\n";
595 }
596  
597 16 return $described;
598 }
599  
600 =head1 AUTHOR
601  
602 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
603  
604 =head1 BUGS
605  
606 Please report any bugs or feature requests to C<bug-proc-processtable-infostring at rt.cpan.org>, or through
607 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Proc-ProcessTable-InfoString>. I will be notified, and then you'll
608 automatically be notified of progress on your bug as I make changes.
609  
610  
611  
612  
613 =head1 SUPPORT
614  
615 You can find documentation for this module with the perldoc command.
616  
617 perldoc Proc::ProcessTable::InfoString
618  
619  
620 You can also look for information at:
621  
622 =over 4
623  
624 =item * RT: CPAN's request tracker (report bugs here)
625  
626 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Proc-ProcessTable-InfoString>
627  
628 =item * Search CPAN
629  
630 L<https://metacpan.org/release/Proc-ProcessTable-InfoString>
631  
632 =back
633  
634  
635 =head1 LICENSE AND COPYRIGHT
636  
637 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
638  
639 This is free software, licensed under:
640  
641 The Artistic License 2.0 (GPL Compatible)
642  
643  
644 =cut
645  
646 3 1; # End of Proc::ProcessTable::InfoString
↑ Summary