Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(595)

Side by Side Diff: runtime/vm/timeline.h

Issue 1170503004: Initial Timeline Events (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 };
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, true) \
169 V(Compiler, true) \
170 V(Embedder, true) \
171 V(GC, true) \
172 V(Isolate, true) \
173
174
175 class TimelineDurationScope : public StackResource {
176 public:
177 TimelineDurationScope(Isolate* isolate,
178 TimelineStream* stream,
179 const char* label)
180 : StackResource(isolate) {
181 event_ = stream->RecordEvent();
182 if (event_ == NULL) {
183 return;
184 }
185 event_->DurationBegin(stream, label);
186 }
187
188 bool enabled() const {
189 return event_ != NULL;
190 }
191
192 void SetNumArguments(intptr_t length) {
193 if (event_ == NULL) {
194 return;
195 }
196 event_->SetNumArguments(length);
197 }
198
199 void SetArgument(intptr_t i, const char* name, char* argument) {
200 if (event_ == NULL) {
201 return;
202 }
203 event_->SetArgument(i, name, argument);
204 }
205
206 void CopyArgument(intptr_t i, const char* name, const char* argument) {
207 if (event_ == NULL) {
208 return;
209 }
210 event_->CopyArgument(i, name, argument);
211 }
212
213 void FormatArgument(intptr_t i,
214 const char* name,
215 const char* fmt, ...) PRINTF_ATTRIBUTE(4, 5);
216
217 ~TimelineDurationScope() {
218 if (event_ == NULL) {
219 return;
220 }
221 event_->DurationEnd();
222 }
223
224 private:
225 TimelineEvent* event_;
226 };
227
228
229 class TimelineEventBuffer {
230 public:
231 static const intptr_t kDefaultCapacity = 8192;
232
233 static intptr_t SizeForCapacity(intptr_t capacity);
234
235 explicit TimelineEventBuffer(intptr_t capacity = kDefaultCapacity);
236 ~TimelineEventBuffer();
237
238 void PrintJSON(JSONStream* js) const;
239
240 void Dump(); // Temporary.
241
242 private:
243 // events_[i] and event_objects_[i] are indexed together.
244 TimelineEvent* events_;
245 RawArray* event_objects_;
246 uintptr_t cursor_;
247 intptr_t capacity_;
248
249 void PrintJSONMeta(JSONArray* array) const;
250 void PrintJSONEvents(JSONArray* array) const;
251
252 intptr_t GetNextIndex();
253 void VisitObjectPointers(ObjectPointerVisitor* visitor);
254 TimelineEvent* RecordEvent(const Object& obj);
255 TimelineEvent* RecordEvent();
256
257 friend class TimelineStream;
258 friend class Isolate;
259 DISALLOW_COPY_AND_ASSIGN(TimelineEventBuffer);
260 };
261
262 } // namespace dart
263
264 #endif // VM_TIMELINE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698