feat(monitors): 实现 CPU 内存采样诊断与菜单栏实时指标
接入 Mach CPU 差分、内存与负载采样、暂停恢复及按需诊断,合并菜单栏单一原生标题的显示修复。 保留核心检查和本机交互验收记录;长期资源预算仍待验收。
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
@MainActor @Observable
|
||||
final class MetricsStore {
|
||||
private(set) var snapshot = MetricsSnapshot()
|
||||
private(set) var isPaused = false
|
||||
@ObservationIgnored private var task: Task<Void, Never>?
|
||||
@ObservationIgnored private let sampler = SystemSampler()
|
||||
|
||||
init() { start() }
|
||||
|
||||
func start() {
|
||||
guard task == nil else { return }
|
||||
isPaused = false
|
||||
task = Task { [weak self, sampler] in
|
||||
await sampler.reset()
|
||||
while !Task.isCancelled {
|
||||
let next = await sampler.sample()
|
||||
guard !Task.isCancelled else { return }
|
||||
self?.snapshot = next
|
||||
SamplingDiagnostics.record(next)
|
||||
do { try await Task.sleep(for: .seconds(2)) }
|
||||
catch { return }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func togglePause() {
|
||||
if isPaused { start() }
|
||||
else {
|
||||
task?.cancel()
|
||||
task = nil
|
||||
isPaused = true
|
||||
SamplingDiagnostics.record(snapshot, event: "pause")
|
||||
}
|
||||
}
|
||||
|
||||
deinit { task?.cancel() }
|
||||
}
|
||||
Reference in New Issue
Block a user