1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <memory>
6 
7 #include "src/compiler/pipeline-statistics.h"
8 #include "src/compiler/zone-stats.h"
9 #include "src/isolate.h"
10 #include "src/objects/shared-function-info.h"
11 #include "src/objects/string.h"
12 #include "src/optimized-compilation-info.h"
13 
14 namespace v8 {
15 namespace internal {
16 namespace compiler {
17 
Begin(PipelineStatistics * pipeline_stats)18 void PipelineStatistics::CommonStats::Begin(
19     PipelineStatistics* pipeline_stats) {
20   DCHECK(!scope_);
21   scope_.reset(new ZoneStats::StatsScope(pipeline_stats->zone_stats_));
22   timer_.Start();
23   outer_zone_initial_size_ = pipeline_stats->OuterZoneSize();
24   allocated_bytes_at_start_ =
25       outer_zone_initial_size_ -
26       pipeline_stats->total_stats_.outer_zone_initial_size_ +
27       pipeline_stats->zone_stats_->GetCurrentAllocatedBytes();
28 }
29 
30 
End(PipelineStatistics * pipeline_stats,CompilationStatistics::BasicStats * diff)31 void PipelineStatistics::CommonStats::End(
32     PipelineStatistics* pipeline_stats,
33     CompilationStatistics::BasicStats* diff) {
34   DCHECK(scope_);
35   diff->function_name_ = pipeline_stats->function_name_;
36   diff->delta_ = timer_.Elapsed();
37   size_t outer_zone_diff =
38       pipeline_stats->OuterZoneSize() - outer_zone_initial_size_;
39   diff->max_allocated_bytes_ = outer_zone_diff + scope_->GetMaxAllocatedBytes();
40   diff->absolute_max_allocated_bytes_ =
41       diff->max_allocated_bytes_ + allocated_bytes_at_start_;
42   diff->total_allocated_bytes_ =
43       outer_zone_diff + scope_->GetTotalAllocatedBytes();
44   scope_.reset();
45   timer_.Stop();
46 }
47 
PipelineStatistics(OptimizedCompilationInfo * info,Isolate * isolate,ZoneStats * zone_stats)48 PipelineStatistics::PipelineStatistics(OptimizedCompilationInfo* info,
49                                        Isolate* isolate, ZoneStats* zone_stats)
50     : isolate_(isolate),
51       outer_zone_(info->zone()),
52       zone_stats_(zone_stats),
53       compilation_stats_(isolate_->GetTurboStatistics()),
54       source_size_(0),
55       phase_kind_name_(nullptr),
56       phase_name_(nullptr) {
57   if (info->has_shared_info()) {
58     source_size_ = static_cast<size_t>(info->shared_info()->SourceSize());
59     std::unique_ptr<char[]> name =
60         info->shared_info()->DebugName()->ToCString();
61     function_name_ = name.get();
62   }
63   total_stats_.Begin(this);
64 }
65 
66 
~PipelineStatistics()67 PipelineStatistics::~PipelineStatistics() {
68   if (InPhaseKind()) EndPhaseKind();
69   CompilationStatistics::BasicStats diff;
70   total_stats_.End(this, &diff);
71   compilation_stats_->RecordTotalStats(source_size_, diff);
72 }
73 
74 
BeginPhaseKind(const char * phase_kind_name)75 void PipelineStatistics::BeginPhaseKind(const char* phase_kind_name) {
76   DCHECK(!InPhase());
77   if (InPhaseKind()) EndPhaseKind();
78   phase_kind_name_ = phase_kind_name;
79   phase_kind_stats_.Begin(this);
80 }
81 
82 
EndPhaseKind()83 void PipelineStatistics::EndPhaseKind() {
84   DCHECK(!InPhase());
85   CompilationStatistics::BasicStats diff;
86   phase_kind_stats_.End(this, &diff);
87   compilation_stats_->RecordPhaseKindStats(phase_kind_name_, diff);
88 }
89 
90 
BeginPhase(const char * name)91 void PipelineStatistics::BeginPhase(const char* name) {
92   DCHECK(InPhaseKind());
93   phase_name_ = name;
94   phase_stats_.Begin(this);
95 }
96 
97 
EndPhase()98 void PipelineStatistics::EndPhase() {
99   DCHECK(InPhaseKind());
100   CompilationStatistics::BasicStats diff;
101   phase_stats_.End(this, &diff);
102   compilation_stats_->RecordPhaseStats(phase_kind_name_, phase_name_, diff);
103 }
104 
105 }  // namespace compiler
106 }  // namespace internal
107 }  // namespace v8
108