Skip to content

Commit 68bfcdd

Browse files
committed
Separate the cache logic from message formatting
Allows the callsites to decide what to output. Specifically, the auto prune in fetching can now omit the message if no files are removed. 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 Test: cvd cache empty
1 parent 0d11bdd commit 68bfcdd

File tree

4 files changed

+60
-36
lines changed

4 files changed

+60
-36
lines changed

base/cvd/cuttlefish/host/commands/cvd/cache/cache.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
#include <android-base/strings.h>
2828
#include <fmt/format.h>
29-
#include <json/json.h>
3029

3130
#include "common/libs/utils/files.h"
3231
#include "common/libs/utils/result.h"
@@ -70,37 +69,32 @@ Result<std::vector<std::string>> CacheFilesDesc(
7069

7170
} // namespace
7271

73-
Result<std::string> EmptyCache(const std::string& cache_directory) {
72+
Result<void> EmptyCache(const std::string& cache_directory) {
7473
CF_EXPECT(EnsureDirectoryExists(cache_directory));
7574
CF_EXPECT(RecursivelyRemoveDirectory(cache_directory));
7675
CF_EXPECT(EnsureDirectoryExists(cache_directory));
77-
return fmt::format("Cache at \"{}\" has been emptied\n", cache_directory);
76+
return {};
7877
}
7978

80-
Result<std::string> GetCacheInfo(const std::string& cache_directory,
81-
const bool json_formatted) {
79+
Result<std::size_t> GetCacheSize(const std::string& cache_directory) {
8280
CF_EXPECT(EnsureDirectoryExists(cache_directory));
83-
std::size_t cache_size = CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
84-
if (json_formatted) {
85-
Json::Value json_output(Json::objectValue);
86-
json_output["path"] = cache_directory;
87-
json_output["size_in_GB"] = std::to_string(cache_size);
88-
return json_output.toStyledString();
89-
}
90-
return fmt::format("path:{}\nsize in GB:{}\n", cache_directory, cache_size);
81+
return CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
9182
}
9283

93-
Result<std::string> PruneCache(const std::string& cache_directory,
94-
const std::size_t allowed_size_GB) {
84+
Result<PruneResult> PruneCache(const std::string& cache_directory,
85+
const std::size_t allowed_size_gb) {
9586
CF_EXPECT(EnsureDirectoryExists(cache_directory));
9687
std::size_t cache_size = CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
88+
PruneResult result{
89+
.before = cache_size,
90+
};
9791
// Descending because elements are removed from the back
9892
std::vector<std::string> cache_files =
9993
CF_EXPECT(CacheFilesDesc(cache_directory));
100-
while (cache_size > allowed_size_GB) {
94+
while (cache_size > allowed_size_gb) {
10195
CHECK(!cache_files.empty()) << fmt::format(
10296
"Cache size is {} of {}, but there are no more files for pruning.",
103-
cache_size, allowed_size_GB);
97+
cache_size, allowed_size_gb);
10498

10599
std::string next = cache_files.back();
106100
cache_files.pop_back();
@@ -109,8 +103,8 @@ Result<std::string> PruneCache(const std::string& cache_directory,
109103
CF_EXPECT(RecursivelyRemoveDirectory(next));
110104
cache_size = CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
111105
}
112-
return fmt::format("Cache at \"{}\": ~{}GB of {}GB max\n", cache_directory,
113-
cache_size, allowed_size_GB);
106+
result.after = cache_size;
107+
return result;
114108
}
115109

116110
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/cvd/cache/cache.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ namespace cuttlefish {
2525

2626
inline constexpr std::size_t kDefaultCacheSizeGb = 25;
2727

28-
Result<std::string> EmptyCache(const std::string& cache_directory);
29-
Result<std::string> GetCacheInfo(const std::string& cache_directory,
30-
bool json_formatted);
31-
Result<std::string> PruneCache(const std::string& cache_directory,
28+
struct PruneResult {
29+
std::size_t before;
30+
std::size_t after;
31+
};
32+
33+
Result<void> EmptyCache(const std::string& cache_directory);
34+
Result<std::size_t> GetCacheSize(const std::string& cache_directory);
35+
Result<PruneResult> PruneCache(const std::string& cache_directory,
3236
std::size_t allowed_size_GB);
3337

3438
} // namespace cuttlefish

base/cvd/cuttlefish/host/commands/cvd/cli/commands/cache.cpp

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
#include <cstddef>
2020
#include <memory>
21+
#include <sstream>
2122
#include <string>
2223
#include <string_view>
2324
#include <unordered_map>
2425
#include <vector>
2526

2627
#include <android-base/logging.h>
2728
#include <fmt/format.h>
29+
#include <json/json.h>
2830

2931
#include "common/libs/utils/files.h"
3032
#include "common/libs/utils/flag_parser.h"
@@ -109,21 +111,39 @@ Result<void> CvdCacheCommandHandler::Handle(const CommandRequest& request) {
109111
CF_EXPECT(ProcessArguments(request.SubcommandArguments()));
110112
std::string cache_directory = PerUserCacheDir();
111113
switch (arguments.action) {
112-
case Action::Empty:
113-
std::cout << CF_EXPECTF(EmptyCache(cache_directory),
114-
"Error emptying cache at {}", cache_directory);
114+
case Action::Empty: {
115+
CF_EXPECTF(EmptyCache(cache_directory), "Error emptying cache at {}",
116+
cache_directory);
117+
fmt::print("Cache at \"{}\" has been emptied\n", cache_directory);
115118
break;
116-
case Action::Info:
117-
std::cout << CF_EXPECTF(
118-
GetCacheInfo(cache_directory, arguments.json_formatted),
119-
"Error retrieving info of cache at {}", cache_directory);
119+
}
120+
case Action::Info: {
121+
const std::size_t cache_size =
122+
CF_EXPECTF(GetCacheSize(cache_directory),
123+
"Error retrieving size of cache at {}", cache_directory);
124+
if (arguments.json_formatted) {
125+
Json::Value json_output(Json::objectValue);
126+
json_output["path"] = cache_directory;
127+
json_output["size_in_GB"] = std::to_string(cache_size);
128+
fmt::print("{}", json_output.toStyledString());
129+
} else {
130+
fmt::print("path:{}\nsize in GB:{}\n", cache_directory, cache_size);
131+
}
120132
break;
121-
case Action::Prune:
122-
std::cout << CF_EXPECTF(
123-
PruneCache(cache_directory, arguments.allowed_size_gb),
124-
"Error pruning cache at {} to {}GB", cache_directory,
125-
arguments.allowed_size_gb);
133+
}
134+
case Action::Prune: {
135+
const PruneResult result =
136+
CF_EXPECTF(PruneCache(cache_directory, arguments.allowed_size_gb),
137+
"Error pruning cache at {} to {}GB", cache_directory,
138+
arguments.allowed_size_gb);
139+
if (result.before > result.after) {
140+
fmt::print("Cache pruned from {}GB down to {}GB\n", result.before,
141+
result.after);
142+
}
143+
fmt::print("Cache at \"{}\": ~{}GB of {}GB max\n", cache_directory,
144+
result.after, arguments.allowed_size_gb);
126145
break;
146+
}
127147
}
128148

129149
return {};

base/cvd/cuttlefish/host/commands/cvd/cli/commands/fetch.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ Result<void> CvdFetchCommandHandler::Handle(const CommandRequest& request) {
6565
Result<void> result = FetchCvdMain(flags);
6666
if (flags.build_api_flags.enable_caching) {
6767
const std::string cache_directory = PerUserCacheDir();
68-
LOG(INFO) << CF_EXPECTF(
68+
const PruneResult prune_result = CF_EXPECTF(
6969
PruneCache(cache_directory, flags.build_api_flags.max_cache_size_gb),
7070
"Error pruning cache at {} to {}GB", cache_directory,
7171
flags.build_api_flags.max_cache_size_gb);
72+
if (prune_result.before > prune_result.after) {
73+
LOG(INFO) << fmt::format(
74+
"Cache at \"{}\" pruned from ~{}GB to ~{}GB of {}GB max size",
75+
cache_directory, prune_result.before, prune_result.after,
76+
flags.build_api_flags.max_cache_size_gb);
77+
}
7278
}
7379
CF_EXPECT(std::move(result));
7480
return {};

0 commit comments

Comments
 (0)