perf(efficiency): 落实首批采样减负与发行包瘦身
This commit is contained in:
@@ -8,7 +8,9 @@ actor SystemSampler {
|
||||
private var temperatureCache: Double?
|
||||
private var gpuTemperatureCache: Double?
|
||||
private var fanCache: [FanMetrics]?
|
||||
private var nextTemperature: TimeInterval = 0
|
||||
private var cpuSchedule = SensorSchedule()
|
||||
private var gpuSchedule = SensorSchedule()
|
||||
private var fanSchedule = SensorSchedule()
|
||||
private var didProbe = false
|
||||
private let diskHealth = DiskHealthMonitor()
|
||||
private let batteryHealth = SystemBatteryHealthMonitor()
|
||||
@@ -50,7 +52,8 @@ actor SystemSampler {
|
||||
// A synchronous OS read may outlive cancellation. Keep the in-flight slot occupied.
|
||||
}
|
||||
|
||||
func sample(includeProcesses: Bool? = nil, processUpdate: (@Sendable (ProcessSnapshot) -> Void)? = nil) -> MetricsSnapshot {
|
||||
func sample(includeProcesses: Bool? = nil, includeSlowMetrics: Bool = true,
|
||||
processUpdate: (@Sendable (ProcessSnapshot) -> Void)? = nil) -> MetricsSnapshot {
|
||||
guard !Task.isCancelled else { return MetricsSnapshot() }
|
||||
if let includeProcesses { setProcessVisible(includeProcesses) }
|
||||
self.processUpdate = processUpdate
|
||||
@@ -61,12 +64,13 @@ actor SystemSampler {
|
||||
let start = ProcessInfo.processInfo.systemUptime
|
||||
var result = MetricsSnapshot()
|
||||
result.cpu = cpu.sample()
|
||||
if temperatureTask == nil && start >= nextTemperature {
|
||||
let readCPU = cpuSchedule.isDue(at: start)
|
||||
let readGPU = gpuSchedule.isDue(at: start)
|
||||
let readFans = fanSchedule.isDue(at: start)
|
||||
if includeSlowMetrics && temperatureTask == nil && (readCPU || readGPU || readFans) {
|
||||
temperatureTask = Task { [self] in
|
||||
async let cpu = CPUTemperatureServiceClient.read()
|
||||
async let gpu = CPUTemperatureServiceClient.read(gpu: true)
|
||||
async let fans = CPUTemperatureServiceClient.readFans()
|
||||
await finishTemperature(cpu, gpu: gpu, fans: fans)
|
||||
let readings = await CPUTemperatureServiceClient.readSensors(cpu: readCPU, gpu: readGPU, fans: readFans)
|
||||
finishTemperature(readings, cpu: readCPU, gpu: readGPU, fans: readFans)
|
||||
}
|
||||
}
|
||||
result.cpuTemperature = temperatureCache
|
||||
@@ -82,16 +86,16 @@ actor SystemSampler {
|
||||
result.batteryState = result.battery == nil ? BatteryMonitor.availability() : .present
|
||||
result.gpu = GPUMonitor.sample()
|
||||
result.gpuUsage = result.gpu?.usage
|
||||
if volumeTask == nil && start - volumeSampleTime >= 30 {
|
||||
if includeSlowMetrics && volumeTask == nil && start - volumeSampleTime >= 30 {
|
||||
volumeTask = Task.detached(priority: .utility) { [self] in
|
||||
let value = DiskMonitor.volumes()
|
||||
await finishVolumes(value)
|
||||
}
|
||||
}
|
||||
if diskTask == nil && start >= nextDisk {
|
||||
if includeSlowMetrics && diskTask == nil && start >= nextDisk {
|
||||
diskTask = Task { [self, diskHealth] in await finishDisk(diskHealth.sample()) }
|
||||
}
|
||||
if result.battery != nil && batteryTask == nil && start >= nextBattery {
|
||||
if includeSlowMetrics && result.battery != nil && batteryTask == nil && start >= nextBattery {
|
||||
batteryTask = Task { [self, batteryHealth] in await finishBattery(batteryHealth.sample()) }
|
||||
}
|
||||
startProcessesIfNeeded()
|
||||
@@ -111,11 +115,11 @@ actor SystemSampler {
|
||||
return result
|
||||
}
|
||||
|
||||
private func finishTemperature(_ value: Double?, gpu: Double?, fans: [FanMetrics]?) {
|
||||
fanCache = fans
|
||||
gpuTemperatureCache = gpu
|
||||
temperatureCache = value
|
||||
nextTemperature = ProcessInfo.processInfo.systemUptime + (value == nil && gpu == nil && fans == nil ? 60 : 5)
|
||||
private func finishTemperature(_ readings: SensorReadings?, cpu: Bool, gpu: Bool, fans: Bool) {
|
||||
let now = ProcessInfo.processInfo.systemUptime
|
||||
if cpu { temperatureCache = readings?.cpu; cpuSchedule.complete(available: temperatureCache != nil, at: now) }
|
||||
if gpu { gpuTemperatureCache = readings?.gpu; gpuSchedule.complete(available: gpuTemperatureCache != nil, at: now) }
|
||||
if fans { fanCache = readings?.fans; fanSchedule.complete(available: fanCache != nil, at: now) }
|
||||
temperatureTask = nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user