Index: runtime/vm/timeline.h |
diff --git a/runtime/vm/timeline.h b/runtime/vm/timeline.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..508f9f81b6cc5e9f972eb608ea93487e71544553 |
--- /dev/null |
+++ b/runtime/vm/timeline.h |
@@ -0,0 +1,189 @@ |
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+#ifndef VM_TIMELINE_H_ |
+#define VM_TIMELINE_H_ |
+ |
+#include "vm/bitfield.h" |
+ |
+namespace dart { |
+ |
+class JSONStream; |
+class Object; |
+class RawArray; |
+class Thread; |
+class TimelineEvent; |
+class TimelineEventBuffer; |
+class TimelineStream; |
+ |
+class TimelineEvent { |
+ public: |
+ // Keep in sync with StateBits below. |
+ enum EventType { |
+ kNone = 0, |
+ kDuration = 1, |
+ kInstant = 2, |
+ }; |
+ |
+ void Reset(); |
+ |
+ bool IsValid() const { |
+ return (event_type() >= kDuration) && (event_type() <= kInstant); |
+ } |
+ |
+ void DurationBegin(TimelineStream* stream, const char* label); |
+ void DurationEnd(); |
+ void Instant(TimelineStream* stream, const char* label); |
+ |
+ void SetArgument(const char* name, const char* value); |
+ |
+ EventType event_type() const { |
+ return EventTypeField::decode(state_); |
+ } |
+ |
+ void PrintJSON(JSONStream* stream); |
+ |
+ private: |
+ int64_t timestamp0_; |
+ int64_t timestamp1_; |
+ uword state_; |
+ const char* label_; |
+ TimelineStream* stream_; |
+ Thread* thread_; |
+ const char* arg_name_; |
+ const char* arg_value_; |
+ |
+ void Init(EventType event_type, TimelineStream* stream, const char* label); |
+ |
+ void set_event_type(EventType event_type) { |
+ state_ = EventTypeField::update(event_type, state_); |
+ } |
+ |
+ enum StateBits { |
+ kEventTypeBit = 0, |
+ // reserve 4 bits for type. |
+ kNextBit = 4, |
+ }; |
+ |
+ class EventTypeField : public BitField<EventType, kEventTypeBit, 4> {}; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TimelineEvent); |
+}; |
+ |
+ |
+// A stream of timeline events. A stream has a name and can be enabled or |
+// disabled individually. |
+class TimelineStream { |
+ public: |
+ TimelineStream(); |
+ |
+ void Init(const char* name, bool enabled); |
+ |
+ const char* name() const { |
+ return name_; |
+ } |
+ |
+ bool enabled() const { |
+ return enabled_; |
+ } |
+ |
+ void set_enabled(bool enabled) { |
+ enabled_ = enabled; |
+ } |
+ |
+ TimelineEventBuffer* buffer() const { |
+ return buffer_; |
+ } |
+ |
+ void set_buffer(TimelineEventBuffer* buffer) { |
+ buffer_ = buffer; |
+ } |
+ |
+ // Records an event. Will return |NULL| if not enabled. The returned |
+ // |TimelineEvent| is in an undefined state and must be initialized. |
+ // |obj| is associated with the returned |TimelineEvent|. |
+ TimelineEvent* RecordEvent(const Object& obj); |
+ |
+ // Records an event. Will return |NULL| if not enabled. The returned |
+ // |TimelineEvent| is in an undefined state and must be initialized. |
+ TimelineEvent* RecordEvent(); |
+ |
+ private: |
+ // Buffer of TimelineEvents. |
+ TimelineEventBuffer* buffer_; |
+ const char* name_; |
+ bool enabled_; |
+}; |
+ |
+ |
+// name, enabled by default. |
+#define ISOLATE_TIMELINE_STREAM_LIST(V) \ |
+ V(API, true) \ |
+ V(Compiler, true) \ |
+ V(GC, true) \ |
+ |
+ |
+class TimelineDurationScope : public ValueObject { |
+ public: |
+ TimelineDurationScope(TimelineStream* stream, const char* label) { |
+ event_ = stream->RecordEvent(); |
+ if (event_ == NULL) { |
+ return; |
+ } |
+ event_->DurationBegin(stream, label); |
+ } |
+ |
+ void SetArgument(const char* arg_name, const char* arg_value) { |
+ if (event_ == NULL) { |
+ return; |
+ } |
+ event_->SetArgument(arg_name, arg_value); |
+ } |
+ |
+ ~TimelineDurationScope() { |
+ if (event_ == NULL) { |
+ return; |
+ } |
+ event_->DurationEnd(); |
+ } |
+ |
+ private: |
+ TimelineEvent* event_; |
+}; |
+ |
+ |
+class TimelineEventBuffer { |
+ public: |
+ static const intptr_t kDefaultCapacity = 8192; |
+ |
+ static intptr_t SizeForCapacity(intptr_t capacity); |
+ |
+ explicit TimelineEventBuffer(intptr_t capacity = kDefaultCapacity); |
+ ~TimelineEventBuffer(); |
+ |
+ void Dump(); |
+ |
+ private: |
+ // events_[i] and event_objects_[i] are indexed together. |
+ TimelineEvent* events_; |
+ RawArray* event_objects_; |
+ uintptr_t cursor_; |
+ intptr_t capacity_; |
+ |
+ void DumpMeta(JSONArray* array); |
+ void DumpEvents(JSONArray* array); |
+ |
+ intptr_t GetNextIndex(); |
+ void VisitObjectPointers(ObjectPointerVisitor* visitor); |
+ TimelineEvent* RecordEvent(const Object& obj); |
+ TimelineEvent* RecordEvent(); |
+ |
+ friend class TimelineStream; |
+ friend class Isolate; |
+ DISALLOW_COPY_AND_ASSIGN(TimelineEventBuffer); |
+}; |
+ |
+} // namespace dart |
+ |
+#endif // VM_TIMELINE_H_ |