File Coverage

blib/lib/Proc/ProcessTable/InfoString.pm
Criterion Covered Total %
statement 11 88 12.5
branch 0 54 0.0
condition 0 12 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 17 162 10.4


line stmt bran cond sub pod time code
1             package Proc::ProcessTable::InfoString;
2              
3 1     1   54715 use 5.006;
  1         3  
4 1     1   5 use strict;
  1         2  
  1         16  
5 1     1   4 use warnings;
  1         1  
  1         30  
6 1     1   534 use Term::ANSIColor;
  1         6839  
  1         596  
7              
8             =head1 NAME
9              
10             Proc::ProcessTable::InfoString - Greats a PS like stat string showing various symbolic represenation of various flags/state as well as the wchan.
11              
12             =head1 VERSION
13              
14             Version 0.0.1
15              
16             =cut
17              
18             our $VERSION = '0.0.1';
19              
20              
21             =head1 SYNOPSIS
22              
23             Quick summary of what the module does.
24              
25             Perhaps a little code snippet.
26              
27             use Proc::ProcessTable::InfoString;
28             use Proc::ProcessTable;
29              
30             my $is = Proc::ProcessTable::InfoString->new();
31              
32             my $p = Proc::ProcessTable->new( 'cache_ttys' => 1 );
33             my $pt = $p->table;
34              
35             foreach my $proc ( @{ $pt } ){
36             print $proc->pid.' '.$is->info( $proc )."\n";
37             }
38              
39             The mapping for the left side of the output is as below.
40              
41             States Description
42             Z Zombie
43             S Sleep
44             W Wait
45             R Run
46              
47             Flags Description
48             O Swapped Output
49             E Exiting
50             s Session Leader
51             L POSIX lock advisory
52             + has controlling terminal
53             X traced by a debugger
54             F being forked
55              
56             =head1 METHODS
57              
58             =head2 new
59              
60             This initiates the object.
61              
62             One argument is taken and that is a optional hash reference.
63              
64             =head3 args hash
65              
66             This will be passed to L.
67              
68             If not specified, no ANSI color codes are used.
69              
70             The return string is terminated by a ANSI color reset character.
71              
72             =head4 flags_color
73              
74             The color to use for the flags section of the string.
75              
76             =head4 wchan_color
77              
78             The color to use for the wait channel section of the string.
79              
80             =cut
81              
82             sub new {
83 0     0 1   my %args;
84 0 0         if (defined($_[1])) {
85 0           %args= %{$_[1]};
  0            
86             }
87              
88 0           my $self = {
89             };
90 0           bless $self;
91              
92 0           my @args_feed=(
93             'flags_color',
94             'wchan_color',
95             );
96              
97 0           foreach my $feed ( @args_feed ){
98 0           $self->{$feed}=$args{$feed};
99             }
100              
101 0           return $self;
102             }
103              
104             =head2 info
105              
106             =cut
107              
108             sub info {
109 0     0 1   my $self=$_[0];
110 0           my $proc=$_[1];
111              
112             # make sure we got the required bits for proceeding
113 0 0 0       if (
114             ( ! defined( $proc ) ) ||
115             ( ref( $proc ) ne 'Proc::ProcessTable::Process' )
116             ){
117 0           return '';
118             }
119 0           my %flags;
120 0           $flags{is_session_leader}=0;
121 0           $flags{is_being_forked}=0;
122 0           $flags{working_on_exiting}=0;
123 0           $flags{has_controlling_terminal}=0;
124 0           $flags{is_locked}=0;
125 0           $flags{traced_by_debugger}=0;
126 0           $flags{is_stopped}=0;
127 0           $flags{is_kern_proc}=0;
128 0           $flags{posix_advisory_lock}=0;
129              
130 0 0         if ( $^O =~ /freebsd/ ) {
131 0 0         if ( hex($proc->flags) & 0x00002 ) {
132 0           $flags{controlling_tty_active}=1;
133             }
134 0 0         if ( hex($proc->flags) & 0x00000002 ) {
135 0           $flags{is_session_leader}=1;
136             }
137             #if ( hex($proc->flags) & ){$flags{is_being_forked}=1; }
138 0 0         if ( hex($proc->flags) & 0x02000 ) {
139 0           $flags{working_on_exiting}=1;
140             }
141 0 0         if ( hex($proc->flags) & 0x00002 ) {
142 0           $flags{has_controlling_terminal}=1;
143             }
144 0 0         if ( hex($proc->flags) & 0x00000004 ) {
145 0           $flags{is_locked}=1;
146             }
147 0 0         if ( hex($proc->flags) & 0x00800 ) {
148 0           $flags{traced_by_debugger}=1;
149             }
150 0 0         if ( hex($proc->flags) & 0x00001 ) {
151 0           $flags{posix_advisory_lock}=1;
152             }
153             }
154              
155 0           my $info=$proc->{state};
156 0 0         if (
    0          
    0          
    0          
157             $info eq 'sleep'
158             ) {
159 0           $info='S';
160             } elsif (
161             $info eq 'zombie'
162             ) {
163 0           $info='Z';
164             } elsif (
165             $info eq 'wait'
166             ) {
167 0           $info='W';
168             } elsif (
169             $info eq 'run'
170             ) {
171 0           $info='R';
172             }
173              
174             #add initial color if needed
175 0 0         if ( defined( $self->{flags_color} ) ){
176 0           $info=color( $self->{flags_color} ).$info;
177             }
178              
179             #checks if it is swapped out
180 0 0 0       if (
      0        
181             ( $proc->{state} ne 'zombie' ) &&
182             ( $proc->{rss} == '0' ) &&
183             ( $flags{is_kern_proc} == '0' )
184             ) {
185 0           $info=$info.'O';
186             }
187              
188             #handles the various flags
189 0 0         if ( $flags{working_on_exiting} ) {
190 0           $info=$info.'E';
191             }
192 0 0         if ( $flags{is_session_leader} ) {
193 0           $info=$info.'s';
194             }
195 0 0 0       if ( $flags{is_locked} || $flags{posix_advisory_lock} ) {
196 0           $info=$info.'L';
197             }
198 0 0         if ( $flags{has_controlling_terminal} ) {
199 0           $info=$info.'+';
200             }
201 0 0         if ( $flags{is_being_forked} ) {
202 0           $info=$info.'F';
203             }
204 0 0         if ( $flags{traced_by_debugger} ) {
205 0           $info=$info.'X';
206             }
207              
208             # adds the initial color reset if needed
209 0 0         if ( defined( $self->{flags_color} ) ){
210 0           $info=$info.color( 'reset' );
211             }
212 0           $info=$info.' ';
213              
214              
215             # adds the second color if needed
216 0 0         if ( defined( $self->{wchan_color} ) ){
217 0           $info=$info.color( $self->{wchan_color} );
218             }
219              
220             # adds the wait channel
221 0 0         if ( $^O =~ /linux/ ) {
222 0           my $wchan='';
223 0 0         if ( -e '/proc/'.$proc->{pid}.'/wchan') {
224 0           open( my $wchan_fh, '<', '/proc/'.$proc->{pid}.'/wchan' );
225 0           $wchan=readline( $wchan_fh );
226 0           close( $wchan_fh );
227             }
228 0           $info=$info.$wchan;
229             } else {
230 0           $info=$info.$proc->{wchan};
231             }
232              
233             # adds the second color reset if needed
234 0 0         if ( defined( $self->{wchan_color} ) ){
235 0           $info=$info.color( 'reset' );
236             }
237              
238 0           return $info;
239             }
240              
241             =head1 AUTHOR
242              
243             Zane C. Bowers-Hadley, C<< >>
244              
245             =head1 BUGS
246              
247             Please report any bugs or feature requests to C, or through
248             the web interface at L. I will be notified, and then you'll
249             automatically be notified of progress on your bug as I make changes.
250              
251              
252              
253              
254             =head1 SUPPORT
255              
256             You can find documentation for this module with the perldoc command.
257              
258             perldoc Proc::ProcessTable::InfoString
259              
260              
261             You can also look for information at:
262              
263             =over 4
264              
265             =item * RT: CPAN's request tracker (report bugs here)
266              
267             L
268              
269             =item * AnnoCPAN: Annotated CPAN documentation
270              
271             L
272              
273             =item * CPAN Ratings
274              
275             L
276              
277             =item * Search CPAN
278              
279             L
280              
281             =item * Repository
282              
283             L
284              
285             =back
286              
287              
288             =head1 ACKNOWLEDGEMENTS
289              
290              
291             =head1 LICENSE AND COPYRIGHT
292              
293             This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
294              
295             This is free software, licensed under:
296              
297             The Artistic License 2.0 (GPL Compatible)
298              
299              
300             =cut
301              
302             1; # End of Proc::ProcessTable::InfoString