v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
counters-scopes.h
Go to the documentation of this file.
1// Copyright 2021 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#ifndef V8_LOGGING_COUNTERS_SCOPES_H_
6#define V8_LOGGING_COUNTERS_SCOPES_H_
7
10#include "src/logging/log.h"
11
12namespace v8 {
13namespace internal {
14
16 protected:
18 : histogram_(histogram) {}
19
21 DCHECK(histogram_->ToggleRunningState(true));
22 timer_.Start();
23 }
24
26 DCHECK(histogram_->ToggleRunningState(false));
27 base::TimeDelta elapsed = timer_.Elapsed();
28 histogram_->AddTimedSample(elapsed);
29 timer_.Stop();
30 return elapsed;
31 }
32
35 }
36
37 // Stops the timer, records the elapsed time in the histogram, and also
38 // returns the elapsed time if the histogram was enabled. Otherwise, returns
39 // a time of -1 microsecond. This behavior should match kTimeNotMeasured in
40 // v8-script.h.
45
50
55
58};
59
60// Helper class for scoping a TimedHistogram.
62 public:
64 Isolate* isolate = nullptr,
65 int64_t* result_in_microseconds = nullptr)
66 : BaseTimedHistogramScope(histogram),
67 isolate_(isolate),
68 result_in_microseconds_(result_in_microseconds) {
69 Start();
70 if (isolate_) LogStart(isolate_);
71 }
72
74 int64_t elapsed = Stop().InMicroseconds();
75 if (isolate_) LogEnd(isolate_);
76 if (result_in_microseconds_) {
77 *result_in_microseconds_ = elapsed;
78 }
79 }
80
81 private:
84
86};
87
89
90// Helper class for scoping a TimedHistogram.
91// It will not take time for mode = DONT_TAKE_TIME.
94 public:
97 : BaseTimedHistogramScope(histogram), isolate_(isolate), mode_(mode) {
98 if (mode != OptionalTimedHistogramScopeMode::TAKE_TIME) return;
99 Start();
100 LogStart(isolate_);
101 }
102
104 if (mode_ != OptionalTimedHistogramScopeMode::TAKE_TIME) return;
105 Stop();
106 LogEnd(isolate_);
107 }
108
109 private:
113};
114
115// Helper class for scoping a TimedHistogram, where the histogram is selected at
116// stop time rather than start time.
118 public:
119 explicit LazyTimedHistogramScope(int64_t* result_in_microseconds)
120 : BaseTimedHistogramScope(nullptr),
121 result_in_microseconds_(result_in_microseconds) {
122 timer_.Start();
123 }
125 // We should set the histogram before this scope exits.
126 int64_t elapsed = Stop().InMicroseconds();
127 if (result_in_microseconds_) {
128 *result_in_microseconds_ = elapsed;
129 }
130 }
131
132 void set_histogram(TimedHistogram* histogram) {
133 DCHECK_IMPLIES(histogram->Enabled(), histogram->ToggleRunningState(true));
134 histogram_ = histogram;
135 }
136
137 private:
139};
140
141// Helper class for scoping a NestedHistogramTimer.
143 public:
145 Isolate* isolate = nullptr)
146 : BaseTimedHistogramScope(histogram), isolate_(isolate) {
147 Start();
148 }
150
151 private:
154
156 previous_scope_ = timed_histogram()->Enter(this);
157 base::TimeTicks now = base::TimeTicks::Now();
158 if (previous_scope_) previous_scope_->Pause(now);
159 timer_.Start(now);
160 }
161
163 timed_histogram()->Leave(previous_scope_);
164 base::TimeTicks now = base::TimeTicks::Now();
165 base::TimeDelta elapsed = timer_.Elapsed(now);
166 histogram_->AddTimedSample(elapsed);
167 if (isolate_) RecordLongTaskTime(elapsed);
168#ifdef DEBUG
169 // StopInternal() is called in the destructor and don't access timer_
170 // after that.
171 timer_.Stop();
172#endif
173 if (previous_scope_) previous_scope_->Resume(now);
174 }
175
177 if (histogram_->Enabled()) StartInteral();
178 LogStart(timed_histogram()->counters()->isolate());
179 }
180
182 if (histogram_->Enabled()) StopInternal();
183 LogEnd(timed_histogram()->counters()->isolate());
184 }
185
187 DCHECK(histogram_->Enabled());
188 timer_.Pause(now);
189 }
190
192 DCHECK(histogram_->Enabled());
193 timer_.Resume(now);
194 }
195
197 if (histogram_ == isolate_->counters()->execute()) {
198 isolate_->GetCurrentLongTaskStats()->v8_execute_us +=
199 elapsed.InMicroseconds();
200 }
201 }
202
204 return static_cast<NestedTimedHistogram*>(histogram_);
205 }
206
209};
210
211// Temporarily pause a NestedTimedHistogram when for instance leaving V8 for
212// external callbacks.
214 public:
216 : histogram_(histogram) {
217 previous_scope_ = histogram_->Enter(nullptr);
218 if (isEnabled()) {
219 previous_scope_->Pause(base::TimeTicks::Now());
220 }
221 }
223 histogram_->Leave(previous_scope_);
224 if (isEnabled()) {
225 previous_scope_->Resume(base::TimeTicks::Now());
226 }
227 }
228
229 private:
230 bool isEnabled() const { return previous_scope_ && histogram_->Enabled(); }
233};
234
235} // namespace internal
236} // namespace v8
237
238#endif // V8_LOGGING_COUNTERS_SCOPES_H_
Isolate * isolate_
TimeDelta Elapsed() const
static constexpr TimeDelta FromMicroseconds(int64_t microseconds)
Definition time.h:87
int64_t InMicroseconds() const
Definition time.cc:251
V8_INLINE void LogStart(Isolate *isolate)
V8_INLINE void LogEnd(Isolate *isolate)
V8_INLINE base::TimeDelta Stop()
BaseTimedHistogramScope(TimedHistogram *histogram)
const char * name() const
Definition counters.h:164
void set_histogram(TimedHistogram *histogram)
LazyTimedHistogramScope(int64_t *result_in_microseconds)
NestedTimedHistogramScope * previous_scope_
NestedTimedHistogramScope(NestedTimedHistogram *histogram, Isolate *isolate=nullptr)
void RecordLongTaskTime(base::TimeDelta elapsed) const
DISALLOW_IMPLICIT_CONSTRUCTORS(OptionalTimedHistogramScope)
const OptionalTimedHistogramScopeMode mode_
OptionalTimedHistogramScope(TimedHistogram *histogram, Isolate *isolate, OptionalTimedHistogramScopeMode mode)
PauseNestedTimedHistogramScope(NestedTimedHistogram *histogram)
DISALLOW_IMPLICIT_CONSTRUCTORS(TimedHistogramScope)
TimedHistogramScope(TimedHistogram *histogram, Isolate *isolate=nullptr, int64_t *result_in_microseconds=nullptr)
V8_EXPORT_PRIVATE void AddTimedSample(base::TimeDelta sample)
Definition counters.cc:66
static V8_INLINE void CallEventLogger(Isolate *isolate, const char *name, v8::LogEventStatus se, bool expose_to_api)
Definition log.h:262
RecordWriteMode const mode_
Isolate * isolate
base::ElapsedTimer timer_
@ kStart
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK(condition)
Definition logging.h:482
#define V8_INLINE
Definition v8config.h:500
#define V8_NODISCARD
Definition v8config.h:693