Chromium Code Reviews| Index: runtime/vm/timeline.cc |
| diff --git a/runtime/vm/timeline.cc b/runtime/vm/timeline.cc |
| index 7e9ba2d179c9ec86ad6615ce49555b26a69e4eb9..c6bb58d7612e6d0d66c3a2d1fc488599d74d13f4 100644 |
| --- a/runtime/vm/timeline.cc |
| +++ b/runtime/vm/timeline.cc |
| @@ -4,6 +4,7 @@ |
| #ifndef PRODUCT |
| +#include <fcntl.h> |
| #include <cstdlib> |
| #include "vm/atomic.h" |
| @@ -20,6 +21,7 @@ namespace dart { |
| DEFINE_FLAG(bool, complete_timeline, false, "Record the complete timeline"); |
| DEFINE_FLAG(bool, startup_timeline, false, "Record the startup timeline"); |
| +DEFINE_FLAG(bool, systrace_timeline, false, "Record the timeline to systrace"); |
| DEFINE_FLAG(bool, trace_timeline, false, |
| "Trace timeline backend"); |
| DEFINE_FLAG(bool, trace_timeline_analysis, false, |
| @@ -35,7 +37,7 @@ DEFINE_FLAG(charp, timeline_streams, NULL, |
| "GC, Isolate, and VM."); |
| DEFINE_FLAG(charp, timeline_recorder, "ring", |
| "Select the timeline recorder used. " |
| - "Valid values: ring, endless, and startup.") |
| + "Valid values: ring, endless, startup, and systrace.") |
| // Implementation notes: |
| // |
| @@ -86,9 +88,19 @@ static TimelineEventRecorder* CreateTimelineRecorder() { |
| (FLAG_timeline_dir != NULL) || FLAG_timing || FLAG_complete_timeline; |
| const bool use_startup_recorder = FLAG_startup_timeline; |
| + const bool use_systrace_recorder = FLAG_systrace_timeline; |
| const char* flag = FLAG_timeline_recorder; |
| + if (use_systrace_recorder || (flag != NULL)) { |
| + if (use_systrace_recorder || (strcmp("systrace", flag) == 0)) { |
| + if (FLAG_trace_timeline) { |
| + THR_Print("Using the Systrace timeline recorder.\n"); |
| + } |
| + return new TimelineEventSystraceRecorder(); |
| + } |
| + } |
| + |
| if (use_endless_recorder || (flag != NULL)) { |
| if (use_endless_recorder || (strcmp("endless", flag) == 0)) { |
| if (FLAG_trace_timeline) { |
| @@ -497,6 +509,39 @@ void TimelineEvent::StealArguments(intptr_t arguments_length, |
| } |
| +intptr_t TimelineEvent::PrintSystrace(char* buffer, intptr_t buffer_size) { |
| + ASSERT(buffer != NULL); |
| + ASSERT(buffer_size > 0); |
| + buffer[0] = '\0'; |
| + intptr_t length = 0; |
| + int64_t pid = OS::ProcessId(); |
| + switch (event_type()) { |
| + case kBegin: { |
| + length = snprintf(buffer, buffer_size, "B|%" Pd64 "|%s", pid, label()); |
|
rmacnak
2016/09/08 18:17:09
OS::SNPrint
Cutch
2016/09/08 18:24:08
Done.
|
| + } |
| + break; |
| + case kEnd: { |
| + length = snprintf(buffer, buffer_size, "E"); |
|
rmacnak
2016/09/08 18:17:09
"
Cutch
2016/09/08 18:24:08
Done.
|
| + } |
| + break; |
| + case kCounter: { |
| + if (arguments_length_ > 0) { |
| + // We only report the first counter value. |
| + length = snprintf(buffer, buffer_size, |
| + "C|%" Pd64 "|%s|%s", |
| + pid, |
| + label(), |
| + arguments_[0].value); |
| + } |
| + } |
| + default: |
| + // Ignore event types that we cannot serialize to the Systrace format. |
| + break; |
| + } |
| + return length; |
| +} |
| + |
| + |
| void TimelineEvent::Complete() { |
| TimelineEventRecorder* recorder = Timeline::recorder(); |
| if (recorder != NULL) { |
| @@ -1354,6 +1399,60 @@ void TimelineEventFixedBufferRecorder::CompleteEvent(TimelineEvent* event) { |
| } |
| +static const char* kSystracePath = "/sys/kernel/debug/tracing/trace_marker"; |
| + |
| + |
| +TimelineEventSystraceRecorder::TimelineEventSystraceRecorder(intptr_t capacity) |
| + : TimelineEventFixedBufferRecorder(capacity), |
| + systrace_fd_(-1) { |
| + systrace_fd_ = open(kSystracePath, O_WRONLY); |
|
rmacnak
2016/09/08 18:17:09
ifdef TARGET_OS_LINUX or TARGET_OS_ANDROID
else
Cutch
2016/09/08 18:24:08
Done.
|
| + if ((systrace_fd_ < 0) && FLAG_trace_timeline) { |
| + OS::PrintErr("TimelineEventSystraceRecorder: Could not open `%s`\n", |
| + kSystracePath); |
| + } |
| +} |
| + |
| + |
| +TimelineEventSystraceRecorder::~TimelineEventSystraceRecorder() { |
| + if (systrace_fd_ >= 0) { |
| + close(systrace_fd_); |
| + } |
| +} |
| + |
| + |
| +TimelineEventBlock* TimelineEventSystraceRecorder::GetNewBlockLocked() { |
| + // TODO(johnmccutchan): This function should only hand out blocks |
| + // which have been marked as finished. |
| + if (block_cursor_ == num_blocks_) { |
| + block_cursor_ = 0; |
| + } |
| + TimelineEventBlock* block = blocks_[block_cursor_++]; |
| + block->Reset(); |
| + block->Open(); |
| + return block; |
| +} |
| + |
| + |
| +void TimelineEventSystraceRecorder::CompleteEvent(TimelineEvent* event) { |
| + if (event == NULL) { |
| + return; |
| + } |
| + if (systrace_fd_ >= 0) { |
|
rmacnak
2016/09/08 18:17:09
ifdef TARGET_OS_LINUX or TARGET_OS_ANDROID
Cutch
2016/09/08 18:24:08
Done.
|
| + // Serialize to the systrace format. |
| + const intptr_t kBufferLength = 1024; |
| + char buffer[kBufferLength]; |
| + const intptr_t event_length = |
| + event->PrintSystrace(&buffer[0], kBufferLength); |
| + if (event_length > 0) { |
| + ssize_t result = write(systrace_fd_, buffer, event_length); |
| + // Ignore failures writing to systrace_fd_. |
| + USE(result); |
| + } |
| + } |
| + ThreadBlockCompleteEvent(event); |
| +} |
| + |
| + |
| TimelineEventBlock* TimelineEventRingRecorder::GetNewBlockLocked() { |
| // TODO(johnmccutchan): This function should only hand out blocks |
| // which have been marked as finished. |