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 class TimelineEvent { |
| 21 public: |
| 22 // Keep in sync with StateBits below. |
| 23 enum EventType { |
| 24 kNone, |
| 25 kDuration, |
| 26 kInstant, |
| 27 kAsyncBegin, |
| 28 kAsyncInstant, |
| 29 kAsyncEnd, |
| 30 kNumEventTypes, |
| 31 }; |
| 32 |
| 33 TimelineEvent(); |
| 34 ~TimelineEvent(); |
| 35 |
| 36 void Reset(); |
| 37 |
| 38 bool IsValid() const { |
| 39 return (event_type() > kNone) && (event_type() < kNumEventTypes); |
| 40 } |
| 41 |
| 42 // Marks the beginning of an asynchronous operation. |
| 43 // Returns |async_id| which must be passed to |AsyncInstant| and |AsyncEnd|. |
| 44 int64_t AsyncBegin(TimelineStream* stream, const char* label); |
| 45 // Marks an instantaneous event associated with |async_id|. |
| 46 void AsyncInstant(TimelineStream* stream, |
| 47 const char* label, |
| 48 int64_t async_id); |
| 49 // Marks the end of an asynchronous operation associated with |async_id|. |
| 50 void AsyncEnd(TimelineStream* stream, |
| 51 const char* label, |
| 52 int64_t async_id); |
| 53 |
| 54 void DurationBegin(TimelineStream* stream, const char* label); |
| 55 void DurationEnd(); |
| 56 void Instant(TimelineStream* stream, const char* label); |
| 57 |
| 58 void Duration(TimelineStream* stream, |
| 59 const char* label, |
| 60 int64_t start_micros, |
| 61 int64_t end_micros); |
| 62 |
| 63 // Set the number of arguments in the event. |
| 64 void SetNumArguments(intptr_t length); |
| 65 // |name| must be a compile time constant. Takes ownership of |argumentp|. |
| 66 void SetArgument(intptr_t i, const char* name, char* argument); |
| 67 // |name| must be a compile time constant. Copies |argument|. |
| 68 void CopyArgument(intptr_t i, const char* name, const char* argument); |
| 69 // |name| must be a compile time constant. |
| 70 void FormatArgument(intptr_t i, |
| 71 const char* name, |
| 72 const char* fmt, ...) PRINTF_ATTRIBUTE(4, 5); |
| 73 EventType event_type() const { |
| 74 return EventTypeField::decode(state_); |
| 75 } |
| 76 |
| 77 int64_t TimeOrigin() const; |
| 78 int64_t AsyncId() const; |
| 79 int64_t TimeDuration() const; |
| 80 |
| 81 void PrintJSON(JSONStream* stream) const; |
| 82 |
| 83 private: |
| 84 struct TimelineEventArgument { |
| 85 const char* name; |
| 86 char* value; |
| 87 }; |
| 88 |
| 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 |