perf(efficiency): 落实首批采样减负与发行包瘦身
This commit is contained in:
@@ -4,6 +4,7 @@ import Darwin
|
||||
nonisolated struct ProcessMonitor: Sendable {
|
||||
private var previous: [ProcessIdentity: (UInt64, TimeInterval)] = [:]
|
||||
private var sampleSerial: UInt64 = 0
|
||||
private var paths: [ProcessIdentity: (name: String, path: String?, expires: TimeInterval)] = [:]
|
||||
|
||||
static func cpu(current: UInt64, previous: UInt64?, elapsed: TimeInterval) -> Double? {
|
||||
guard let previous, current >= previous, elapsed.isFinite, elapsed > 0 else { return nil }
|
||||
@@ -15,11 +16,13 @@ nonisolated struct ProcessMonitor: Sendable {
|
||||
let date = Date()
|
||||
guard let entries = Self.entries() else {
|
||||
previous = [:]
|
||||
paths = [:]
|
||||
return ProcessSnapshot(date: date, enumerated: 0, readable: 0, rows: [], failed: true)
|
||||
}
|
||||
let now = ProcessInfo.processInfo.systemUptime
|
||||
var next: [ProcessIdentity: (UInt64, TimeInterval)] = [:]
|
||||
var rows: [ProcessMetrics] = []
|
||||
var pathBuffer = [CChar](repeating: 0, count: 4 * Int(MAXPATHLEN))
|
||||
for entry in entries where entry.kp_proc.p_pid > 0 {
|
||||
let pid = entry.kp_proc.p_pid
|
||||
var info = proc_taskallinfo()
|
||||
@@ -34,8 +37,14 @@ nonisolated struct ProcessMonitor: Sendable {
|
||||
let name = withUnsafeBytes(of: ©) { raw in
|
||||
String(decoding: raw.prefix { $0 != 0 }, as: UTF8.self)
|
||||
}
|
||||
var path = [CChar](repeating: 0, count: 4 * Int(MAXPATHLEN))
|
||||
let pathSize = proc_pidpath(pid, &path, UInt32(path.count))
|
||||
let path: String?
|
||||
if let cached = paths[id], seconds > 0, cached.name == name, now < cached.expires {
|
||||
path = cached.path
|
||||
} else {
|
||||
let size = proc_pidpath(pid, &pathBuffer, UInt32(pathBuffer.count))
|
||||
path = size > 0 ? String(cString: pathBuffer) : nil
|
||||
paths[id] = (name, path, now + 30)
|
||||
}
|
||||
var usage: Double?
|
||||
if valid && seconds > 0 {
|
||||
let (total, overflow) = info.ptinfo.pti_total_user.addingReportingOverflow(info.ptinfo.pti_total_system)
|
||||
@@ -44,7 +53,7 @@ nonisolated struct ProcessMonitor: Sendable {
|
||||
next[id] = (total, now)
|
||||
}
|
||||
}
|
||||
rows.append(ProcessMetrics(id: id, name: name, path: pathSize > 0 ? String(cString: path) : nil,
|
||||
rows.append(ProcessMetrics(id: id, name: name, path: path,
|
||||
cpu: usage, residentBytes: valid ? info.ptinfo.pti_resident_size : nil,
|
||||
threads: valid ? Int(info.ptinfo.pti_threadnum) : nil, restricted: !valid))
|
||||
}
|
||||
@@ -56,11 +65,13 @@ nonisolated struct ProcessMonitor: Sendable {
|
||||
})
|
||||
rows.removeAll { $0.id.seconds > 0 && !identities.contains($0.id) }
|
||||
} 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) }
|
||||
}
|
||||
let retained = Set(rows.map(\.id))
|
||||
previous = next.filter { retained.contains($0.key) }
|
||||
paths = paths.filter { retained.contains($0.key) && $0.key.seconds > 0 }
|
||||
return ProcessSnapshot(date: date, enumerated: entries.filter { $0.kp_proc.p_pid > 0 }.count,
|
||||
readable: rows.filter { !$0.restricted }.count, rows: rows, failed: false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user