import Darwin import Foundation /// Spec ยง15.30 (2026-10-29): how the machine itself fared. Paul's Mac "got /// completely overloaded" in a call (call txt4wuhyg6qqellf, 0.4.13), and the /// encoder never named the CPU as its limit, so nothing could say whether /// Calls or something else was eating it. Each quality report now carries /// Calls' own CPU over the window, its resident memory, the load average /// (Mac), the worst thermal state, and Low Power Mode; and the same /// readings, once a second, tell the sender's own watch when this device /// is struggling. public struct QualityHost: Encodable, Equatable, Sendable { /// Calls' own process, percent of one core, over the window. public var procCpuPct: Double? /// Its resident memory at the window's end, in MB. public var procMemMB: Double? /// The system's one-minute load average (Mac only). public var sysLoad1: Double? /// The worst thermal state in the window: nominal, fair, serious, critical. public var thermal: String? /// Low Power Mode, at any point in the window. public var lowPower: Bool? } /// The readings, kept between looks: the process's CPU time at the last /// second and at the window's start, and the worst the window has seen. /// Pure over what it is given; `Reading.now()` is the one place the system /// is asked. struct HostLoadSampler: Equatable, Sendable { struct Reading: Equatable, Sendable { var at: Double /// Seconds of CPU this process has used, user and system. var cpuSeconds: Double var thermal: ProcessInfo.ThermalState var lowPower: Bool var memMB: Double? var load1: Double? static func now(at: Double) -> Reading { var usage = rusage() getrusage(RUSAGE_SELF, &usage) let cpu = Double(usage.ru_utime.tv_sec) + Double(usage.ru_utime.tv_usec) / 1e6 + Double(usage.ru_stime.tv_sec) + Double(usage.ru_stime.tv_usec) / 1e6 var info = mach_task_basic_info() var count = mach_msg_type_number_t(MemoryLayout.size / MemoryLayout.size) let ok = withUnsafeMutablePointer(to: &info) { $0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count) } } #if os(macOS) var loads = [Double](repeating: 0, count: 3) let load: Double? = getloadavg(&loads, 3) >= 1 ? loads[0] : nil #else let load: Double? = nil #endif return Reading( at: at, cpuSeconds: cpu, thermal: ProcessInfo.processInfo.thermalState, lowPower: ProcessInfo.processInfo.isLowPowerModeEnabled, memMB: ok == KERN_SUCCESS ? Double(info.resident_size) / 1_048_576 : nil, load1: load) } } private var last: Reading? private var windowStart: Reading? private var worstThermal = ProcessInfo.ThermalState.nominal private var lowPowerSeen = false /// One look, about once a second: the process's CPU over the last /// second, percent of one core, or nil for the first look. mutating func tick(_ r: Reading) -> Double? { defer { last = r } if windowStart == nil { windowStart = r } worstThermal = max(worstThermal, r.thermal) lowPowerSeen = lowPowerSeen || r.lowPower guard let last, r.at > last.at else { return nil } return max(0, r.cpuSeconds - last.cpuSeconds) / (r.at - last.at) * 100 } /// The window's host load, and the next window started from `r`. mutating func take(_ r: Reading) -> QualityHost { _ = tick(r) var host = QualityHost() if let start = windowStart, r.at > start.at { host.procCpuPct = QualityReportBuilder.bound( max(0, r.cpuSeconds - start.cpuSeconds) / (r.at - start.at) * 100, QualityBounds.cpuPct) } host.procMemMB = QualityReportBuilder.bound(r.memMB, QualityBounds.memMB) host.sysLoad1 = QualityReportBuilder.bound(r.load1, QualityBounds.load) host.thermal = Self.word(worstThermal) host.lowPower = lowPowerSeen windowStart = r worstThermal = r.thermal lowPowerSeen = r.lowPower return host } static func word(_ t: ProcessInfo.ThermalState) -> String { switch t { case .nominal: return "nominal" case .fair: return "fair" case .serious: return "serious" case .critical: return "critical" @unknown default: return "nominal" } } } extension ProcessInfo.ThermalState: @retroactive Comparable { public static func < (a: Self, b: Self) -> Bool { a.rawValue < b.rawValue } }