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
+23 -1
View File
@@ -11,7 +11,7 @@ nonisolated struct ProcessMetrics: Identifiable, Sendable {
let name: String
let path: String?
let cpu: Double? // One logical CPU = 100%; may exceed 1.
let residentBytes: UInt64?
let footprintBytes: UInt64? // Physical footprint; never RSS.
let threads: Int?
let restricted: Bool
}
@@ -41,3 +41,25 @@ nonisolated extension ProcessSnapshot {
highestCPUProcesses(limit: 1).first
}
}
nonisolated struct ProcessMemoryRequest: Codable, Sendable {
let token: UUID
let identities: [ProcessIdentity]
static let limit = 256
static let byteLimit = 65_536
}
nonisolated struct ProcessMemoryReading: Codable, Sendable {
let id: ProcessIdentity
let footprintBytes: UInt64?
}
nonisolated struct ProcessMemoryReply: Codable, Sendable {
let token: UUID
let readings: [ProcessMemoryReading]
}
nonisolated extension ProcessSnapshot {
func withMemory(_ values: [ProcessIdentity: UInt64]) -> ProcessSnapshot {
ProcessSnapshot(date: date, enumerated: enumerated, readable: readable,
rows: rows.map { ProcessMetrics(id: $0.id, name: $0.name, path: $0.path, cpu: $0.cpu,
footprintBytes: values[$0.id], threads: $0.threads, restricted: $0.restricted) }, failed: failed)
}
}
+2 -2
View File
@@ -63,7 +63,7 @@ nonisolated struct ProcessMonitor: Sendable {
}
}
rows.append(ProcessMetrics(id: id, name: name, path: path,
cpu: usage, residentBytes: valid ? info.ptinfo.pti_resident_size : nil,
cpu: usage, footprintBytes: nil,
threads: valid ? Int(info.ptinfo.pti_threadnum) : nil, restricted: !valid))
}
// Recheck the enumeration identity after path reads; never attach a reused PID's path.
@@ -76,7 +76,7 @@ nonisolated struct ProcessMonitor: Sendable {
} else {
paths = [:]
rows = rows.map { ProcessMetrics(id: $0.id, name: $0.name, path: nil, cpu: $0.cpu,
residentBytes: $0.residentBytes, threads: $0.threads, restricted: $0.restricted) }
footprintBytes: $0.footprintBytes, threads: $0.threads, restricted: $0.restricted) }
}
let retained = Set(rows.map(\.id))
previous = next.filter { retained.contains($0.key) }
+2 -1
View File
@@ -161,7 +161,8 @@ actor SystemSampler {
processTask = Task.detached(priority: .utility) { [self] in
var worker = monitor
let value = read(&worker)
await finishProcesses(value, monitor: worker, generation: generation)
let memory = await ProcessMemoryServiceClient.read(value.rows.map(\.id))
await finishProcesses(value.withMemory(memory), monitor: worker, generation: generation)
}
}
@@ -0,0 +1,27 @@
import Foundation
nonisolated enum ProcessMemoryServiceClient {
static func read(_ identities: [ProcessIdentity]) async -> [ProcessIdentity: UInt64] {
var values: [ProcessIdentity: UInt64] = [:]
let deadline = ProcessInfo.processInfo.systemUptime + 3
for start in stride(from: 0, to: identities.count, by: ProcessMemoryRequest.limit) {
let remaining = deadline - ProcessInfo.processInfo.systemUptime
guard !Task.isCancelled, remaining > 0 else { break }
let chunk = Array(identities[start..<min(start + ProcessMemoryRequest.limit, identities.count)])
let request = ProcessMemoryRequest(token: UUID(), identities: chunk)
guard let data = try? JSONEncoder().encode(request), data.count <= ProcessMemoryRequest.byteLimit else { continue }
let reply: ProcessMemoryReply? = await ServiceRequest.perform(timeout: min(1.5, remaining)) { proxy, finish in
proxy.readProcessMemory(data) { data in
guard let data, data.count <= 262_144,
let reply = try? JSONDecoder().decode(ProcessMemoryReply.self, from: data),
reply.token == request.token, reply.readings.count == chunk.count,
Set(reply.readings.map(\.id)) == Set(chunk) else { finish(nil); return }
finish(reply)
}
}
guard let reply else { continue }
for reading in reply.readings { values[reading.id] = reading.footprintBytes }
}
return values
}
}
@@ -2,6 +2,7 @@ import Foundation
/// Fixed read-only operation: no caller-supplied paths or commands.
@objc protocol SMARTServiceProtocol {
nonisolated func readProcessMemory(_ request: Data, withReply reply: @escaping (Data?) -> Void)
nonisolated func readProcessEnergy(_ request: Data, withReply reply: @escaping (Data?) -> Void)
nonisolated func readSensors(cpu: Bool, gpu: Bool, fans: Bool,
withReply reply: @escaping (NSNumber?, NSNumber?, [NSNumber]?) -> Void)