fix(processes): 对齐活动监视器内存口径与显示单位
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -147,7 +147,7 @@ import SwiftUI
|
||||
case "name": text = process.name
|
||||
case "path": text = process.path ?? "--"
|
||||
case "cpu": text = process.cpu.map { String(format: "%.1f", $0 * 100) } ?? "--"
|
||||
case "memory": text = process.residentBytes.map { String(format: "%.1f MB", Double($0) / 1_000_000) } ?? "--"
|
||||
case "memory": text = process.footprintBytes.map { String(format: "%.1f MB", Double($0) / 1_048_576) } ?? "--"
|
||||
default: text = process.threads.map(String.init) ?? "--"
|
||||
}
|
||||
if cell.textField?.stringValue != text { cell.textField?.stringValue = text }
|
||||
|
||||
@@ -45,7 +45,7 @@ nonisolated struct ProcessSort: SortComparator, Equatable {
|
||||
case .name: return directed(lhs.name.localizedStandardCompare(rhs.name))
|
||||
case .path: return directed((lhs.path ?? "").localizedStandardCompare(rhs.path ?? ""))
|
||||
case .cpu: return metric(lhs.cpu, rhs.cpu)
|
||||
case .memory: return metric(lhs.residentBytes.map { Double($0) }, rhs.residentBytes.map { Double($0) })
|
||||
case .memory: return metric(lhs.footprintBytes.map { Double($0) }, rhs.footprintBytes.map { Double($0) })
|
||||
case .threads: return metric(lhs.threads.map { Double($0) }, rhs.threads.map { Double($0) })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ private struct TextReportHost: NSViewRepresentable {
|
||||
c.rows = ProcessTableProjection.rows(processes?.rows ?? [], query: "", sorting: [ProcessSort(key: .cpu)], order: [])
|
||||
}
|
||||
let heading = Self.resourceText(snapshot) + "\nHistory points retained: \(historyPoints)\nPublic IP: \(publicAddress ?? "--")\n\n" + Self.energyText(energy)
|
||||
+ "\nProcesses: \(processes?.readable ?? 0)/\(processes?.enumerated ?? 0), failed=\(processes?.failed ?? false)\nPID\tCPU %\tRSS bytes\tThreads\tRestricted\tName\tPath"
|
||||
+ "\nProcesses: \(processes?.readable ?? 0)/\(processes?.enumerated ?? 0), failed=\(processes?.failed ?? false)\nPID\tCPU %\tFootprint bytes\tThreads\tRestricted\tName\tPath"
|
||||
c.lines = heading.components(separatedBy: "\n")
|
||||
let origin = scroll.contentView.bounds.origin
|
||||
let oldCount = table.numberOfRows
|
||||
@@ -150,7 +150,7 @@ private struct TextReportHost: NSViewRepresentable {
|
||||
return "CPU energy: \(energy.state), measured/readable/enumerated=\(energy.measured)/\(energy.readable)/\(energy.candidates)\n" + energy.rows.map { "\($0.id.pid) \($0.process.name): \($0.formattedPower), interval \(number($0.seconds)) s" }.joined(separator: "\n") + "\n"
|
||||
}
|
||||
private static func processLine(_ process: ProcessMetrics) -> String {
|
||||
"\(process.id.pid)\t\(number(process.cpu.map { $0 * 100 }))\t\(bytes(process.residentBytes))\t\(process.threads.map(String.init) ?? "--")\t\(process.restricted)\t\(process.name)\t\(process.path ?? "--")"
|
||||
"\(process.id.pid)\t\(number(process.cpu.map { $0 * 100 }))\t\(bytes(process.footprintBytes))\t\(process.threads.map(String.init) ?? "--")\t\(process.restricted)\t\(process.name)\t\(process.path ?? "--")"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3581,19 +3581,19 @@
|
||||
"en" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "CPU: one core = 100%, may exceed 100% · Memory: physical resident size (RSS) · Restricted fields shown as --"
|
||||
"value" : "CPU: one core = 100%, may exceed 100% · Memory: physical footprint · Restricted fields shown as --"
|
||||
}
|
||||
},
|
||||
"zh-Hans" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "CPU:单核 100%,可超过 100% · 内存:实际驻留物理内存(RSS)· 受限字段显示 --"
|
||||
"value" : "CPU:单核 100%,可超过 100% · 内存:物理内存占用(footprint)· 受限字段显示 --"
|
||||
}
|
||||
},
|
||||
"zh-Hant" : {
|
||||
"stringUnit" : {
|
||||
"state" : "translated",
|
||||
"value" : "CPU:單核 100%,可超過 100% · 記憶體:實際駐留實體記憶體(RSS)· 受限欄位顯示 --"
|
||||
"value" : "CPU:單核 100%,可超過 100% · 記憶體:實體記憶體用量(footprint)· 受限欄位顯示 --"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user