Skip to content

Commit e64b0f0

Browse files
authored
Avoid calling Dart_TimelineGetMicros when systrace is enabled (flutter#32968)
1 parent 561f962 commit e64b0f0

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

fml/trace_event.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ bool TraceHasTimelineEventHandler() {
5656
gTimelineEventHandler.load(std::memory_order_relaxed));
5757
}
5858

59+
int64_t TraceGetTimelineMicros() {
60+
return gTimelineMicrosSource.load()();
61+
}
62+
5963
void TraceSetTimelineMicrosSource(TimelineMicrosSource source) {
6064
gTimelineMicrosSource = source;
6165
}
@@ -316,6 +320,10 @@ bool TraceHasTimelineEventHandler() {
316320
return false;
317321
}
318322

323+
int64_t TraceGetTimelineMicros() {
324+
return -1;
325+
}
326+
319327
void TraceSetTimelineMicrosSource(TimelineMicrosSource source) {}
320328

321329
size_t TraceNonce() {

fml/trace_event.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ bool TraceHasTimelineEventHandler();
163163

164164
void TraceSetTimelineMicrosSource(TimelineMicrosSource source);
165165

166+
int64_t TraceGetTimelineMicros();
167+
166168
void TraceTimelineEvent(TraceArg category_group,
167169
TraceArg name,
168170
int64_t timestamp_micros,

runtime/dart_vm.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ DartVM::DartVM(std::shared_ptr<const DartVMData> vm_data,
463463
params.entropy_source = dart::bin::GetEntropy;
464464
params.get_service_assets = GetVMServiceAssetsArchiveCallback;
465465
DartVMInitializer::Initialize(&params,
466-
settings_.enable_timeline_event_handler);
466+
settings_.enable_timeline_event_handler,
467+
settings_.trace_systrace);
467468
// Send the earliest available timestamp in the application lifecycle to
468469
// timeline. The difference between this timestamp and the time we render
469470
// the very first frame gives us a good idea about Flutter's startup time.

runtime/dart_vm_initializer.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ void ReportUnhandledException(Dart_Handle exception_handle,
8080
} // namespace
8181

8282
void DartVMInitializer::Initialize(Dart_InitializeParams* params,
83-
bool enable_timeline_event_handler) {
83+
bool enable_timeline_event_handler,
84+
bool trace_systrace) {
8485
FML_DCHECK(!gDartInitialized);
8586

8687
char* error = Dart_Initialize(params);
@@ -92,7 +93,16 @@ void DartVMInitializer::Initialize(Dart_InitializeParams* params,
9293
}
9394

9495
if (enable_timeline_event_handler) {
95-
fml::tracing::TraceSetTimelineMicrosSource(Dart_TimelineGetMicros);
96+
if (!trace_systrace) {
97+
// Systrace on all platforms except Fuchsia ignores the timestamp provided
98+
// here. On Android in particular, calls to get the system clock show up
99+
// in profiles.
100+
// Fuchsia does not use the TraceSetTimelineMicrosSource.
101+
fml::tracing::TraceSetTimelineMicrosSource(Dart_TimelineGetMicros);
102+
} else {
103+
fml::tracing::TraceSetTimelineMicrosSource(
104+
[]() -> int64_t { return -1; });
105+
}
96106
fml::tracing::TraceSetTimelineEventHandler(LogDartTimelineEvent);
97107
}
98108

runtime/dart_vm_initializer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
class DartVMInitializer {
1212
public:
1313
static void Initialize(Dart_InitializeParams* params,
14-
bool enable_timeline_event_handler);
14+
bool enable_timeline_event_handler,
15+
bool trace_systrace);
1516
static void Cleanup();
1617

1718
private:

runtime/dart_vm_unittests.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,13 @@ TEST_F(DartVMTest, DisableTimelineEventHandler) {
5252
ASSERT_FALSE(fml::tracing::TraceHasTimelineEventHandler());
5353
}
5454

55+
TEST_F(DartVMTest, TraceGetTimelineMicrosDoesNotGetClockWhenSystraceIsEnabled) {
56+
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
57+
auto settings = CreateSettingsForFixture();
58+
settings.trace_systrace = true;
59+
auto vm = DartVMRef::Create(settings);
60+
ASSERT_EQ(-1, fml::tracing::TraceGetTimelineMicros());
61+
}
62+
5563
} // namespace testing
5664
} // namespace flutter

0 commit comments

Comments
 (0)