perf(monitors): 复用网络接口元数据并一致读取路径状态
This commit is contained in:
@@ -15,9 +15,15 @@ nonisolated struct NetworkInterfaceMetrics: Sendable {
|
||||
}
|
||||
|
||||
nonisolated struct NetworkMonitor {
|
||||
struct InterfaceMetadata: Equatable {
|
||||
let index: UInt32
|
||||
var addresses: [String] = []
|
||||
let active: Bool
|
||||
}
|
||||
|
||||
private var previous: [String: (UInt64, UInt64)] = [:]
|
||||
private var timestamp: TimeInterval?
|
||||
private var identities: [String: String] = [:]
|
||||
private var previousMetadata: [String: InterfaceMetadata] = [:]
|
||||
private var counterBuffer: [UInt8] = []
|
||||
|
||||
static func rate(current: UInt64, previous: UInt64?, elapsed: TimeInterval) -> Double? {
|
||||
@@ -28,31 +34,31 @@ nonisolated struct NetworkMonitor {
|
||||
mutating func sample() -> [NetworkInterfaceMetrics]? {
|
||||
guard let counters = counters() else { previous = [:]; timestamp = nil; return nil }
|
||||
let now = ProcessInfo.processInfo.systemUptime
|
||||
return update(counters: counters, metadata: Self.interfaces(), at: now)
|
||||
}
|
||||
|
||||
mutating func update(counters: [String: (UInt64, UInt64)], metadata: [String: InterfaceMetadata],
|
||||
at now: TimeInterval) -> [NetworkInterfaceMetrics] {
|
||||
let elapsed = timestamp.map { now - $0 } ?? 0
|
||||
let metadata = Self.interfaces()
|
||||
let result = counters.keys.sorted().map { name in
|
||||
let current = counters[name]!
|
||||
let result = counters.sorted { $0.key < $1.key }.map { name, current in
|
||||
let item = metadata[name]
|
||||
let identity = "\(item?.index ?? 0):\(name):\(item?.active ?? false):\(item?.addresses.joined(separator: ",") ?? "")"
|
||||
let old = identities[name] == identity ? previous[name] : nil
|
||||
let old = previousMetadata[name] == item ? previous[name] : nil
|
||||
return NetworkInterfaceMetrics(name: name, received: current.0, sent: current.1,
|
||||
download: Self.rate(current: current.0, previous: old?.0, elapsed: elapsed),
|
||||
upload: Self.rate(current: current.1, previous: old?.1, elapsed: elapsed),
|
||||
index: item?.index ?? 0, addresses: item?.addresses ?? [], active: item?.active ?? false)
|
||||
}
|
||||
identities = Dictionary(uniqueKeysWithValues: result.map {
|
||||
($0.name, "\($0.index):\($0.name):\($0.active):\($0.addresses.joined(separator: ","))")
|
||||
})
|
||||
previousMetadata = metadata
|
||||
previous = counters
|
||||
timestamp = now
|
||||
return result
|
||||
}
|
||||
|
||||
private static func interfaces() -> [String: (index: UInt32, addresses: [String], active: Bool)] {
|
||||
private static func interfaces() -> [String: InterfaceMetadata] {
|
||||
var head: UnsafeMutablePointer<ifaddrs>?
|
||||
guard getifaddrs(&head) == 0 else { return [:] }
|
||||
defer { freeifaddrs(head) }
|
||||
var result: [String: (index: UInt32, addresses: [String], active: Bool)] = [:]
|
||||
var result: [String: InterfaceMetadata] = [:]
|
||||
var hostBuffer = [CChar](repeating: 0, count: Int(NI_MAXHOST))
|
||||
var cursor = head
|
||||
while let entry = cursor {
|
||||
@@ -60,7 +66,7 @@ nonisolated struct NetworkMonitor {
|
||||
let name = String(cString: entry.pointee.ifa_name)
|
||||
let flags = entry.pointee.ifa_flags
|
||||
let active = flags & UInt32(IFF_UP | IFF_RUNNING) == UInt32(IFF_UP | IFF_RUNNING)
|
||||
var item = result[name] ?? (if_nametoindex(entry.pointee.ifa_name), [], active)
|
||||
var item = result[name] ?? InterfaceMetadata(index: if_nametoindex(entry.pointee.ifa_name), active: active)
|
||||
if let address = entry.pointee.ifa_addr, [UInt8(AF_INET), UInt8(AF_INET6)].contains(address.pointee.sa_family) {
|
||||
if let host = numericAddress(address, buffer: &hostBuffer) {
|
||||
item.addresses.append(host)
|
||||
@@ -68,7 +74,8 @@ nonisolated struct NetworkMonitor {
|
||||
}
|
||||
result[name] = item
|
||||
}
|
||||
return result.mapValues { (index: $0.index, addresses: $0.addresses.sorted(), active: $0.active) }
|
||||
for name in result.keys { result[name]?.addresses.sort() }
|
||||
return result
|
||||
}
|
||||
|
||||
static func numericAddress(_ address: UnsafePointer<sockaddr>, buffer: inout [CChar]) -> String? {
|
||||
|
||||
@@ -29,16 +29,10 @@ nonisolated final class NetworkPathObserver: @unchecked Sendable {
|
||||
monitor.start(queue: DispatchQueue(label: "cn.airnan.kymo.network-path"))
|
||||
}
|
||||
|
||||
func snapshot() -> NetworkPathStatus? {
|
||||
func snapshot() -> (status: NetworkPathStatus?, primaryInterface: String?) {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return status
|
||||
}
|
||||
|
||||
func defaultInterface() -> String? {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return primaryInterface
|
||||
return (status, primaryInterface)
|
||||
}
|
||||
|
||||
deinit { monitor.cancel() }
|
||||
|
||||
@@ -102,8 +102,7 @@ actor SystemSampler {
|
||||
result.fans = fanCache
|
||||
result.memory = MemoryMonitor.sample()
|
||||
result.network = network.sample()
|
||||
result.networkPath = networkPath.snapshot()
|
||||
result.defaultNetworkInterface = networkPath.defaultInterface()
|
||||
(result.networkPath, result.defaultNetworkInterface) = networkPath.snapshot()
|
||||
result.disk = DiskMonitor.sample()
|
||||
result.diskIO = diskIO.sample()
|
||||
result.battery = batteryMonitor.sample()
|
||||
|
||||
Reference in New Issue
Block a user