53 lines
2.0 KiB
Swift
53 lines
2.0 KiB
Swift
import Foundation
|
|
|
|
nonisolated enum EnergyReadStatus: String, Codable, Sendable {
|
|
case available, denied, gone, failed, identityChanged, deadline, unsupported, unverified
|
|
}
|
|
nonisolated enum EnergyCapability: String, Codable, Sendable { case verifiedCPU, unverified, unsupported, unavailable }
|
|
nonisolated struct ProcessEnergyRequest: Codable, Sendable {
|
|
let token: UUID
|
|
let identities: [ProcessIdentity]
|
|
static let limit = 256
|
|
static let byteLimit = 65_536
|
|
}
|
|
nonisolated struct ProcessEnergyReading: Codable, Sendable {
|
|
let id: ProcessIdentity
|
|
let birth: UInt64
|
|
let uptime: TimeInterval
|
|
let nanojoules: UInt64?
|
|
let status: EnergyReadStatus
|
|
}
|
|
nonisolated struct ProcessEnergyReply: Codable, Sendable {
|
|
let token: UUID
|
|
let capability: EnergyCapability
|
|
let readings: [ProcessEnergyReading]
|
|
}
|
|
nonisolated struct ProcessEnergyUsage: Identifiable, Sendable {
|
|
let process: ProcessMetrics
|
|
let watts: Double
|
|
let seconds: TimeInterval
|
|
var id: ProcessIdentity { process.id }
|
|
var formattedPower: String { watts >= 1 ? String(format: "%.2f W", watts) : String(format: "%.1f mW", watts * 1000) }
|
|
}
|
|
nonisolated struct ProcessEnergySnapshot: Sendable {
|
|
enum State: Sendable { case waiting, available, partial, unavailable, unsupported }
|
|
let date: Date
|
|
let candidates: Int
|
|
let readable: Int
|
|
let measured: Int
|
|
let rows: [ProcessEnergyUsage] // All valid measurements; no CPU prefilter.
|
|
let state: State
|
|
}
|
|
|
|
nonisolated extension ProcessSnapshot {
|
|
func withEnergy(_ energy: ProcessEnergySnapshot?) -> ProcessSnapshot {
|
|
let values = Dictionary((energy?.rows ?? []).map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
|
|
return ProcessSnapshot(date: date, enumerated: enumerated, readable: readable, rows: rows.map { process in
|
|
var row = process
|
|
row.cpuPower = values[process.id]?.watts
|
|
row.powerWindow = values[process.id]?.seconds
|
|
return row
|
|
}, failed: failed)
|
|
}
|
|
}
|