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
+32
View File
@@ -52,3 +52,35 @@ nonisolated enum ProcessEnergyReader {
return status == 0 ? info : nil
}
}
/// Independent of energy capability: physical footprint is available on Intel and Apple silicon.
nonisolated enum ProcessMemoryReader {
private static let readLock = NSLock()
static func read(_ data: Data) -> Data? {
guard data.count <= ProcessMemoryRequest.byteLimit,
let request = try? JSONDecoder().decode(ProcessMemoryRequest.self, from: data),
request.identities.count <= ProcessMemoryRequest.limit,
Set(request.identities.map(\.pid)).count == request.identities.count,
readLock.try() else { return nil }
defer { readLock.unlock() }
let deadline = ProcessInfo.processInfo.systemUptime + 1
let readings = request.identities.map { id -> ProcessMemoryReading in
var info = rusage_info_v4()
var bytes: UInt64?
if ProcessInfo.processInfo.systemUptime < deadline, id.pid > 0, id.seconds > 0,
matches(id) {
let status = withUnsafeMutablePointer(to: &info) { pointer in
pointer.withMemoryRebound(to: rusage_info_t?.self, capacity: 1) { proc_pid_rusage(id.pid, RUSAGE_INFO_V4, $0) }
}
if status == 0, matches(id) { bytes = info.ri_phys_footprint }
}
return ProcessMemoryReading(id: id, footprintBytes: bytes)
}
return try? JSONEncoder().encode(ProcessMemoryReply(token: request.token, readings: readings))
}
private static func matches(_ id: ProcessIdentity) -> Bool {
var info = proc_bsdinfo()
return proc_pidinfo(id.pid, PROC_PIDTBSDINFO, 0, &info, Int32(MemoryLayout.size(ofValue: info))) == MemoryLayout.size(ofValue: info)
&& info.pbi_uid == getuid() && info.pbi_start_tvsec == id.seconds && info.pbi_start_tvusec == id.microseconds
}
}