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 = 0, |
| 25 kDuration = 1, |
| 26 kInstant = 2, |
| 27 }; |
| 28 |
| 29 void Reset(); |
| 30 |
| 31 bool IsValid() const { |
| 32 return (event_type() >= kDuration) && (event_type() <= kInstant); |
| 33 } |
| 34 |
| 35 void DurationBegin(TimelineStream* stream, const char* label); |
| 36 void DurationEnd(); |
| 37 void Instant(TimelineStream* stream, const char* label); |
| 38 |
| 39 void SetArgument(const char* name, const char* value); |
| 40 |
| 41 EventType event_type() const { |
| 42 return EventTypeField::decode(state_); |
| 43 } |
| 44 |
| 45 void PrintJSON(JSONStream* stream); |
| 46 |
| 47 private: |
| 48 int64_t timestamp0_; |
| 49 int64_t timestamp1_; |
| 50 uword state_; |
| 51 const char* label_; |
| 52 TimelineStream* stream_; |
| 53 Thread* thread_; |
| 54 const char* arg_name_; |
| 55 const char* arg_value_; |
| 56 |
| 57 void Init(EventType event_type, TimelineStream* stream, const char* label); |
| 58 |
| 59 void set_event_type(EventType event_type) { |
| 60 state_ = EventTypeField::update(event_type, state_); |
| 61 } |
| 62 |
| 63 enum StateBits { |
| 64 kEventTypeBit = 0, |
| 65 // reserve 4 bits for type. |
| 66 kNextBit = 4, |
| 67 }; |
| 68 |
| 69 class EventTypeField : public BitField<EventType, kEventTypeBit, 4> {}; |
| 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(TimelineEvent); |
| 72 }; |
| 73 |
| 74 |
| 75 // A stream of timeline events. A stream has a name and can be enabled or |
| 76 // disabled individually. |
| 77 class TimelineStream { |
| 78 public: |
| 79 TimelineStream(); |
| 80 |
| 81 void Init(const char* name, bool enabled); |
| 82 |
| 83 const char* name() const { |
| 84 return name_; |
| 85 } |
| 86 |
| 87 bool enabled() const { |
| 88 return enabled_; |
| 89 } |
| 90 |
| 91 void set_enabled(bool enabled) { |
| 92 enabled_ = enabled; |
| 93 } |
| 94 |
| 95 TimelineEventBuffer* buffer() const { |
| 96 return buffer_; |
| 97 } |
| 98 |
| 99 void set_buffer(TimelineEventBuffer* buffer) { |
| 100 buffer_ = buffer; |
| 101 } |
| 102 |
| 103 // Records an event. Will return |NULL| if not enabled. The returned |
| 104 // |TimelineEvent| is in an undefined state and must be initialized. |
| 105 // |obj| is associated with the returned |TimelineEvent|. |
| 106 TimelineEvent* RecordEvent(const Object& obj); |
| 107 |
| 108 // Records an event. Will return |NULL| if not enabled. The returned |
| 109 // |TimelineEvent| is in an undefined state and must be initialized. |
| 110 TimelineEvent* RecordEvent(); |
| 111 |
| 112 private: |
| 113 // Buffer of TimelineEvents. |
| 114 TimelineEventBuffer* buffer_; |
| 115 const char* name_; |
| 116 bool enabled_; |
| 117 }; |
| 118 |
| 119 |
| 120 // name, enabled by default. |
| 121 #define ISOLATE_TIMELINE_STREAM_LIST(V) \ |
| 122 V(API, true) \ |
| 123 V(Compiler, true) \ |
| 124 V(GC, true) \ |
| 125 |
| 126 |
| 127 class TimelineDurationScope : public ValueObject { |
| 128 public: |
| 129 TimelineDurationScope(TimelineStream* stream, const char* label) { |
| 130 event_ = stream->RecordEvent(); |
| 131 if (event_ == NULL) { |
| 132 return; |
| 133 } |
| 134 event_->DurationBegin(stream, label); |
| 135 } |
| 136 |
| 137 void SetArgument(const char* arg_name, const char* arg_value) { |
| 138 if (event_ == NULL) { |
| 139 return; |
| 140 } |
| 141 event_->SetArgument(arg_name, arg_value); |
| 142 } |
| 143 |
| 144 ~TimelineDurationScope() { |
| 145 if (event_ == NULL) { |
| 146 return; |
| 147 } |
| 148 event_->DurationEnd(); |
| 149 } |
| 150 |
| 151 private: |
| 152 TimelineEvent* event_; |
| 153 }; |
| 154 |
| 155 |
| 156 class TimelineEventBuffer { |
| 157 public: |
| 158 static const intptr_t kDefaultCapacity = 8192; |
| 159 |
| 160 static intptr_t SizeForCapacity(intptr_t capacity); |
| 161 |
| 162 explicit TimelineEventBuffer(intptr_t capacity = kDefaultCapacity); |
| 163 ~TimelineEventBuffer(); |
| 164 |
| 165 void Dump(); |
| 166 |
| 167 private: |
| 168 // events_[i] and event_objects_[i] are indexed together. |
| 169 TimelineEvent* events_; |
| 170 RawArray* event_objects_; |
| 171 uintptr_t cursor_; |
| 172 intptr_t capacity_; |
| 173 |
| 174 void DumpMeta(JSONArray* array); |
| 175 void DumpEvents(JSONArray* array); |
| 176 |
| 177 intptr_t GetNextIndex(); |
| 178 void VisitObjectPointers(ObjectPointerVisitor* visitor); |
| 179 TimelineEvent* RecordEvent(const Object& obj); |
| 180 TimelineEvent* RecordEvent(); |
| 181 |
| 182 friend class TimelineStream; |
| 183 friend class Isolate; |
| 184 DISALLOW_COPY_AND_ASSIGN(TimelineEventBuffer); |
| 185 }; |
| 186 |
| 187 } // namespace dart |
| 188 |
| 189 #endif // VM_TIMELINE_H_ |
OLD | NEW |