import Darwin import Foundation import ParcelTestSupport import Testing @_silgen_name("thread_selfcounts") private func threadSelfcounts(_ flavor: Int32, _ buffer: UnsafeMutableRawPointer, _ size: Int32) -> Int32 enum BenchmarkCounterSource: String, Codable { case selfcounts, rusage case cpuOnly = "cpu-only" case smoke } struct BenchmarkCounterReading: Equatable { let instructions: UInt64 let cycles: UInt64 func delta(from previous: Self) throws -> Self { guard instructions >= previous.instructions, cycles >= previous.cycles else { throw BenchmarkFailure.measurement("instruction counters moved backwards") } return Self(instructions: instructions - previous.instructions, cycles: cycles - previous.cycles) } } struct BenchmarkCounters { let source: BenchmarkCounterSource func read() throws -> BenchmarkCounterReading? { switch source { case .selfcounts: return try Self.selfcounts() case .rusage: return try Self.processRusage() case .cpuOnly, .smoke: return nil } } static func selfcounts() throws -> BenchmarkCounterReading { guard dlsym(UnsafeMutableRawPointer(bitPattern: -2), "thread_selfcounts") != nil else { throw BenchmarkFailure.measurement("thread_selfcounts unavailable") } var counts: (UInt64, UInt64) = (0, 0) let status = withUnsafeMutablePointer(to: &counts) { threadSelfcounts(1, UnsafeMutableRawPointer($0), 16) } guard status == 0 else { throw BenchmarkFailure.measurement("thread_selfcounts failed: \(status)") } return BenchmarkCounterReading(instructions: counts.0, cycles: counts.1) } static func processRusage() throws -> BenchmarkCounterReading { var usage = rusage_info_v4() let status = withUnsafeMutablePointer(to: &usage) { $0.withMemoryRebound(to: rusage_info_t?.self, capacity: 1) { proc_pid_rusage(getpid(), RUSAGE_INFO_V4, $0) } } guard status == 0 else { throw BenchmarkFailure.measurement("process instruction counters unavailable") } return BenchmarkCounterReading(instructions: usage.ri_instructions, cycles: usage.ri_cycles) } @inline(never) static func knownWork(seed: UInt64) -> UInt64 { var value = seed for _ in 0..<50_000 { value = (value &* 6_364_136_223_846_793_005 &+ 1) ^ (value >> 17) } return value } static func probe(read: () throws -> BenchmarkCounterReading) throws { let before = try read() guard before.instructions > 0 else { throw BenchmarkFailure.measurement("instruction counter is zero") } let checksum = knownWork(seed: before.instructions) let after = try read() guard after.instructions > before.instructions, after.cycles >= before.cycles, checksum != 0 else { throw BenchmarkFailure.measurement("instruction counter did not grow across known work") } } static func startup(full: Bool, hosted: Bool) -> Self { do { try probe(read: selfcounts) return Self(source: full ? .selfcounts : .smoke) } catch { if hosted { print("instruction counters unavailable: smoke mode only") return Self(source: .smoke) } if String(describing: error).contains("zero") || String(describing: error).contains("did not grow") { preconditionFailure(String(describing: error)) } do { try probe(read: processRusage) print("thread counters unavailable; explicitly using process rusage counters on serialized workloads") return Self(source: full ? .rusage : .smoke) } catch { #if arch(x86_64) if !String(describing: error).contains("zero") { print("instruction counters unavailable on Intel: CPU-only gate requires a quiet host") return Self(source: full ? .cpuOnly : .smoke) } #endif preconditionFailure("Benchmark counter startup check failed: \(error)") } } } } extension PerformanceTestSuite { @Suite("Instruction counter validation", .tags(.benchmark)) struct BenchmarkCounterTests { @Test func zeroCounterIsRejected() { #expect(throws: BenchmarkFailure.self) { try BenchmarkCounters.probe { BenchmarkCounterReading(instructions: 0, cycles: 0) } } } @Test func stuckCounterIsRejected() { #expect(throws: BenchmarkFailure.self) { try BenchmarkCounters.probe { BenchmarkCounterReading(instructions: 10, cycles: 20) } } } @Test func increasingCounterIsAccepted() throws { var value: UInt64 = 10 try BenchmarkCounters.probe { defer { value += 1 } return BenchmarkCounterReading(instructions: value, cycles: value) } } @Test func failuresAndBackwardsCountersAreRejected() { #expect(throws: BenchmarkFailure.self) { try BenchmarkCounters.probe { throw BenchmarkFailure.measurement("unavailable") } } #expect(throws: BenchmarkFailure.self) { try BenchmarkCounterReading(instructions: 1, cycles: 2).delta( from: BenchmarkCounterReading(instructions: 2, cycles: 2)) } } @Test func deltaUsesBothCounters() throws { let delta = try BenchmarkCounterReading(instructions: 50, cycles: 90).delta( from: BenchmarkCounterReading(instructions: 20, cycles: 30)) #expect(delta == BenchmarkCounterReading(instructions: 30, cycles: 60)) } } }