diff --git a/runtime/vm/timeline.cc b/runtime/vm/timeline.cc index 2cf812bceec..ca6e9a2fd18 100644 --- a/runtime/vm/timeline.cc +++ b/runtime/vm/timeline.cc @@ -721,6 +721,7 @@ void TimelineBeginEndScope::EmitBegin() { TimelineEvent* event = stream()->StartEvent(); if (event == NULL) { // Stream is now disabled. + set_enabled(false); return; } ASSERT(event != NULL); @@ -737,6 +738,7 @@ void TimelineBeginEndScope::EmitEnd() { TimelineEvent* event = stream()->StartEvent(); if (event == NULL) { // Stream is now disabled. + set_enabled(false); return; } ASSERT(event != NULL); @@ -1160,17 +1162,35 @@ TimelineEventBlock* TimelineEventEndlessRecorder::GetNewBlockLocked() { return head_; } +static int TimelineEventBlockCompare(TimelineEventBlock* const* a, + TimelineEventBlock* const* b) { + return (*a)->LowerTimeBound() - (*b)->LowerTimeBound(); +} + void TimelineEventEndlessRecorder::PrintJSONEvents( JSONArray* events, TimelineEventFilter* filter) { MutexLocker ml(&lock_); + // Collect all interesting blocks. + MallocGrowableArray blocks(8); TimelineEventBlock* current = head_; while (current != NULL) { - if (!filter->IncludeBlock(current)) { - current = current->next(); - continue; + if (filter->IncludeBlock(current)) { + blocks.Add(current); } + current = current->next(); + } + // Bail early. + if (blocks.length() == 0) { + return; + } + // Sort the interesting blocks so that blocks with earlier events are + // outputted first. + blocks.Sort(TimelineEventBlockCompare); + // Output blocks in sorted order. + for (intptr_t block_idx = 0; block_idx < blocks.length(); block_idx++) { + current = blocks[block_idx]; intptr_t length = current->length(); for (intptr_t i = 0; i < length; i++) { TimelineEvent* event = current->At(i); @@ -1180,7 +1200,6 @@ void TimelineEventEndlessRecorder::PrintJSONEvents( events->AddValue(event); } } - current = current->next(); } } diff --git a/runtime/vm/timeline.h b/runtime/vm/timeline.h index f918567ac16..ae714e5920c 100644 --- a/runtime/vm/timeline.h +++ b/runtime/vm/timeline.h @@ -94,7 +94,6 @@ class TimelineEvent { // Keep in sync with StateBits below. enum EventType { kNone, - kSerializedJSON, // Events from Dart code. kBegin, kEnd, kDuration, @@ -387,6 +386,10 @@ class TimelineEventScope : public StackResource { return enabled_; } + void set_enabled(bool enabled) { + enabled_ = enabled; + } + const char* label() const { return label_; }