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/codegen/optimized-compilation-info.h"
8 #include "src/compiler/pipeline-statistics.h"
9 #include "src/compiler/zone-stats.h"
10 #include "src/objects/shared-function-info.h"
11 #include "src/objects/string.h"
12 #include "src/tracing/trace-event.h"
13
14 namespace v8 {
15 namespace internal {
16 namespace compiler {
17
18 namespace {
19
20 // We log detailed phase information about the pipeline
21 // in both the v8.turbofan and the v8.wasm.detailed categories.
22 constexpr const char kTraceCategory[] = // --
23 TRACE_DISABLED_BY_DEFAULT("v8.turbofan") "," // --
24 TRACE_DISABLED_BY_DEFAULT("v8.wasm.detailed");
25
26 } // namespace
27
Begin(PipelineStatistics * pipeline_stats)28 void PipelineStatistics::CommonStats::Begin(
29 PipelineStatistics* pipeline_stats) {
30 DCHECK(!scope_);
31 scope_.reset(new ZoneStats::StatsScope(pipeline_stats->zone_stats_));
32 timer_.Start();
33 outer_zone_initial_size_ = pipeline_stats->OuterZoneSize();
34 allocated_bytes_at_start_ =
35 outer_zone_initial_size_ -
36 pipeline_stats->total_stats_.outer_zone_initial_size_ +
37 pipeline_stats->zone_stats_->GetCurrentAllocatedBytes();
38 }
39
40
End(PipelineStatistics * pipeline_stats,CompilationStatistics::BasicStats * diff)41 void PipelineStatistics::CommonStats::End(
42 PipelineStatistics* pipeline_stats,
43 CompilationStatistics::BasicStats* diff) {
44 DCHECK(scope_);
45 diff->function_name_ = pipeline_stats->function_name_;
46 diff->delta_ = timer_.Elapsed();
47 size_t outer_zone_diff =
48 pipeline_stats->OuterZoneSize() - outer_zone_initial_size_;
49 diff->max_allocated_bytes_ = outer_zone_diff + scope_->GetMaxAllocatedBytes();
50 diff->absolute_max_allocated_bytes_ =
51 diff->max_allocated_bytes_ + allocated_bytes_at_start_;
52 diff->total_allocated_bytes_ =
53 outer_zone_diff + scope_->GetTotalAllocatedBytes();
54 scope_.reset();
55 timer_.Stop();
56 }
57
PipelineStatistics(OptimizedCompilationInfo * info,CompilationStatistics * compilation_stats,ZoneStats * zone_stats)58 PipelineStatistics::PipelineStatistics(OptimizedCompilationInfo* info,
59 CompilationStatistics* compilation_stats,
60 ZoneStats* zone_stats)
61 : outer_zone_(info->zone()),
62 zone_stats_(zone_stats),
63 compilation_stats_(compilation_stats),
64 phase_kind_name_(nullptr),
65 phase_name_(nullptr) {
66 if (info->has_shared_info()) {
67 std::unique_ptr<char[]> name = info->shared_info()->DebugName().ToCString();
68 function_name_ = name.get();
69 }
70 total_stats_.Begin(this);
71 }
72
73
~PipelineStatistics()74 PipelineStatistics::~PipelineStatistics() {
75 if (InPhaseKind()) EndPhaseKind();
76 CompilationStatistics::BasicStats diff;
77 total_stats_.End(this, &diff);
78 compilation_stats_->RecordTotalStats(diff);
79 }
80
81
BeginPhaseKind(const char * phase_kind_name)82 void PipelineStatistics::BeginPhaseKind(const char* phase_kind_name) {
83 DCHECK(!InPhase());
84 if (InPhaseKind()) EndPhaseKind();
85 TRACE_EVENT_BEGIN0(kTraceCategory, phase_kind_name);
86 phase_kind_name_ = phase_kind_name;
87 phase_kind_stats_.Begin(this);
88 }
89
EndPhaseKind()90 void PipelineStatistics::EndPhaseKind() {
91 DCHECK(!InPhase());
92 CompilationStatistics::BasicStats diff;
93 phase_kind_stats_.End(this, &diff);
94 compilation_stats_->RecordPhaseKindStats(phase_kind_name_, diff);
95 TRACE_EVENT_END0(kTraceCategory, phase_kind_name_);
96 }
97
BeginPhase(const char * phase_name)98 void PipelineStatistics::BeginPhase(const char* phase_name) {
99 TRACE_EVENT_BEGIN0(kTraceCategory, phase_name);
100 DCHECK(InPhaseKind());
101 phase_name_ = phase_name;
102 phase_stats_.Begin(this);
103 }
104
EndPhase()105 void PipelineStatistics::EndPhase() {
106 DCHECK(InPhaseKind());
107 CompilationStatistics::BasicStats diff;
108 phase_stats_.End(this, &diff);
109 compilation_stats_->RecordPhaseStats(phase_kind_name_, phase_name_, diff);
110 TRACE_EVENT_END0(kTraceCategory, phase_name_);
111 }
112
113 } // namespace compiler
114 } // namespace internal
115 } // namespace v8
116