Skip to content

Commit

Permalink
Factor out the cache logic for reuse
Browse files Browse the repository at this point in the history
For the automatic cache management, rather than call `cvd cache`
directly we will call `RunPrune` and bypass the `CommandHandler`.

Test: cvd cache info
Test: cvd cache prune
Test: cvd cache empty
  • Loading branch information
cjreynol committed Jan 14, 2025
1 parent 1df9f8d commit 7448680
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 83 deletions.
2 changes: 2 additions & 0 deletions base/cvd/cuttlefish/host/commands/cvd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ cc_library(
"acloud/config.cpp",
"acloud/converter.cpp",
"acloud/create_converter_parser.cpp",
"cache/cache.cpp",
"cli/command_request.cpp",
"cli/command_sequence.cpp",
"cli/commands/acloud_command.cpp",
Expand Down Expand Up @@ -150,6 +151,7 @@ cc_library(
"acloud/config.h",
"acloud/converter.h",
"acloud/create_converter_parser.h",
"cache/cache.h",
"cli/command_request.h",
"cli/command_sequence.h",
"cli/commands/acloud_command.h",
Expand Down
116 changes: 116 additions & 0 deletions base/cvd/cuttlefish/host/commands/cvd/cache/cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "host/commands/cvd/cache/cache.h"

#include <algorithm>
#include <chrono>
#include <cstddef>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include <android-base/strings.h>
#include <fmt/format.h>
#include <json/json.h>

#include "common/libs/utils/files.h"
#include "common/libs/utils/result.h"

namespace cuttlefish {

namespace {

Result<std::vector<std::string>> CacheFilesDesc(
const std::string& cache_directory) {
std::vector<std::string> contents = CF_EXPECTF(
DirectoryContentsPaths(cache_directory),
"Failure retrieving contents of directory at \"{}\"", cache_directory);

auto not_self_or_parent_directory = [](std::string_view filepath) {
return !android::base::EndsWith(filepath, ".") &&
!android::base::EndsWith(filepath, "..");
};
std::vector<std::string> filtered;
std::copy_if(contents.begin(), contents.end(), std::back_inserter(filtered),
not_self_or_parent_directory);

using ModTimePair =
std::pair<std::string, std::chrono::system_clock::time_point>;
std::vector<ModTimePair> to_sort;
for (const std::string& filename : filtered) {
to_sort.emplace_back(
std::pair(filename, CF_EXPECT(FileModificationTime(filename))));
}
std::sort(to_sort.begin(), to_sort.end(),
[](const ModTimePair& a, const ModTimePair& b) {
return a.second > b.second;
});

std::vector<std::string> result;
for (const ModTimePair& pair : to_sort) {
result.emplace_back(pair.first);
}
return result;
}

} // namespace

Result<std::string> EmptyCache(const std::string& cache_directory) {
CF_EXPECT(EnsureDirectoryExists(cache_directory));
CF_EXPECT(RecursivelyRemoveDirectory(cache_directory));
CF_EXPECT(EnsureDirectoryExists(cache_directory));
return fmt::format("Cache at \"{}\" has been emptied\n", cache_directory);
}

Result<std::string> GetCacheInfo(const std::string& cache_directory,
const bool json_formatted) {
CF_EXPECT(EnsureDirectoryExists(cache_directory));
std::size_t cache_size = CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
if (json_formatted) {
Json::Value json_output(Json::objectValue);
json_output["path"] = cache_directory;
json_output["size_in_GB"] = std::to_string(cache_size);
return json_output.toStyledString();
}
return fmt::format("path:{}\nsize in GB:{}\n", cache_directory, cache_size);
}

Result<std::string> PruneCache(const std::string& cache_directory,
const std::size_t allowed_size_GB) {
CF_EXPECT(EnsureDirectoryExists(cache_directory));
std::size_t cache_size = CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
// Descending because elements are removed from the back
std::vector<std::string> cache_files =
CF_EXPECT(CacheFilesDesc(cache_directory));
while (cache_size > allowed_size_GB) {
CHECK(!cache_files.empty()) << fmt::format(
"Cache size is {} of {}, but there are no more files for pruning.",
cache_size, allowed_size_GB);

std::string next = cache_files.back();
cache_files.pop_back();
LOG(DEBUG) << fmt::format("Deleting \"{}\" for prune", next);
// handles removal of non-directory top-level files as well
CF_EXPECT(RecursivelyRemoveDirectory(next));
cache_size = CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
}
return fmt::format("Cache at \"{}\": ~{}GB of {}GB max\n", cache_directory,
cache_size, allowed_size_GB);
}

} // namespace cuttlefish
30 changes: 30 additions & 0 deletions base/cvd/cuttlefish/host/commands/cvd/cache/cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <cstddef>
#include <string>

#include "common/libs/utils/result.h"

namespace cuttlefish {

Result<std::string> EmptyCache(const std::string& cache_directory);
Result<std::string> GetCacheInfo(const std::string& cache_directory,
bool json_formatted);
Result<std::string> PruneCache(const std::string& cache_directory,
std::size_t allowed_size_GB);

} // namespace cuttlefish
88 changes: 5 additions & 83 deletions base/cvd/cuttlefish/host/commands/cvd/cli/commands/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,29 @@

#include "host/commands/cvd/cli/commands/cache.h"

#include <algorithm>
#include <chrono>
#include <cstddef>
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>

#include <android-base/logging.h>
#include <android-base/strings.h>
#include <fmt/format.h>
#include <json/json.h>

#include "common/libs/utils/files.h"
#include "common/libs/utils/flag_parser.h"
#include "common/libs/utils/result.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/utils/common.h"

namespace cuttlefish {

namespace {

constexpr int kDefaultCacheSizeGB = 25;

constexpr char kSummaryHelpText[] = "Manage the files cached by cvd";
Expand Down Expand Up @@ -98,79 +95,6 @@ Result<CacheArguments> ProcessArguments(
return result;
}

Result<std::string> RunEmpty(const std::string& cache_directory) {
CF_EXPECT(RecursivelyRemoveDirectory(cache_directory));
CF_EXPECT(EnsureDirectoryExists(cache_directory));
return fmt::format("Cache at \"{}\" has been emptied\n", cache_directory);
}

Result<std::string> RunInfo(const std::string& cache_directory,
const bool json_formatted) {
std::size_t cache_size = CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
if (json_formatted) {
Json::Value json_output(Json::objectValue);
json_output["path"] = cache_directory;
json_output["size_in_GB"] = std::to_string(cache_size);
return json_output.toStyledString();
}
return fmt::format("path:{}\nsize in GB:{}\n", cache_directory, cache_size);
}

Result<std::vector<std::string>> CacheFilesDesc(
const std::string& cache_directory) {
std::vector<std::string> contents = CF_EXPECTF(
DirectoryContentsPaths(cache_directory),
"Failure retrieving contents of directory at \"{}\"", cache_directory);

auto not_self_or_parent_directory = [](std::string_view filepath) {
return !android::base::EndsWith(filepath, ".") &&
!android::base::EndsWith(filepath, "..");
};
std::vector<std::string> filtered;
std::copy_if(contents.begin(), contents.end(), std::back_inserter(filtered),
not_self_or_parent_directory);

using ModTimePair =
std::pair<std::string, std::chrono::system_clock::time_point>;
std::vector<ModTimePair> to_sort;
for (const std::string& filename : filtered) {
to_sort.emplace_back(
std::pair(filename, CF_EXPECT(FileModificationTime(filename))));
}
std::sort(to_sort.begin(), to_sort.end(),
[](const ModTimePair& a, const ModTimePair& b) {
return a.second > b.second;
});

std::vector<std::string> result;
for (const ModTimePair& pair : to_sort) {
result.emplace_back(pair.first);
}
return result;
}

Result<std::string> RunPrune(const std::string& cache_directory,
const std::size_t allowed_size_GB) {
std::size_t cache_size = CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
// Descending because elements are removed from the back
std::vector<std::string> cache_files =
CF_EXPECT(CacheFilesDesc(cache_directory));
while (cache_size > allowed_size_GB) {
CHECK(!cache_files.empty()) << fmt::format(
"Cache size is {} of {}, but there are no more files for pruning.",
cache_size, allowed_size_GB);

std::string next = cache_files.back();
cache_files.pop_back();
LOG(DEBUG) << fmt::format("Deleting \"{}\" for prune", next);
// handles removal of non-directory top-level files as well
CF_EXPECT(RecursivelyRemoveDirectory(next));
cache_size = CF_EXPECT(GetDiskUsageGigabytes(cache_directory));
}
return fmt::format("Cache at \"{}\": ~{}GB of {}GB max\n", cache_directory,
cache_size, allowed_size_GB);
}

class CvdCacheCommandHandler : public CvdCommandHandler {
public:
Result<void> Handle(const CommandRequest& request) override;
Expand All @@ -182,25 +106,23 @@ class CvdCacheCommandHandler : public CvdCommandHandler {

Result<void> CvdCacheCommandHandler::Handle(const CommandRequest& request) {
CF_EXPECT(CanHandle(request));
auto logger = ScopedTeeLogger(LogToStderr());

CacheArguments arguments =
CF_EXPECT(ProcessArguments(request.SubcommandArguments()));
std::string cache_directory = PerUserCacheDir();
CF_EXPECT(EnsureDirectoryExists(cache_directory));
switch (arguments.action) {
case Action::Empty:
std::cout << CF_EXPECTF(RunEmpty(cache_directory),
std::cout << CF_EXPECTF(EmptyCache(cache_directory),
"Error emptying cache at {}", cache_directory);
break;
case Action::Info:
std::cout << CF_EXPECTF(
RunInfo(cache_directory, arguments.json_formatted),
GetCacheInfo(cache_directory, arguments.json_formatted),
"Error retrieving info of cache at {}", cache_directory);
break;
case Action::Prune:
std::cout << CF_EXPECTF(
RunPrune(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);
break;
Expand Down

0 comments on commit 7448680

Please sign in to comment.