Repository navigation
Read Linux UDP statistics by counter name - #3800
Conversation
getUDPv6Stats() passed its four counters to UdpStats in the order /proc/net/snmp6 lists them rather than the order the constructor takes them, so every field held another field's value: datagrams sent reported those received, received reported those dropped for no port, no port reported the input errors, and received errors reported those sent. getUDPv4Stats() had the same rotation, hidden one level down. It mapped the /proc/net/snmp columns positionally with ParseUtil.stringToEnumMap, and the private UdpStat enum declared OutDatagrams first while the kernel emits it fourth, after InDatagrams, NoPorts and InErrors. Both are now read through ProcUtil, which keys each counter by its name in the file, so a column order that does not match cannot misalign the fields. That retires the two hand-rolled parsers along with the ordinal coupling, and gives ProcUtil, added in oshi#2854 for exactly these two files, its first caller. TCPv4 moves across unchanged in behavior; its enum order already matched the kernel's. Verified against a live kernel by generating 15 sent, 7 received and 3 no-port datagrams over both IPv4 and IPv6 and comparing OSHI's deltas to the /proc counter deltas: 7.7.1 misses all eight, this passes all eight.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📜 Recent review details
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3800 +/- ##
============================================
- Coverage 80.74% 80.71% -0.04%
- Complexity 83 627 +544
============================================
Files 725 725
Lines 25975 25928 -47
Branches 4413 4404 -9
============================================
- Hits 20973 20927 -46
- Misses 2771 2777 +6
+ Partials 2231 2224 -7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
InternetProtocolStatsreported every UDP field on Linux as another field's value, on both IPv4 and IPv6.UDPv6
getUDPv6Stats()read its four counters from/proc/net/snmp6by name, then passed them toUdpStatsin the order the file lists them rather than the order the constructor takes them:So "sent" held the datagrams received, "received" held the no-port count, "no port" held the input errors, and "received errors" held the datagrams sent. This came in with #2846.
UDPv4
The same rotation, one level down and easier to miss.
getUDPv4Stats()mapped the/proc/net/snmpcolumns positionally withParseUtil.stringToEnumMap, which assigns fields to enum constants in ordinal order — and the privateUdpStatenum declaredOutDatagramsfirst:The kernel emits it fourth:
TcpStat's order does matchsnmp4_tcp_list, so TCPv4 was unaffected and moves across with no behavior change.Fix
Both files are now read through
oshi.util.ProcUtil, which keys each counter by its name in the file, so a column order that doesn't match can no longer misalign the fields. That retires the two hand-rolled parsers and the ordinal coupling behind them, and givesProcUtil— added in #2854 for exactly these two files, and with no caller inmainuntil now — its first one. Net 85 lines deleted, 44 added.Each getter now hands its name→value map to a package-private static (
parseTcp4Stats,parseUdp4Stats,parseUdp6Stats), which is what the new tests exercise against fixtures where every counter holds a distinct value.One trade-off worth naming: the old UDPv6 path scanned the file bottom-up and stopped after four hits, where
ProcUtil.parseStatisticsbuilds a map of all ~90 counters. The file read dominates either way, but I'm happy to add a filtering overload if you'd rather keep the early exit.Verification
Fixture tests aside, I checked this against a live kernel in a container: generate deliberately asymmetric traffic — 15 datagrams sent, 7 received, 3 to a closed port — and compare OSHI's deltas to the
/proccounter deltas field for field.IPv6 the same shape (10 sent, 7 received, 3 no port), the same result. The released build misses all eight checks; this passes all eight.
Gate run clean on
oshi-common,oshi-core,oshi-core-ffm -am, including-Paggregate-coverageand-Perror-prone.Summary by CodeRabbit