87 lines
4.9 KiB
Swift
87 lines
4.9 KiB
Swift
import Foundation
|
|
import Darwin
|
|
|
|
nonisolated enum ProcessEnergyReader {
|
|
private static let readLock = NSLock()
|
|
static func read(_ data: Data) -> Data? {
|
|
guard data.count <= ProcessEnergyRequest.byteLimit,
|
|
let request = try? JSONDecoder().decode(ProcessEnergyRequest.self, from: data),
|
|
request.identities.count <= ProcessEnergyRequest.limit,
|
|
Set(request.identities.map(\.pid)).count == request.identities.count else { return nil }
|
|
guard readLock.try() else { return nil }
|
|
defer { readLock.unlock() }
|
|
let capability: EnergyCapability
|
|
#if arch(arm64)
|
|
let own = usage(getpid())
|
|
capability = own.map { $0.ri_energy_nj > 0 } == true ? .verifiedCPU : .unverified
|
|
#else
|
|
capability = .unsupported
|
|
#endif
|
|
let deadline = ProcessInfo.processInfo.systemUptime + 1
|
|
let values = request.identities.map { id -> ProcessEnergyReading in
|
|
func result(_ status: EnergyReadStatus, _ usage: rusage_info_v6? = nil) -> ProcessEnergyReading {
|
|
ProcessEnergyReading(id: id, birth: usage?.ri_proc_start_abstime ?? 0,
|
|
uptime: ProcessInfo.processInfo.systemUptime, nanojoules: usage?.ri_energy_nj, status: status)
|
|
}
|
|
guard capability != .unsupported else { return result(.unsupported) }
|
|
guard capability == .verifiedCPU else { return result(.unverified) }
|
|
guard ProcessInfo.processInfo.systemUptime < deadline else { return result(.deadline) }
|
|
guard id.pid > 0, id.seconds > 0 else { return result(.identityChanged) }
|
|
guard let before = identity(id.pid) else { return result(errno == ESRCH ? .gone : .denied) }
|
|
guard before.pbi_uid == getuid() else { return result(.denied) }
|
|
guard matches(before, id) else { return result(.identityChanged) }
|
|
guard let value = usage(id.pid) else { return result(errno == EPERM || errno == EACCES ? .denied : .failed) }
|
|
guard let after = identity(id.pid), matches(after, id), after.pbi_uid == getuid() else { return result(.identityChanged) }
|
|
return result(.available, value)
|
|
}
|
|
return try? JSONEncoder().encode(ProcessEnergyReply(token: request.token, capability: capability, readings: values))
|
|
}
|
|
|
|
private static func matches(_ info: proc_bsdinfo, _ id: ProcessIdentity) -> Bool {
|
|
info.pbi_start_tvsec == id.seconds && info.pbi_start_tvusec == id.microseconds
|
|
}
|
|
private static func identity(_ pid: Int32) -> proc_bsdinfo? {
|
|
var info = proc_bsdinfo()
|
|
return proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, Int32(MemoryLayout.size(ofValue: info))) == MemoryLayout.size(ofValue: info) ? info : nil
|
|
}
|
|
private static func usage(_ pid: Int32) -> rusage_info_v6? {
|
|
var info = rusage_info_v6()
|
|
let status = withUnsafeMutablePointer(to: &info) { pointer in
|
|
pointer.withMemoryRebound(to: rusage_info_t?.self, capacity: 1) { proc_pid_rusage(pid, RUSAGE_INFO_V6, $0) }
|
|
}
|
|
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
|
|
}
|
|
}
|