blib/lib/Linux/GetPidstat/Reader.pm

filter: Statementstmt 96.9% 63 / 65 Branchbran 95.0% 19 / 20 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 3 totaltotal 91.8% 101 / 110 CC 15 SCAR 16.2
CC
15
Cov
96%
search_child_pids
9.0
get_program_pid_mapping
6.0
_command_search_child_pids
2.0
CRAP 5.0
« blib/lib/Linux/GetPidstat.pm ↑ Summary blib/lib/Linux/GetPidstat/Collector/Parser.pm »
1 package Linux::GetPidstat::Reader;
2 14 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Capture::Tiny qw/capture/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use Path::Tiny qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 54 ▶ my ( $class, %opt ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 54 bless \%opt, $class;
13 }
14  
15 sub get_program_pid_mapping {
16 53 ▶ my $self = shift;
Subroutine get_program_pid_mapping: called CC=6 SCAR=17.9
Pod undocumented
17  
18 53 my $pid_dir = path($self->{pid_dir});
19  
20 53 my @program_pid_mapping;
21 53 for my $pid_file ($pid_dir->children) {
22 # Skip processing if there are no more files after directory scanning
23 105 ▶ next unless -e $pid_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $pid_file 0 105 105
24  
25 105 my $pid = $pid_file->slurp;
26 # Skip processing if it could not read anything from the file
27 105 ▶ next unless length($pid);
Branch: 1 branch
BranchTrueFalseTotal
unless length $pid 1 104 105
28  
29 104 chomp($pid);
30 104 ▶ unless (_is_valid_pid($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_valid_pid($pid)) 26 78 104
31 26 next;
32 }
33  
34 78 my @pids;
35 78 push @pids, $pid;
36  
37 78 ▶ if ($self->{include_child}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"include_child"}) 76 2 78
38 76 my $child_pids = $self->search_child_pids($pid);
39 76 push @pids, @$child_pids;
40 }
41  
42 78 push @program_pid_mapping, {
43 program_name => $pid_file->basename,
44 pids => \@pids,
45 };
46 }
47  
48 53 return \@program_pid_mapping;
49 }
50  
51 sub search_child_pids {
52 76 ▶ my ($self, $pid) = @_;
Subroutine search_child_pids: called CC=9 SCAR=22.0
Pod undocumented
53 76 my $command = _command_search_child_pids($pid);
54 76 ▶ my ($stdout, $stderr, $exit) = capture { system $command };
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56 76 ▶ if (length $stderr or $exit != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $stderr or $exit != 0) 1 75 76
Condition66%2/3
length $stderr or $exit != 0
l!l&&r!l&&!r
1075
MC/DC50%1/2
length $stderr or $exit != 0
length $stderr $exit != 0
Truth table66%2/3
A or B
A= length $stderrB= $exit != 0
ABresult
1X1
011
000
57 1 chomp ($stderr);
58 1 carp "Failed a command: $command, stdout=$stdout, stderr=$stderr, exit=$exit";
59 }
60 76 ▶ unless (length $stdout) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $stdout) 1 75 76
61 1 return [];
62 }
63  
64 75 my @child_pids;
65  
66 75 my @lines = split '\n', $stdout;
67 75 for (@lines) {
68 1925 ▶ while (/[^}]\((\d+)\)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/[^}]\((\d+)\)/g) 260 1923 2183
69 260 my $child_pid = $1;
70 260 ▶ next if $child_pid == $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $child_pid == $pid 75 185 260
71  
72 # TODO: Remove the limit.
73 ## FIXME: Replace calling pidstat with reading /proc manually
74 185 my $max = $self->{max_child_limit};
75 185 ▶ if ($max && $max <= scalar @child_pids) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max and $max <= scalar @child_pids) 2 183 185
Condition100%3/3
$max and $max <= scalar @child_pids
!ll&&!rl&&r
18032
MC/DC100%2/2
$max and $max <= scalar @child_pids
$max $max <= scalar @child_pids
Truth table100%3/3
A and B
A= $maxB= $max <= scalar @child_pids
ABresult
0X0
100
111
76 2 carp "Stop searching child pids. max_child_limit is too little. pid=$pid";
77 2 last;
78 }
79 183 push @child_pids, $child_pid;
80 }
81 }
82 75 return \@child_pids;
83 }
84  
85 # for mock in tests
86 sub _command_search_child_pids {
87 0 ▶ my $pid = shift;
Subroutine _command_search_child_pids: not called CC=1 SCAR=6.9
88 0 return "pstree -pn $pid";
89 }
90  
91 sub _is_valid_pid {
92 104 ▶ my $pid = shift;
Subroutine _is_valid_pid: called CC=2 SCAR=6.9
93 104 ▶ unless ($pid =~ /^[0-9]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid =~ /^[0-9]+$/) 26 78 104
94 26 carp "invalid pid: $pid";
95 26 return 0;
96 }
97 78 return 1;
98 }
99  
100 14 1;
101 __END__
« blib/lib/Linux/GetPidstat.pm ↑ Summary blib/lib/Linux/GetPidstat/Collector/Parser.pm »