blib/lib/Devel/StatProfiler/Aggregate.pm

filter: Statementstmt 65.7% 73 / 111 Branchbran 37.5% 9 / 24 Conditioncond 40.9% 9 / 22 MC/DCmcdc 30.7% 4 / 13 Subroutinesub 68.7% 11 / 16 Podpod 0.0% 0 / 8 totaltotal 54.6% 106 / 194 CC 27 SCAR 28.6
CC
27
Cov
58%
discard_expired_process_data
48.1
merged_report_metadata
12.0
merged_report
8.1
CRAP 17.4
« blib/lib/Devel/StatProfiler.pm ↑ Summary blib/lib/Devel/StatProfiler/EvalSource.pm »
1 package Devel::StatProfiler::Aggregate;
2 # ABSTRACT: profile aggregation result
3  
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Devel::StatProfiler::Report;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Devel::StatProfiler::Metadata;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Devel::StatProfiler::Utils qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 check_serializer
11 state_dir
12 write_data
13 15 );
14  
15 15 ▶use File::Glob qw(bsd_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 1 ▶sub shards {
Subroutine shards: called CC=1 SCAR=0.0
Pod undocumented
19 1 my ($class, $root_dir) = @_;
20 1 my $state_dir = state_dir({root_dir => $root_dir});
21 1 my @files = bsd_glob File::Spec::Functions::catfile($state_dir, 'shard.*');
22  
23 1 return map m{[/\\]shard\.([^/\\]+)$}, @files;
24 }
25  
26 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
27 5 my ($class, %opts) = @_;
28 my $self = bless {
29 root_dir => $opts{root_directory},
30 shard => $opts{shard},
31 shards => $opts{shards} || [$opts{shard}],
32 flamegraph => $opts{flamegraph},
33 serializer => $opts{serializer} || 'storable',
34 source => undef,
35 sourcemap => undef,
36 metadata => undef,
37 genealogy => undef,
38 last_sample => undef,
39 fetchers => $opts{fetchers},
40 now => time,
41 timebox => $opts{timebox},
42 5 ▶ }, $class;
Condition100%4/4
$opts{'shards'} || [$opts{'shard'}]
l!l
32
$opts{'serializer'} || 'storable'
l!l
14
MC/DC100%1/1
$opts{'shards'} || [$opts{'shard'}]
$opts{'shards'}
MC/DC100%1/1
$opts{'serializer'} || 'storable'
$opts{'serializer'}
Truth table100%2/2
A
A= $opts{'shards'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'serializer'}
Aresult
11
00
43  
44 5 check_serializer($self->{serializer});
45  
46 5 return $self;
47 }
48  
49 4 ▶sub merged_report {
Subroutine merged_report: called CC=8 SCAR=20.9
Pod undocumented
50 4 my ($self, $report_id, $map_source) = @_;
51  
52 4 $self->_load_all_metadata;
53  
54 4 my $res = $self->_fresh_report(mixed_process => 1);
55  
56 # TODO fix this incestuous relation
57 4 $res->{source} = $self->{source};
58 4 $res->{sourcemap} = $self->{sourcemap};
59 4 $res->{genealogy} = $self->{genealogy};
60  
61 4 my $first = 1;
62 4 for my $shard (@{$self->{shards}}) {
63 4 my $data_glob = File::Spec::Functions::catfile($self->{root_dir}, $report_id, "report.*.$shard");
64 4 my $metadata = File::Spec::Functions::catfile($self->{root_dir}, $report_id, "metadata.$shard");
65  
66 4 for my $data (bsd_glob $data_glob) {
67 6 ▶ my $report = $first ? $res : $self->_fresh_report;
Branch: 1 branch
BranchTrueFalseTotal
$first ? : 4 2 6
68  
69 6 $report->load($data);
70 6 ▶ $res->merge($report) if !$first && $report->{tick}; # TODO add accessor
Branch: 1 branch
BranchTrueFalseTotal
if not $first and $report->{"tick"} 2 4 6
Condition66%2/3
not $first and $report->{"tick"}
!ll&&!rl&&r
402
MC/DC50%1/2
not $first and $report->{"tick"}
not $first $report->{"tick"}
Truth table66%2/3
not(A) and B
A= not $firstB= $report->{"tick"}
ABresult
0X0
100
111
71 6 $first = 0;
72 }
73 4 ▶ if (-f $metadata) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $metadata) 0 4 4
74 0 $res->load_and_merge_metadata($metadata);
75 }
76 }
77  
78 4 $res->add_metadata($self->global_metadata);
79 4 ▶ $res->map_source if $map_source;
Branch: 1 branch
BranchTrueFalseTotal
if $map_source 0 4 4
80  
81 4 return $res;
82 }
83  
84 0 ▶sub merged_report_metadata {
Subroutine merged_report_metadata: not called CC=3 SCAR=24.8
Pod undocumented
85 0 my ($self, $report_id) = @_;
86  
87 0 $self->_load_metadata;
88  
89 my $res = Devel::StatProfiler::Metadata->new(
90 serializer => $self->{serializer},
91 root_directory => $self->{root_dir},
92 shard => $self->{shard},
93 0 );
94  
95 0 for my $shard (@{$self->{shards}}) {
96 0 my $metadata = File::Spec::Functions::catfile($self->{root_dir}, $report_id, "metadata.$shard");
97  
98 0 ▶ if (-f $metadata) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $metadata) 0 0 0
99 0 $res->load_and_merge($metadata);
100 }
101 }
102  
103 0 $res->add_entries($self->global_metadata);
104  
105 0 return $res;
106 }
107  
108 6 ▶sub _fresh_report {
Subroutine _fresh_report: called CC=3 SCAR=11.7
109 6 my ($self, %opts) = @_;
110  
111 return Devel::StatProfiler::Report->new(
112 flamegraph => $self->{flamegraph},
113 serializer => $self->{serializer},
114 sources => 0,
115 root_directory => $self->{root_dir},
116 shard => $self->{shard},
117 mixed_process => $opts{mixed_process} // $self->{mixed_process},
118 fetchers => $self->{fetchers},
119 6 ▶ suffix => $opts{suffix} // '',
Condition60%3/5
$opts{"mixed_process"} // $self->{"mixed_process"}
l!l&&r!l&&!r
402
$opts{"suffix"} // ""
l!l
06
MC/DC50%1/2
$opts{"mixed_process"} // $self->{"mixed_process"}
$opts{"mixed_process"} $self->{"mixed_process"}
MC/DC0%0/1
$opts{"suffix"} // ""
$opts{"suffix"}
Truth table66%2/3
A // B
A= $opts{"mixed_process"}B= $self->{"mixed_process"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $opts{"suffix"}
Aresult
11
00
120 );
121 }
122  
123 0 ▶sub _all_reports {
Subroutine _all_reports: not called CC=2 SCAR=17.9
124 0 my ($self, @dirs) = @_;
125 my @reports = grep {
126 0 ▶ $_ eq '__main__' || $_ !~ /^__/
Condition0%0/3
$_ eq '__main__' || !($_ =~ /^__/)
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ eq '__main__' || !($_ =~ /^__/)
$_ eq '__main__' !($_ =~ /^__/)
Truth table0%0/3
A || not(B)
A= $_ eq '__main__'B= !($_ =~ /^__/)
ABresult
1X1
011
000
127 } map File::Basename::basename($_),
128 grep -d $_,
129 map bsd_glob($_ . '/*'),
130 @dirs;
131 0 my %uniq; @uniq{@reports} = ();
132  
133 0 return keys %uniq;
134 }
135  
136 0 ▶sub all_reports { my ($self) = @_; return $self->_all_reports($self->{root_dir}) }
Subroutine all_reports: not called CC=1 SCAR=6.9
Pod undocumented
137 0 ▶sub all_unmerged_reports { my ($self) = @_; return $self->_all_reports($self->{parts_dir}) }
Subroutine all_unmerged_reports: not called CC=1 SCAR=6.9
Pod undocumented
138  
139 1 ▶sub discard_expired_process_data {
Subroutine discard_expired_process_data: called CC=12 SCAR=38.7
Pod undocumented
140 1 my ($self, $expiration) = @_;
141  
142 1 $self->_load_all_metadata;
143  
144 1 my @shards = __PACKAGE__->shards($self->{root_dir});
145 my $aggregator = __PACKAGE__->new(
146 root_directory => $self->{root_dir},
147 shards => \@shards,
148 serializer => $self->{serializer},
149 1 );
150  
151 1 $aggregator->_load_all_metadata;
152  
153 1 my $last_sample = $aggregator->{last_sample};
154 1 my $genealogy = $aggregator->{genealogy};
155  
156 1 my @queue = keys %{$self->{last_sample}};
157 1 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 0 1 1
158 0 my %updated;
159  
160 0 for my $process_id (@queue) {
161 0 my $parent = $genealogy->{$process_id}{1};
162 0 my $parent_id = $parent->[0];
163  
164 0 ▶ unless ($parent_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_id) 0 0 0
165 # warn "Broken genealogy: '$process_id' has no parent";
166 0 next;
167 }
168 0 ▶ next if $parent_id eq "00" x 24;
Branch: 1 branch
BranchTrueFalseTotal
if $parent_id eq "000000000000000000000000000000000000000000000000" 0 0 0
169 0 $updated{$parent_id} = undef;
170 $last_sample->{$parent_id} = $last_sample->{$process_id}
171 0 ▶ if ($last_sample->{$parent_id} // 0) < $last_sample->{$process_id};
Branch: 1 branch
BranchTrueFalseTotal
if ($last_sample->{$parent_id} // 0) < $last_sample->{$process_id} 0 0 0
Condition0%0/2
$last_sample->{$parent_id} // 0
l!l
00
MC/DC0%0/1
$last_sample->{$parent_id} // 0
$last_sample->{$parent_id}
Truth table0%0/2
A
A= $last_sample->{$parent_id}
Aresult
11
00
172 }
173  
174 0 @queue = keys %updated;
175 }
176  
177 1 for my $process_id (keys %$genealogy) {
178 0 ▶ next if ($last_sample->{$process_id} // 0) > $expiration;
Branch: 1 branch
BranchTrueFalseTotal
if ($last_sample->{$process_id} // 0) > $expiration 0 0 0
Condition0%0/2
$last_sample->{$process_id} // 0
l!l
00
MC/DC0%0/1
$last_sample->{$process_id} // 0
$last_sample->{$process_id}
Truth table0%0/2
A
A= $last_sample->{$process_id}
Aresult
11
00
179  
180 # garbage-collect process-related metadata, under the
181 # assumption that if that neither the process nor its childs
182 # produced any samples in the given timeframe, they are "dead"
183 0 delete $self->{genealogy}{$process_id};
184 0 delete $self->{last_sample}{$process_id};
185 0 $self->{source}->delete_process($process_id);
186 }
187  
188 # TODO Garbage-collect eval source code
189  
190 write_data($self, state_dir($self), 'genealogy', $self->{genealogy})
191 1 ▶ if $self->{genealogy};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"genealogy"} 0 1 1
192 write_data($self, state_dir($self), 'last_sample', $self->{last_sample})
193 1 ▶ if $self->{last_sample};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"last_sample"} 1 0 1
194 1 $self->{source}->save_merged;
195 }
196  
197 0 ▶sub report_names {
Subroutine report_names: not called CC=2 SCAR=17.9
Pod undocumented
198 0 my ($self) = @_;
199 my @dirs = grep $_ ne '__state__' && $_ ne '__source__',
200 map File::Basename::basename($_),
201 grep -d $_,
202 0 ▶ bsd_glob File::Spec::Functions::catfile($self->{root_dir}, '*');
Condition0%0/3
$_ ne '__state__' && $_ ne '__source__'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '__state__' && $_ ne '__source__'
$_ ne '__state__' $_ ne '__source__'
Truth table0%0/3
A && B
A= $_ ne '__state__'B= $_ ne '__source__'
ABresult
0X0
100
111
203  
204 0 return \@dirs;
205 }
206  
207 # temporary during refactoring
208 15 *_load_all_metadata = \&Devel::StatProfiler::Aggregator::_load_all_metadata;
209 15 *_load_metadata = \&Devel::StatProfiler::Aggregator::_load_metadata;
210 15 *_load_genealogy = \&Devel::StatProfiler::Aggregator::_load_genealogy;
211 15 *_load_last_sample = \&Devel::StatProfiler::Aggregator::_load_last_sample;
212 15 *_load_source = \&Devel::StatProfiler::Aggregator::_load_source;
213 15 *_load_sourcemap = \&Devel::StatProfiler::Aggregator::_load_sourcemap;
214 15 *_merge_genealogy = \&Devel::StatProfiler::Aggregator::_merge_genealogy;
215 15 *_merge_last_sample = \&Devel::StatProfiler::Aggregator::_merge_last_sample;
216 15 *_clone_genealogy = \&Devel::StatProfiler::Aggregator::_clone_genealogy;
217  
218 15 *global_metadata = \&Devel::StatProfiler::Aggregator::global_metadata;
219  
220 15 1;
221  
222 __END__
« blib/lib/Devel/StatProfiler.pm ↑ Summary blib/lib/Devel/StatProfiler/EvalSource.pm »