Skip to content

Commit cd407fd

Browse files
committed
[BuildOperation] Make sure to use thread-safe access for aggregatedCounters and aggregatedTaskCounters
1 parent 33c2d9e commit cd407fd

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

Sources/SWBBuildSystem/BuildOperation.swift

+21-9
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,13 @@ package final class BuildOperation: BuildSystemOperation {
663663
}
664664
}
665665

666+
let aggregatedCounters = await adaptor.getAggregatedCounters()
667+
let aggregatedTaskCounters = await adaptor.getAggregatedTaskCounters()
666668
do {
667-
let swiftCacheHits = self.delegate.aggregatedCounters[.swiftCacheHits, default: 0]
668-
let swiftCacheMisses = self.delegate.aggregatedCounters[.swiftCacheMisses, default: 0]
669-
let clangCacheHits = self.delegate.aggregatedCounters[.clangCacheHits, default: 0]
670-
let clangCacheMisses = self.delegate.aggregatedCounters[.clangCacheMisses, default: 0]
669+
let swiftCacheHits = aggregatedCounters[.swiftCacheHits, default: 0]
670+
let swiftCacheMisses = aggregatedCounters[.swiftCacheMisses, default: 0]
671+
let clangCacheHits = aggregatedCounters[.clangCacheHits, default: 0]
672+
let clangCacheMisses = aggregatedCounters[.clangCacheMisses, default: 0]
671673
if swiftCacheHits + swiftCacheMisses > 0 || clangCacheHits + clangCacheMisses > 0 {
672674
adaptor.withActivity(ruleInfo: "CompilationCacheMetrics", executionDescription: "Report compilation cache metrics", signature: "compilation_cache_metrics", target: nil, parentActivity: nil) { activity in
673675
func getSummary(hits: Int, misses: Int) -> String {
@@ -693,10 +695,10 @@ package final class BuildOperation: BuildSystemOperation {
693695
var tasks: [String: [String: Double]] = [:]
694696
}
695697
var serializedCounters = AllCounters()
696-
for (key, value) in self.delegate.aggregatedCounters {
698+
for (key, value) in aggregatedCounters {
697699
serializedCounters.global[key.rawValue] = Double(value)
698700
}
699-
for (taskId, taskCounters) in self.delegate.aggregatedTaskCounters {
701+
for (taskId, taskCounters) in aggregatedTaskCounters {
700702
var serialTaskCounters: [String: Double] = [:]
701703
for (counterKey, counterValue) in taskCounters {
702704
serialTaskCounters[counterKey.rawValue] = Double(counterValue)
@@ -732,10 +734,10 @@ package final class BuildOperation: BuildSystemOperation {
732734
var tasks: [String: [String: Double]] = [:]
733735
}
734736
var serializedCounters = AllCounters()
735-
for (key, value) in self.delegate.aggregatedCounters {
737+
for (key, value) in aggregatedCounters {
736738
serializedCounters.global[key.rawValue] = Double(value)
737739
}
738-
for (taskId, taskCounters) in self.delegate.aggregatedTaskCounters {
740+
for (taskId, taskCounters) in aggregatedTaskCounters {
739741
var serialTaskCounters: [String: Double] = [:]
740742
for (counterKey, counterValue) in taskCounters {
741743
serialTaskCounters[counterKey.rawValue] = Double(counterValue)
@@ -801,7 +803,7 @@ package final class BuildOperation: BuildSystemOperation {
801803
}
802804

803805
// `buildComplete()` should not run within `queue`, otherwise there can be a deadlock during cancelling.
804-
return delegate.buildComplete(self, status: effectiveStatus, delegate: buildOutputDelegate, metrics: .init(counters: delegate.aggregatedCounters))
806+
return delegate.buildComplete(self, status: effectiveStatus, delegate: buildOutputDelegate, metrics: .init(counters: aggregatedCounters))
805807
}
806808

807809
func prepareForBuilding() -> ([String], [String])? {
@@ -1384,6 +1386,16 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act
13841386

13851387
fileprivate let dynamicOperationContext: DynamicTaskOperationContext
13861388

1389+
/// Returns the delegate's `aggregatedCounters` in a thread-safe manner.
1390+
func getAggregatedCounters() async -> [BuildOperationMetrics.Counter: Int] {
1391+
return await queue.sync { self.operation.delegate.aggregatedCounters }
1392+
}
1393+
1394+
/// Returns the delegate's `aggregatedTaskCounters` in a thread-safe manner.
1395+
func getAggregatedTaskCounters() async -> [String: [BuildOperationMetrics.TaskCounter: Int]] {
1396+
return await queue.sync { self.operation.delegate.aggregatedTaskCounters }
1397+
}
1398+
13871399
init(operation: BuildOperation, buildOutputDelegate: any BuildOutputDelegate, core: Core) {
13881400
self.operation = operation
13891401
self.buildOutputDelegate = buildOutputDelegate

0 commit comments

Comments
 (0)