fix(monitors): 补齐联网权限并释放采样端口引用

This commit is contained in:
2026-11-03 21:59:49 +08:00
parent cecd646376
commit ea4d0e7f23
7 changed files with 89 additions and 4 deletions
+4 -1
View File
@@ -5,10 +5,13 @@ nonisolated struct CPUMonitor {
private var previous: [[UInt32]]?
mutating func sample() -> CPUMetrics? {
// mach_host_self() returns a send right owned by this call, including failure paths.
let host = mach_host_self()
defer { mach_port_deallocate(mach_task_self_, host) }
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,
guard host_processor_info(host, PROCESSOR_CPU_LOAD_INFO,
&count, &info, &infoCount) == KERN_SUCCESS,
let info else { previous = nil; return nil }
defer {
+4 -1
View File
@@ -3,11 +3,14 @@ import Darwin
nonisolated enum MemoryMonitor {
static func sample() -> MemoryMetrics? {
// mach_host_self() returns a send right owned by this call, including failure paths.
let host = mach_host_self()
defer { mach_port_deallocate(mach_task_self_, host) }
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)
host_statistics64(host, HOST_VM_INFO64, $0, &count)
}
}
guard status == KERN_SUCCESS else { return nil }