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