ProcessTable/Role/Table/ProcFS.pm

filter: Statementstmt 69.0% 96 / 139 Branchbran 38.3% 23 / 60 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 2 totaltotal 59.6% 133 / 223 CC 41 SCAR 51.2
CC
41
Cov
59%
_process_hash
244.2
fields
6.0
list
5.0
CRAP 166.8
↑ Summary ProcessTable/Role/Table/OS/MSWin32.pm »
1 package P9Y::ProcessTable::Role::Table::ProcFS;
2  
3 3 our $AUTHORITY = 'cpan:BBYRD'; # AUTHORITY
4 3 our $VERSION = '1.06_01'; # VERSION
5  
6 #############################################################################
7 # Modules
8  
9 # use sanity;
10 3 ▶use strict qw(subs vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 requires 'table';
18 3 requires 'process';
19  
20 3 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #############################################################################
28 # Methods
29  
30 sub list {
31 1 ▶ my $self = shift;
Subroutine list: called CC=5 SCAR=16.1
Pod undocumented
32  
33 1 my @list;
34 1 my $dir = dir('', 'proc');
35 1 ▶ while (my $pdir = $dir->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pdir = $dir->next) 73 1 74
36 73 ▶ next unless ($pdir->is_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless $pdir->is_dir 48 25 73
37 25 ▶ next unless (-e $pdir->file('status'));
Branch: 1 branch
BranchTrueFalseTotal
unless -e $pdir->file("status") 14 11 25
38 11 ▶ next unless ($pdir->basename =~ /^\d+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $pdir->basename =~ /^\d+$/ 2 9 11
39  
40 9 push @list, $pdir->basename;
41 }
42  
43 1 return sort { $a <=> $b } @list;
44 }
45  
46 sub fields {
47 0 ▶ return $^O eq /solaris|sunos/i ?
Subroutine fields: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$^O eq /solaris|sunos/i ? : 0 0 0
48 ( qw/
49 pid uid gid euid egid ppid pgrp sess
50 cwd exe root cmdline
51 utime stime cutime cstime start time ctime
52 fname ttynum flags threads size rss pctcpu pctmem
53 / ) :
54 ( qw/
55 pid uid gid ppid pgrp sess
56 cwd exe root cmdline environ
57 minflt cminflt majflt cmajflt ttlflt cttlflt utime stime cutime cstime start time ctime
58 priority fname state ttynum flags threads size rss wchan cpuid
59 / );
60 }
61  
62 sub _process_hash {
63 12 ▶ my ($self, $pid) = @_;
Subroutine _process_hash: called CC=36 SCAR=55.0
64  
65 12 my $pdir = dir('', 'proc', $pid);
66 12 ▶ return unless (-d $pdir);
Branch: 1 branch
BranchTrueFalseTotal
unless -d $pdir 1 11 12
67 11 my $hash = {
68 pid => $pid,
69 uid => $pdir->stat->uid,
70 gid => $pdir->stat->gid,
71 start => $pdir->stat->mtime, # not reliable
72 };
73  
74 # process links
75 11 foreach my $ln (qw{cwd exe root}) {
76 33 my $link = $pdir->file($ln);
77 33 ▶ $hash->{$ln} = readlink $link if (-l $link);
Branch: 1 branch
BranchTrueFalseTotal
if -l $link 33 0 33
78 }
79  
80 # process simple cats
81 11 foreach my $fn (qw{cmdline}) {
82 11 my $file = $pdir->file($fn);
83 11 ▶ next unless (-f $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 11 11
84 11 $hash->{$fn} = $file->slurp;
85 11 $hash->{$fn} =~ s/\0/ /g;
86 11 $hash->{$fn} =~ s/^\s+|\s+$//g;
87 }
88  
89 # process environment
90 11 my $env_file = $pdir->file('environ');
91 11 ▶ if (-f $env_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $env_file) 11 0 11
92 11 my $data;
93 11 eval { $data = $env_file->slurp; }; # skip permission failures
94 11 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 11 0 11
95 11 $data =~ s/^\0+|\0+$//g;
96 11 $hash->{environ} = { map { split /\=/, $_, 2 } grep { /\=/ } split /\0/, $data };
97 }
98 }
99  
100 11 my $clock_ticks = POSIX::sysconf( &POSIX::_SC_CLK_TCK );
101  
102 # start time is measured in the number of clock ticks since boot, so we need the boot time
103 11 my $boot_time;
104 11 my $uptime_file = file('', 'proc', 'uptime');
105 11 ▶ if ( -f $uptime_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $uptime_file) 11 0 11
106 11 my $time = time;
107 11 my $uptime = $uptime_file->slurp;
108 11 ▶ $boot_time = $time - $1 if $uptime =~ /^([\d\.]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $uptime =~ /^([\d\.]+)/ 11 0 11
109 }
110  
111 # process main PID stats
112 11 ▶ if ( -f $pdir->file('status') and -f $pdir->file('statm') and -f $pdir->file('stat') ) {
Branch: 3 branches
BranchTrueFalseTotal
if (-f $pdir->file('status') and -f $pdir->file('statm') and -f $pdir->file('stat')) { } 11 0 11
elsif ($^O =~ /solaris|sunos/i) { } 0 0 0
elsif ($^O =~ /dragonfly|bsd/i) { } 0 0 0
Condition33%2/6
-f $pdir->file("status") and -f $pdir->file("statm") and -f $pdir->file("stat")
!ll&&!rl&&r
0011
-f $pdir->file("status") and -f $pdir->file("statm")
!ll&&!rl&&r
0011
MC/DC0%0/3
-f $pdir->file("status") and -f $pdir->file("statm") and -f $pdir->file("stat")
-f $pdir->file("status") -f $pdir->file("statm") -f $pdir->file("stat")
Truth table25%1/4
A and B and C
A= -f $pdir->file("status")B= -f $pdir->file("statm")C= -f $pdir->file("stat")
ABCresult
0XX0
10X0
1100
1111
113 ### Linux ###
114 # stat has more needed information than the friendier status, so we'll use that file instead
115  
116 # stat
117 11 my $data = $pdir->file('stat')->slurp;
118 11 my @data = split /\s+/, $data;
119  
120 11 my $states = {
121 R => 'run',
122 S => 'sleep',
123 D => 'disk sleep',
124 Z => 'defunct',
125 T => 'stop',
126 W => 'paging',
127 };
128  
129 11 my $stat_loc = [ qw(
130 pid fname state ppid pgrp sess ttynum . flags minflt cminflt majflt cmajflt utime stime cutime cstime priority . threads .
131 starttime size rss . . . . . . . . . . wchan . . . cpuid . . . . .
132 ) ];
133  
134 11 foreach my $i (0 .. @data - 1) {
135 495 ▶ next if $stat_loc->[$i] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $stat_loc->[$i] eq "." 231 264 495
136 264 ▶ last if ($i >= @$stat_loc);
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @$stat_loc 11 253 264
137 253 $hash->{ $stat_loc->[$i] } = $data[$i];
138 }
139  
140 # normalize clock ticks into seconds
141 11 ▶ if ($clock_ticks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clock_ticks) 11 0 11
142 11 $hash->{$_} /= $clock_ticks for (qw[ utime stime cutime cstime starttime ]);
143 11 ▶ $hash->{start} = $boot_time + $hash->{starttime} if $boot_time;
Branch: 1 branch
BranchTrueFalseTotal
if $boot_time 11 0 11
144 }
145 11 delete $hash->{starttime};
146  
147 11 $hash->{fname} =~ s/^\((.+)\)$/$1/;
148 11 $hash->{state} = $states->{ $hash->{state} };
149 11 $hash->{ time} = $hash->{ utime} + $hash->{ stime};
150 11 $hash->{ctime} = $hash->{cutime} + $hash->{cstime};
151  
152 11 $hash->{ ttlflt} = $hash->{ minflt} + $hash->{ majflt};
153 11 $hash->{cttlflt} = $hash->{cminflt} + $hash->{cmajflt};
154  
155 11 $hash->{rss} *= POSIX::sysconf( &POSIX::_SC_PAGESIZE );
156 }
157 elsif ($^O =~ /solaris|sunos/i) {
158 ### Solaris ###
159 0 ▶ my $ptr = $Config{longsize} >= 8 ? 'Q' : 'I';
Branch: 1 branch
BranchTrueFalseTotal
$P9Y::ProcessTable::Role::Table::ProcFS::Config{'longsize'} >= 8 ? : 0 0 0
160  
161 0 my $data = '';
162 0 eval { $data = $pdir->file('status')->slurp; }; # skip permission failures
163 0 ▶ if (length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 0 0 0
164 0 my @data = unpack 'I[10]'.$ptr.'[4]I[12]CI[4]', $data;
165  
166 # 1 int pr_flags; /* flags (see below) */
167 # 2 int pr_nlwp; /* number of active lwps in the process */
168 # 3 int pr_nzomb; /* number of zombie lwps in the process */
169 # 4 pid_tpr_pid; /* process id */
170 # 5 pid_tpr_ppid; /* parent process id */
171 # 6 pid_tpr_pgid; /* process group id */
172 # 7 pid_tpr_sid; /* session id */
173 # 8 id_t pr_aslwpid; /* obsolete */
174 # 9 id_t pr_agentid; /* lwp-id of the agent lwp, if any */
175 # 10 sigset_t pr_sigpend; /* set of process pending signals */
176 # 11 uintptr_t pr_brkbase; /* virtual address of the process heap */
177 # 12 size_t pr_brksize; /* size of the process heap, in bytes */
178 # 13 uintptr_t pr_stkbase; /* virtual address of the process stack */
179 # 14 size_tpr_stksize; /* size of the process stack, in bytes */
180 #
181 # 15 timestruc_t pr_utime; /* process user cpu time */
182 # 17 timestruc_t pr_stime; /* process system cpu time */
183 # 19 timestruc_t pr_cutime; /* sum of children's user times */
184 # 21 timestruc_t pr_cstime; /* sum of children's system times */
185  
186 # some Solaris versions don't have pr_nzomb
187 0 ▶ if ($data[2] == $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data[2] == $pid) 0 0 0
188 0 @data = unpack 'I[9]'.$ptr.'[4]I[12]CI[4]', $data;
189 0 splice @data, 2, 0, (0);
190 }
191  
192 0 my $stat_loc = [ qw(
193 flags threads . pid ppid pgrp sess . . . . . . . utime . stime . cutime . cstime .
194 ) ];
195  
196 0 foreach my $i (0 .. @data - 1) {
197 0 ▶ next if $stat_loc->[$i] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $stat_loc->[$i] eq "." 0 0 0
198 0 ▶ last if ($i >= @$stat_loc);
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @$stat_loc 0 0 0
199 0 $hash->{ $stat_loc->[$i] } = $data[$i];
200 }
201  
202 0 $hash->{time} = $hash->{utime} + $hash->{stime};
203 0 $hash->{ctime} = $hash->{cutime} + $hash->{stime};
204 }
205  
206 0 $data = '';
207 0 eval { $data = $pdir->file('psinfo')->slurp; }; # skip permission failures
208 0 ▶ if (length $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data) 0 0 0
209 0 my @data = unpack 'I[11]'.$ptr.'[3]IS[2]I[6]A[16]A[80]I', $data;
210  
211 #define PRFNSZ 16 /* Maximum size of execed filename */
212 #define PRARGSZ 80 /* number of chars of arguments */
213  
214 # 1 int pr_flag; /* process flags (DEPRECATED: see below) */
215 # 2 int pr_nlwp; /* number of active lwps in the process */
216 # 3 int pr_nzomb; /* number of zombie lwps in the process */
217 # 4 pid_t pr_pid; /* process id */
218 # 5 pid_t pr_ppid; /* process id of parent */
219 # 6 pid_t pr_pgid; /* process id of process group leader */
220 # 7 pid_t pr_sid; /* session id */
221 # 8 uid_t pr_uid; /* real user id */
222 # 9 uid_t pr_euid; /* effective user id */
223 # 10 gid_t pr_gid; /* real group id */
224 # 11 gid_t pr_egid; /* effective group id */
225 # 12 uintptr_t pr_addr; /* address of process */
226 # 13 size_t pr_size; /* size of process image in Kbytes */
227 # 14 size_t pr_rssize; /* resident set size in Kbytes */
228 # 15 dev_t pr_ttydev; /* controlling tty device (or PRNODEV) */
229 # 16 ushort_t pr_pctcpu; /* % of recent cpu time used by all lwps */
230 # 17 ushort_t pr_pctmem; /* % of system memory used by process */
231 # 18 timestruc_t pr_start; /* process start time, from the epoch */
232 # 20 timestruc_t pr_time; /* cpu time for this process */
233 # 22 timestruc_t pr_ctime; /* cpu time for reaped children */
234 # 23 char pr_fname[PRFNSZ]; /* name of exec'ed file */
235 # 24 char pr_psargs[PRARGSZ]; /* initial characters of arg list */
236 # 25 int pr_wstat; /* if zombie, the wait() status */
237  
238 # some Solaris versions don't have pr_nzomb
239 0 ▶ if ($data[2] == $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data[2] == $pid) 0 0 0
240 0 @data = unpack 'I[10]'.$ptr.'[3]IS[2]I[6]A[16]A[80]I', $data;
241 0 splice @data, 2, 0, (0);
242 }
243  
244 0 my $psinfo_loc = [ qw(
245 . threads . pid ppid pgrp sess uid euid gid egid . size rss ttynum pctcpu pctmem start time ctime fname cmdline .
246 ) ];
247  
248 0 foreach my $i (0 .. @data - 1) {
249 0 ▶ next if $psinfo_loc->[$i] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $psinfo_loc->[$i] eq "." 0 0 0
250 0 ▶ last if ($i >= @$psinfo_loc);
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @$psinfo_loc 0 0 0
251 0 $hash->{ $psinfo_loc->[$i] } = $data[$i];
252 }
253  
254 0 $hash->{size} *= 1024;
255 0 $hash->{rss} *= 1024;
256 }
257 }
258 elsif ($^O =~ /dragonfly|bsd/i) {
259 ### Dragonfly ###
260  
261 # stat
262 0 my $data = $pdir->file('status')->slurp;
263 0 my @data = split /\s+/, $data;
264  
265 0 my $stat_loc = [ qw(
266 fname pid ppid pgrp sess ttynum flags start utime stime state euid
267 ) ];
268  
269 0 foreach my $i (0 .. @data - 1) {
270 0 ▶ next if $stat_loc->[$i] eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $stat_loc->[$i] eq "." 0 0 0
271 0 ▶ last if ($i >= @$stat_loc);
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @$stat_loc 0 0 0
272 0 $hash->{ $stat_loc->[$i] } = $data[$i];
273 }
274  
275 0 $hash->{fname} =~ s/^\((.+)\)$/$1/;
276 0 ($hash->{euid}, $hash->{egid}) = split(/,/, $hash->{euid}, 3);
277 0 $hash->{$_} =~ s!\,!.! for qw[start utime stime];
278  
279 ### TODO: State normalization, like $states in the Linux block ###
280 #$hash->{state} = $states->{ $hash->{state} };
281  
282 0 $hash->{ time} = $hash->{ utime} + $hash->{ stime};
283 }
284  
285 11 return $hash;
286 }
287  
288 3 42;
↑ Summary ProcessTable/Role/Table/OS/MSWin32.pm »