Add purge-persistent-cache (flutter/engine#20013)

For https://github.com/flutter/flutter/issues/39526
This commit is contained in:
Yuqian Li
2020-07-28 10:29:59 -07:00
committed by GitHub
parent 8a56925414
commit d026bc360e
10 changed files with 77 additions and 0 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -4,6 +4,7 @@
#include "flutter/shell/common/persistent_cache.h"
#include <future>
#include <memory>
#include <string>
#include <string_view>
@@ -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<bool> 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";

View File

@@ -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<SkData> load(const SkData& key) override;

View File

@@ -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> 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

View File

@@ -566,6 +566,10 @@ bool Shell::Setup(std::unique_ptr<PlatformView> 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

View File

@@ -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;
}

View File

@@ -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",

View File

@@ -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");
}

View File

@@ -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);
}