| 1 | package Linux::GetPidstat::Collector; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 22 | ▶ | use 5.008001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 22 | ▶ | use Capture::Tiny qw/capture/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use Parallel::ForkManager; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use Linux::GetPidstat::Collector::Parser; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | my $GETPIDSTAT_DEBUG = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 117 | ▶ | $ENV{GETPIDSTAT_DEBUG} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 22 | }; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 53 | ▶ | my ( $class, %opt ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 53 | bless \%opt, $class; | |||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | sub get_pidstats_results { | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 59 | ▶ | my ($self, $program_pid_mapping) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_pidstats_results: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 59 | my $ret_pidstats; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 59 | my $pm = Parallel::ForkManager->new(scalar @$program_pid_mapping); | |||||||||||||||||||||||||||||||||||||||||
| 25 | $pm->run_on_finish(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 82 | ▶ | if (my $ret = $_[5]) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 82 | my ($program_name, $ret_pidstat, $stdout, $stderr) = @$ret; | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 82 | ▶ | print $stdout, "\n" if $stdout; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 82 | ▶ | warn $stderr if $stderr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 82 | ▶ | if ($program_name && $ret_pidstat) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$program_name and $ret_pidstat
MC/DC50%1/2
$program_name and $ret_pidstat
$program_name
$ret_pidstat
Truth table66%2/3
A and B
A= $program_nameB= $ret_pidstat
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 56 | push @{$ret_pidstats->{$program_name}}, $ret_pidstat; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 56 | return; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 26 | ▶ | warn "Failed to collect metrics\n" if $GETPIDSTAT_DEBUG->(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 59 | }); | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | METHODS: | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 59 | for my $info (@$program_pid_mapping) { | |||||||||||||||||||||||||||||||||||||||||
| 43 | 109 | my $program_name = $info->{program_name}; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 109 | my $pids = join(",", @{$info->{pids}}); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 109 | ▶ | if (my $child_pid = $pm->start) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 91 | ▶ | printf "child_pid=%d, program_name=%s, target_pids=%s\n", | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | $child_pid, $program_name, $pids if $GETPIDSTAT_DEBUG->(); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 91 | next METHODS; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 18 | my $ret_pidstat; | |||||||||||||||||||||||||||||||||||||||||
| 53 | my ($stdout, $stderr) = capture { | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 18 | ▶ | $ret_pidstat = $self->get_pidstat($pids); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 18 | ▶ | unless ($ret_pidstat && %$ret_pidstat) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ret_pidstat and %$ret_pidstat
MC/DC50%1/2
$ret_pidstat and %$ret_pidstat
$ret_pidstat
%$ret_pidstat
Truth table66%2/3
A and B
A= $ret_pidstatB= %$ret_pidstat
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 6 | warn sprintf | |||||||||||||||||||||||||||||||||||||||||
| 57 | "Failed getting pidstat: pid=%d, target_pids=%s, program_name=%s\n", | ||||||||||||||||||||||||||||||||||||||||||
| 58 | $$, $pids, $program_name; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 18 | }; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 18 | $pm->finish(0, [$program_name, $ret_pidstat, $stdout, $stderr]); | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 41 | $pm->wait_all_children; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 41 | return summarize($ret_pidstats); | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | sub get_pidstat { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 18 | ▶ | my ($self, $pids) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_pidstat: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 18 | my $command = _command_get_pidstat($pids, $self->{interval}, $self->{count}); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 18 | ▶ | my ($stdout, $stderr, $exit) = capture { system $command }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 18 | ▶ | if (!$stdout or $stderr or $exit != 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
not $stdout or $stderr or $exit != 0
not $stdout or $stderr
MC/DC33%1/3
not $stdout or $stderr or $exit != 0
not $stdout
$stderr
$exit != 0
Truth table50%2/4
not(A) or B or C
A= not $stdoutB= $stderrC= $exit != 0
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | chomp($stderr); | |||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | warn sprintf | |||||||||||||||||||||||||||||||||||||||||
| 77 | "Failed a command: %s, pid=%d, stdout=%s, stderr=%s, exit=%s\n", | ||||||||||||||||||||||||||||||||||||||||||
| 78 | $command, $$, $stdout, $stderr, $exit; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 16 | my @lines = split '\n', $stdout; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 16 | return parse_pidstat_output(\@lines); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | # for mock in tests | ||||||||||||||||||||||||||||||||||||||||||
| 87 | sub _command_get_pidstat { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my ($pids, $interval, $count) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_command_get_pidstat: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | return "pidstat -h -u -r -s -d -w -p $pids $interval $count"; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | sub summarize($) { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 41 | ▶ | my $ret_pidstats = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
summarize: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 41 | my $summary = {}; | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | # in : backup_mysql => [ { cpu => 21.0... } ... ] ... | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # out: backup_mysql => { cpu => 42.0 } ... } ... | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 41 | ▶ | while (my ($program_name, $rets) = each %$ret_pidstats) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 56 | for my $ret (@{$rets}) { | |||||||||||||||||||||||||||||||||||||||||
| 101 | 56 | ▶ | while (my ($metric_name, $metric) = each %$ret) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 504 | $summary->{$program_name}->{$metric_name} += $metric; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 41 | return $summary; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 22 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||||||||||||||||||||||||