fix(processes): 对齐活动监视器内存口径与显示单位
This commit is contained in:
@@ -13,7 +13,7 @@ import Observation
|
||||
rows = (0..<500).map { index in
|
||||
ProcessMetrics(id: ProcessIdentity(pid: Int32(index + 1), seconds: 1, microseconds: 0),
|
||||
name: "worker-\(index)", path: nil, cpu: Double(index) / 100,
|
||||
residentBytes: 10_000_000, threads: 2, restricted: true)
|
||||
footprintBytes: 10_000_000, threads: 2, restricted: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,8 @@ private struct Host: View {
|
||||
precondition(table.numberOfRows == 500 && table.tableColumns.count == 6)
|
||||
precondition(table.frame.width >= table.enclosingScrollView!.contentSize.width - 1, "Native table must fill its viewport")
|
||||
precondition(coordinator.createdCells > 0 && coordinator.createdCells < 200, "Rows must be virtualized")
|
||||
let memoryCell = table.view(atColumn: 4, row: 0, makeIfNecessary: true) as! NSTableCellView
|
||||
precondition(memoryCell.textField?.stringValue == "9.5 MB", "Memory column must match Activity Monitor binary MB display")
|
||||
let chosen = state.rows[10].id
|
||||
table.selectRowIndexes(IndexSet(integer: 10), byExtendingSelection: false)
|
||||
precondition(state.selection == nil, "Native selection must be deferred")
|
||||
@@ -72,7 +74,7 @@ private struct Host: View {
|
||||
await until { table.selectedRow == 489 }
|
||||
precondition(state.selection == chosen, "Selection must follow identity after reordering")
|
||||
for index in 0..<12 {
|
||||
state.rows[0] = ProcessMetrics(id: state.rows[0].id, name: "updated-\(index)", path: nil, cpu: Double(index), residentBytes: nil, threads: nil, restricted: false)
|
||||
state.rows[0] = ProcessMetrics(id: state.rows[0].id, name: "updated-\(index)", path: nil, cpu: Double(index), footprintBytes: nil, threads: nil, restricted: false)
|
||||
try? await Task.sleep(for: .milliseconds(20))
|
||||
}
|
||||
let visible = table.rows(in: table.visibleRect)
|
||||
|
||||
@@ -32,7 +32,7 @@ import CoreGraphics
|
||||
let id = ProcessIdentity(pid: Int32(index + 1), seconds: 1, microseconds: 0)
|
||||
let path: String? = index == 42 ? "/test/needle" : nil
|
||||
return ProcessMetrics(id: id, name: "worker-\(index)",
|
||||
path: path, cpu: Double(index), residentBytes: UInt64(2000 - index), threads: 1, restricted: false)
|
||||
path: path, cpu: Double(index), footprintBytes: UInt64(2000 - index), threads: 1, restricted: false)
|
||||
}
|
||||
let cpu = ProcessTableProjection.rows(input, query: "", sorting: [ProcessSort(key: .cpu)], order: [])
|
||||
precondition(cpu.first?.id.pid == 2000 && cpu.count == 2000)
|
||||
@@ -46,7 +46,7 @@ import CoreGraphics
|
||||
let ascending = ProcessTableProjection.rows(input, query: "", sorting: [ProcessSort(key: .cpu, order: .forward)], order: [])
|
||||
precondition(ascending.first?.id.pid == 1)
|
||||
let missing = ProcessMetrics(id: ProcessIdentity(pid: 3000, seconds: 1, microseconds: 0), name: "missing", path: nil,
|
||||
cpu: nil, residentBytes: nil, threads: nil, restricted: true)
|
||||
cpu: nil, footprintBytes: nil, threads: nil, restricted: true)
|
||||
for direction in [SortOrder.forward, .reverse] {
|
||||
for key in [ProcessSort.Key.cpu, .memory, .threads] {
|
||||
let result = ProcessTableProjection.rows(input + [missing], query: "", sorting: [ProcessSort(key: key, order: direction)], order: [])
|
||||
|
||||
@@ -22,7 +22,7 @@ import Foundation
|
||||
precondition(ProcessEnergyMonitor.watts(current: invalid, previous: reading(5, at: 10)) == nil)
|
||||
}
|
||||
func row(_ id: ProcessIdentity, _ cpu: Double) -> ProcessMetrics {
|
||||
ProcessMetrics(id: id, name: "fixture", path: nil, cpu: cpu, residentBytes: nil, threads: nil, restricted: false)
|
||||
ProcessMetrics(id: id, name: "fixture", path: nil, cpu: cpu, footprintBytes: nil, threads: nil, restricted: false)
|
||||
}
|
||||
let id2 = ProcessIdentity(pid: 43, seconds: 100, microseconds: 2)
|
||||
let processes = ProcessSnapshot(date: Date(), enumerated: 3, readable: 2, rows: [row(id, 5), row(id2, 0.01)], failed: false)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -4,7 +4,7 @@ import Foundation
|
||||
static func main() {
|
||||
let id = ProcessIdentity(pid: 42, seconds: 100, microseconds: 0)
|
||||
let row = ProcessMetrics(id: id, name: "worker", path: "/test/worker", cpu: 0.2,
|
||||
residentBytes: 10, threads: 1, restricted: false)
|
||||
footprintBytes: 10, threads: 1, restricted: false)
|
||||
let live = ProcessSnapshot(date: Date(), enumerated: 1, readable: 1, rows: [row], failed: false)
|
||||
let filtered = ProcessTableProjection.rows(live.rows, query: "does-not-match", sorting: [], order: [])
|
||||
precondition(filtered.isEmpty)
|
||||
@@ -17,10 +17,17 @@ import Foundation
|
||||
precondition(ProcessTableProjection.exitedSelection(id, in: exited) == id)
|
||||
let reused = ProcessIdentity(pid: 42, seconds: 101, microseconds: 0)
|
||||
let replacement = ProcessMetrics(id: reused, name: row.name, path: row.path, cpu: nil,
|
||||
residentBytes: nil, threads: nil, restricted: true)
|
||||
footprintBytes: nil, threads: nil, restricted: true)
|
||||
let recycled = ProcessSnapshot(date: Date(), enumerated: 1, readable: 0, rows: [replacement], failed: false)
|
||||
precondition(ProcessTableProjection.exitedSelection(id, in: recycled) == id, "PID reuse cannot keep the previous process alive")
|
||||
precondition(ProcessTableProjection.exitedSelection(reused, in: recycled) == nil)
|
||||
let large = ProcessMetrics(id: ProcessIdentity(pid: 43, seconds: 1, microseconds: 0), name: "large", path: nil,
|
||||
cpu: nil, footprintBytes: 1_000, threads: nil, restricted: false)
|
||||
for direction in [SortOrder.forward, .reverse] {
|
||||
let sorted = ProcessTableProjection.rows([replacement, row, large], query: "", sorting: [ProcessSort(key: .memory, order: direction)], order: [])
|
||||
precondition(sorted.last?.id == replacement.id, "Missing footprint sorts last in both directions")
|
||||
precondition(sorted.first?.id == (direction == .forward ? row.id : large.id))
|
||||
}
|
||||
print("PASS: filtered/live selection, absent selection/snapshot, failed enumeration, actual exit and PID reuse")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ import Foundation
|
||||
precondition(GPUMonitor.normalized(53) == 0.53)
|
||||
precondition(GPUMonitor.normalized(101) == nil && GPUMonitor.normalized(.nan) == nil)
|
||||
let processRows = [
|
||||
ProcessMetrics(id: ProcessIdentity(pid: 1, seconds: 1, microseconds: 1), name: "unknown", path: nil, cpu: nil, residentBytes: nil, threads: nil, restricted: true),
|
||||
ProcessMetrics(id: ProcessIdentity(pid: 2, seconds: 1, microseconds: 2), name: "busy", path: nil, cpu: 2.5, residentBytes: nil, threads: nil, restricted: false),
|
||||
ProcessMetrics(id: ProcessIdentity(pid: 3, seconds: 1, microseconds: 3), name: "idle", path: nil, cpu: 0, residentBytes: nil, threads: nil, restricted: false),
|
||||
ProcessMetrics(id: ProcessIdentity(pid: 4, seconds: 1, microseconds: 4), name: "invalid", path: nil, cpu: -.infinity, residentBytes: nil, threads: nil, restricted: false)
|
||||
ProcessMetrics(id: ProcessIdentity(pid: 1, seconds: 1, microseconds: 1), name: "unknown", path: nil, cpu: nil, footprintBytes: nil, threads: nil, restricted: true),
|
||||
ProcessMetrics(id: ProcessIdentity(pid: 2, seconds: 1, microseconds: 2), name: "busy", path: nil, cpu: 2.5, footprintBytes: nil, threads: nil, restricted: false),
|
||||
ProcessMetrics(id: ProcessIdentity(pid: 3, seconds: 1, microseconds: 3), name: "idle", path: nil, cpu: 0, footprintBytes: nil, threads: nil, restricted: false),
|
||||
ProcessMetrics(id: ProcessIdentity(pid: 4, seconds: 1, microseconds: 4), name: "invalid", path: nil, cpu: -.infinity, footprintBytes: nil, threads: nil, restricted: false)
|
||||
]
|
||||
let processSnapshot = ProcessSnapshot(date: Date(), enumerated: 4, readable: 3, rows: processRows, failed: false)
|
||||
precondition(processSnapshot.highestCPUProcess?.name == "busy")
|
||||
|
||||
Reference in New Issue
Block a user