feat(monitors): 实现 CPU 内存采样诊断与菜单栏实时指标
接入 Mach CPU 差分、内存与负载采样、暂停恢复及按需诊断,合并菜单栏单一原生标题的显示修复。 保留核心检查和本机交互验收记录;长期资源预算仍待验收。
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
import Foundation
|
||||
import Darwin
|
||||
|
||||
/// Explicit opt-in for local verification. Normal runs perform no diagnostics IO.
|
||||
nonisolated enum SamplingDiagnostics {
|
||||
static let enabled = ProcessInfo.processInfo.environment["KYMO_DIAGNOSTICS"] == "1"
|
||||
|
||||
static func record(_ snapshot: MetricsSnapshot, event: String = "sample") {
|
||||
guard enabled else { return }
|
||||
var payload: [String: Any] = [
|
||||
"event": event, "timestamp": snapshot.date.timeIntervalSince1970,
|
||||
"pid": ProcessInfo.processInfo.processIdentifier,
|
||||
"load": snapshot.load
|
||||
]
|
||||
payload["cpu"] = snapshot.cpu?.usage
|
||||
payload["cores"] = snapshot.cpu?.cores.count
|
||||
payload["memoryUsed"] = snapshot.memory?.used
|
||||
payload["memoryTotal"] = snapshot.memory?.total
|
||||
payload["swapUsed"] = snapshot.memory?.swapUsed
|
||||
payload["pressure"] = snapshot.memory?.pressure
|
||||
var info = task_vm_info_data_t()
|
||||
var count = mach_msg_type_number_t(MemoryLayout<task_vm_info_data_t>.size / MemoryLayout<integer_t>.size)
|
||||
let status = withUnsafeMutablePointer(to: &info) { pointer in
|
||||
pointer.withMemoryRebound(to: integer_t.self, capacity: Int(count)) {
|
||||
task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count)
|
||||
}
|
||||
}
|
||||
if status == KERN_SUCCESS {
|
||||
payload["residentBytes"] = info.resident_size
|
||||
payload["footprintBytes"] = info.phys_footprint
|
||||
}
|
||||
guard let data = try? JSONSerialization.data(withJSONObject: payload, options: [.sortedKeys]) else { return }
|
||||
try? FileHandle.standardOutput.write(contentsOf: data + Data([10]))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user