OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_TIMER_H_ | 5 #ifndef VM_TIMER_H_ |
6 #define VM_TIMER_H_ | 6 #define VM_TIMER_H_ |
7 | 7 |
8 #include "vm/allocation.h" | 8 #include "vm/allocation.h" |
9 #include "vm/flags.h" | 9 #include "vm/flags.h" |
10 #include "vm/os.h" | 10 #include "vm/os.h" |
11 | 11 |
12 namespace dart { | 12 namespace dart { |
13 | 13 |
14 // Timer class allows timing of specific operations in the VM. | 14 // Timer class allows timing of specific operations in the VM. |
15 class Timer : public ValueObject { | 15 class Timer : public ValueObject { |
16 public: | 16 public: |
17 Timer(bool enabled, const char* message) | 17 Timer(bool enabled, const char* message) |
18 : start_(0), stop_(0), total_(0), enabled_(enabled), message_(message) {} | 18 : start_(0), stop_(0), total_(0), |
| 19 enabled_(enabled), running_(false), message_(message) {} |
19 ~Timer() {} | 20 ~Timer() {} |
20 | 21 |
21 // Start timer. | 22 // Start timer. |
22 void Start() { | 23 void Start() { |
23 if (enabled_) { | 24 if (enabled_) { |
24 start_ = OS::GetCurrentTimeMicros(); | 25 start_ = OS::GetCurrentTimeMicros(); |
| 26 running_ = true; |
25 } | 27 } |
26 } | 28 } |
27 | 29 |
28 // Stop timer. | 30 // Stop timer. |
29 void Stop() { | 31 void Stop() { |
30 if (enabled_) { | 32 if (enabled_) { |
31 ASSERT(start_ != 0); | 33 ASSERT(start_ != 0); |
| 34 ASSERT(running()); |
32 stop_ = OS::GetCurrentTimeMicros(); | 35 stop_ = OS::GetCurrentTimeMicros(); |
33 total_ += ElapsedMicros(); | 36 total_ += ElapsedMicros(); |
| 37 running_ = false; |
34 } | 38 } |
35 } | 39 } |
36 | 40 |
37 // Get total cummulative elapsed time in micros and reset the counters. | 41 // Get total cummulative elapsed time in micros and reset the counters. |
38 int64_t TotalElapsedTime() { | 42 int64_t TotalElapsedTime() { |
39 if (enabled_) { | 43 if (enabled_) { |
40 int64_t result = total_; | 44 int64_t result = total_; |
41 Reset(); | 45 Reset(); |
42 return result; | 46 return result; |
43 } | 47 } |
44 return 0; | 48 return 0; |
45 } | 49 } |
46 | 50 |
47 // Accessors. | 51 // Accessors. |
48 bool enabled() const { return enabled_; } | 52 bool enabled() const { return enabled_; } |
| 53 bool running() const { return running_; } |
49 const char* message() const { return message_; } | 54 const char* message() const { return message_; } |
50 | 55 |
51 private: | 56 private: |
52 int64_t ElapsedMicros() const { | 57 int64_t ElapsedMicros() const { |
53 if (enabled_) { | 58 if (enabled_) { |
54 ASSERT(start_ != 0); | 59 ASSERT(start_ != 0); |
55 ASSERT(stop_ != 0); | 60 ASSERT(stop_ != 0); |
56 return stop_ - start_; | 61 return stop_ - start_; |
57 } | 62 } |
58 return 0; | 63 return 0; |
59 } | 64 } |
60 | 65 |
61 void Reset() { | 66 void Reset() { |
62 if (enabled_) { | 67 if (enabled_) { |
63 start_ = 0; | 68 start_ = 0; |
64 stop_ = 0; | 69 stop_ = 0; |
65 total_ = 0; | 70 total_ = 0; |
| 71 running_ = false; |
66 } | 72 } |
67 } | 73 } |
68 | 74 |
69 int64_t start_; | 75 int64_t start_; |
70 int64_t stop_; | 76 int64_t stop_; |
71 int64_t total_; | 77 int64_t total_; |
72 bool enabled_; | 78 bool enabled_; |
| 79 bool running_; |
73 const char* message_; | 80 const char* message_; |
74 | 81 |
75 DISALLOW_COPY_AND_ASSIGN(Timer); | 82 DISALLOW_COPY_AND_ASSIGN(Timer); |
76 }; | 83 }; |
77 | 84 |
78 // List of per isolate timers. | 85 // List of per isolate timers. |
79 #define TIMER_LIST(V) \ | 86 #define TIMER_LIST(V) \ |
80 V(time_script_loading, "Script Loading : ") \ | 87 V(time_script_loading, "Script Loading : ") \ |
81 V(time_creating_snapshot, "Snapshot Creation : ") \ | 88 V(time_creating_snapshot, "Snapshot Creation : ") \ |
82 V(time_isolate_initialization, "Isolate initialization : ") \ | 89 V(time_isolate_initialization, "Isolate initialization : ") \ |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // It is used as follows: | 135 // It is used as follows: |
129 // { | 136 // { |
130 // TIMERSCOPE(name_of_timer); | 137 // TIMERSCOPE(name_of_timer); |
131 // .... | 138 // .... |
132 // ..... | 139 // ..... |
133 // code that needs to be timed. | 140 // code that needs to be timed. |
134 // .... | 141 // .... |
135 // } | 142 // } |
136 class TimerScope : public ValueObject { | 143 class TimerScope : public ValueObject { |
137 public: | 144 public: |
138 TimerScope(bool flag, Timer* timer) : flag_(flag), timer_(timer) { | 145 TimerScope(bool flag, Timer* timer) |
| 146 : flag_(flag), nested_(false), timer_(timer) { |
139 if (flag_) { | 147 if (flag_) { |
140 timer_->Start(); | 148 if (!timer_->running()) { |
| 149 timer_->Start(); |
| 150 } else { |
| 151 nested_ = true; |
| 152 } |
141 } | 153 } |
142 } | 154 } |
143 ~TimerScope() { | 155 ~TimerScope() { |
144 if (flag_) { | 156 if (flag_) { |
145 timer_->Stop(); | 157 if (!nested_) { |
| 158 timer_->Stop(); |
| 159 } |
146 } | 160 } |
147 } | 161 } |
148 | 162 |
149 private: | 163 private: |
150 bool flag_; | 164 bool flag_; |
| 165 bool nested_; |
151 Timer* timer_; | 166 Timer* timer_; |
152 DISALLOW_COPY_AND_ASSIGN(TimerScope); | 167 DISALLOW_COPY_AND_ASSIGN(TimerScope); |
153 }; | 168 }; |
154 | 169 |
155 #define TIMERSCOPE(name) \ | 170 #define TIMERSCOPE(name) \ |
156 TimerScope vm_internal_timer_((FLAG_##name || FLAG_time_all), \ | 171 TimerScope vm_internal_timer_((FLAG_##name || FLAG_time_all), \ |
157 &(Isolate::Current()->timer_list().name())) | 172 &(Isolate::Current()->timer_list().name())) |
158 | 173 |
159 } // namespace dart | 174 } // namespace dart |
160 | 175 |
161 #endif // VM_TIMER_H_ | 176 #endif // VM_TIMER_H_ |
OLD | NEW |