Skip to content

Commit

Permalink
Disable the timeline in release mode on Android unless systrace is en…
Browse files Browse the repository at this point in the history
…abled (flutter#32909)
  • Loading branch information
jason-simmons authored Apr 26, 2022
1 parent 0b08b62 commit 9570ba3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ struct Settings {
std::optional<std::vector<std::string>> trace_skia_allowlist;
bool trace_startup = false;
bool trace_systrace = false;
bool enable_timeline_event_handler = true;
bool dump_skp_on_shader_compilation = false;
bool cache_sksl = false;
bool purge_persistent_cache = false;
Expand Down
9 changes: 9 additions & 0 deletions fml/trace_event.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ void TraceSetTimelineEventHandler(TimelineEventHandler handler) {
gTimelineEventHandler = handler;
}

bool TraceHasTimelineEventHandler() {
return static_cast<bool>(
gTimelineEventHandler.load(std::memory_order_relaxed));
}

void TraceSetTimelineMicrosSource(TimelineMicrosSource source) {
gTimelineMicrosSource = source;
}
Expand Down Expand Up @@ -307,6 +312,10 @@ void TraceSetAllowlist(const std::vector<std::string>& allowlist) {}

void TraceSetTimelineEventHandler(TimelineEventHandler handler) {}

bool TraceHasTimelineEventHandler() {
return false;
}

void TraceSetTimelineMicrosSource(TimelineMicrosSource source) {}

size_t TraceNonce() {
Expand Down
2 changes: 2 additions & 0 deletions fml/trace_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ using TimelineMicrosSource = int64_t (*)();

void TraceSetTimelineEventHandler(TimelineEventHandler handler);

bool TraceHasTimelineEventHandler();

void TraceSetTimelineMicrosSource(TimelineMicrosSource source);

void TraceTimelineEvent(TraceArg category_group,
Expand Down
3 changes: 2 additions & 1 deletion runtime/dart_vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@ DartVM::DartVM(std::shared_ptr<const DartVMData> vm_data,
params.thread_exit = ThreadExitCallback;
params.get_service_assets = GetVMServiceAssetsArchiveCallback;
params.entropy_source = dart::bin::GetEntropy;
DartVMInitializer::Initialize(&params);
DartVMInitializer::Initialize(&params,
settings_.enable_timeline_event_handler);
// Send the earliest available timestamp in the application lifecycle to
// timeline. The difference between this timestamp and the time we render
// the very first frame gives us a good idea about Flutter's startup time.
Expand Down
10 changes: 7 additions & 3 deletions runtime/dart_vm_initializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ void ReportUnhandledException(Dart_Handle exception_handle,
}
} // namespace

void DartVMInitializer::Initialize(Dart_InitializeParams* params) {
void DartVMInitializer::Initialize(Dart_InitializeParams* params,
bool enable_timeline_event_handler) {
FML_DCHECK(!gDartInitialized);

char* error = Dart_Initialize(params);
Expand All @@ -90,9 +91,12 @@ void DartVMInitializer::Initialize(Dart_InitializeParams* params) {
gDartInitialized = true;
}

if (enable_timeline_event_handler) {
fml::tracing::TraceSetTimelineMicrosSource(Dart_TimelineGetMicros);
fml::tracing::TraceSetTimelineEventHandler(LogDartTimelineEvent);
}

fml::TimePoint::SetClockSource(flutter::DartTimelineTicksSinceEpoch);
fml::tracing::TraceSetTimelineEventHandler(LogDartTimelineEvent);
fml::tracing::TraceSetTimelineMicrosSource(Dart_TimelineGetMicros);
tonic::SetUnhandledExceptionReporter(&ReportUnhandledException);
}

Expand Down
3 changes: 2 additions & 1 deletion runtime/dart_vm_initializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

class DartVMInitializer {
public:
static void Initialize(Dart_InitializeParams* params);
static void Initialize(Dart_InitializeParams* params,
bool enable_timeline_event_handler);
static void Cleanup();

private:
Expand Down
9 changes: 9 additions & 0 deletions runtime/dart_vm_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,14 @@ TEST_F(DartVMTest, OldGenHeapSize) {
ASSERT_TRUE(vm);
}

TEST_F(DartVMTest, DisableTimelineEventHandler) {
ASSERT_FALSE(DartVMRef::IsInstanceRunning());
fml::tracing::TraceSetTimelineEventHandler(nullptr);
auto settings = CreateSettingsForFixture();
settings.enable_timeline_event_handler = false;
auto vm = DartVMRef::Create(settings);
ASSERT_FALSE(fml::tracing::TraceHasTimelineEventHandler());
}

} // namespace testing
} // namespace flutter
6 changes: 6 additions & 0 deletions shell/platform/android/flutter_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ void FlutterMain::Init(JNIEnv* env,
}
}

#if FLUTTER_RELEASE
// On most platforms the timeline is always disabled in release mode.
// On Android, enable it in release mode only when using systrace.
settings.enable_timeline_event_handler = settings.trace_systrace;
#endif // FLUTTER_RELEASE

int64_t init_time_micros = initTimeMillis * 1000;
settings.engine_start_timestamp =
std::chrono::microseconds(Dart_TimelineGetMicros() - init_time_micros);
Expand Down

0 comments on commit 9570ba3

Please sign in to comment.