blib/lib/Linux/GetPidstat/Reader.pm

filter: Statementstmt 96.7% 60 / 62 Branchbran 100% 16 / 16 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 3 totaltotal 92.2% 95 / 103 CC 13 SCAR 15.3
CC
13
Cov
96%
search_child_pids
9.0
get_program_pid_mapping
4.0
_command_search_child_pids
2.0
CRAP 4.6
« blib/lib/Linux/GetPidstat/Collector/Parser.pm ↑ Summary blib/lib/Linux/GetPidstat/Writer.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 53 ▶ my ( $class, %opt ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 53 bless \%opt, $class;
13 }
14  
15 sub get_program_pid_mapping {
16 52 ▶ my $self = shift;
Subroutine get_program_pid_mapping: called CC=4 SCAR=13.9
Pod undocumented
17  
18 52 my $pid_dir = path($self->{pid_dir});
19  
20 52 my @program_pid_mapping;
21 52 for my $pid_file ($pid_dir->children) {
22 104 chomp(my $pid = $pid_file->slurp);
23 104 ▶ unless (_is_valid_pid($pid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_valid_pid($pid)) 26 78 104
24 26 next;
25 }
26  
27 78 my @pids;
28 78 push @pids, $pid;
29  
30 78 ▶ if ($self->{include_child}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"include_child"}) 76 2 78
31 76 my $child_pids = $self->search_child_pids($pid);
32 76 push @pids, @$child_pids;
33 }
34  
35 78 push @program_pid_mapping, {
36 program_name => $pid_file->basename,
37 pids => \@pids,
38 };
39 }
40  
41 52 return \@program_pid_mapping;
42 }
43  
44 sub search_child_pids {
45 76 ▶ my ($self, $pid) = @_;
Subroutine search_child_pids: called CC=9 SCAR=22.0
Pod undocumented
46 76 my $command = _command_search_child_pids($pid);
47 76 ▶ my ($stdout, $stderr, $exit) = capture { system $command };
Subroutine __ANON__: called CC=1 SCAR=0.0
48  
49 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
50 1 chomp ($stderr);
51 1 carp "Failed a command: $command, stdout=$stdout, stderr=$stderr, exit=$exit";
52 }
53 76 ▶ unless (length $stdout) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $stdout) 1 75 76
54 1 return [];
55 }
56  
57 75 my @child_pids;
58  
59 75 my @lines = split '\n', $stdout;
60 75 for (@lines) {
61 1925 ▶ while (/[^}]\((\d+)\)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/[^}]\((\d+)\)/g) 260 1923 2183
62 260 my $child_pid = $1;
63 260 ▶ next if $child_pid == $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $child_pid == $pid 75 185 260
64  
65 # TODO: Remove the limit.
66 ## FIXME: Replace calling pidstat with reading /proc manually
67 185 my $max = $self->{max_child_limit};
68 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
69 2 carp "Stop searching child pids. max_child_limit is too little. pid=$pid";
70 2 last;
71 }
72 183 push @child_pids, $child_pid;
73 }
74 }
75 75 return \@child_pids;
76 }
77  
78 # for mock in tests
79 sub _command_search_child_pids {
80 0 ▶ my $pid = shift;
Subroutine _command_search_child_pids: not called CC=1 SCAR=6.9
81 0 return "pstree -pn $pid";
82 }
83  
84 sub _is_valid_pid {
85 104 ▶ my $pid = shift;
Subroutine _is_valid_pid: called CC=2 SCAR=6.9
86 104 ▶ unless ($pid =~ /^[0-9]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid =~ /^[0-9]+$/) 26 78 104
87 26 carp "invalid pid: $pid";
88 26 return 0;
89 }
90 78 return 1;
91 }
92  
93 14 1;
94 __END__
« blib/lib/Linux/GetPidstat/Collector/Parser.pm ↑ Summary blib/lib/Linux/GetPidstat/Writer.pm »