aboutsummaryrefslogtreecommitdiff
path: root/src/compiler-dispatcher/compiler-dispatcher-tracer.cc
blob: d98209b14753d89db5a36c23a93d4c22a0ffea88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler-dispatcher/compiler-dispatcher-tracer.h"

#include "src/isolate.h"
#include "src/utils.h"

namespace v8 {
namespace internal {

namespace {

double MonotonicallyIncreasingTimeInMs() {
  return V8::GetCurrentPlatform()->MonotonicallyIncreasingTime() *
         static_cast<double>(base::Time::kMillisecondsPerSecond);
}

const double kEstimatedRuntimeWithoutData = 1.0;

}  // namespace

CompilerDispatcherTracer::Scope::Scope(CompilerDispatcherTracer* tracer,
                                       ScopeID scope_id, size_t num)
    : tracer_(tracer), scope_id_(scope_id), num_(num) {
  start_time_ = MonotonicallyIncreasingTimeInMs();
}

CompilerDispatcherTracer::Scope::~Scope() {
  double elapsed = MonotonicallyIncreasingTimeInMs() - start_time_;
  switch (scope_id_) {
    case ScopeID::kPrepareToParse:
      tracer_->RecordPrepareToParse(elapsed);
      break;
    case ScopeID::kParse:
      tracer_->RecordParse(elapsed, num_);
      break;
    case ScopeID::kFinalizeParsing:
      tracer_->RecordFinalizeParsing(elapsed);
      break;
    case ScopeID::kAnalyze:
      tracer_->RecordAnalyze(elapsed);
      break;
    case ScopeID::kPrepareToCompile:
      tracer_->RecordPrepareToCompile(elapsed);
      break;
    case ScopeID::kCompile:
      tracer_->RecordCompile(elapsed, num_);
      break;
    case ScopeID::kFinalizeCompiling:
      tracer_->RecordFinalizeCompiling(elapsed);
      break;
  }
}

// static
const char* CompilerDispatcherTracer::Scope::Name(ScopeID scope_id) {
  switch (scope_id) {
    case ScopeID::kPrepareToParse:
      return "V8.BackgroundCompile_PrepareToParse";
    case ScopeID::kParse:
      return "V8.BackgroundCompile_Parse";
    case ScopeID::kFinalizeParsing:
      return "V8.BackgroundCompile_FinalizeParsing";
    case ScopeID::kAnalyze:
      return "V8.BackgroundCompile_Analyze";
    case ScopeID::kPrepareToCompile:
      return "V8.BackgroundCompile_PrepareToCompile";
    case ScopeID::kCompile:
      return "V8.BackgroundCompile_Compile";
    case ScopeID::kFinalizeCompiling:
      return "V8.BackgroundCompile_FinalizeCompiling";
  }
  UNREACHABLE();
  return nullptr;
}

CompilerDispatcherTracer::CompilerDispatcherTracer(Isolate* isolate)
    : runtime_call_stats_(nullptr) {
  // isolate might be nullptr during unittests.
  if (isolate) {
    runtime_call_stats_ = isolate->counters()->runtime_call_stats();
  }
}

CompilerDispatcherTracer::~CompilerDispatcherTracer() {}

void CompilerDispatcherTracer::RecordPrepareToParse(double duration_ms) {
  base::LockGuard<base::Mutex> lock(&mutex_);
  prepare_parse_events_.Push(duration_ms);
}

void CompilerDispatcherTracer::RecordParse(double duration_ms,
                                           size_t source_length) {
  base::LockGuard<base::Mutex> lock(&mutex_);
  parse_events_.Push(std::make_pair(source_length, duration_ms));
}

void CompilerDispatcherTracer::RecordFinalizeParsing(double duration_ms) {
  base::LockGuard<base::Mutex> lock(&mutex_);
  finalize_parsing_events_.Push(duration_ms);
}

void CompilerDispatcherTracer::RecordAnalyze(double duration_ms) {
  base::LockGuard<base::Mutex> lock(&mutex_);
  analyze_events_.Push(duration_ms);
}

void CompilerDispatcherTracer::RecordPrepareToCompile(double duration_ms) {
  base::LockGuard<base::Mutex> lock(&mutex_);
  prepare_compile_events_.Push(duration_ms);
}

void CompilerDispatcherTracer::RecordCompile(double duration_ms,
                                             size_t ast_size_in_bytes) {
  base::LockGuard<base::Mutex> lock(&mutex_);
  compile_events_.Push(std::make_pair(ast_size_in_bytes, duration_ms));
}

void CompilerDispatcherTracer::RecordFinalizeCompiling(double duration_ms) {
  base::LockGuard<base::Mutex> lock(&mutex_);
  finalize_compiling_events_.Push(duration_ms);
}

double CompilerDispatcherTracer::EstimatePrepareToParseInMs() const {
  base::LockGuard<base::Mutex> lock(&mutex_);
  return Average(prepare_parse_events_);
}

double CompilerDispatcherTracer::EstimateParseInMs(size_t source_length) const {
  base::LockGuard<base::Mutex> lock(&mutex_);
  return Estimate(parse_events_, source_length);
}

double CompilerDispatcherTracer::EstimateFinalizeParsingInMs() const {
  base::LockGuard<base::Mutex> lock(&mutex_);
  return Average(finalize_parsing_events_);
}

double CompilerDispatcherTracer::EstimateAnalyzeInMs() const {
  base::LockGuard<base::Mutex> lock(&mutex_);
  return Average(analyze_events_);
}

double CompilerDispatcherTracer::EstimatePrepareToCompileInMs() const {
  base::LockGuard<base::Mutex> lock(&mutex_);
  return Average(prepare_compile_events_);
}

double CompilerDispatcherTracer::EstimateCompileInMs(
    size_t ast_size_in_bytes) const {
  base::LockGuard<base::Mutex> lock(&mutex_);
  return Estimate(compile_events_, ast_size_in_bytes);
}

double CompilerDispatcherTracer::EstimateFinalizeCompilingInMs() const {
  base::LockGuard<base::Mutex> lock(&mutex_);
  return Average(finalize_compiling_events_);
}

void CompilerDispatcherTracer::DumpStatistics() const {
  PrintF(
      "CompilerDispatcherTracer: "
      "prepare_parsing=%.2lfms parsing=%.2lfms/kb finalize_parsing=%.2lfms "
      "analyze=%.2lfms prepare_compiling=%.2lfms compiling=%.2lfms/kb "
      "finalize_compiling=%.2lfms\n",
      EstimatePrepareToParseInMs(), EstimateParseInMs(1 * KB),
      EstimateFinalizeParsingInMs(), EstimateAnalyzeInMs(),
      EstimatePrepareToCompileInMs(), EstimateCompileInMs(1 * KB),
      EstimateFinalizeCompilingInMs());
}

double CompilerDispatcherTracer::Average(
    const base::RingBuffer<double>& buffer) {
  if (buffer.Count() == 0) return 0.0;
  double sum = buffer.Sum([](double a, double b) { return a + b; }, 0.0);
  return sum / buffer.Count();
}

double CompilerDispatcherTracer::Estimate(
    const base::RingBuffer<std::pair<size_t, double>>& buffer, size_t num) {
  if (buffer.Count() == 0) return kEstimatedRuntimeWithoutData;
  std::pair<size_t, double> sum = buffer.Sum(
      [](std::pair<size_t, double> a, std::pair<size_t, double> b) {
        return std::make_pair(a.first + b.first, a.second + b.second);
      },
      std::make_pair(0, 0.0));
  return num * (sum.second / sum.first);
}

}  // namespace internal
}  // namespace v8