blib/lib/Linux/GetPidstat/Collector.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 87.5% 21 / 24 Conditioncond 66.6% 8 / 12 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 4 totaltotal 86.3% 114 / 132 CC 19 SCAR 13.0
CC
19
Cov
92%
__ANON__
7.1
get_pidstat
4.1
get_pidstats_results
4.0
CRAP 3.7
« blib/lib/Linux/GetPidstat/Writer.pm ↑ Summary blib/lib/Linux/GetPidstat/Writer/File.pm »
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
BranchTrueFalseTotal
$ENV{'GETPIDSTAT_DEBUG'} ? : 18 99 117
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
BranchTrueFalseTotal
if (my $ret = $_[5]) 82 0 82
27 82 my ($program_name, $ret_pidstat, $stdout, $stderr) = @$ret;
28  
29 82 ▶ print $stdout, "\n" if $stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $stdout 0 82 82
30 82 ▶ warn $stderr if $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr 26 56 82
31  
32 82 ▶ if ($program_name && $ret_pidstat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($program_name and $ret_pidstat) 56 26 82
Condition66%2/3
$program_name and $ret_pidstat
!ll&&!rl&&r
02656
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if &$GETPIDSTAT_DEBUG() 8 18 26
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
BranchTrueFalseTotal
if (my $child_pid = $pm->start) 91 18 109
47 91 ▶ printf "child_pid=%d, program_name=%s, target_pids=%s\n",
Branch: 1 branch
BranchTrueFalseTotal
if &$GETPIDSTAT_DEBUG() 10 81 91
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
BranchTrueFalseTotal
unless ($ret_pidstat and %$ret_pidstat) 6 12 18
Condition66%2/3
$ret_pidstat and %$ret_pidstat
!ll&&!rl&&r
6012
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (not $stdout or $stderr or $exit != 0) 0 16 16
Condition66%4/6
not $stdout or $stderr or $exit != 0
l!l&&r!l&&!r
2016
not $stdout or $stderr
l!l&&r!l&&!r
2016
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
ABCresult
1XX1
01X1
0011
0000
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
BranchTrueFalseTotal
if (() = each %$ret_pidstats) 56 41 97
100 56 for my $ret (@{$rets}) {
101 56 ▶ while (my ($metric_name, $metric) = each %$ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$ret) 504 56 560
102 504 $summary->{$program_name}->{$metric_name} += $metric;
103 }
104 }
105 }
106 41 return $summary;
107 }
108  
109 22 1;
110 __END__
« blib/lib/Linux/GetPidstat/Writer.pm ↑ Summary blib/lib/Linux/GetPidstat/Writer/File.pm »