perf(efficiency): 落实首批采样减负与发行包瘦身
This commit is contained in:
@@ -4,6 +4,7 @@ import Darwin
|
||||
nonisolated struct ProcessMonitor: Sendable {
|
||||
private var previous: [ProcessIdentity: (UInt64, TimeInterval)] = [:]
|
||||
private var sampleSerial: UInt64 = 0
|
||||
private var paths: [ProcessIdentity: (name: String, path: String?, expires: TimeInterval)] = [:]
|
||||
|
||||
static func cpu(current: UInt64, previous: UInt64?, elapsed: TimeInterval) -> Double? {
|
||||
guard let previous, current >= previous, elapsed.isFinite, elapsed > 0 else { return nil }
|
||||
@@ -15,11 +16,13 @@ nonisolated struct ProcessMonitor: Sendable {
|
||||
let date = Date()
|
||||
guard let entries = Self.entries() else {
|
||||
previous = [:]
|
||||
paths = [:]
|
||||
return ProcessSnapshot(date: date, enumerated: 0, readable: 0, rows: [], failed: true)
|
||||
}
|
||||
let now = ProcessInfo.processInfo.systemUptime
|
||||
var next: [ProcessIdentity: (UInt64, TimeInterval)] = [:]
|
||||
var rows: [ProcessMetrics] = []
|
||||
var pathBuffer = [CChar](repeating: 0, count: 4 * Int(MAXPATHLEN))
|
||||
for entry in entries where entry.kp_proc.p_pid > 0 {
|
||||
let pid = entry.kp_proc.p_pid
|
||||
var info = proc_taskallinfo()
|
||||
@@ -34,8 +37,14 @@ nonisolated struct ProcessMonitor: Sendable {
|
||||
let name = withUnsafeBytes(of: ©) { raw in
|
||||
String(decoding: raw.prefix { $0 != 0 }, as: UTF8.self)
|
||||
}
|
||||
var path = [CChar](repeating: 0, count: 4 * Int(MAXPATHLEN))
|
||||
let pathSize = proc_pidpath(pid, &path, UInt32(path.count))
|
||||
let path: String?
|
||||
if let cached = paths[id], seconds > 0, cached.name == name, now < cached.expires {
|
||||
path = cached.path
|
||||
} else {
|
||||
let size = proc_pidpath(pid, &pathBuffer, UInt32(pathBuffer.count))
|
||||
path = size > 0 ? String(cString: pathBuffer) : nil
|
||||
paths[id] = (name, path, now + 30)
|
||||
}
|
||||
var usage: Double?
|
||||
if valid && seconds > 0 {
|
||||
let (total, overflow) = info.ptinfo.pti_total_user.addingReportingOverflow(info.ptinfo.pti_total_system)
|
||||
@@ -44,7 +53,7 @@ nonisolated struct ProcessMonitor: Sendable {
|
||||
next[id] = (total, now)
|
||||
}
|
||||
}
|
||||
rows.append(ProcessMetrics(id: id, name: name, path: pathSize > 0 ? String(cString: path) : nil,
|
||||
rows.append(ProcessMetrics(id: id, name: name, path: path,
|
||||
cpu: usage, residentBytes: valid ? info.ptinfo.pti_resident_size : nil,
|
||||
threads: valid ? Int(info.ptinfo.pti_threadnum) : nil, restricted: !valid))
|
||||
}
|
||||
@@ -56,11 +65,13 @@ nonisolated struct ProcessMonitor: Sendable {
|
||||
})
|
||||
rows.removeAll { $0.id.seconds > 0 && !identities.contains($0.id) }
|
||||
} else {
|
||||
paths = [:]
|
||||
rows = rows.map { ProcessMetrics(id: $0.id, name: $0.name, path: nil, cpu: $0.cpu,
|
||||
residentBytes: $0.residentBytes, threads: $0.threads, restricted: $0.restricted) }
|
||||
}
|
||||
let retained = Set(rows.map(\.id))
|
||||
previous = next.filter { retained.contains($0.key) }
|
||||
paths = paths.filter { retained.contains($0.key) && $0.key.seconds > 0 }
|
||||
return ProcessSnapshot(date: date, enumerated: entries.filter { $0.kp_proc.p_pid > 0 }.count,
|
||||
readable: rows.filter { !$0.restricted }.count, rows: rows, failed: false)
|
||||
}
|
||||
|
||||
@@ -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