feat(menubar): 拆分紧凑双页面板并按页采样
This commit is contained in:
@@ -35,6 +35,18 @@ nonisolated struct ProcessEnergySnapshot: Sendable {
|
||||
let candidates: Int
|
||||
let readable: Int
|
||||
let measured: Int
|
||||
let rows: [ProcessEnergyUsage] // Only the top three; no CPU prefilter.
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,12 @@ nonisolated struct ProcessMetrics: Identifiable, Sendable {
|
||||
let footprintBytes: UInt64? // Physical footprint; never RSS.
|
||||
let threads: Int?
|
||||
let restricted: Bool
|
||||
var cpuPower: Double? = nil // Average attributed CPU power, watts; not Energy Impact.
|
||||
var powerWindow: TimeInterval? = nil
|
||||
var formattedPower: String {
|
||||
guard let cpuPower, cpuPower.isFinite, cpuPower >= 0 else { return "--" }
|
||||
return cpuPower >= 1 ? String(format: "%.2f W", cpuPower) : String(format: "%.1f mW", cpuPower * 1000)
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated struct ProcessSnapshot: Sendable {
|
||||
|
||||
@@ -29,15 +29,12 @@ nonisolated struct ProcessEnergyMonitor: Sendable {
|
||||
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() }
|
||||
usages.append(usage)
|
||||
}
|
||||
}
|
||||
}
|
||||
previous = next // Failed / missing readings invalidate the old baseline.
|
||||
usages.sort { $0.watts == $1.watts ? $0.id.pid < $1.id.pid : $0.watts > $1.watts }
|
||||
let state: ProcessEnergySnapshot.State
|
||||
if capability == .unsupported { state = .unsupported }
|
||||
else if capability == .unavailable || processes.failed || (capability == .verifiedCPU && next.isEmpty) { state = .unavailable }
|
||||
|
||||
@@ -11,9 +11,12 @@ final class MetricsStore {
|
||||
private(set) var isSystemSleeping = false
|
||||
private(set) var isDisplaySleeping = false
|
||||
private(set) var isPanelVisible = false
|
||||
private(set) var processUpdatesRequested = false
|
||||
private(set) var slowMetricsRequested = true
|
||||
let sampling: SamplingPreferences
|
||||
private let energyEnabled: Bool
|
||||
private var panelDemand: Bool { isPanelVisible && !isDisplaySleeping }
|
||||
private var processDemand: Bool { panelDemand && processUpdatesRequested }
|
||||
var effectiveMilliseconds: Int { panelDemand ? sampling.panelMilliseconds : sampling.backgroundMilliseconds }
|
||||
private var hasSamplingDemand: Bool { !isSystemSleeping && (panelDemand || sampling.backgroundEnabled) }
|
||||
// Permit timer coalescing without changing the requested cadence or delta timestamps.
|
||||
@@ -72,25 +75,25 @@ final class MetricsStore {
|
||||
while !Task.isCancelled {
|
||||
let sampledAt = ContinuousClock.now
|
||||
let visibleGeneration = self?.visibilityGeneration
|
||||
var next = await sampler.sample(includeProcesses: self?.panelDemand == true,
|
||||
includeSlowMetrics: self?.panelDemand == true,
|
||||
var next = await sampler.sample(includeProcesses: self?.processDemand == true,
|
||||
includeSlowMetrics: self?.panelDemand == true && self?.slowMetricsRequested == true,
|
||||
maximumEnergyInterval: max(30, Double(self?.effectiveMilliseconds ?? 2000) / 500),
|
||||
includeEnergy: self?.energyEnabled == true, demandRevision: visibleGeneration, processUpdate: { [weak self] processes in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.visibilityGeneration == visibleGeneration,
|
||||
self.panelDemand, !self.isPaused, !self.isSystemSleeping else { return }
|
||||
self.processDemand, !self.isPaused, !self.isSystemSleeping else { return }
|
||||
if self.processSnapshot.map({ $0.date > processes.date }) != true { self.processSnapshot = processes }
|
||||
}
|
||||
}, energyUpdate: { [weak self] energy in
|
||||
Task { @MainActor [weak self] in
|
||||
guard let self, self.visibilityGeneration == visibleGeneration,
|
||||
self.panelDemand, !self.isPaused, !self.isSystemSleeping else { return }
|
||||
self.processDemand, !self.isPaused, !self.isSystemSleeping else { return }
|
||||
self.energy = energy
|
||||
SamplingDiagnostics.recordEnergy(energy)
|
||||
}
|
||||
})
|
||||
guard !Task.isCancelled, self?.generation == currentGeneration else { return }
|
||||
if self?.panelDemand != true || self?.visibilityGeneration != visibleGeneration { next.processes = nil }
|
||||
if self?.processDemand != true || self?.visibilityGeneration != visibleGeneration { next.processes = nil }
|
||||
else if let completed = self?.processSnapshot, completed.date >= next.date {
|
||||
// A fast detached process result may reach MainActor before this quick snapshot.
|
||||
next.processes = completed
|
||||
@@ -143,10 +146,18 @@ final class MetricsStore {
|
||||
updateDemand(reset: !sampling.backgroundEnabled, sampleImmediately: panelDemand)
|
||||
}
|
||||
|
||||
/// The caller supplies data needs, without introducing UI types into Core.
|
||||
func setDetailDemand(processes: Bool, slowMetrics: Bool) {
|
||||
guard processUpdatesRequested != processes || slowMetricsRequested != slowMetrics else { return }
|
||||
processUpdatesRequested = processes
|
||||
slowMetricsRequested = slowMetrics
|
||||
updateDemand(reset: false, sampleImmediately: panelDemand)
|
||||
}
|
||||
|
||||
private func updateDemand(reset: Bool, sampleImmediately: Bool = true) {
|
||||
visibilityGeneration += 1
|
||||
let revision = visibilityGeneration
|
||||
let active = panelDemand && !isPaused && !isSystemSleeping
|
||||
let active = processDemand && !isPaused && !isSystemSleeping
|
||||
if !active { processSnapshot = nil }
|
||||
energy = nil
|
||||
Task { await sampler.setProcessVisible(active, revision: revision) }
|
||||
|
||||
Reference in New Issue
Block a user