import Darwin import Foundation import ParcelTestSupport import Synchronization import 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(0) static let count = Atomic(0) static let bytes = Atomic(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.. 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) } } } }