2017-07-21 15:06:34 +00:00
|
|
|
// Copyright (c) 2017, 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.
|
|
|
|
|
2019-01-18 00:06:10 +00:00
|
|
|
#include "vm/globals.h"
|
|
|
|
#if defined(HOST_OS_LINUX) && defined(SUPPORT_TIMELINE)
|
2017-07-21 15:06:34 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2018-03-28 23:16:09 +00:00
|
|
|
#include "platform/atomic.h"
|
2017-07-21 15:06:34 +00:00
|
|
|
#include "vm/isolate.h"
|
|
|
|
#include "vm/json_stream.h"
|
|
|
|
#include "vm/lockers.h"
|
|
|
|
#include "vm/log.h"
|
|
|
|
#include "vm/object.h"
|
|
|
|
#include "vm/service_event.h"
|
|
|
|
#include "vm/thread.h"
|
|
|
|
#include "vm/timeline.h"
|
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
|
|
|
DECLARE_FLAG(bool, trace_timeline);
|
|
|
|
|
2018-04-02 18:34:35 +00:00
|
|
|
TimelineEventSystraceRecorder::TimelineEventSystraceRecorder()
|
|
|
|
: TimelineEventPlatformRecorder(), systrace_fd_(-1) {
|
2017-07-21 15:06:34 +00:00
|
|
|
const char* kSystracePath = "/sys/kernel/debug/tracing/trace_marker";
|
|
|
|
systrace_fd_ = open(kSystracePath, O_WRONLY);
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
intptr_t TimelineEventSystraceRecorder::PrintSystrace(TimelineEvent* event,
|
|
|
|
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->event_type()) {
|
|
|
|
case TimelineEvent::kBegin: {
|
2018-03-01 02:07:46 +00:00
|
|
|
length = Utils::SNPrint(buffer, buffer_size, "B|%" Pd64 "|%s", pid,
|
|
|
|
event->label());
|
2017-07-21 15:06:34 +00:00
|
|
|
} break;
|
|
|
|
case TimelineEvent::kEnd: {
|
2018-03-01 02:07:46 +00:00
|
|
|
length = Utils::SNPrint(buffer, buffer_size, "E");
|
2017-07-21 15:06:34 +00:00
|
|
|
} break;
|
|
|
|
case TimelineEvent::kCounter: {
|
|
|
|
if (event->arguments_length() > 0) {
|
|
|
|
// We only report the first counter value.
|
2018-03-01 02:07:46 +00:00
|
|
|
length = Utils::SNPrint(buffer, buffer_size, "C|%" Pd64 "|%s|%s", pid,
|
|
|
|
event->label(), event->arguments()[0].value);
|
2017-07-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Ignore event types that we cannot serialize to the Systrace format.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2018-04-02 18:34:35 +00:00
|
|
|
void TimelineEventSystraceRecorder::OnEvent(TimelineEvent* event) {
|
2017-07-21 15:06:34 +00:00
|
|
|
if (event == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-02 18:34:35 +00:00
|
|
|
if (systrace_fd_ < 0) {
|
|
|
|
return;
|
2017-07-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-02 18:34:35 +00:00
|
|
|
// Serialize to the systrace format.
|
|
|
|
const intptr_t kBufferLength = 1024;
|
|
|
|
char buffer[kBufferLength];
|
|
|
|
const intptr_t event_length = PrintSystrace(event, &buffer[0], kBufferLength);
|
|
|
|
if (event_length > 0) {
|
|
|
|
ssize_t result;
|
|
|
|
// Repeatedly attempt the write while we are being interrupted.
|
|
|
|
do {
|
|
|
|
result = write(systrace_fd_, buffer, event_length);
|
|
|
|
} while ((result == -1L) && (errno == EINTR));
|
2017-07-21 15:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dart
|
|
|
|
|
|
|
|
#endif // defined(HOST_OS_LINUX) && !defined(PRODUCT)
|