feat(storage): 新增只读 SMART 辅助服务与寿命通电详情
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import Foundation
|
||||
|
||||
/// SMART belongs to the physical storage backing the home volume, not each APFS volume.
|
||||
actor DiskHealthMonitor {
|
||||
private var cached: DiskHealthMetrics?
|
||||
private var nextRead: TimeInterval = 0
|
||||
private var pending: Task<DiskHealthMetrics?, Never>?
|
||||
|
||||
func sample() async -> DiskHealthMetrics? {
|
||||
if let pending { return await pending.value }
|
||||
guard ProcessInfo.processInfo.systemUptime >= nextRead else { return cached }
|
||||
let task = Task { await SMARTServiceClient.read() }
|
||||
pending = task
|
||||
cached = await task.value
|
||||
pending = nil
|
||||
nextRead = ProcessInfo.processInfo.systemUptime + (cached == nil ? 600 : 3600)
|
||||
return cached
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import Foundation
|
||||
|
||||
nonisolated enum DiskHealthReport {
|
||||
static func parse(_ data: Data) -> DiskHealthMetrics? {
|
||||
guard let root = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any],
|
||||
let smart = root["SMARTDeviceSpecificKeysMayVaryNotGuaranteed"] as? [String: Any] else { return nil }
|
||||
// Multiple physical stores cannot be represented as one device's lifetime.
|
||||
if let stores = root["APFSPhysicalStores"] as? [Any], stores.count != 1 { return nil }
|
||||
func integer(_ key: String) -> UInt64? {
|
||||
guard let value = smart[key] as? NSNumber,
|
||||
CFGetTypeID(value) != CFBooleanGetTypeID() else { return nil }
|
||||
return UInt64(value.stringValue)
|
||||
}
|
||||
let used = integer("PERCENTAGE_USED").flatMap { $0 <= 255 ? $0 : nil }
|
||||
let remaining = used.map { Double(100 - min(100, $0)) / 100 }
|
||||
let hours = integer("POWER_ON_HOURS_1") == 0 ? integer("POWER_ON_HOURS_0") : nil
|
||||
guard remaining != nil || hours != nil else { return nil }
|
||||
return DiskHealthMetrics(remainingLife: remaining, powerOnHours: hours)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import Darwin
|
||||
/// Serial ownership keeps CPU baselines coherent and system calls off MainActor.
|
||||
actor SystemSampler {
|
||||
private var cpu = CPUMonitor()
|
||||
private let diskHealth = DiskHealthMonitor()
|
||||
private let batteryHealth = SystemBatteryHealthMonitor()
|
||||
|
||||
private var network = NetworkMonitor()
|
||||
@@ -21,6 +22,7 @@ actor SystemSampler {
|
||||
result.network = network.sample()
|
||||
result.networkPath = networkPath.snapshot()
|
||||
result.disk = DiskMonitor.sample()
|
||||
result.diskHealth = await diskHealth.sample()
|
||||
result.diskIO = diskIO.sample()
|
||||
let now = ProcessInfo.processInfo.systemUptime
|
||||
if volumeSampleTime == nil || now - (volumeSampleTime ?? 0) >= 30 {
|
||||
|
||||
Reference in New Issue
Block a user