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 class JSONObject; | |
15 | |
14 // Timer class allows timing of specific operations in the VM. | 16 // Timer class allows timing of specific operations in the VM. |
15 class Timer : public ValueObject { | 17 class Timer : public ValueObject { |
16 public: | 18 public: |
17 Timer(bool enabled, const char* message) | 19 Timer(bool report, const char* message) |
18 : start_(0), stop_(0), total_(0), | 20 : start_(0), stop_(0), total_(0), |
19 enabled_(enabled), running_(false), message_(message) {} | 21 report_(report), enabled_(true), running_(false), message_(message) {} |
Cutch
2014/01/28 19:32:51
Enabled is always true and has no setter, so why k
turnidge
2014/01/28 23:18:55
Removed.
| |
20 ~Timer() {} | 22 ~Timer() {} |
21 | 23 |
22 // Start timer. | 24 // Start timer. |
23 void Start() { | 25 void Start() { |
24 if (enabled_) { | 26 if (enabled_) { |
25 start_ = OS::GetCurrentTimeMicros(); | 27 start_ = OS::GetCurrentTimeMicros(); |
26 running_ = true; | 28 running_ = true; |
27 } | 29 } |
28 } | 30 } |
29 | 31 |
30 // Stop timer. | 32 // Stop timer. |
31 void Stop() { | 33 void Stop() { |
32 if (enabled_) { | 34 if (enabled_) { |
33 ASSERT(start_ != 0); | 35 ASSERT(start_ != 0); |
34 ASSERT(running()); | 36 ASSERT(running()); |
35 stop_ = OS::GetCurrentTimeMicros(); | 37 stop_ = OS::GetCurrentTimeMicros(); |
36 total_ += ElapsedMicros(); | 38 total_ += ElapsedMicros(); |
37 running_ = false; | 39 running_ = false; |
38 } | 40 } |
39 } | 41 } |
40 | 42 |
41 // Get total cummulative elapsed time in micros. | 43 // Get total cummulative elapsed time in micros. |
42 int64_t TotalElapsedTime() const { | 44 int64_t TotalElapsedTime() const { |
43 if (enabled_) { | 45 if (enabled_) { |
44 int64_t result = total_; | 46 int64_t result = total_; |
47 if (running_) { | |
48 int64_t now = OS::GetCurrentTimeMicros(); | |
49 result += (now - start_); | |
50 } | |
45 return result; | 51 return result; |
46 } | 52 } |
47 return 0; | 53 return 0; |
48 } | 54 } |
49 | 55 |
50 void Reset() { | 56 void Reset() { |
51 if (enabled_) { | 57 if (enabled_) { |
52 start_ = 0; | 58 start_ = 0; |
53 stop_ = 0; | 59 stop_ = 0; |
54 total_ = 0; | 60 total_ = 0; |
55 running_ = false; | 61 running_ = false; |
56 } | 62 } |
57 } | 63 } |
58 | 64 |
59 // Accessors. | 65 // Accessors. |
66 bool report() const { return report_; } | |
60 bool enabled() const { return enabled_; } | 67 bool enabled() const { return enabled_; } |
61 bool running() const { return running_; } | 68 bool running() const { return running_; } |
62 const char* message() const { return message_; } | 69 const char* message() const { return message_; } |
63 | 70 |
64 private: | 71 private: |
65 int64_t ElapsedMicros() const { | 72 int64_t ElapsedMicros() const { |
66 if (enabled_) { | 73 if (enabled_) { |
67 ASSERT(start_ != 0); | 74 ASSERT(start_ != 0); |
68 ASSERT(stop_ != 0); | 75 ASSERT(stop_ != 0); |
69 return stop_ - start_; | 76 return stop_ - start_; |
70 } | 77 } |
71 return 0; | 78 return 0; |
72 } | 79 } |
73 | 80 |
74 int64_t start_; | 81 int64_t start_; |
75 int64_t stop_; | 82 int64_t stop_; |
76 int64_t total_; | 83 int64_t total_; |
84 bool report_; | |
77 bool enabled_; | 85 bool enabled_; |
78 bool running_; | 86 bool running_; |
79 const char* message_; | 87 const char* message_; |
80 | 88 |
81 DISALLOW_COPY_AND_ASSIGN(Timer); | 89 DISALLOW_COPY_AND_ASSIGN(Timer); |
82 }; | 90 }; |
83 | 91 |
84 // List of per isolate timers. | 92 // List of per isolate timers. |
85 #define TIMER_LIST(V) \ | 93 #define TIMER_LIST(V) \ |
86 V(time_script_loading, "Script Loading : ") \ | 94 V(time_script_loading, "Script Loading") \ |
87 V(time_creating_snapshot, "Snapshot Creation : ") \ | 95 V(time_creating_snapshot, "Snapshot Creation") \ |
88 V(time_isolate_initialization, "Isolate initialization : ") \ | 96 V(time_isolate_initialization, "Isolate initialization") \ |
89 V(time_compilation, "Function compilation : ") \ | 97 V(time_compilation, "Function compilation") \ |
90 V(time_bootstrap, "Bootstrap of core classes : ") \ | 98 V(time_bootstrap, "Bootstrap of core classes") \ |
91 V(time_total_runtime, "Total runtime for isolate : ") \ | 99 V(time_dart_execution, "Dart execution") \ |
100 V(time_total_runtime, "Total runtime for isolate") \ | |
92 | 101 |
93 // Declare command line flags for the timers. | 102 // Declare command line flags for the timers. |
94 #define DECLARE_TIMER_FLAG(name, msg) \ | 103 #define DECLARE_TIMER_FLAG(name, msg) \ |
95 DECLARE_FLAG(bool, name); | 104 DECLARE_FLAG(bool, name); |
96 TIMER_LIST(DECLARE_TIMER_FLAG) | 105 TIMER_LIST(DECLARE_TIMER_FLAG) |
97 #undef DECLARE_TIMER_FLAG | 106 #undef DECLARE_TIMER_FLAG |
98 DECLARE_FLAG(bool, time_all); // In order to turn on all the timer flags. | 107 DECLARE_FLAG(bool, time_all); // In order to turn on all the timer flags. |
99 | 108 |
100 // Maintains a list of timers per isolate. | 109 // Maintains a list of timers per isolate. |
101 class TimerList : public ValueObject { | 110 class TimerList : public ValueObject { |
102 public: | 111 public: |
103 TimerList(); | 112 TimerList(); |
104 ~TimerList() {} | 113 ~TimerList() {} |
105 | 114 |
106 // Accessors. | 115 // Accessors. |
107 #define TIMER_FIELD_ACCESSOR(name, msg) \ | 116 #define TIMER_FIELD_ACCESSOR(name, msg) \ |
108 Timer& name() { return name##_; } | 117 Timer& name() { return name##_; } |
109 TIMER_LIST(TIMER_FIELD_ACCESSOR) | 118 TIMER_LIST(TIMER_FIELD_ACCESSOR) |
110 #undef TIMER_FIELD_ACCESSOR | 119 #undef TIMER_FIELD_ACCESSOR |
111 | 120 |
112 void ReportTimers(); | 121 void ReportTimers(); |
113 | 122 |
123 void PrintTimersToJSONProperty(JSONObject* jsobj); | |
124 | |
114 private: | 125 private: |
115 #define TIMER_FIELD(name, msg) Timer name##_; | 126 #define TIMER_FIELD(name, msg) Timer name##_; |
116 TIMER_LIST(TIMER_FIELD) | 127 TIMER_LIST(TIMER_FIELD) |
117 #undef TIMER_FIELD | 128 #undef TIMER_FIELD |
118 bool padding_; | 129 bool padding_; |
119 DISALLOW_COPY_AND_ASSIGN(TimerList); | 130 DISALLOW_COPY_AND_ASSIGN(TimerList); |
120 }; | 131 }; |
121 | 132 |
122 // Timer Usage. | 133 // Timer Usage. |
123 #define START_TIMER(name) \ | 134 #define START_TIMER(name) \ |
124 if (FLAG_##name || FLAG_time_all) { \ | 135 Isolate::Current()->timer_list().name().Start(); |
125 Isolate::Current()->timer_list().name().Start(); \ | 136 |
126 } | |
127 #define STOP_TIMER(name) \ | 137 #define STOP_TIMER(name) \ |
128 if (FLAG_##name || FLAG_time_all) { \ | 138 Isolate::Current()->timer_list().name().Stop(); |
129 Isolate::Current()->timer_list().name().Stop(); \ | |
130 } | |
131 | 139 |
132 | 140 |
133 // The class TimerScope is used to start and stop a timer within a scope. | 141 // The class TimerScope is used to start and stop a timer within a scope. |
134 // It is used as follows: | 142 // It is used as follows: |
135 // { | 143 // { |
136 // TIMERSCOPE(name_of_timer); | 144 // TIMERSCOPE(name_of_timer); |
137 // .... | 145 // .... |
138 // ..... | 146 // ..... |
139 // code that needs to be timed. | 147 // code that needs to be timed. |
140 // .... | 148 // .... |
141 // } | 149 // } |
142 class TimerScope : public StackResource { | 150 class TimerScope : public StackResource { |
143 public: | 151 public: |
144 TimerScope(bool flag, Timer* timer, BaseIsolate* isolate = NULL) | 152 TimerScope(bool flag, Timer* timer, BaseIsolate* isolate = NULL) |
145 : StackResource(isolate), flag_(flag), nested_(false), timer_(timer) { | 153 : StackResource(isolate), flag_(flag), nested_(false), timer_(timer) { |
146 if (flag_) { | 154 if (flag_ || FLAG_time_all) { |
147 if (!timer_->running()) { | 155 if (!timer_->running()) { |
148 timer_->Start(); | 156 timer_->Start(); |
149 } else { | 157 } else { |
150 nested_ = true; | 158 nested_ = true; |
151 } | 159 } |
152 } | 160 } |
153 } | 161 } |
154 ~TimerScope() { | 162 ~TimerScope() { |
155 if (flag_) { | 163 if (flag_) { |
156 if (!nested_) { | 164 if (!nested_) { |
157 timer_->Stop(); | 165 timer_->Stop(); |
158 } | 166 } |
159 } | 167 } |
160 } | 168 } |
161 | 169 |
162 private: | 170 private: |
163 bool flag_; | 171 bool flag_; |
164 bool nested_; | 172 bool nested_; |
165 Timer* timer_; | 173 Timer* timer_; |
166 DISALLOW_COPY_AND_ASSIGN(TimerScope); | 174 DISALLOW_COPY_AND_ASSIGN(TimerScope); |
167 }; | 175 }; |
168 | 176 |
169 #define TIMERSCOPE(name) \ | 177 #define TIMERSCOPE(name) \ |
170 TimerScope vm_internal_timer_((FLAG_##name || FLAG_time_all), \ | 178 TimerScope vm_internal_timer_(true, \ |
Cutch
2014/01/28 19:32:51
Perhaps we to respect FLAG_enable_vm_service ?
turnidge
2014/01/28 23:18:55
That flag is in bin/main.cc -- not available.
| |
171 &(Isolate::Current()->timer_list().name())) | 179 &(Isolate::Current()->timer_list().name())) |
172 | 180 |
173 } // namespace dart | 181 } // namespace dart |
174 | 182 |
175 #endif // VM_TIMER_H_ | 183 #endif // VM_TIMER_H_ |
OLD | NEW |