From 6a088c7a02ab90c3c54b2fde56067123db971b7b Mon Sep 17 00:00:00 2001 From: Derek Xu Date: Tue, 12 Sep 2023 11:35:10 -0400 Subject: [PATCH] Add trace-to-file switch (flutter/engine#45553) --- engine/src/flutter/common/settings.h | 1 + engine/src/flutter/runtime/dart_vm.cc | 14 ++++++++++++++ engine/src/flutter/shell/common/switches.cc | 3 +++ engine/src/flutter/shell/common/switches.h | 5 +++++ .../src/flutter/shell/common/switches_unittests.cc | 8 ++++++++ .../io/flutter/app/FlutterActivityDelegate.java | 3 +++ .../flutter/embedding/engine/FlutterShellArgs.java | 5 +++++ 7 files changed, 39 insertions(+) diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index d9df7e707b..eb07585a33 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -146,6 +146,7 @@ struct Settings { std::optional> trace_skia_allowlist; bool trace_startup = false; bool trace_systrace = false; + std::string trace_to_file; bool enable_timeline_event_handler = true; bool dump_skp_on_shader_compilation = false; bool cache_sksl = false; diff --git a/engine/src/flutter/runtime/dart_vm.cc b/engine/src/flutter/runtime/dart_vm.cc index fb13492143..48715c5cc2 100644 --- a/engine/src/flutter/runtime/dart_vm.cc +++ b/engine/src/flutter/runtime/dart_vm.cc @@ -99,6 +99,12 @@ static const char* kDartSystraceTraceBufferArgs[] = { "--systrace_timeline", }; +static std::string DartFileRecorderArgs(const std::string& path) { + std::ostringstream oss; + oss << "--timeline_recorder=perfettofile:" << path; + return oss.str(); +} + FML_ALLOW_UNUSED_TYPE static const char* kDartDefaultTraceStreamsArgs[]{ "--timeline_streams=Dart,Embedder,GC", @@ -390,6 +396,14 @@ DartVM::DartVM(const std::shared_ptr& vm_data, fml::size(kDartSystraceTraceStreamsArgs)); } + std::string file_recorder_args; + if (!settings_.trace_to_file.empty()) { + file_recorder_args = DartFileRecorderArgs(settings_.trace_to_file); + args.push_back(file_recorder_args.c_str()); + PushBackAll(&args, kDartSystraceTraceStreamsArgs, + fml::size(kDartSystraceTraceStreamsArgs)); + } + if (settings_.trace_startup) { PushBackAll(&args, kDartStartupTraceStreamsArgs, fml::size(kDartStartupTraceStreamsArgs)); diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index e0d4ae3d58..f9cbb548cc 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -354,6 +354,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { settings.trace_systrace = command_line.HasOption(FlagForSwitch(Switch::TraceSystrace)); + command_line.GetOptionValue(FlagForSwitch(Switch::TraceToFile), + &settings.trace_to_file); + settings.skia_deterministic_rendering_on_cpu = command_line.HasOption(FlagForSwitch(Switch::SkiaDeterministicRendering)); diff --git a/engine/src/flutter/shell/common/switches.h b/engine/src/flutter/shell/common/switches.h index c8a75f7786..7dd04fc7e1 100644 --- a/engine/src/flutter/shell/common/switches.h +++ b/engine/src/flutter/shell/common/switches.h @@ -197,6 +197,11 @@ DEF_SWITCH( "Trace to the system tracer (instead of the timeline) on platforms where " "such a tracer is available. Currently only supported on Android and " "Fuchsia.") +DEF_SWITCH(TraceToFile, + "trace-to-file", + "Write the timeline trace to a file at the specified path. The file " + "will be in Perfetto's proto format; it will be possible to load " + "the file into Perfetto's trace viewer.") DEF_SWITCH(UseTestFonts, "use-test-fonts", "Running tests that layout and measure text will not yield " diff --git a/engine/src/flutter/shell/common/switches_unittests.cc b/engine/src/flutter/shell/common/switches_unittests.cc index 0307b9d62b..617c0a891c 100644 --- a/engine/src/flutter/shell/common/switches_unittests.cc +++ b/engine/src/flutter/shell/common/switches_unittests.cc @@ -51,6 +51,14 @@ TEST(SwitchesTest, SkiaTraceAllowlistFlag) { #endif } +TEST(SwitchesTest, TraceToFile) { + fml::CommandLine command_line = fml::CommandLineFromInitializerList( + {"command", "--trace-to-file=trace.binpb"}); + EXPECT_TRUE(command_line.HasOption("trace-to-file")); + Settings settings = SettingsFromCommandLine(command_line); + EXPECT_EQ(settings.trace_to_file, "trace.binpb"); +} + TEST(SwitchesTest, RouteParsedFlag) { fml::CommandLine command_line = fml::CommandLineFromInitializerList({"command", "--route=/animation"}); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java index bcc54b1e97..b57e20c061 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/app/FlutterActivityDelegate.java @@ -314,6 +314,9 @@ public final class FlutterActivityDelegate if (intent.getBooleanExtra("trace-systrace", false)) { args.add("--trace-systrace"); } + if (intent.hasExtra("trace-to-file")) { + args.add("--trace-to-file=" + intent.getStringExtra("trace-to-file")); + } if (intent.getBooleanExtra("dump-skp-on-shader-compilation", false)) { args.add("--dump-skp-on-shader-compilation"); } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterShellArgs.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterShellArgs.java index e97eb66bfc..b3775217a8 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterShellArgs.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterShellArgs.java @@ -42,6 +42,8 @@ public class FlutterShellArgs { public static final String ARG_TRACE_SKIA_ALLOWLIST = "--trace-skia-allowlist="; public static final String ARG_KEY_TRACE_SYSTRACE = "trace-systrace"; public static final String ARG_TRACE_SYSTRACE = "--trace-systrace"; + public static final String ARG_KEY_TRACE_TO_FILE = "trace-to-file"; + public static final String ARG_TRACE_TO_FILE = "--trace-to-file"; public static final String ARG_KEY_ENABLE_IMPELLER = "enable-impeller"; public static final String ARG_ENABLE_IMPELLER = "--enable-impeller"; public static final String ARG_KEY_ENABLE_VULKAN_VALIDATION = "enable-vulkan-validation"; @@ -120,6 +122,9 @@ public class FlutterShellArgs { if (intent.getBooleanExtra(ARG_KEY_TRACE_SYSTRACE, false)) { args.add(ARG_TRACE_SYSTRACE); } + if (intent.hasExtra(ARG_KEY_TRACE_TO_FILE)) { + args.add(ARG_TRACE_TO_FILE + "=" + intent.getStringExtra(ARG_KEY_TRACE_TO_FILE)); + } if (intent.getBooleanExtra(ARG_KEY_ENABLE_IMPELLER, false)) { args.add(ARG_ENABLE_IMPELLER); }