Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251import Darwinimport Foundationimport ParcelTestSupportimport Synchronizationimport Testing
enum EngineCoreMeasurementError: Error, CustomStringConvertible { case unavailable(String) case invalid(String)
var description: String { switch self { case .unavailable(let detail): "Engine core counter unavailable: \(detail)" case .invalid(let detail): "Invalid engine core measurement: \(detail)" } }}
struct EngineCoreInstructions { static func read(pid: Int32 = getpid()) throws -> UInt64 { var info = rusage_info_v4() let status = withUnsafeMutablePointer(to: &info) { pointer in pointer.withMemoryRebound(to: rusage_info_t?.self, capacity: 1) { proc_pid_rusage(pid, RUSAGE_INFO_V4, $0) } } guard status == 0 else { throw EngineCoreMeasurementError.unavailable("proc_pid_rusage returned \(status), errno \(errno)") } guard info.ri_instructions > 0 else { throw EngineCoreMeasurementError.unavailable("RUSAGE_INFO_V4 ri_instructions is zero") } return info.ri_instructions }
static func delta(start: UInt64, end: UInt64) throws -> UInt64 { guard start > 0, end > start else { throw EngineCoreMeasurementError.invalid("instruction counter did not advance monotonically") } return end - start }}
struct EngineCoreAllocations: Equatable { let count: UInt64 let requestedBytes: UInt64}
private typealias EngineCoreMallocLogger = @convention(c) (UInt32, UInt, UInt, UInt, UInt, UInt32) -> Void
private enum EngineCoreAllocationStorage { static let thread = Atomic<UInt>(0) static let count = Atomic<UInt64>(0) static let bytes = Atomic<UInt64>(0)}
private func engineCoreMallocLog( _ type: UInt32, _ arg1: UInt, _ arg2: UInt, _ arg3: UInt, _ result: UInt, _ frames: UInt32) { guard EngineCoreAllocationStorage.thread.load(ordering: .relaxed) == UInt(pthread_mach_thread_np(pthread_self())), type & 2 != 0, result != 0 else { return } // The zone logger's realloc flag includes deallocation; its size is the third argument. let bytes = type & 4 != 0 ? arg3 : arg2 _ = EngineCoreAllocationStorage.count.wrappingAdd(1, ordering: .relaxed) _ = EngineCoreAllocationStorage.bytes.wrappingAdd(UInt64(bytes), ordering: .relaxed)}
@inline(never)private func engineCoreKeepAllocation(_ pointer: UnsafeMutableRawPointer) { // An external pointer use prevents release builds from deleting malloc/free probes. precondition(write(STDERR_FILENO, pointer, 0) == 0, "Allocation probe pointer use failed")}
struct EngineCoreAllocationCounter { static func validate() throws { let observed = try measure { guard let initial = malloc(123) else { throw EngineCoreMeasurementError.unavailable("malloc probe") } engineCoreKeepAllocation(initial) guard let resized = realloc(initial, 234) else { free(initial) throw EngineCoreMeasurementError.unavailable("realloc probe") } engineCoreKeepAllocation(resized) free(resized) } guard observed == EngineCoreAllocations(count: 2, requestedBytes: 357) else { throw EngineCoreMeasurementError.unavailable("malloc_logger failed the known allocation traffic probe") } }
static func measure(_ operation: () throws -> Void) throws -> EngineCoreAllocations { guard let symbol = dlsym(UnsafeMutableRawPointer(bitPattern: -2), "malloc_logger") else { throw EngineCoreMeasurementError.unavailable("libmalloc malloc_logger symbol missing") } let logger = symbol.assumingMemoryBound(to: EngineCoreMallocLogger?.self) guard logger.pointee == nil else { throw EngineCoreMeasurementError.unavailable("malloc_logger already in use; run without malloc tracing") } guard EngineCoreAllocationStorage.thread.load(ordering: .relaxed) == 0 else { throw EngineCoreMeasurementError.invalid("allocation measurements cannot nest") } EngineCoreAllocationStorage.count.store(0, ordering: .relaxed) EngineCoreAllocationStorage.bytes.store(0, ordering: .relaxed) logger.pointee = engineCoreMallocLog EngineCoreAllocationStorage.thread.store(UInt(pthread_mach_thread_np(pthread_self())), ordering: .relaxed) defer { EngineCoreAllocationStorage.thread.store(0, ordering: .relaxed) logger.pointee = nil } try operation() EngineCoreAllocationStorage.thread.store(0, ordering: .relaxed) return EngineCoreAllocations( count: EngineCoreAllocationStorage.count.load(ordering: .relaxed), requestedBytes: EngineCoreAllocationStorage.bytes.load(ordering: .relaxed)) }}
struct EngineCoreSample: Codable { let instructionsPerOperation: Double let cpuNanosecondsPerOperation: Double let allocationsPerOperation: Double let requestedBytesPerOperation: Double}
struct EngineCoreResult: Codable { let name: String let iterations: Int let samples: [EngineCoreSample] let checksum: Int
var minimumInstructions: Double { samples.map(\.instructionsPerOperation).min() ?? .nan } var minimumCPU: Double { samples.map(\.cpuNanosecondsPerOperation).min() ?? .nan } var minimumAllocations: Double { samples.map(\.allocationsPerOperation).min() ?? .nan } var minimumBytes: Double { samples.map(\.requestedBytesPerOperation).min() ?? .nan }
func validate() throws { guard iterations > 0, !samples.isEmpty, checksum != 0, samples.allSatisfy({ $0.instructionsPerOperation.isFinite && $0.instructionsPerOperation > 0 && $0.cpuNanosecondsPerOperation.isFinite && $0.cpuNanosecondsPerOperation > 0 && $0.allocationsPerOperation.isFinite && $0.allocationsPerOperation >= 0 && $0.requestedBytesPerOperation.isFinite && $0.requestedBytesPerOperation >= 0 }) else { throw EngineCoreMeasurementError.invalid(name) } }
func exceedsInstructionBudget(reference: Double) throws -> Bool { try validate() guard reference.isFinite, reference > 0 else { throw EngineCoreMeasurementError.invalid("instruction budget reference") } return minimumInstructions > reference * 1.03 }}
enum EngineCoreSampler { static func measure( name: String, iterations: Int, samples: Int, operation: () throws -> Int ) throws -> EngineCoreResult { precondition(iterations > 0 && samples > 0) var checksum = try operation() var observations: [EngineCoreSample] = [] for _ in 0..<samples { let instructions = try EngineCoreInstructions.read() let cpu = BenchmarkCPUClock.now() for _ in 0..<iterations { checksum &+= try operation() } let elapsedCPU = BenchmarkCPUClock.elapsed(since: cpu) let elapsedInstructions = try EngineCoreInstructions.delta( start: instructions, end: EngineCoreInstructions.read()) // Allocator hooks perturb instructions and CPU, so count traffic in a separate replay. let allocations = try EngineCoreAllocationCounter.measure { for _ in 0..<iterations { checksum &+= try operation() } } observations.append( EngineCoreSample( instructionsPerOperation: Double(elapsedInstructions) / Double(iterations), cpuNanosecondsPerOperation: elapsedCPU / Double(iterations), allocationsPerOperation: Double(allocations.count) / Double(iterations), requestedBytesPerOperation: Double(allocations.requestedBytes) / Double(iterations))) } let result = EngineCoreResult(name: name, iterations: iterations, samples: observations, checksum: checksum) try result.validate() return result }}
@Suite("Engine core performance", .serialized, .tags(.benchmark))struct EngineCorePerformanceTests {}
extension EngineCorePerformanceTests { @Suite("Engine core counter validation") struct EngineCoreCounterTests { @Test func instructionsAdvanceForRealWorkAndInvalidPIDFails() throws { let start = try EngineCoreInstructions.read() var checksum: UInt64 = 1 for _ in 0..<10_000 { checksum = checksum &* 6_364_136_223_846_793_005 &+ 1 } let delta = try EngineCoreInstructions.delta(start: start, end: EngineCoreInstructions.read()) #expect(delta > 0 && checksum != 0) #expect(throws: EngineCoreMeasurementError.self) { try EngineCoreInstructions.read(pid: -1) } #expect(throws: EngineCoreMeasurementError.self) { try EngineCoreInstructions.delta(start: 0, end: 1) } #expect(throws: EngineCoreMeasurementError.self) { try EngineCoreInstructions.delta(start: 10, end: 10) } #expect(throws: EngineCoreMeasurementError.self) { try EngineCoreInstructions.delta(start: 10, end: 9) } }
@Test func allocatorCountsFreedTrafficAndReallocRequestedBytes() throws { let allocation = try EngineCoreAllocationCounter.measure { guard let initial = malloc(123) else { throw EngineCoreMeasurementError.unavailable("malloc probe") } engineCoreKeepAllocation(initial) guard let resized = realloc(initial, 234) else { free(initial) throw EngineCoreMeasurementError.unavailable("realloc probe") } engineCoreKeepAllocation(resized) free(resized) } #expect(allocation == EngineCoreAllocations(count: 2, requestedBytes: 357)) }
@Test func throwingOperationRestoresAllocatorHook() throws { enum Probe: Error { case failure } #expect(throws: Probe.self) { try EngineCoreAllocationCounter.measure { throw Probe.failure } } let result = try EngineCoreAllocationCounter.measure { guard let allocation = malloc(128) else { throw EngineCoreMeasurementError.unavailable("malloc probe") } engineCoreKeepAllocation(allocation) free(allocation) } #expect(result == EngineCoreAllocations(count: 1, requestedBytes: 128)) }
@Test func instructionBudgetUsesRawCounterAndThreePercentBoundary() throws { func result(_ value: Double) -> EngineCoreResult { EngineCoreResult( name: "probe", iterations: 1, samples: [ EngineCoreSample( instructionsPerOperation: value, cpuNanosecondsPerOperation: 1_000, allocationsPerOperation: 2, requestedBytesPerOperation: 128) ], checksum: 1) } #expect(try !result(103).exceedsInstructionBudget(reference: 100)) #expect(try result(103.01).exceedsInstructionBudget(reference: 100)) #expect(throws: EngineCoreMeasurementError.self) { try result(0).validate() } #expect(throws: EngineCoreMeasurementError.self) { try result(.nan).validate() } #expect(throws: EngineCoreMeasurementError.self) { try result(100).exceedsInstructionBudget(reference: 0) } } }}