From d026bc360e5ac6ad8b20ee78a8f687b85c7a4708 Mon Sep 17 00:00:00 2001 From: Yuqian Li Date: Tue, 28 Jul 2020 10:29:59 -0700 Subject: [PATCH] Add purge-persistent-cache (flutter/engine#20013) For https://github.com/flutter/flutter/issues/39526 --- engine/src/flutter/common/settings.cc | 1 + engine/src/flutter/common/settings.h | 1 + .../flutter/shell/common/persistent_cache.cc | 20 ++++++++++++ .../flutter/shell/common/persistent_cache.h | 4 +++ .../common/persistent_cache_unittests.cc | 32 +++++++++++++++++++ engine/src/flutter/shell/common/shell.cc | 4 +++ engine/src/flutter/shell/common/switches.cc | 3 ++ engine/src/flutter/shell/common/switches.h | 4 +++ .../flutter/app/FlutterActivityDelegate.java | 3 ++ .../embedding/engine/FlutterShellArgs.java | 5 +++ 10 files changed, 77 insertions(+) diff --git a/engine/src/flutter/common/settings.cc b/engine/src/flutter/common/settings.cc index c6a0d8a32a..1508e213bc 100644 --- a/engine/src/flutter/common/settings.cc +++ b/engine/src/flutter/common/settings.cc @@ -41,6 +41,7 @@ std::string Settings::ToString() const { stream << "dump_skp_on_shader_compilation: " << dump_skp_on_shader_compilation << std::endl; stream << "cache_sksl: " << cache_sksl << std::endl; + stream << "purge_persistent_cache: " << purge_persistent_cache << std::endl; stream << "endless_trace_buffer: " << endless_trace_buffer << std::endl; stream << "enable_dart_profiling: " << enable_dart_profiling << std::endl; stream << "disable_dart_asserts: " << disable_dart_asserts << std::endl; diff --git a/engine/src/flutter/common/settings.h b/engine/src/flutter/common/settings.h index bf61537963..15166b8d38 100644 --- a/engine/src/flutter/common/settings.h +++ b/engine/src/flutter/common/settings.h @@ -97,6 +97,7 @@ struct Settings { bool trace_systrace = false; bool dump_skp_on_shader_compilation = false; bool cache_sksl = false; + bool purge_persistent_cache = false; bool endless_trace_buffer = false; bool enable_dart_profiling = false; bool disable_dart_asserts = false; diff --git a/engine/src/flutter/shell/common/persistent_cache.cc b/engine/src/flutter/shell/common/persistent_cache.cc index 993e8c73f2..a3c626c205 100644 --- a/engine/src/flutter/shell/common/persistent_cache.cc +++ b/engine/src/flutter/shell/common/persistent_cache.cc @@ -4,6 +4,7 @@ #include "flutter/shell/common/persistent_cache.h" +#include #include #include #include @@ -78,6 +79,25 @@ void PersistentCache::SetCacheDirectoryPath(std::string path) { cache_base_path_ = path; } +bool PersistentCache::Purge() { + // Make sure that this is called after the worker task runner setup so all the + // file system modifications would happen on that single thread to avoid + // racing. + FML_CHECK(GetWorkerTaskRunner()); + + std::promise removed; + GetWorkerTaskRunner()->PostTask( + [&removed, cache_directory = cache_directory_]() { + if (cache_directory->is_valid()) { + FML_LOG(INFO) << "Purge persistent cache."; + removed.set_value(RemoveFilesInDirectory(*cache_directory)); + } else { + removed.set_value(false); + } + }); + return removed.get_future().get(); +} + namespace { constexpr char kEngineComponent[] = "flutter_engine"; diff --git a/engine/src/flutter/shell/common/persistent_cache.h b/engine/src/flutter/shell/common/persistent_cache.h index b920a30e52..905eb9b55f 100644 --- a/engine/src/flutter/shell/common/persistent_cache.h +++ b/engine/src/flutter/shell/common/persistent_cache.h @@ -58,6 +58,10 @@ class PersistentCache : public GrContextOptions::PersistentCache { bool IsDumpingSkp() const { return is_dumping_skp_; } void SetIsDumpingSkp(bool value) { is_dumping_skp_ = value; } + // Remove all files inside the persistent cache directory. + // Return whether the purge is successful. + bool Purge(); + // |GrContextOptions::PersistentCache| sk_sp load(const SkData& key) override; diff --git a/engine/src/flutter/shell/common/persistent_cache_unittests.cc b/engine/src/flutter/shell/common/persistent_cache_unittests.cc index cae8d18fb9..4ec0506043 100644 --- a/engine/src/flutter/shell/common/persistent_cache_unittests.cc +++ b/engine/src/flutter/shell/common/persistent_cache_unittests.cc @@ -240,5 +240,37 @@ TEST_F(ShellTest, CanRemoveOldPersistentCache) { fml::RemoveFilesInDirectory(base_dir.fd()); } +TEST_F(ShellTest, CanPurgePersistentCache) { + fml::ScopedTemporaryDirectory base_dir; + ASSERT_TRUE(base_dir.fd().is_valid()); + auto cache_dir = fml::CreateDirectory( + base_dir.fd(), + {"flutter_engine", GetFlutterEngineVersion(), "skia", GetSkiaVersion()}, + fml::FilePermission::kReadWrite); + PersistentCache::SetCacheDirectoryPath(base_dir.path()); + PersistentCache::ResetCacheForProcess(); + + // Generate a dummy persistent cache. + fml::DataMapping test_data(std::string("test")); + ASSERT_TRUE(fml::WriteAtomically(cache_dir, "test", test_data)); + auto file = fml::OpenFileReadOnly(cache_dir, "test"); + ASSERT_TRUE(file.is_valid()); + + // Run engine with purge_persistent_cache to remove the dummy cache. + auto settings = CreateSettingsForFixture(); + settings.purge_persistent_cache = true; + auto config = RunConfiguration::InferFromSettings(settings); + std::unique_ptr shell = CreateShell(settings); + RunEngine(shell.get(), std::move(config)); + + // Verify that the dummy is purged. + file = fml::OpenFileReadOnly(cache_dir, "test"); + ASSERT_FALSE(file.is_valid()); + + // Cleanup + fml::RemoveFilesInDirectory(base_dir.fd()); + DestroyShell(std::move(shell)); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/common/shell.cc b/engine/src/flutter/shell/common/shell.cc index 5467ff99e4..9137ebcd94 100644 --- a/engine/src/flutter/shell/common/shell.cc +++ b/engine/src/flutter/shell/common/shell.cc @@ -566,6 +566,10 @@ bool Shell::Setup(std::unique_ptr platform_view, PersistentCache::GetCacheForProcess()->SetIsDumpingSkp( settings_.dump_skp_on_shader_compilation); + if (settings_.purge_persistent_cache) { + PersistentCache::GetCacheForProcess()->Purge(); + } + // TODO(gw280): The WeakPtr here asserts that we are derefing it on the // same thread as it was created on. Shell is constructed on the platform // thread but we need to call into the Engine on the UI thread, so we need diff --git a/engine/src/flutter/shell/common/switches.cc b/engine/src/flutter/shell/common/switches.cc index ea7d523187..e2f83f3f9c 100644 --- a/engine/src/flutter/shell/common/switches.cc +++ b/engine/src/flutter/shell/common/switches.cc @@ -394,6 +394,9 @@ Settings SettingsFromCommandLine(const fml::CommandLine& command_line) { settings.cache_sksl = command_line.HasOption(FlagForSwitch(Switch::CacheSkSL)); + settings.purge_persistent_cache = + command_line.HasOption(FlagForSwitch(Switch::PurgePersistentCache)); + return settings; } diff --git a/engine/src/flutter/shell/common/switches.h b/engine/src/flutter/shell/common/switches.h index 8506bc7e72..9b91355c33 100644 --- a/engine/src/flutter/shell/common/switches.h +++ b/engine/src/flutter/shell/common/switches.h @@ -148,6 +148,10 @@ DEF_SWITCH(CacheSkSL, "should only be used during development phases. The generated SkSLs " "can later be used in the release build for shader precompilation " "at launch in order to eliminate the shader-compile jank.") +DEF_SWITCH(PurgePersistentCache, + "purge-persistent-cache", + "Remove all existing persistent cache. This is mainly for debugging " + "purposes such as reproducing the shader compilation jank.") DEF_SWITCH( TraceSystrace, "trace-systrace", 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 236344d327..b1619baebf 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 @@ -307,6 +307,9 @@ public final class FlutterActivityDelegate if (intent.getBooleanExtra("cache-sksl", false)) { args.add("--cache-sksl"); } + if (intent.getBooleanExtra("purge-persistent-cache", false)) { + args.add("--purge-persistent-cache"); + } if (intent.getBooleanExtra("verbose-logging", false)) { args.add("--verbose-logging"); } 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 004035bb9e..9f0c4eef07 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 @@ -46,6 +46,8 @@ public class FlutterShellArgs { "--dump-skp-on-shader-compilation"; public static final String ARG_KEY_CACHE_SKSL = "cache-sksl"; public static final String ARG_CACHE_SKSL = "--cache-sksl"; + public static final String ARG_KEY_PURGE_PERSISTENT_CACHE = "purge-persistent-cache"; + public static final String ARG_PURGE_PERSISTENT_CACHE = "--purge-persistent-cache"; public static final String ARG_KEY_VERBOSE_LOGGING = "verbose-logging"; public static final String ARG_VERBOSE_LOGGING = "--verbose-logging"; public static final String ARG_KEY_OBSERVATORY_PORT = "observatory-port"; @@ -102,6 +104,9 @@ public class FlutterShellArgs { if (intent.getBooleanExtra(ARG_KEY_CACHE_SKSL, false)) { args.add(ARG_CACHE_SKSL); } + if (intent.getBooleanExtra(ARG_KEY_PURGE_PERSISTENT_CACHE, false)) { + args.add(ARG_PURGE_PERSISTENT_CACHE); + } if (intent.getBooleanExtra(ARG_KEY_VERBOSE_LOGGING, false)) { args.add(ARG_VERBOSE_LOGGING); }