fix(processes): 对齐活动监视器内存口径与显示单位

This commit is contained in:
2026-11-04 14:28:56 +08:00
parent 44f0415ab7
commit bbec1325a1
23 changed files with 195 additions and 26 deletions
+31
View File
@@ -13,6 +13,37 @@ import Darwin
precondition(reply.token == request.token && reply.readings.count == ids.count)
return reply
}
func memory(_ ids: [ProcessIdentity]) throws -> ProcessMemoryReply {
let request = ProcessMemoryRequest(token: UUID(), identities: ids)
let reply = try JSONDecoder().decode(ProcessMemoryReply.self, from: ProcessMemoryReader.read(try JSONEncoder().encode(request))!)
precondition(reply.token == request.token && reply.readings.count == ids.count)
return reply
}
var vm = task_vm_info_data_t()
var count = mach_msg_type_number_t(MemoryLayout.size(ofValue: vm) / MemoryLayout<integer_t>.size)
precondition(withUnsafeMutablePointer(to: &vm) { pointer in
pointer.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count) }
} == KERN_SUCCESS)
let memoryBytes = try memory([id]).readings[0].footprintBytes!
precondition(abs(Double(memoryBytes) - Double(vm.phys_footprint)) < 4_000_000, "Must agree with task physical footprint, not RSS")
let wrongMemoryID = ProcessIdentity(pid: id.pid, seconds: id.seconds + 1, microseconds: id.microseconds)
let wrongMemory = try memory([wrongMemoryID]); precondition(wrongMemory.readings[0].footprintBytes == nil)
let zeroPID = try memory([ProcessIdentity(pid: 0, seconds: 1, microseconds: 0)]); precondition(zeroPID.readings[0].footprintBytes == nil)
let otherUID = try memory([ProcessIdentity(pid: 1, seconds: 1, microseconds: 0)]); precondition(otherUID.readings[0].footprintBytes == nil)
let invalidMemory = try JSONEncoder().encode(ProcessMemoryRequest(token: UUID(), identities: [id, id]))
precondition(ProcessMemoryReader.read(invalidMemory) == nil)
precondition(ProcessMemoryReader.read(Data(repeating: 0, count: ProcessMemoryRequest.byteLimit + 1)) == nil)
precondition(ProcessMemoryReader.read(Data("bad".utf8)) == nil)
let tooManyMemory = try JSONEncoder().encode(ProcessMemoryRequest(token: UUID(), identities: (1...257).map {
ProcessIdentity(pid: Int32($0), seconds: 1, microseconds: 0)
}))
precondition(ProcessMemoryReader.read(tooManyMemory) == nil)
let snapshot = ProcessSnapshot(date: Date(), enumerated: 1, readable: 1, rows: [
ProcessMetrics(id: id, name: "test", path: nil, cpu: nil, footprintBytes: 123, threads: nil, restricted: false)
], failed: false)
precondition(snapshot.withMemory([wrongMemoryID: 999]).rows[0].footprintBytes == nil, "Never attach reused PID memory or retain a stale value")
precondition(snapshot.withMemory([id: 0]).rows[0].footprintBytes == 0, "Valid zero is distinct from unavailable")
print("PASS: physical footprint matches TASK_VM_INFO; memory identity, request bounds, missing/zero and stale-value checks")
usleep(10_000)
let first = try read([id]).readings[0]
#if arch(arm64)