Skip to content

Commit

Permalink
Automatically prune the fetch cache after fetches
Browse files Browse the repository at this point in the history
Keeps the fetch cache from growing unbounded.

Test: # populate cache if necessary
Test: cvd fetch --target_directory=/tmp/cvd/fetch_test
--default_build=12924704,12924909,12924919
Test: cvd cache info
Test: cvd fetch --target_directory=/tmp/cvd/fetch_test2
--default_build=12924921 --max_cache_size_GB=2
Test: # only prunes when caching is enabled
Test: cvd fetch --target_directory=/tmp/cvd/fetch_test3
--default_build=12924578 --max_cache_size_GB=0 --enable_caching=false
Test: cvd cache info
  • Loading branch information
cjreynol committed Jan 16, 2025
1 parent 40fd1fd commit c581683
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions base/cvd/cuttlefish/host/commands/cvd/cache/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace cuttlefish {

inline constexpr std::size_t kDefaultCacheSizeGb = 25;

Result<std::string> EmptyCache(const std::string& cache_directory);
Result<std::string> GetCacheInfo(const std::string& cache_directory,
bool json_formatted);
Expand Down
14 changes: 6 additions & 8 deletions base/cvd/cuttlefish/host/commands/cvd/cli/commands/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ namespace cuttlefish {

namespace {

constexpr int kDefaultCacheSizeGB = 25;

constexpr char kSummaryHelpText[] = "Manage the files cached by cvd";

enum class Action {
Expand All @@ -51,7 +49,7 @@ enum class Action {

struct CacheArguments {
Action action = Action::Info;
std::size_t allowed_size_GB = kDefaultCacheSizeGB;
std::size_t allowed_size_gb = kDefaultCacheSizeGb;
bool json_formatted = false;
};

Expand Down Expand Up @@ -82,7 +80,7 @@ Result<CacheArguments> ProcessArguments(
};

std::vector<Flag> flags;
flags.emplace_back(GflagsCompatFlag("allowed_size_GB", result.allowed_size_GB)
flags.emplace_back(GflagsCompatFlag("allowed_size_gb", result.allowed_size_gb)
.Help("Allowed size of the cache during prune "
"operation, in gigabytes."));
flags.emplace_back(GflagsCompatFlag("json", result.json_formatted)
Expand Down Expand Up @@ -122,9 +120,9 @@ Result<void> CvdCacheCommandHandler::Handle(const CommandRequest& request) {
break;
case Action::Prune:
std::cout << CF_EXPECTF(
PruneCache(cache_directory, arguments.allowed_size_GB),
PruneCache(cache_directory, arguments.allowed_size_gb),
"Error pruning cache at {} to {}GB", cache_directory,
arguments.allowed_size_GB);
arguments.allowed_size_gb);
break;
}

Expand All @@ -146,13 +144,13 @@ Example usage:
cvd cache info --json - the same as above, but in JSON format
cvd cache prune - caps the cache at the default size ({}GB)
cvd cache prune --allowed_size_GB=<n> - caps the cache at the given size
cvd cache prune --allowed_size_gb=<n> - caps the cache at the given size
**Notes**:
- info and prune round the cache size up to the nearest gigabyte
- prune uses last modification time to remove oldest files first
)",
kDefaultCacheSizeGB);
kDefaultCacheSizeGb);
}

} // namespace
Expand Down
12 changes: 11 additions & 1 deletion base/cvd/cuttlefish/host/commands/cvd/cli/commands/fetch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
#include "common/libs/utils/result.h"
#include "common/libs/utils/subprocess.h"
#include "common/libs/utils/tee_logging.h"
#include "host/commands/cvd/cache/cache.h"
#include "host/commands/cvd/cli/commands/command_handler.h"
#include "host/commands/cvd/cli/types.h"
#include "host/commands/cvd/fetch/fetch_cvd.h"
#include "host/commands/cvd/fetch/fetch_cvd_parser.h"
#include "host/commands/cvd/utils/common.h"

namespace cuttlefish {

Expand Down Expand Up @@ -60,7 +62,15 @@ Result<void> CvdFetchCommandHandler::Handle(const CommandRequest& request) {
ScopedTeeLogger logger(
LogToStderrAndFiles({log_file}, "", metadata_level, flags.verbosity));

CF_EXPECT(FetchCvdMain(flags));
Result<void> result = FetchCvdMain(flags);
if (flags.build_api_flags.enable_caching) {
const std::string cache_directory = PerUserCacheDir();
LOG(INFO) << CF_EXPECTF(
PruneCache(cache_directory, flags.build_api_flags.max_cache_size_gb),
"Error pruning cache at {} to {}GB", cache_directory,
flags.build_api_flags.max_cache_size_gb);
}
CF_EXPECT(std::move(result));
return {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ std::vector<Flag> GetFlagsVector(FetchFlags& fetch_flags,
flags.emplace_back(
GflagsCompatFlag("enable_caching", build_api_flags.enable_caching)
.Help("Whether to enable local fetch file caching or not"));
flags.emplace_back(
GflagsCompatFlag("max_cache_size_gb", build_api_flags.max_cache_size_gb)
.Help("Max allowed size(in gigabytes) of the local fetch file cache. "
" If the cache grows beyond this size it will be pruned after "
"the fetches complete."));

CredentialFlags& credential_flags = build_api_flags.credential_flags;
flags.emplace_back(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
#pragma once

#include <chrono>
#include <cstddef>
#include <optional>
#include <string>
#include <vector>

#include <android-base/logging.h>

#include "common/libs/utils/result.h"
#include "host/commands/cvd/cache/cache.h"
#include "host/libs/web/android_build_api.h"
#include "host/libs/web/android_build_string.h"
#include "host/libs/web/cas/cas_downloader.h"
Expand Down Expand Up @@ -69,6 +71,7 @@ struct BuildApiFlags {
bool external_dns_resolver = kDefaultExternalDnsResolver;
std::string api_base_url = kAndroidBuildServiceUrl;
bool enable_caching = kDefaultEnableCaching;
std::size_t max_cache_size_gb = kDefaultCacheSizeGb;
CasDownloaderFlags cas_downloader_flags;
};

Expand Down

0 comments on commit c581683

Please sign in to comment.