summaryrefslogtreecommitdiff
path: root/chromium/v8/src/logging/counters-scopes.h
blob: 039ce21b2fd3013654e23bdd8fa146edc1d76064 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2021 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.

#ifndef V8_LOGGING_COUNTERS_SCOPES_H_
#define V8_LOGGING_COUNTERS_SCOPES_H_

#include "src/execution/isolate.h"
#include "src/logging/counters.h"
#include "src/logging/log.h"

namespace v8 {
namespace internal {

class BaseTimedHistogramScope {
 protected:
  explicit BaseTimedHistogramScope(TimedHistogram* histogram)
      : histogram_(histogram) {}

  void StartInternal() {
    DCHECK(histogram_->ToggleRunningState(true));
    timer_.Start();
  }

  void StopInternal() {
    DCHECK(histogram_->ToggleRunningState(false));
    histogram_->AddTimedSample(timer_.Elapsed());
    timer_.Stop();
  }

  V8_INLINE void Start() {
    if (histogram_->Enabled()) StartInternal();
  }

  V8_INLINE void Stop() {
    if (histogram_->Enabled()) StopInternal();
  }

  V8_INLINE void LogStart(Isolate* isolate) {
    V8FileLogger::CallEventLogger(isolate, histogram_->name(),
                                  v8::LogEventStatus::kStart, true);
  }

  V8_INLINE void LogEnd(Isolate* isolate) {
    V8FileLogger::CallEventLogger(isolate, histogram_->name(),
                                  v8::LogEventStatus::kEnd, true);
  }

  base::ElapsedTimer timer_;
  TimedHistogram* histogram_;
};

// Helper class for scoping a TimedHistogram.
class V8_NODISCARD TimedHistogramScope : public BaseTimedHistogramScope {
 public:
  explicit TimedHistogramScope(TimedHistogram* histogram,
                               Isolate* isolate = nullptr)
      : BaseTimedHistogramScope(histogram), isolate_(isolate) {
    Start();
    if (isolate_) LogStart(isolate_);
  }

  ~TimedHistogramScope() {
    Stop();
    if (isolate_) LogEnd(isolate_);
  }

 private:
  Isolate* const isolate_;

  DISALLOW_IMPLICIT_CONSTRUCTORS(TimedHistogramScope);
};

enum class OptionalTimedHistogramScopeMode { TAKE_TIME, DONT_TAKE_TIME };

// Helper class for scoping a TimedHistogram.
// It will not take time for mode = DONT_TAKE_TIME.
class V8_NODISCARD OptionalTimedHistogramScope
    : public BaseTimedHistogramScope {
 public:
  OptionalTimedHistogramScope(TimedHistogram* histogram, Isolate* isolate,
                              OptionalTimedHistogramScopeMode mode)
      : BaseTimedHistogramScope(histogram), isolate_(isolate), mode_(mode) {
    if (mode != OptionalTimedHistogramScopeMode::TAKE_TIME) return;
    Start();
    LogStart(isolate_);
  }

  ~OptionalTimedHistogramScope() {
    if (mode_ != OptionalTimedHistogramScopeMode::TAKE_TIME) return;
    Stop();
    LogEnd(isolate_);
  }

 private:
  Isolate* const isolate_;
  const OptionalTimedHistogramScopeMode mode_;
  DISALLOW_IMPLICIT_CONSTRUCTORS(OptionalTimedHistogramScope);
};

// Helper class for scoping a TimedHistogram, where the histogram is selected at
// stop time rather than start time.
class V8_NODISCARD LazyTimedHistogramScope : public BaseTimedHistogramScope {
 public:
  LazyTimedHistogramScope() : BaseTimedHistogramScope(nullptr) {
    timer_.Start();
  }
  ~LazyTimedHistogramScope() {
    // We should set the histogram before this scope exits.
    Stop();
  }

  void set_histogram(TimedHistogram* histogram) {
    DCHECK_IMPLIES(histogram->Enabled(), histogram->ToggleRunningState(true));
    histogram_ = histogram;
  }
};

// Helper class for scoping a NestedHistogramTimer.
class V8_NODISCARD NestedTimedHistogramScope : public BaseTimedHistogramScope {
 public:
  explicit NestedTimedHistogramScope(NestedTimedHistogram* histogram,
                                     Isolate* isolate = nullptr)
      : BaseTimedHistogramScope(histogram), isolate_(isolate) {
    Start();
  }
  ~NestedTimedHistogramScope() { Stop(); }

 private:
  friend NestedTimedHistogram;
  friend PauseNestedTimedHistogramScope;

  void StartInteral() {
    previous_scope_ = timed_histogram()->Enter(this);
    base::TimeTicks now = base::TimeTicks::Now();
    if (previous_scope_) previous_scope_->Pause(now);
    timer_.Start(now);
  }

  void StopInternal() {
    timed_histogram()->Leave(previous_scope_);
    base::TimeTicks now = base::TimeTicks::Now();
    base::TimeDelta elapsed = timer_.Elapsed(now);
    histogram_->AddTimedSample(elapsed);
    if (isolate_) RecordLongTaskTime(elapsed);
#ifdef DEBUG
    // StopInternal() is called in the destructor and don't access timer_
    // after that.
    timer_.Stop();
#endif
    if (previous_scope_) previous_scope_->Resume(now);
  }

  V8_INLINE void Start() {
    if (histogram_->Enabled()) StartInteral();
    LogStart(timed_histogram()->counters()->isolate());
  }

  V8_INLINE void Stop() {
    if (histogram_->Enabled()) StopInternal();
    LogEnd(timed_histogram()->counters()->isolate());
  }

  void Pause(base::TimeTicks now) {
    DCHECK(histogram_->Enabled());
    timer_.Pause(now);
  }

  void Resume(base::TimeTicks now) {
    DCHECK(histogram_->Enabled());
    timer_.Resume(now);
  }

  void RecordLongTaskTime(base::TimeDelta elapsed) const {
    if (histogram_ == isolate_->counters()->execute()) {
      isolate_->GetCurrentLongTaskStats()->v8_execute_us +=
          elapsed.InMicroseconds();
    }
  }

  NestedTimedHistogram* timed_histogram() {
    return static_cast<NestedTimedHistogram*>(histogram_);
  }

  NestedTimedHistogramScope* previous_scope_;
  Isolate* isolate_;
};

// Temporarily pause a NestedTimedHistogram when for instance leaving V8 for
// external callbacks.
class V8_NODISCARD PauseNestedTimedHistogramScope {
 public:
  explicit PauseNestedTimedHistogramScope(NestedTimedHistogram* histogram)
      : histogram_(histogram) {
    previous_scope_ = histogram_->Enter(nullptr);
    if (isEnabled()) {
      previous_scope_->Pause(base::TimeTicks::Now());
    }
  }
  ~PauseNestedTimedHistogramScope() {
    histogram_->Leave(previous_scope_);
    if (isEnabled()) {
      previous_scope_->Resume(base::TimeTicks::Now());
    }
  }

 private:
  bool isEnabled() const { return previous_scope_ && histogram_->Enabled(); }
  NestedTimedHistogram* histogram_;
  NestedTimedHistogramScope* previous_scope_;
};

}  // namespace internal
}  // namespace v8

#endif  // V8_LOGGING_COUNTERS_SCOPES_H_