Skip to content

Commit cc8d321

Browse files
authored
Handle unsupported metrics, rename protobuf field (#968)
* Make partially supported fields in `NetworkProtocolCounters` of protobuf optional. Those fields are `{received,transmitted}_bytes`, `{received,transmitted}_drops`, `{received,transmitted}_errors`. Instead of filling unsupported metrics value with 0, leave them nil so that receiver can distinguish it. * Rename `protocol` to `network_protocol` in protobuf. The same in `ContainerStatistics` (i.e., `protocol` to `networkProtocol`).
1 parent fcc92dd commit cc8d321

8 files changed

Lines changed: 226 additions & 133 deletions

File tree

‎Sources/Containerization/ContainerStatistics.swift‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public struct ContainerStatistics: Sendable {
2525
public var memoryEvents: MemoryEventStatistics?
2626
public var filesystem: [FilesystemStatistics]?
2727
/// Per-protocol traffic for the network namespace. `nil` if not requested or not reported by the guest.
28-
public var protocols: NetworkProtocolStatistics?
28+
public var networkProtocols: NetworkProtocolStatistics?
2929

3030
public init(
3131
id: String,
@@ -36,7 +36,7 @@ public struct ContainerStatistics: Sendable {
3636
networks: [NetworkStatistics]? = nil,
3737
memoryEvents: MemoryEventStatistics? = nil,
3838
filesystem: [FilesystemStatistics]? = nil,
39-
protocols: NetworkProtocolStatistics? = nil
39+
networkProtocols: NetworkProtocolStatistics? = nil
4040
) {
4141
self.id = id
4242
self.process = process
@@ -46,7 +46,7 @@ public struct ContainerStatistics: Sendable {
4646
self.networks = networks
4747
self.memoryEvents = memoryEvents
4848
self.filesystem = filesystem
49-
self.protocols = protocols
49+
self.networkProtocols = networkProtocols
5050
}
5151

5252
/// Process statistics for a container.
@@ -184,8 +184,8 @@ public struct ContainerStatistics: Sendable {
184184
public var transmittedPackets: UInt64
185185
public var receivedBytes: UInt64
186186
public var transmittedBytes: UInt64
187-
public var receivedErrors: UInt64
188-
public var transmittedErrors: UInt64
187+
public var receivedErrors: UInt64?
188+
public var transmittedErrors: UInt64?
189189
/// Packets the device dropped (e.g. full rx ring or tx queue).
190190
public var receivedDropped: UInt64
191191
public var transmittedDropped: UInt64
@@ -217,37 +217,37 @@ public struct ContainerStatistics: Sendable {
217217
public struct NetworkProtocolCounters: Sendable {
218218
public var receivedPackets: UInt64
219219
public var transmittedPackets: UInt64
220-
/// Zero when the protocol has no byte counter (Linux only has one for IP as a whole).
221-
public var receivedBytes: UInt64
222-
public var transmittedBytes: UInt64
220+
/// `nil` when the protocol has no byte counter (Linux only has one for IP as a whole).
221+
public var receivedBytes: UInt64?
222+
public var transmittedBytes: UInt64?
223223
/// Packets the stack discarded for lack of resources or a destination.
224224
///
225225
/// The kernel attributes drops and errors to different causes for each protocol, so
226-
/// values are comparable over time for one protocol but not across protocols. Zero
227-
/// when the kernel has no matching counter, which is not the same as "none happened".
226+
/// values are comparable over time for one protocol but not across protocols. `nil`
227+
/// when the protocol has no matching counter, which is not the same as zero ("none happened").
228228
/// - TCP: listen queue overflow, socket backlog and receive queue drops (receive only).
229229
/// - UDP: full socket buffer or no socket on the port (receive); full send buffer (transmit).
230230
/// - ICMP: no drop counters.
231231
/// - IP: discarded valid packets and missing routes.
232-
public var receivedDrops: UInt64
233-
public var transmittedDrops: UInt64
232+
public var receivedDrops: UInt64?
233+
public var transmittedDrops: UInt64?
234234
/// Packets that were malformed or failed processing.
235235
/// - TCP: bad checksum or header (receive only).
236236
/// - UDP: e.g. bad checksum, excluding full-buffer drops (receive only).
237237
/// - ICMP: receive and transmit errors.
238238
/// - IP: bad header, bad address or unknown protocol (receive); fragmentation failures (transmit).
239-
public var receivedErrors: UInt64
240-
public var transmittedErrors: UInt64
239+
public var receivedErrors: UInt64?
240+
public var transmittedErrors: UInt64?
241241

242242
public init(
243243
receivedPackets: UInt64 = 0,
244244
transmittedPackets: UInt64 = 0,
245-
receivedBytes: UInt64 = 0,
246-
transmittedBytes: UInt64 = 0,
247-
receivedDrops: UInt64 = 0,
248-
transmittedDrops: UInt64 = 0,
249-
receivedErrors: UInt64 = 0,
250-
transmittedErrors: UInt64 = 0
245+
receivedBytes: UInt64? = nil,
246+
transmittedBytes: UInt64? = nil,
247+
receivedDrops: UInt64? = nil,
248+
transmittedDrops: UInt64? = nil,
249+
receivedErrors: UInt64? = nil,
250+
transmittedErrors: UInt64? = nil
251251
) {
252252
self.receivedPackets = receivedPackets
253253
self.transmittedPackets = transmittedPackets

‎Sources/Containerization/SandboxContext/SandboxContext.pb.swift‎

Lines changed: 112 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,14 +1604,14 @@ public nonisolated struct Com_Apple_Containerization_Sandbox_V3_ContainerStats:
16041604
set {_uniqueStorage()._filesystem = newValue}
16051605
}
16061606

1607-
public var protocols: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats {
1608-
get {_storage._protocols ?? Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats()}
1609-
set {_uniqueStorage()._protocols = newValue}
1607+
public var networkProtocols: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats {
1608+
get {_storage._networkProtocols ?? Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats()}
1609+
set {_uniqueStorage()._networkProtocols = newValue}
16101610
}
1611-
/// Returns true if `protocols` has been explicitly set.
1612-
public var hasProtocols: Bool {_storage._protocols != nil}
1613-
/// Clears the value of `protocols`. Subsequent reads from it will return its default value.
1614-
public mutating func clearProtocols() {_uniqueStorage()._protocols = nil}
1611+
/// Returns true if `networkProtocols` has been explicitly set.
1612+
public var hasNetworkProtocols: Bool {_storage._networkProtocols != nil}
1613+
/// Clears the value of `networkProtocols`. Subsequent reads from it will return its default value.
1614+
public mutating func clearNetworkProtocols() {_uniqueStorage()._networkProtocols = nil}
16151615

16161616
public var unknownFields = SwiftProtobuf.UnknownStorage()
16171617

@@ -1772,42 +1772,92 @@ public nonisolated struct Com_Apple_Containerization_Sandbox_V3_NetworkProtocolC
17721772

17731773
public var transmittedPackets: UInt64 = 0
17741774

1775-
/// Zero when the protocol has no byte counter (Linux only has one for IP as a whole).
1776-
public var receivedBytes: UInt64 = 0
1775+
/// Unset when the protocol has no byte counter (Linux only has one for IP as a whole).
1776+
public var receivedBytes: UInt64 {
1777+
get {_receivedBytes ?? 0}
1778+
set {_receivedBytes = newValue}
1779+
}
1780+
/// Returns true if `receivedBytes` has been explicitly set.
1781+
public var hasReceivedBytes: Bool {self._receivedBytes != nil}
1782+
/// Clears the value of `receivedBytes`. Subsequent reads from it will return its default value.
1783+
public mutating func clearReceivedBytes() {self._receivedBytes = nil}
17771784

1778-
public var transmittedBytes: UInt64 = 0
1785+
public var transmittedBytes: UInt64 {
1786+
get {_transmittedBytes ?? 0}
1787+
set {_transmittedBytes = newValue}
1788+
}
1789+
/// Returns true if `transmittedBytes` has been explicitly set.
1790+
public var hasTransmittedBytes: Bool {self._transmittedBytes != nil}
1791+
/// Clears the value of `transmittedBytes`. Subsequent reads from it will return its default value.
1792+
public mutating func clearTransmittedBytes() {self._transmittedBytes = nil}
17791793

17801794
/// Drops and errors. The kernel attributes these to different causes for each
17811795
/// protocol, so they are comparable over time for one protocol but not across
17821796
/// protocols. A drop is a packet the stack discarded for lack of resources or a
17831797
/// destination; an error is a packet that was malformed or failed processing.
1784-
/// Zero when the kernel has no matching counter (which is not the same as "none
1785-
/// happened"). Sources, from /proc/net/snmp and /proc/net/netstat:
1798+
/// Unset when the protocol has no matching counter, so unset means "not
1799+
/// supported" while zero means "none happened". Sources, from /proc/net/snmp
1800+
/// and /proc/net/netstat:
17861801
/// tcp: received_drops = TcpExt ListenDrops + TCPBacklogDrop + TCPRcvQDrop
17871802
/// received_errors = Tcp InErrs (bad checksum or header)
1788-
/// transmitted_* = unavailable
1803+
/// transmitted_* = unset (unsupported)
17891804
/// udp: received_drops = Udp RcvbufErrors + NoPorts (socket buffer full, or no socket)
17901805
/// received_errors = Udp InErrors minus RcvbufErrors (e.g. bad checksum)
17911806
/// transmitted_drops = Udp SndbufErrors
1792-
/// transmitted_errors = unavailable
1807+
/// transmitted_errors = unset (unsupported)
17931808
/// icmp: received_errors = Icmp InErrors
17941809
/// transmitted_errors = Icmp OutErrors
1795-
/// *_drops = unavailable
1810+
/// *_drops = unset (unsupported)
17961811
/// ip: received_drops = Ip InDiscards + IpExt InNoRoutes
17971812
/// received_errors = Ip InHdrErrors + InAddrErrors + InUnknownProtos
17981813
/// transmitted_drops = Ip OutDiscards + OutNoRoutes
17991814
/// transmitted_errors = Ip FragFails
1800-
public var receivedDrops: UInt64 = 0
1801-
1802-
public var transmittedDrops: UInt64 = 0
1803-
1804-
public var receivedErrors: UInt64 = 0
1805-
1806-
public var transmittedErrors: UInt64 = 0
1815+
public var receivedDrops: UInt64 {
1816+
get {_receivedDrops ?? 0}
1817+
set {_receivedDrops = newValue}
1818+
}
1819+
/// Returns true if `receivedDrops` has been explicitly set.
1820+
public var hasReceivedDrops: Bool {self._receivedDrops != nil}
1821+
/// Clears the value of `receivedDrops`. Subsequent reads from it will return its default value.
1822+
public mutating func clearReceivedDrops() {self._receivedDrops = nil}
1823+
1824+
public var transmittedDrops: UInt64 {
1825+
get {_transmittedDrops ?? 0}
1826+
set {_transmittedDrops = newValue}
1827+
}
1828+
/// Returns true if `transmittedDrops` has been explicitly set.
1829+
public var hasTransmittedDrops: Bool {self._transmittedDrops != nil}
1830+
/// Clears the value of `transmittedDrops`. Subsequent reads from it will return its default value.
1831+
public mutating func clearTransmittedDrops() {self._transmittedDrops = nil}
1832+
1833+
public var receivedErrors: UInt64 {
1834+
get {_receivedErrors ?? 0}
1835+
set {_receivedErrors = newValue}
1836+
}
1837+
/// Returns true if `receivedErrors` has been explicitly set.
1838+
public var hasReceivedErrors: Bool {self._receivedErrors != nil}
1839+
/// Clears the value of `receivedErrors`. Subsequent reads from it will return its default value.
1840+
public mutating func clearReceivedErrors() {self._receivedErrors = nil}
1841+
1842+
public var transmittedErrors: UInt64 {
1843+
get {_transmittedErrors ?? 0}
1844+
set {_transmittedErrors = newValue}
1845+
}
1846+
/// Returns true if `transmittedErrors` has been explicitly set.
1847+
public var hasTransmittedErrors: Bool {self._transmittedErrors != nil}
1848+
/// Clears the value of `transmittedErrors`. Subsequent reads from it will return its default value.
1849+
public mutating func clearTransmittedErrors() {self._transmittedErrors = nil}
18071850

18081851
public var unknownFields = SwiftProtobuf.UnknownStorage()
18091852

18101853
public init() {}
1854+
1855+
fileprivate var _receivedBytes: UInt64? = nil
1856+
fileprivate var _transmittedBytes: UInt64? = nil
1857+
fileprivate var _receivedDrops: UInt64? = nil
1858+
fileprivate var _transmittedDrops: UInt64? = nil
1859+
fileprivate var _receivedErrors: UInt64? = nil
1860+
fileprivate var _transmittedErrors: UInt64? = nil
18111861
}
18121862

18131863
/// Per-protocol traffic for the container's network namespace (not per interface;
@@ -4385,7 +4435,7 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_ContainerStatisticsR
43854435

43864436
nonisolated extension Com_Apple_Containerization_Sandbox_V3_ContainerStats: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
43874437
public static let protoMessageName: String = _protobuf_package + ".ContainerStats"
4388-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{3}container_id\0\u{1}process\0\u{1}memory\0\u{1}cpu\0\u{3}block_io\0\u{1}networks\0\u{3}memory_events\0\u{1}filesystem\0\u{1}protocols\0")
4438+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{3}container_id\0\u{1}process\0\u{1}memory\0\u{1}cpu\0\u{3}block_io\0\u{1}networks\0\u{3}memory_events\0\u{1}filesystem\0\u{3}network_protocols\0")
43894439

43904440
fileprivate class _StorageClass {
43914441
var _containerID: String = String()
@@ -4396,7 +4446,7 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_ContainerStats: Swif
43964446
var _networks: [Com_Apple_Containerization_Sandbox_V3_NetworkStats] = []
43974447
var _memoryEvents: Com_Apple_Containerization_Sandbox_V3_MemoryEventStats? = nil
43984448
var _filesystem: [Com_Apple_Containerization_Sandbox_V3_FilesystemStats] = []
4399-
var _protocols: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats? = nil
4449+
var _networkProtocols: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats? = nil
44004450

44014451
// This property is used as the initial default value for new instances of the type.
44024452
// The type itself is protecting the reference to its storage via CoW semantics.
@@ -4415,7 +4465,7 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_ContainerStats: Swif
44154465
_networks = source._networks
44164466
_memoryEvents = source._memoryEvents
44174467
_filesystem = source._filesystem
4418-
_protocols = source._protocols
4468+
_networkProtocols = source._networkProtocols
44194469
}
44204470
}
44214471

@@ -4442,7 +4492,7 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_ContainerStats: Swif
44424492
case 6: try { try decoder.decodeRepeatedMessageField(value: &_storage._networks) }()
44434493
case 7: try { try decoder.decodeSingularMessageField(value: &_storage._memoryEvents) }()
44444494
case 8: try { try decoder.decodeRepeatedMessageField(value: &_storage._filesystem) }()
4445-
case 9: try { try decoder.decodeSingularMessageField(value: &_storage._protocols) }()
4495+
case 9: try { try decoder.decodeSingularMessageField(value: &_storage._networkProtocols) }()
44464496
default: break
44474497
}
44484498
}
@@ -4479,7 +4529,7 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_ContainerStats: Swif
44794529
if !_storage._filesystem.isEmpty {
44804530
try visitor.visitRepeatedMessageField(value: _storage._filesystem, fieldNumber: 8)
44814531
}
4482-
try { if let v = _storage._protocols {
4532+
try { if let v = _storage._networkProtocols {
44834533
try visitor.visitSingularMessageField(value: v, fieldNumber: 9)
44844534
} }()
44854535
}
@@ -4499,7 +4549,7 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_ContainerStats: Swif
44994549
if _storage._networks != rhs_storage._networks {return false}
45004550
if _storage._memoryEvents != rhs_storage._memoryEvents {return false}
45014551
if _storage._filesystem != rhs_storage._filesystem {return false}
4502-
if _storage._protocols != rhs_storage._protocols {return false}
4552+
if _storage._networkProtocols != rhs_storage._networkProtocols {return false}
45034553
return true
45044554
}
45054555
if !storagesAreEqual {return false}
@@ -4871,54 +4921,58 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCount
48714921
switch fieldNumber {
48724922
case 1: try { try decoder.decodeSingularUInt64Field(value: &self.receivedPackets) }()
48734923
case 2: try { try decoder.decodeSingularUInt64Field(value: &self.transmittedPackets) }()
4874-
case 3: try { try decoder.decodeSingularUInt64Field(value: &self.receivedBytes) }()
4875-
case 4: try { try decoder.decodeSingularUInt64Field(value: &self.transmittedBytes) }()
4876-
case 5: try { try decoder.decodeSingularUInt64Field(value: &self.receivedDrops) }()
4877-
case 6: try { try decoder.decodeSingularUInt64Field(value: &self.transmittedDrops) }()
4878-
case 7: try { try decoder.decodeSingularUInt64Field(value: &self.receivedErrors) }()
4879-
case 8: try { try decoder.decodeSingularUInt64Field(value: &self.transmittedErrors) }()
4924+
case 3: try { try decoder.decodeSingularUInt64Field(value: &self._receivedBytes) }()
4925+
case 4: try { try decoder.decodeSingularUInt64Field(value: &self._transmittedBytes) }()
4926+
case 5: try { try decoder.decodeSingularUInt64Field(value: &self._receivedDrops) }()
4927+
case 6: try { try decoder.decodeSingularUInt64Field(value: &self._transmittedDrops) }()
4928+
case 7: try { try decoder.decodeSingularUInt64Field(value: &self._receivedErrors) }()
4929+
case 8: try { try decoder.decodeSingularUInt64Field(value: &self._transmittedErrors) }()
48804930
default: break
48814931
}
48824932
}
48834933
}
48844934

48854935
public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
4936+
// The use of inline closures is to circumvent an issue where the compiler
4937+
// allocates stack space for every if/case branch local when no optimizations
4938+
// are enabled. https://github.com/apple/swift-protobuf/issues/1034 and
4939+
// https://github.com/apple/swift-protobuf/issues/1182
48864940
if self.receivedPackets != 0 {
48874941
try visitor.visitSingularUInt64Field(value: self.receivedPackets, fieldNumber: 1)
48884942
}
48894943
if self.transmittedPackets != 0 {
48904944
try visitor.visitSingularUInt64Field(value: self.transmittedPackets, fieldNumber: 2)
48914945
}
4892-
if self.receivedBytes != 0 {
4893-
try visitor.visitSingularUInt64Field(value: self.receivedBytes, fieldNumber: 3)
4894-
}
4895-
if self.transmittedBytes != 0 {
4896-
try visitor.visitSingularUInt64Field(value: self.transmittedBytes, fieldNumber: 4)
4897-
}
4898-
if self.receivedDrops != 0 {
4899-
try visitor.visitSingularUInt64Field(value: self.receivedDrops, fieldNumber: 5)
4900-
}
4901-
if self.transmittedDrops != 0 {
4902-
try visitor.visitSingularUInt64Field(value: self.transmittedDrops, fieldNumber: 6)
4903-
}
4904-
if self.receivedErrors != 0 {
4905-
try visitor.visitSingularUInt64Field(value: self.receivedErrors, fieldNumber: 7)
4906-
}
4907-
if self.transmittedErrors != 0 {
4908-
try visitor.visitSingularUInt64Field(value: self.transmittedErrors, fieldNumber: 8)
4909-
}
4946+
try { if let v = self._receivedBytes {
4947+
try visitor.visitSingularUInt64Field(value: v, fieldNumber: 3)
4948+
} }()
4949+
try { if let v = self._transmittedBytes {
4950+
try visitor.visitSingularUInt64Field(value: v, fieldNumber: 4)
4951+
} }()
4952+
try { if let v = self._receivedDrops {
4953+
try visitor.visitSingularUInt64Field(value: v, fieldNumber: 5)
4954+
} }()
4955+
try { if let v = self._transmittedDrops {
4956+
try visitor.visitSingularUInt64Field(value: v, fieldNumber: 6)
4957+
} }()
4958+
try { if let v = self._receivedErrors {
4959+
try visitor.visitSingularUInt64Field(value: v, fieldNumber: 7)
4960+
} }()
4961+
try { if let v = self._transmittedErrors {
4962+
try visitor.visitSingularUInt64Field(value: v, fieldNumber: 8)
4963+
} }()
49104964
try unknownFields.traverse(visitor: &visitor)
49114965
}
49124966

49134967
public static func ==(lhs: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters, rhs: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters) -> Bool {
49144968
if lhs.receivedPackets != rhs.receivedPackets {return false}
49154969
if lhs.transmittedPackets != rhs.transmittedPackets {return false}
4916-
if lhs.receivedBytes != rhs.receivedBytes {return false}
4917-
if lhs.transmittedBytes != rhs.transmittedBytes {return false}
4918-
if lhs.receivedDrops != rhs.receivedDrops {return false}
4919-
if lhs.transmittedDrops != rhs.transmittedDrops {return false}
4920-
if lhs.receivedErrors != rhs.receivedErrors {return false}
4921-
if lhs.transmittedErrors != rhs.transmittedErrors {return false}
4970+
if lhs._receivedBytes != rhs._receivedBytes {return false}
4971+
if lhs._transmittedBytes != rhs._transmittedBytes {return false}
4972+
if lhs._receivedDrops != rhs._receivedDrops {return false}
4973+
if lhs._transmittedDrops != rhs._transmittedDrops {return false}
4974+
if lhs._receivedErrors != rhs._receivedErrors {return false}
4975+
if lhs._transmittedErrors != rhs._transmittedErrors {return false}
49224976
if lhs.unknownFields != rhs.unknownFields {return false}
49234977
return true
49244978
}

0 commit comments

Comments
 (0)