OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 #ifndef VM_TIMELINE_H_ | |
6 #define VM_TIMELINE_H_ | |
7 | |
8 #include "vm/bitfield.h" | |
9 | |
10 namespace dart { | |
11 | |
12 class JSONStream; | |
13 class Object; | |
14 class RawArray; | |
15 class Thread; | |
16 class TimelineEvent; | |
17 class TimelineEventBuffer; | |
18 class TimelineStream; | |
19 | |
20 struct TimelineEventArgument { | |
21 const char* name; | |
22 char* value; | |
23 }; | |
siva
2015/06/16 15:42:31
Since this is only used as a type of a private fie
Cutch
2015/06/16 17:08:17
Done.
| |
24 | |
25 class TimelineEvent { | |
26 public: | |
27 // Keep in sync with StateBits below. | |
28 enum EventType { | |
29 kNone, | |
30 kDuration, | |
31 kInstant, | |
32 kAsyncBegin, | |
33 kAsyncInstant, | |
34 kAsyncEnd, | |
35 kNumEventTypes, | |
36 }; | |
37 | |
38 TimelineEvent(); | |
39 ~TimelineEvent(); | |
40 | |
41 void Reset(); | |
42 | |
43 bool IsValid() const { | |
44 return (event_type() > kNone) && (event_type() < kNumEventTypes); | |
45 } | |
46 | |
47 // Marks the beginning of an asynchronous operation. | |
48 // Returns |async_id| which must be passed to |AsyncInstant| and |AsyncEnd|. | |
49 int64_t AsyncBegin(TimelineStream* stream, const char* label); | |
50 // Marks an instantaneous event associated with |async_id|. | |
51 void AsyncInstant(TimelineStream* stream, | |
52 const char* label, | |
53 int64_t async_id); | |
54 // Marks the end of an asynchronous operation associated with |async_id|. | |
55 void AsyncEnd(TimelineStream* stream, | |
56 const char* label, | |
57 int64_t async_id); | |
58 | |
59 void DurationBegin(TimelineStream* stream, const char* label); | |
60 void DurationEnd(); | |
61 void Instant(TimelineStream* stream, const char* label); | |
62 | |
63 void Duration(TimelineStream* stream, | |
64 const char* label, | |
65 int64_t start_micros, | |
66 int64_t end_micros); | |
67 | |
68 // Set the number of arguments in the event. | |
69 void SetNumArguments(intptr_t length); | |
70 // |name| must be a compile time constant. Takes ownership of |argumentp|. | |
71 void SetArgument(intptr_t i, const char* name, char* argument); | |
72 // |name| must be a compile time constant. Copies |argument|. | |
73 void CopyArgument(intptr_t i, const char* name, const char* argument); | |
74 // |name| must be a compile time constant. | |
75 void FormatArgument(intptr_t i, | |
76 const char* name, | |
77 const char* fmt, ...) PRINTF_ATTRIBUTE(4, 5); | |
78 EventType event_type() const { | |
79 return EventTypeField::decode(state_); | |
80 } | |
81 | |
82 int64_t TimeOrigin() const; | |
83 int64_t AsyncId() const; | |
84 int64_t TimeDuration() const; | |
85 | |
86 void PrintJSON(JSONStream* stream) const; | |
87 | |
88 private: | |
89 int64_t timestamp0_; | |
90 int64_t timestamp1_; | |
91 TimelineEventArgument* arguments_; | |
92 intptr_t arguments_length_; | |
93 uword state_; | |
94 const char* label_; | |
95 TimelineStream* stream_; | |
96 Thread* thread_; | |
97 | |
98 void FreeArguments(); | |
99 | |
100 void Init(EventType event_type, TimelineStream* stream, const char* label); | |
101 | |
102 void set_event_type(EventType event_type) { | |
103 state_ = EventTypeField::update(event_type, state_); | |
104 } | |
105 | |
106 enum StateBits { | |
107 kEventTypeBit = 0, | |
108 // reserve 4 bits for type. | |
109 kNextBit = 4, | |
110 }; | |
111 | |
112 class EventTypeField : public BitField<EventType, kEventTypeBit, 4> {}; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(TimelineEvent); | |
115 }; | |
116 | |
117 | |
118 // A stream of timeline events. A stream has a name and can be enabled or | |
119 // disabled. | |
120 class TimelineStream { | |
121 public: | |
122 TimelineStream(); | |
123 | |
124 void Init(const char* name, bool enabled); | |
125 | |
126 const char* name() const { | |
127 return name_; | |
128 } | |
129 | |
130 bool enabled() const { | |
131 return enabled_; | |
132 } | |
133 | |
134 void set_enabled(bool enabled) { | |
135 enabled_ = enabled; | |
136 } | |
137 | |
138 TimelineEventBuffer* buffer() const { | |
139 return buffer_; | |
140 } | |
141 | |
142 void set_buffer(TimelineEventBuffer* buffer) { | |
143 buffer_ = buffer; | |
144 } | |
145 | |
146 // Records an event. Will return |NULL| if not enabled. The returned | |
147 // |TimelineEvent| is in an undefined state and must be initialized. | |
148 // |obj| is associated with the returned |TimelineEvent|. | |
149 TimelineEvent* RecordEvent(const Object& obj); | |
150 | |
151 // Records an event. Will return |NULL| if not enabled. The returned | |
152 // |TimelineEvent| is in an undefined state and must be initialized. | |
153 TimelineEvent* RecordEvent(); | |
154 | |
155 int64_t GetNextSeq(); | |
156 | |
157 private: | |
158 // Buffer of TimelineEvents. | |
159 TimelineEventBuffer* buffer_; | |
160 const char* name_; | |
161 bool enabled_; | |
162 int64_t seq_; | |
163 }; | |
164 | |
165 | |
166 // (name, enabled by default). | |
167 #define ISOLATE_TIMELINE_STREAM_LIST(V) \ | |
168 V(API, false) \ | |
169 V(Compiler, false) \ | |
170 V(Embedder, false) \ | |
171 V(GC, false) \ | |
172 V(Isolate, false) \ | |
173 | |
174 | |
175 #define TIMELINE_FUNCTION_COMPILATION_DURATION(isolate, suffix, function) \ | |
176 TimelineDurationScope tds(isolate, \ | |
177 isolate->GetCompilerStream(), \ | |
178 "Compile"#suffix); \ | |
179 if (tds.enabled()) { \ | |
180 tds.SetNumArguments(1); \ | |
181 tds.CopyArgument( \ | |
182 0, \ | |
183 "function", \ | |
184 const_cast<char*>(function.QualifiedUserVisibleNameCString())); \ | |
185 } | |
186 | |
187 class TimelineDurationScope : public StackResource { | |
188 public: | |
189 TimelineDurationScope(Isolate* isolate, | |
190 TimelineStream* stream, | |
191 const char* label) | |
192 : StackResource(isolate) { | |
193 event_ = stream->RecordEvent(); | |
194 if (event_ == NULL) { | |
195 return; | |
196 } | |
197 event_->DurationBegin(stream, label); | |
198 } | |
199 | |
200 bool enabled() const { | |
201 return event_ != NULL; | |
202 } | |
203 | |
204 void SetNumArguments(intptr_t length) { | |
205 if (event_ == NULL) { | |
206 return; | |
207 } | |
208 event_->SetNumArguments(length); | |
209 } | |
210 | |
211 void SetArgument(intptr_t i, const char* name, char* argument) { | |
212 if (event_ == NULL) { | |
213 return; | |
214 } | |
215 event_->SetArgument(i, name, argument); | |
216 } | |
217 | |
218 void CopyArgument(intptr_t i, const char* name, const char* argument) { | |
219 if (event_ == NULL) { | |
220 return; | |
221 } | |
222 event_->CopyArgument(i, name, argument); | |
223 } | |
224 | |
225 void FormatArgument(intptr_t i, | |
226 const char* name, | |
227 const char* fmt, ...) PRINTF_ATTRIBUTE(4, 5); | |
228 | |
229 ~TimelineDurationScope() { | |
230 if (event_ == NULL) { | |
231 return; | |
232 } | |
233 event_->DurationEnd(); | |
234 } | |
235 | |
236 private: | |
237 TimelineEvent* event_; | |
238 }; | |
239 | |
240 | |
241 class TimelineEventBuffer { | |
242 public: | |
243 static const intptr_t kDefaultCapacity = 8192; | |
244 | |
245 static intptr_t SizeForCapacity(intptr_t capacity); | |
246 | |
247 explicit TimelineEventBuffer(intptr_t capacity = kDefaultCapacity); | |
248 ~TimelineEventBuffer(); | |
249 | |
250 void PrintJSON(JSONStream* js) const; | |
251 | |
252 void WriteTo(const char* directory); | |
253 | |
254 private: | |
255 // events_[i] and event_objects_[i] are indexed together. | |
256 TimelineEvent* events_; | |
257 RawArray* event_objects_; | |
258 uintptr_t cursor_; | |
259 intptr_t capacity_; | |
260 | |
261 void PrintJSONMeta(JSONArray* array) const; | |
262 void PrintJSONEvents(JSONArray* array) const; | |
263 | |
264 intptr_t GetNextIndex(); | |
265 void VisitObjectPointers(ObjectPointerVisitor* visitor); | |
266 TimelineEvent* RecordEvent(const Object& obj); | |
267 TimelineEvent* RecordEvent(); | |
268 | |
269 friend class TimelineStream; | |
270 friend class Isolate; | |
271 DISALLOW_COPY_AND_ASSIGN(TimelineEventBuffer); | |
272 }; | |
273 | |
274 } // namespace dart | |
275 | |
276 #endif // VM_TIMELINE_H_ | |
OLD | NEW |