diff --git a/include/swift-c/DependencyScan/DependencyScan.h b/include/swift-c/DependencyScan/DependencyScan.h index 711c59a7af2c5..c65c431d070ed 100644 --- a/include/swift-c/DependencyScan/DependencyScan.h +++ b/include/swift-c/DependencyScan/DependencyScan.h @@ -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); diff --git a/tools/libSwiftScan/SwiftCaching.cpp b/tools/libSwiftScan/SwiftCaching.cpp index 1f4a749d1c2b1..21d93206d90d8 100644 --- a/tools/libSwiftScan/SwiftCaching.cpp +++ b/tools/libSwiftScan/SwiftCaching.cpp @@ -47,6 +47,7 @@ #include "llvm/Support/VirtualOutputBackend.h" #include "llvm/Support/VirtualOutputBackends.h" #include "llvm/Support/raw_ostream.h" +#include #include namespace { @@ -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 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 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. diff --git a/tools/libSwiftScan/libSwiftScan.exports b/tools/libSwiftScan/libSwiftScan.exports index 0b749aeaf7daf..89bc2dbfaa8d5 100644 --- a/tools/libSwiftScan/libSwiftScan.exports +++ b/tools/libSwiftScan/libSwiftScan.exports @@ -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