| 1 | import Darwin |
| 2 | import Foundation |
| 3 | |
| 4 | /// Spec §15.30 (2026-10-29): how the machine itself fared. Paul's Mac "got |
| 5 | /// completely overloaded" in a call (call txt4wuhyg6qqellf, 0.4.13), and the |
| 6 | /// encoder never named the CPU as its limit, so nothing could say whether |
| 7 | /// Calls or something else was eating it. Each quality report now carries |
| 8 | /// Calls' own CPU over the window, its resident memory, the load average |
| 9 | /// (Mac), the worst thermal state, and Low Power Mode; and the same |
| 10 | /// readings, once a second, tell the sender's own watch when this device |
| 11 | /// is struggling. |
| 12 | public struct QualityHost: Encodable, Equatable, Sendable { |
| 13 | /// Calls' own process, percent of one core, over the window. |
| 14 | public var procCpuPct: Double? |
| 15 | /// Its resident memory at the window's end, in MB. |
| 16 | public var procMemMB: Double? |
| 17 | /// The system's one-minute load average (Mac only). |
| 18 | public var sysLoad1: Double? |
| 19 | /// The worst thermal state in the window: nominal, fair, serious, critical. |
| 20 | public var thermal: String? |
| 21 | /// Low Power Mode, at any point in the window. |
| 22 | public var lowPower: Bool? |
| 23 | } |
| 24 | |
| 25 | /// The readings, kept between looks: the process's CPU time at the last |
| 26 | /// second and at the window's start, and the worst the window has seen. |
| 27 | /// Pure over what it is given; `Reading.now()` is the one place the system |
| 28 | /// is asked. |
| 29 | struct HostLoadSampler: Equatable, Sendable { |
| 30 | struct Reading: Equatable, Sendable { |
| 31 | var at: Double |
| 32 | /// Seconds of CPU this process has used, user and system. |
| 33 | var cpuSeconds: Double |
| 34 | var thermal: ProcessInfo.ThermalState |
| 35 | var lowPower: Bool |
| 36 | var memMB: Double? |
| 37 | var load1: Double? |
| 38 | |
| 39 | static func now(at: Double) -> Reading { |
| 40 | var usage = rusage() |
| 41 | getrusage(RUSAGE_SELF, &usage) |
| 42 | let cpu = |
| 43 | Double(usage.ru_utime.tv_sec) + Double(usage.ru_utime.tv_usec) / 1e6 + Double(usage.ru_stime.tv_sec) |
| 44 | + Double(usage.ru_stime.tv_usec) / 1e6 |
| 45 | var info = mach_task_basic_info() |
| 46 | var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size / MemoryLayout<natural_t>.size) |
| 47 | let ok = withUnsafeMutablePointer(to: &info) { |
| 48 | $0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { |
| 49 | task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count) |
| 50 | } |
| 51 | } |
| 52 | #if os(macOS) |
| 53 | var loads = [Double](repeating: 0, count: 3) |
| 54 | let load: Double? = getloadavg(&loads, 3) >= 1 ? loads[0] : nil |
| 55 | #else |
| 56 | let load: Double? = nil |
| 57 | #endif |
| 58 | return Reading( |
| 59 | at: at, cpuSeconds: cpu, thermal: ProcessInfo.processInfo.thermalState, |
| 60 | lowPower: ProcessInfo.processInfo.isLowPowerModeEnabled, |
| 61 | memMB: ok == KERN_SUCCESS ? Double(info.resident_size) / 1_048_576 : nil, load1: load) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private var last: Reading? |
| 66 | private var windowStart: Reading? |
| 67 | private var worstThermal = ProcessInfo.ThermalState.nominal |
| 68 | private var lowPowerSeen = false |
| 69 | |
| 70 | /// One look, about once a second: the process's CPU over the last |
| 71 | /// second, percent of one core, or nil for the first look. |
| 72 | mutating func tick(_ r: Reading) -> Double? { |
| 73 | defer { last = r } |
| 74 | if windowStart == nil { windowStart = r } |
| 75 | worstThermal = max(worstThermal, r.thermal) |
| 76 | lowPowerSeen = lowPowerSeen || r.lowPower |
| 77 | guard let last, r.at > last.at else { return nil } |
| 78 | return max(0, r.cpuSeconds - last.cpuSeconds) / (r.at - last.at) * 100 |
| 79 | } |
| 80 | |
| 81 | /// The window's host load, and the next window started from `r`. |
| 82 | mutating func take(_ r: Reading) -> QualityHost { |
| 83 | _ = tick(r) |
| 84 | var host = QualityHost() |
| 85 | if let start = windowStart, r.at > start.at { |
| 86 | host.procCpuPct = QualityReportBuilder.bound( |
| 87 | max(0, r.cpuSeconds - start.cpuSeconds) / (r.at - start.at) * 100, QualityBounds.cpuPct) |
| 88 | } |
| 89 | host.procMemMB = QualityReportBuilder.bound(r.memMB, QualityBounds.memMB) |
| 90 | host.sysLoad1 = QualityReportBuilder.bound(r.load1, QualityBounds.load) |
| 91 | host.thermal = Self.word(worstThermal) |
| 92 | host.lowPower = lowPowerSeen |
| 93 | windowStart = r |
| 94 | worstThermal = r.thermal |
| 95 | lowPowerSeen = r.lowPower |
| 96 | return host |
| 97 | } |
| 98 | |
| 99 | static func word(_ t: ProcessInfo.ThermalState) -> String { |
| 100 | switch t { |
| 101 | case .nominal: return "nominal" |
| 102 | case .fair: return "fair" |
| 103 | case .serious: return "serious" |
| 104 | case .critical: return "critical" |
| 105 | @unknown default: return "nominal" |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | extension ProcessInfo.ThermalState: @retroactive Comparable { |
| 111 | public static func < (a: Self, b: Self) -> Bool { a.rawValue < b.rawValue } |
| 112 | } |
| 113 | |