perf(efficiency): 接入真实CPU能量并隔离展示与采样需求
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import Foundation
|
||||
|
||||
/// Uses system CPU energy attribution; never derives energy from CPU time or percentage.
|
||||
nonisolated struct ProcessEnergyMonitor: Sendable {
|
||||
private var previous: [ProcessIdentity: ProcessEnergyReading] = [:]
|
||||
|
||||
static func watts(current: ProcessEnergyReading, previous: ProcessEnergyReading?, maximumInterval: TimeInterval = 30) -> (Double, Double)? {
|
||||
guard current.status == .available, let currentEnergy = current.nanojoules,
|
||||
let previous, previous.status == .available, let previousEnergy = previous.nanojoules,
|
||||
current.id == previous.id, current.birth != 0, current.birth == previous.birth,
|
||||
currentEnergy >= previousEnergy else { return nil }
|
||||
let elapsed = current.uptime - previous.uptime
|
||||
guard elapsed.isFinite, elapsed > 0, maximumInterval.isFinite, elapsed <= maximumInterval else { return nil }
|
||||
// Integer subtraction preserves low bits of large cumulative counters.
|
||||
let power = Double(currentEnergy - previousEnergy) / 1_000_000_000 / elapsed
|
||||
return power.isFinite && power >= 0 ? (power, elapsed) : nil
|
||||
}
|
||||
|
||||
mutating func update(processes: ProcessSnapshot, readings: [ProcessEnergyReading], capability: EnergyCapability, maximumInterval: TimeInterval = 30) -> ProcessEnergySnapshot {
|
||||
let processesByID = Dictionary(processes.rows.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
|
||||
var next: [ProcessIdentity: ProcessEnergyReading] = [:]
|
||||
var usages: [ProcessEnergyUsage] = []
|
||||
var measured = 0
|
||||
if capability == .verifiedCPU && !processes.failed {
|
||||
for reading in readings where next[reading.id] == nil {
|
||||
guard let process = processesByID[reading.id], reading.status == .available,
|
||||
reading.nanojoules != nil, reading.birth > 0, reading.uptime.isFinite else { continue }
|
||||
next[reading.id] = reading
|
||||
if let (power, seconds) = Self.watts(current: reading, previous: previous[reading.id], maximumInterval: maximumInterval) {
|
||||
measured += 1
|
||||
let usage = ProcessEnergyUsage(process: process, watts: power, seconds: seconds)
|
||||
let index = usages.firstIndex {
|
||||
$0.watts < power || ($0.watts == power && $0.id.pid > process.id.pid)
|
||||
} ?? usages.endIndex
|
||||
usages.insert(usage, at: index)
|
||||
if usages.count > 3 { usages.removeLast() }
|
||||
}
|
||||
}
|
||||
}
|
||||
previous = next // Failed / missing readings invalidate the old baseline.
|
||||
let state: ProcessEnergySnapshot.State
|
||||
if capability == .unsupported { state = .unsupported }
|
||||
else if capability == .unavailable || processes.failed || (capability == .verifiedCPU && next.isEmpty) { state = .unavailable }
|
||||
else if measured == 0 { state = .waiting }
|
||||
else { state = measured < processes.enumerated ? .partial : .available }
|
||||
return ProcessEnergySnapshot(date: Date(), candidates: processes.enumerated, readable: next.count,
|
||||
measured: measured, rows: usages, state: state)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user