Skip to content

Commit bc8efec

Browse files
Merge pull request swiftlang#71257 from cachemeifyoucan/eng/PR-121944849
[libSwiftScan][Caching] Add APIs to manage CAS size
2 parents 42a4d2d + 46e0f7a commit bc8efec

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,29 @@ SWIFTSCAN_PUBLIC swiftscan_string_ref_t
496496
swiftscan_cas_store(swiftscan_cas_t cas, uint8_t *data, unsigned size,
497497
swiftscan_string_ref_t *error);
498498

499+
/// Get the local storage size for the CAS in bytes. Return the local storage
500+
/// size of the CAS/cache data, or -1 if the implementation does not support
501+
/// reporting such size, or -2 if an error occurred.
502+
/// If error happens, the error message is returned via `error` parameter, and
503+
/// caller needs to free the error message via `swiftscan_string_dispose`.
504+
SWIFTSCAN_PUBLIC int64_t
505+
swiftscan_cas_get_ondisk_size(swiftscan_cas_t, swiftscan_string_ref_t *error);
506+
507+
/// Set the size for the limiting disk storage size for CAS. \c size_limit is
508+
/// the maximum size limit in bytes (0 means no limit, negative is invalid).
509+
/// Return true if error. If error happens, the error message is returned via
510+
/// `error` parameter, and caller needs to free the error message via
511+
/// `swiftscan_string_dispose`.
512+
SWIFTSCAN_PUBLIC bool
513+
swiftscan_cas_set_ondisk_size_limit(swiftscan_cas_t, int64_t size_limit,
514+
swiftscan_string_ref_t *error);
515+
516+
/// Prune local CAS storage according to the size limit. Return true if error.
517+
/// If error happens, the error message is returned via `error` parameter, and
518+
/// caller needs to free the error message via `swiftscan_string_dispose`.
519+
SWIFTSCAN_PUBLIC bool
520+
swiftscan_cas_prune_ondisk_data(swiftscan_cas_t, swiftscan_string_ref_t *error);
521+
499522
/// Dispose the \c cas instance.
500523
SWIFTSCAN_PUBLIC void swiftscan_cas_dispose(swiftscan_cas_t cas);
501524

tools/libSwiftScan/SwiftCaching.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "llvm/Support/VirtualOutputBackend.h"
4848
#include "llvm/Support/VirtualOutputBackends.h"
4949
#include "llvm/Support/raw_ostream.h"
50+
#include <optional>
5051
#include <variant>
5152

5253
namespace {
@@ -197,6 +198,53 @@ swiftscan_string_ref_t swiftscan_cas_store(swiftscan_cas_t cas, uint8_t *data,
197198
CAS.getID(*Result).toString().c_str());
198199
}
199200

201+
int64_t swiftscan_cas_get_ondisk_size(swiftscan_cas_t cas,
202+
swiftscan_string_ref_t *error) {
203+
auto &CAS = unwrap(cas)->getCAS();
204+
std::optional<uint64_t> Size;
205+
if (auto E = CAS.getStorageSize().moveInto(Size)) {
206+
*error =
207+
swift::c_string_utils::create_clone(toString(std::move(E)).c_str());
208+
return -2;
209+
}
210+
211+
*error = swift::c_string_utils::create_null();
212+
return Size ? *Size : -1;
213+
}
214+
215+
bool
216+
swiftscan_cas_set_ondisk_size_limit(swiftscan_cas_t cas, int64_t size_limit,
217+
swiftscan_string_ref_t *error) {
218+
if (size_limit < 0) {
219+
*error = swift::c_string_utils::create_clone(
220+
"invalid size limit passing to swiftscan_cas_set_ondisk_size_limit");
221+
return true;
222+
}
223+
auto &CAS = unwrap(cas)->getCAS();
224+
std::optional<uint64_t> SizeLimit;
225+
if (size_limit > 0)
226+
SizeLimit = size_limit;
227+
if (auto E = CAS.setSizeLimit(SizeLimit)) {
228+
*error =
229+
swift::c_string_utils::create_clone(toString(std::move(E)).c_str());
230+
return true;
231+
}
232+
*error = swift::c_string_utils::create_null();
233+
return false;
234+
}
235+
236+
bool swiftscan_cas_prune_ondisk_data(swiftscan_cas_t cas,
237+
swiftscan_string_ref_t *error) {
238+
auto &CAS = unwrap(cas)->getCAS();
239+
if (auto E = CAS.pruneStorageData()) {
240+
*error =
241+
swift::c_string_utils::create_clone(toString(std::move(E)).c_str());
242+
return true;
243+
}
244+
*error = swift::c_string_utils::create_null();
245+
return false;
246+
}
247+
200248
/// Expand the invocation if there is repsonseFile into Args that are passed in
201249
/// the parameter. Return swift-frontend arguments in an ArrayRef, which has the
202250
/// first "-frontend" option dropped if needed.

tools/libSwiftScan/libSwiftScan.exports

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ swiftscan_diagnostic_get_message
7777
swiftscan_diagnostic_get_severity
7878
swiftscan_diagnostics_set_dispose
7979
swiftscan_cas_create_from_options
80+
swiftscan_cas_get_ondisk_size
81+
swiftscan_cas_set_ondisk_size_limit
82+
swiftscan_cas_prune_ondisk_data
8083
swiftscan_cas_dispose
8184
swiftscan_cas_options_create
8285
swiftscan_cas_options_dispose

0 commit comments

Comments
 (0)