v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
elapsed-timer.h
Go to the documentation of this file.
1// Copyright 2013 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_BASE_PLATFORM_ELAPSED_TIMER_H_
6#define V8_BASE_PLATFORM_ELAPSED_TIMER_H_
7
8#include "src/base/logging.h"
10
11namespace v8 {
12namespace base {
13
14class ElapsedTimer final {
15 public:
17
18 // Starts this timer. Once started a timer can be checked with
19 // |Elapsed()| or |HasExpired()|, and may be restarted using |Restart()|.
20 // This method must not be called on an already started timer.
21 void Start() { Start(Now()); }
22
23 void Start(TimeTicks now) {
24 DCHECK(!now.IsNull());
25 DCHECK(!IsStarted());
26 set_start_ticks(now);
27#ifdef DEBUG
28 started_ = true;
29#endif
31 }
32
33 // Stops this timer. Must not be called on a timer that was not
34 // started before.
35 void Stop() {
38#ifdef DEBUG
39 started_ = false;
40#endif
41 DCHECK(!IsStarted());
42 }
43
44 // Returns |true| if this timer was started previously.
45 bool IsStarted() const {
46 DCHECK(!paused_);
47 DCHECK_NE(started_, start_ticks_.IsNull());
48 return !start_ticks_.IsNull();
49 }
50
51#if DEBUG
52 bool IsPaused() const { return paused_; }
53#endif
54
55 // Restarts the timer and returns the time elapsed since the previous start.
56 // This method is equivalent to obtaining the elapsed time with |Elapsed()|
57 // and then starting the timer again, but does so in one single operation,
58 // avoiding the need to obtain the clock value twice. It may only be called
59 // on a previously started timer.
60 TimeDelta Restart() { return Restart(Now()); }
61
63 DCHECK(!now.IsNull());
65 TimeDelta elapsed = now - start_ticks_;
66 DCHECK_GE(elapsed.InMicroseconds(), 0);
67 set_start_ticks(now);
69 return elapsed;
70 }
71
72 void Pause() { Pause(Now()); }
73
74 void Pause(TimeTicks now) {
75 TimeDelta elapsed = Elapsed(now);
77#ifdef DEBUG
78 paused_ = true;
79#endif
80 set_paused_elapsed(elapsed);
81 }
82
83 void Resume() { Resume(Now()); }
84
85 void Resume(TimeTicks now) {
86 DCHECK(!now.IsNull());
87 DCHECK(started_);
88 DCHECK(paused_);
89 TimeDelta elapsed = paused_elapsed();
90#ifdef DEBUG
91 paused_ = false;
92#endif
93 set_start_ticks(now - elapsed);
95 }
96
97 // Returns the time elapsed since the previous start. This method may only
98 // be called on a previously started timer.
99 TimeDelta Elapsed() const { return Elapsed(Now()); }
100
102 DCHECK(!now.IsNull());
103 DCHECK(IsStarted());
104 TimeDelta elapsed = now - start_ticks();
105 DCHECK_GE(elapsed.InMicroseconds(), 0);
106 return elapsed;
107 }
108
109 // Returns |true| if the specified |time_delta| has elapsed since the
110 // previous start, or |false| if not. This method may only be called on
111 // a previously started timer.
112 bool HasExpired(TimeDelta time_delta) const {
113 DCHECK(IsStarted());
114 return Elapsed() >= time_delta;
115 }
116
117 private:
120 DCHECK(!now.IsNull());
121 return now;
122 }
123
125 // Only used started_ since paused_elapsed_ can be 0.
126 DCHECK(paused_);
127 DCHECK(started_);
128 return paused_elapsed_;
129 }
130
132 DCHECK(paused_);
133 DCHECK(started_);
134 paused_elapsed_ = delta;
135 }
136
138 DCHECK(!paused_);
139 return start_ticks_;
140 }
145
146 union {
149 };
150#ifdef DEBUG
151 bool started_ = false;
152 bool paused_ = false;
153#endif
154};
155
156// Helper that times a scoped region and records the elapsed time.
158 explicit ScopedTimer(TimeDelta* location) : location_(location) {
159 if (location_) {
160 timer_.Start();
161 }
162 }
163
165 if (location_) {
167 }
168 }
169
172};
173
174} // namespace base
175} // namespace v8
176
177#endif // V8_BASE_PLATFORM_ELAPSED_TIMER_H_
bool HasExpired(TimeDelta time_delta) const
TimeDelta Elapsed() const
void Resume(TimeTicks now)
TimeTicks start_ticks() const
void set_start_ticks(TimeTicks start_ticks)
void set_paused_elapsed(TimeDelta delta)
TimeDelta Restart(TimeTicks now)
static V8_INLINE TimeTicks Now()
void Start(TimeTicks now)
void Pause(TimeTicks now)
TimeDelta Elapsed(TimeTicks now) const
int64_t InMicroseconds() const
Definition time.cc:251
static TimeTicks Now()
Definition time.cc:736
constexpr bool IsNull() const
Definition time.h:265
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define DCHECK_GE(v1, v2)
Definition logging.h:488
#define DCHECK(condition)
Definition logging.h:482
ScopedTimer(TimeDelta *location)
#define V8_INLINE
Definition v8config.h:500