Skip to content

[libSwiftScan][Caching] Add APIs to manage CAS size #71257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions include/swift-c/DependencyScan/DependencyScan.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,29 @@ SWIFTSCAN_PUBLIC swiftscan_string_ref_t
swiftscan_cas_store(swiftscan_cas_t cas, uint8_t *data, unsigned size,
swiftscan_string_ref_t *error);

/// Get the local storage size for the CAS in bytes. Return the local storage
/// size of the CAS/cache data, or -1 if the implementation does not support
/// reporting such size, or -2 if an error occurred.
/// If error happens, the error message is returned via `error` parameter, and
/// caller needs to free the error message via `swiftscan_string_dispose`.
SWIFTSCAN_PUBLIC int64_t
swiftscan_cas_get_ondisk_size(swiftscan_cas_t, swiftscan_string_ref_t *error);

/// Set the size for the limiting disk storage size for CAS. \c size_limit is
/// the maximum size limit in bytes (0 means no limit, negative is invalid).
/// Return true if error. If error happens, the error message is returned via
/// `error` parameter, and caller needs to free the error message via
/// `swiftscan_string_dispose`.
SWIFTSCAN_PUBLIC bool
swiftscan_cas_set_ondisk_size_limit(swiftscan_cas_t, int64_t size_limit,
swiftscan_string_ref_t *error);

/// Prune local CAS storage according to the size limit. Return true if error.
/// If error happens, the error message is returned via `error` parameter, and
/// caller needs to free the error message via `swiftscan_string_dispose`.
SWIFTSCAN_PUBLIC bool
swiftscan_cas_prune_ondisk_data(swiftscan_cas_t, swiftscan_string_ref_t *error);

/// Dispose the \c cas instance.
SWIFTSCAN_PUBLIC void swiftscan_cas_dispose(swiftscan_cas_t cas);

Expand Down
48 changes: 48 additions & 0 deletions tools/libSwiftScan/SwiftCaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "llvm/Support/VirtualOutputBackend.h"
#include "llvm/Support/VirtualOutputBackends.h"
#include "llvm/Support/raw_ostream.h"
#include <optional>
#include <variant>

namespace {
Expand Down Expand Up @@ -197,6 +198,53 @@ swiftscan_string_ref_t swiftscan_cas_store(swiftscan_cas_t cas, uint8_t *data,
CAS.getID(*Result).toString().c_str());
}

int64_t swiftscan_cas_get_ondisk_size(swiftscan_cas_t cas,
swiftscan_string_ref_t *error) {
auto &CAS = unwrap(cas)->getCAS();
std::optional<uint64_t> Size;
if (auto E = CAS.getStorageSize().moveInto(Size)) {
*error =
swift::c_string_utils::create_clone(toString(std::move(E)).c_str());
return -2;
}

*error = swift::c_string_utils::create_null();
return Size ? *Size : -1;
}

bool
swiftscan_cas_set_ondisk_size_limit(swiftscan_cas_t cas, int64_t size_limit,
swiftscan_string_ref_t *error) {
if (size_limit < 0) {
*error = swift::c_string_utils::create_clone(
"invalid size limit passing to swiftscan_cas_set_ondisk_size_limit");
return true;
}
auto &CAS = unwrap(cas)->getCAS();
std::optional<uint64_t> SizeLimit;
if (size_limit > 0)
SizeLimit = size_limit;
if (auto E = CAS.setSizeLimit(SizeLimit)) {
*error =
swift::c_string_utils::create_clone(toString(std::move(E)).c_str());
return true;
}
*error = swift::c_string_utils::create_null();
return false;
}

bool swiftscan_cas_prune_ondisk_data(swiftscan_cas_t cas,
swiftscan_string_ref_t *error) {
auto &CAS = unwrap(cas)->getCAS();
if (auto E = CAS.pruneStorageData()) {
*error =
swift::c_string_utils::create_clone(toString(std::move(E)).c_str());
return true;
}
*error = swift::c_string_utils::create_null();
return false;
}

/// Expand the invocation if there is repsonseFile into Args that are passed in
/// the parameter. Return swift-frontend arguments in an ArrayRef, which has the
/// first "-frontend" option dropped if needed.
Expand Down
3 changes: 3 additions & 0 deletions tools/libSwiftScan/libSwiftScan.exports
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ swiftscan_diagnostic_get_message
swiftscan_diagnostic_get_severity
swiftscan_diagnostics_set_dispose
swiftscan_cas_create_from_options
swiftscan_cas_get_ondisk_size
swiftscan_cas_set_ondisk_size_limit
swiftscan_cas_prune_ondisk_data
swiftscan_cas_dispose
swiftscan_cas_options_create
swiftscan_cas_options_dispose
Expand Down