Chromium Code Reviews

Side by Side Diff: src/base/platform/elapsed-timer.h

Issue 2511093002: [counters] RuntimeStats: fix wrong bookkeeping when dynamically changing counters. (Closed)
Patch Set: adding subtime_ field on RuntimeCallTimer Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « no previous file | src/counters.h » ('j') | src/counters.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_BASE_PLATFORM_ELAPSED_TIMER_H_ 5 #ifndef V8_BASE_PLATFORM_ELAPSED_TIMER_H_
6 #define V8_BASE_PLATFORM_ELAPSED_TIMER_H_ 6 #define V8_BASE_PLATFORM_ELAPSED_TIMER_H_
7 7
8 #include "src/base/logging.h" 8 #include "src/base/logging.h"
9 #include "src/base/platform/time.h" 9 #include "src/base/platform/time.h"
10 10
11 namespace v8 { 11 namespace v8 {
12 namespace base { 12 namespace base {
13 13
14 class ElapsedTimer final { 14 class ElapsedTimer final {
15 public: 15 public:
16 #ifdef DEBUG 16 #ifdef DEBUG
17 ElapsedTimer() : started_(false) {} 17 ElapsedTimer() : started_(false) {}
18 #endif 18 #endif
19 19
20 // Starts this timer. Once started a timer can be checked with 20 // Starts this timer. Once started a timer can be checked with
21 // |Elapsed()| or |HasExpired()|, and may be restarted using |Restart()|. 21 // |Elapsed()| or |HasExpired()|, and may be restarted using |Restart()|.
22 // This method must not be called on an already started timer. 22 // This method must not be called on an already started timer.
23 void Start() { 23 void Start() { Start(Now()); }
24 void Start(TimeTicks now) {
24 DCHECK(!IsStarted()); 25 DCHECK(!IsStarted());
25 start_ticks_ = Now(); 26 start_ticks_ = now;
26 #ifdef DEBUG 27 #ifdef DEBUG
27 started_ = true; 28 started_ = true;
28 #endif 29 #endif
29 DCHECK(IsStarted()); 30 DCHECK(IsStarted());
30 } 31 }
31 32
32 // Stops this timer. Must not be called on a timer that was not 33 // Stops this timer. Must not be called on a timer that was not
33 // started before. 34 // started before.
34 void Stop() { 35 void Stop() {
35 DCHECK(IsStarted()); 36 DCHECK(IsStarted());
36 start_ticks_ = TimeTicks(); 37 start_ticks_ = TimeTicks();
37 #ifdef DEBUG 38 #ifdef DEBUG
38 started_ = false; 39 started_ = false;
39 #endif 40 #endif
40 DCHECK(!IsStarted()); 41 DCHECK(!IsStarted());
41 } 42 }
42 43
43 // Returns |true| if this timer was started previously. 44 // Returns |true| if this timer was started previously.
44 bool IsStarted() const { 45 bool IsStarted() const {
45 DCHECK(started_ || start_ticks_.IsNull()); 46 DCHECK(started_ || start_ticks_.IsNull());
46 DCHECK(!started_ || !start_ticks_.IsNull()); 47 DCHECK(!started_ || !start_ticks_.IsNull());
47 return !start_ticks_.IsNull(); 48 return !start_ticks_.IsNull();
48 } 49 }
49 50
50 // Restarts the timer and returns the time elapsed since the previous start. 51 // Restarts the timer and returns the time elapsed since the previous start.
51 // This method is equivalent to obtaining the elapsed time with |Elapsed()| 52 // This method is equivalent to obtaining the elapsed time with |Elapsed()|
52 // and then starting the timer again, but does so in one single operation, 53 // and then starting the timer again, but does so in one single operation,
53 // avoiding the need to obtain the clock value twice. It may only be called 54 // avoiding the need to obtain the clock value twice. It may only be called
54 // on a previously started timer. 55 // on a previously started timer.
55 TimeDelta Restart() { 56 TimeDelta Restart() { return Restart(Now()); }
57
58 TimeDelta Restart(TimeTicks now) {
56 DCHECK(IsStarted()); 59 DCHECK(IsStarted());
57 TimeTicks ticks = Now(); 60 TimeDelta elapsed = now - start_ticks_;
58 TimeDelta elapsed = ticks - start_ticks_;
59 DCHECK(elapsed.InMicroseconds() >= 0); 61 DCHECK(elapsed.InMicroseconds() >= 0);
60 start_ticks_ = ticks; 62 start_ticks_ = now;
61 DCHECK(IsStarted()); 63 DCHECK(IsStarted());
62 return elapsed; 64 return elapsed;
63 } 65 }
64 66
65 // Returns the time elapsed since the previous start. This method may only 67 // Returns the time elapsed since the previous start. This method may only
66 // be called on a previously started timer. 68 // be called on a previously started timer.
67 TimeDelta Elapsed() const { 69 TimeDelta Elapsed() const { return Elapsed(Now()); }
70 TimeDelta Elapsed(TimeTicks now) const {
68 DCHECK(IsStarted()); 71 DCHECK(IsStarted());
69 TimeDelta elapsed = Now() - start_ticks_; 72 TimeDelta elapsed = now - start_ticks_;
70 DCHECK(elapsed.InMicroseconds() >= 0); 73 DCHECK(elapsed.InMicroseconds() >= 0);
71 return elapsed; 74 return elapsed;
72 } 75 }
73 76
74 // Returns |true| if the specified |time_delta| has elapsed since the 77 // Returns |true| if the specified |time_delta| has elapsed since the
75 // previous start, or |false| if not. This method may only be called on 78 // previous start, or |false| if not. This method may only be called on
76 // a previously started timer. 79 // a previously started timer.
77 bool HasExpired(TimeDelta time_delta) const { 80 bool HasExpired(TimeDelta time_delta) const {
78 DCHECK(IsStarted()); 81 DCHECK(IsStarted());
79 return Elapsed() >= time_delta; 82 return Elapsed() >= time_delta;
80 } 83 }
81 84
82 private:
83 static V8_INLINE TimeTicks Now() { 85 static V8_INLINE TimeTicks Now() {
84 TimeTicks now = TimeTicks::HighResolutionNow(); 86 TimeTicks now = TimeTicks::HighResolutionNow();
85 DCHECK(!now.IsNull()); 87 DCHECK(!now.IsNull());
86 return now; 88 return now;
87 } 89 }
88 90
91 private:
89 TimeTicks start_ticks_; 92 TimeTicks start_ticks_;
90 #ifdef DEBUG 93 #ifdef DEBUG
91 bool started_; 94 bool started_;
92 #endif 95 #endif
93 }; 96 };
94 97
95 } // namespace base 98 } // namespace base
96 } // namespace v8 99 } // namespace v8
97 100
98 #endif // V8_BASE_PLATFORM_ELAPSED_TIMER_H_ 101 #endif // V8_BASE_PLATFORM_ELAPSED_TIMER_H_
OLDNEW
« no previous file with comments | « no previous file | src/counters.h » ('j') | src/counters.cc » ('J')

Powered by Google App Engine