From 33aaf940e522cc1ad7bdceaabcc32a48da86b2cf Mon Sep 17 00:00:00 2001 From: John McCutchan Date: Mon, 1 Feb 2016 13:12:34 -0800 Subject: [PATCH] Output TimelineEventBlocks in sorted order - Before outputting to JSON sort TimelineEventBlocks by earliest event time. - Output using sorted block order. - Works around a bug in mojo (https://github.com/domokit/mojo/issues/649) R=zra@google.com Review URL: https://codereview.chromium.org/1657843003 . --- runtime/vm/timeline.cc | 27 +++++++++++++++++++++++---- runtime/vm/timeline.h | 5 ++++- 2 files changed, 27 insertions(+), 5 deletions(-) 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_; }