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