feat(monitors): 实现 CPU 内存采样诊断与菜单栏实时指标
接入 Mach CPU 差分、内存与负载采样、暂停恢复及按需诊断,合并菜单栏单一原生标题的显示修复。 保留核心检查和本机交互验收记录;长期资源预算仍待验收。
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import Foundation
|
||||
import Darwin
|
||||
|
||||
nonisolated struct CPUMonitor {
|
||||
private var previous: [[UInt32]]?
|
||||
|
||||
mutating func sample() -> CPUMetrics? {
|
||||
var count: natural_t = 0
|
||||
var info: processor_info_array_t?
|
||||
var infoCount: mach_msg_type_number_t = 0
|
||||
guard host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO,
|
||||
&count, &info, &infoCount) == KERN_SUCCESS,
|
||||
let info else { previous = nil; return nil }
|
||||
defer {
|
||||
vm_deallocate(mach_task_self_, vm_address_t(UInt(bitPattern: info)),
|
||||
vm_size_t(infoCount) * vm_size_t(MemoryLayout<integer_t>.stride))
|
||||
}
|
||||
let ticks = (0..<Int(count)).map { core in
|
||||
(0..<Int(CPU_STATE_MAX)).map { UInt32(bitPattern: info[core * Int(CPU_STATE_MAX) + $0]) }
|
||||
}
|
||||
defer { previous = ticks }
|
||||
guard let previous, previous.count == ticks.count else { return nil }
|
||||
return Self.calculate(current: ticks, previous: previous)
|
||||
}
|
||||
|
||||
static func calculate(current ticks: [[UInt32]], previous: [[UInt32]]) -> CPUMetrics? {
|
||||
guard !ticks.isEmpty, ticks.count == previous.count,
|
||||
ticks.allSatisfy({ $0.count == Int(CPU_STATE_MAX) }),
|
||||
previous.allSatisfy({ $0.count == Int(CPU_STATE_MAX) }) else { return nil }
|
||||
let delta = zip(ticks, previous).map { now, old in zip(now, old).map { $0 &- $1 } }
|
||||
let totals = delta.map { $0.reduce(0.0) { $0 + Double($1) } }
|
||||
let total = totals.reduce(0, +)
|
||||
guard total > 0 else { return nil }
|
||||
let user = delta.reduce(0.0) { $0 + Double($1[Int(CPU_STATE_USER)]) + Double($1[Int(CPU_STATE_NICE)]) }
|
||||
let system = delta.reduce(0.0) { $0 + Double($1[Int(CPU_STATE_SYSTEM)]) }
|
||||
let cores = zip(delta, totals).map { values, total in
|
||||
total > 0 ? 1 - Double(values[Int(CPU_STATE_IDLE)]) / total : 0
|
||||
}
|
||||
return CPUMetrics(usage: (user + system) / total, user: user / total,
|
||||
system: system / total, cores: cores)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import Foundation
|
||||
import Darwin
|
||||
|
||||
nonisolated enum MemoryMonitor {
|
||||
static func sample() -> MemoryMetrics? {
|
||||
var info = vm_statistics64()
|
||||
var count = mach_msg_type_number_t(MemoryLayout<vm_statistics64>.size / MemoryLayout<integer_t>.size)
|
||||
let status = withUnsafeMutablePointer(to: &info) { pointer in
|
||||
pointer.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
|
||||
host_statistics64(mach_host_self(), HOST_VM_INFO64, $0, &count)
|
||||
}
|
||||
}
|
||||
guard status == KERN_SUCCESS else { return nil }
|
||||
let page = UInt64(vm_kernel_page_size)
|
||||
let total = ProcessInfo.processInfo.physicalMemory
|
||||
let wired = UInt64(info.wire_count) * page
|
||||
let compressed = UInt64(info.compressor_page_count) * page
|
||||
let internalBytes = UInt64(info.internal_page_count) * page
|
||||
let purgeable = UInt64(info.purgeable_count) * page
|
||||
let app = internalBytes > purgeable ? internalBytes - purgeable : 0
|
||||
var swap = xsw_usage()
|
||||
var size = MemoryLayout<xsw_usage>.size
|
||||
let swapOK = sysctlbyname("vm.swapusage", &swap, &size, nil, 0) == 0
|
||||
var pressure: Int32 = 0
|
||||
size = MemoryLayout<Int32>.size
|
||||
let pressureOK = sysctlbyname("kern.memorystatus_vm_pressure_level", &pressure, &size, nil, 0) == 0
|
||||
return MemoryMetrics(total: total, used: min(total, app + wired + compressed),
|
||||
wired: wired, compressed: compressed,
|
||||
swapUsed: swapOK ? swap.xsu_used : nil,
|
||||
pressure: pressureOK ? Int(pressure) : nil)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import Foundation
|
||||
import Darwin
|
||||
|
||||
/// Serial ownership keeps CPU baselines coherent and system calls off MainActor.
|
||||
actor SystemSampler {
|
||||
private var cpu = CPUMonitor()
|
||||
|
||||
func reset() { cpu = CPUMonitor() }
|
||||
|
||||
func sample() -> MetricsSnapshot {
|
||||
var result = MetricsSnapshot()
|
||||
result.cpu = cpu.sample()
|
||||
result.memory = MemoryMonitor.sample()
|
||||
var load = [Double](repeating: 0, count: 3)
|
||||
if getloadavg(&load, 3) == 3 { result.load = load }
|
||||
return result
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user