Skip to content

Commit 1d9760b

Browse files
[Caching] Add CAS size management APIs
rdar://121944849
1 parent 5fae1fd commit 1d9760b

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

Sources/CSwiftScan/include/swiftscan_header.h

+7
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ typedef struct {
274274

275275
//=== Scanner CAS Operations ----------------------------------------------===//
276276
swiftscan_cas_options_t (*swiftscan_cas_options_create)(void);
277+
int64_t (*swiftscan_cas_get_ondisk_size)(swiftscan_cas_t,
278+
swiftscan_string_ref_t *error);
279+
bool (*swiftscan_cas_set_ondisk_size_limit)(swiftscan_cas_t,
280+
int64_t size_limit,
281+
swiftscan_string_ref_t *error);
282+
bool (*swiftscan_cas_prune_ondisk_data)(swiftscan_cas_t,
283+
swiftscan_string_ref_t *error);
277284
void (*swiftscan_cas_options_dispose)(swiftscan_cas_options_t options);
278285
void (*swiftscan_cas_options_set_ondisk_path)(swiftscan_cas_options_t options,
279286
const char *path);

Sources/SwiftDriver/SwiftScan/SwiftScan.swift

+14
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,17 @@ internal extension swiftscan_diagnostic_severity_t {
300300
#endif
301301
}
302302

303+
@_spi(Testing) public var supportsCASSizeManagement : Bool {
304+
#if os(Windows)
305+
// CAS is currently not supported on Windows hosts.
306+
return false
307+
#else
308+
return api.swiftscan_cas_get_ondisk_size != nil &&
309+
api.swiftscan_cas_set_ondisk_size_limit != nil &&
310+
api.swiftscan_cas_prune_ondisk_data != nil
311+
#endif
312+
}
313+
303314
@_spi(Testing) public var supportsBridgingHeaderPCHCommand : Bool {
304315
return api.swiftscan_swift_textual_detail_get_bridging_pch_command_line != nil
305316
}
@@ -511,6 +522,9 @@ private extension swiftscan_functions_t {
511522
self.swiftscan_cas_options_set_plugin_option = try loadOptional("swiftscan_cas_options_set_plugin_option")
512523
self.swiftscan_cas_options_dispose = try loadOptional("swiftscan_cas_options_dispose")
513524
self.swiftscan_cas_create_from_options = try loadOptional("swiftscan_cas_create_from_options")
525+
self.swiftscan_cas_get_ondisk_size = try loadOptional("swiftscan_cas_get_ondisk_size")
526+
self.swiftscan_cas_set_ondisk_size_limit = try loadOptional("swiftscan_cas_set_ondisk_size_limit")
527+
self.swiftscan_cas_prune_ondisk_data = try loadOptional("swiftscan_cas_prune_ondisk_data")
514528
self.swiftscan_cas_dispose = try loadOptional("swiftscan_cas_dispose")
515529
self.swiftscan_cache_compute_key = try loadOptional("swiftscan_cache_compute_key")
516530
self.swiftscan_cache_compute_key_from_input_index = try loadOptional("swiftscan_cache_compute_key_from_input_index")

Sources/SwiftDriver/SwiftScan/SwiftScanCAS.swift

+26
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,32 @@ public final class SwiftScanCAS {
198198
return try scanner.toSwiftString(casid)
199199
}
200200

201+
public var supportsSizeManagement: Bool {
202+
scanner.supportsCASSizeManagement
203+
}
204+
205+
public func getStorageSize() throws -> Int64 {
206+
let size = try scanner.handleCASError { err_msg in
207+
scanner.api.swiftscan_cas_get_ondisk_size(cas, &err_msg)
208+
}
209+
if size == -1 {
210+
throw DependencyScanningError.casError("CAS storage size query is not supported")
211+
}
212+
return size
213+
}
214+
215+
public func setSizeLimit(_ size: Int64) throws {
216+
_ = try scanner.handleCASError { err_msg in
217+
scanner.api.swiftscan_cas_set_ondisk_size_limit(cas, size, &err_msg)
218+
}
219+
}
220+
221+
public func prune() throws {
222+
_ = try scanner.handleCASError { err_msg in
223+
scanner.api.swiftscan_cas_prune_ondisk_data(cas, &err_msg)
224+
}
225+
}
226+
201227
@available(*, deprecated)
202228
public func computeCacheKey(commandLine: [String], input: String) throws -> String {
203229
let casid = try scanner.handleCASError { err_msg in

Tests/SwiftDriverTests/CachingBuildTests.swift

+23
Original file line numberDiff line numberDiff line change
@@ -849,4 +849,27 @@ final class CachingBuildTests: XCTestCase {
849849
}
850850
}
851851
}
852+
853+
func testCASManagement() throws {
854+
try withTemporaryDirectory { path in
855+
let casPath = path.appending(component: "cas")
856+
let driver = try Driver(args: ["swiftc", "-v"])
857+
let scanLibPath = try XCTUnwrap(driver.getSwiftScanLibPath())
858+
try dependencyOracle.verifyOrCreateScannerInstance(fileSystem: localFileSystem,
859+
swiftScanLibPath: scanLibPath)
860+
let cas = try dependencyOracle.getOrCreateCAS(pluginPath: nil, onDiskPath: casPath, pluginOptions: [])
861+
guard cas.supportsSizeManagement else {
862+
throw XCTSkip("CAS size management is not supported")
863+
}
864+
let preSize = try cas.getStorageSize()
865+
let dataToStore = Data(count: 1000)
866+
_ = try cas.store(data: dataToStore)
867+
let postSize = try cas.getStorageSize()
868+
XCTAssertTrue(postSize > preSize)
869+
870+
// Try prune.
871+
try cas.setSizeLimit(100)
872+
try cas.prune()
873+
}
874+
}
852875
}

0 commit comments

Comments
 (0)