| 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
| |||||||||||||||||||||||||||||
| 24 | 26 | next; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 78 | my @pids; | |||||||||||||||||||||||||||
| 28 | 78 | push @pids, $pid; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 78 | ▶ | if ($self->{include_child}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
length $stderr or $exit != 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | 260 | my $child_pid = $1; | |||||||||||||||||||||||||||
| 63 | 260 | ▶ | next if $child_pid == $pid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$max and $max <= scalar @child_pids
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 87 | 26 | carp "invalid pid: $pid"; | |||||||||||||||||||||||||||
| 88 | 26 | return 0; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 78 | return 1; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 14 | 1; | |||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||