Skip to content

Read Linux UDP statistics by counter name - #3800

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:linux-udp6-stats
Oct 13, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:linux-udp6-stats

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

InternetProtocolStats reported every UDP field on Linux as another field's value, on both IPv4 and IPv6.

UDPv6

getUDPv6Stats() read its four counters from /proc/net/snmp6 by name, then passed them to UdpStats in the order the file lists them rather than the order the constructor takes them:

return new UdpStats(inDatagrams, noPorts, inErrors, outDatagrams);
// UdpStats(datagramsSent, datagramsReceived, datagramsNoPort, datagramsReceivedErrors)

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/snmp columns positionally with ParseUtil.stringToEnumMap, which assigns fields to enum constants in ordinal order — and the private UdpStat enum declared OutDatagrams first:

private enum UdpStat {
    OutDatagrams, InDatagrams, NoPorts, InErrors, ...
}

The kernel emits it fourth:

Udp: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors InCsumErrors IgnoredMulti MemErrors

TcpStat's order does match snmp4_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 gives ProcUtil — added in #2854 for exactly these two files, and with no caller in main until 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.parseStatistics builds 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 /proc counter deltas field for field.

field kernel delta 7.7.1 this PR
datagrams sent 15 7 ❌ 15 ✅
datagrams received 7 3 ❌ 7 ✅
datagrams no port 3 0 ❌ 3 ✅
datagrams received errors 0 15 ❌ 0 ✅

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-coverage and -Perror-prone.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected Linux network statistics so IPv4 and IPv6 UDP counters report the values for their respective metrics, rather than values from other counters. UDP statistics now use consistent ordering across both IP versions, and missing counters continue to be reported as zero.

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.
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b02f365c-2405-4166-bec7-b836a0d57f2a
📥 Commits

Reviewing files that changed from the base of the PR and between e989574 and 58fbd0f.

📒 Files selected for processing: 3
  • CHANGELOG.md
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java
  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📜 Recent review details
⏰ Context from checks skipped due to timeout: 14
  • GitHub Check: Test JDK 25, OpenIndiana
  • GitHub Check: Test JDK 25, NetBSD
  • GitHub Check: Test JDK 25, FreeBSD
  • GitHub Check: Test JDK 25, OpenBSD
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Analyze (java)
  • GitHub Check: Error Prone
🧰 Additional context used
📚 Code guidelines: 2
AGENTS.md — auto-discovered
CONTRIBUTING.md — auto-discovered
📓 Path-based instructions: 2
Source excerpt: **Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`): `oshi-common` may not reference `com.sun.jna.**`, `java.lang.foreign.**`, or any `System.load`/`loadLibrary`.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java
Source excerpt: The Changelog lets users know what's changed.

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Files:

  • CHANGELOG.md
🔇 Additional comments: 3
oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java: 1

33-87: LGTM!

oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java: 1

1-122: LGTM!

CHANGELOG.md: 1

14-14: LGTM!

📝 Walkthrough

Linux TCP and UDP statistics now use counter names to read values from /proc/net/snmp and /proc/net/snmp6. UDPv6 parsing reads the full statistics map. Tests check counter mappings and zero defaults for missing values.

Changes: Linux protocol statistics

Layer / File(s) Summary
Named counter parsing and validation
oshi-common/src/main/java/oshi/software/common/os/linux/LinuxInternetProtocolStats.java, oshi-common/src/test/java/oshi/software/common/os/linux/LinuxInternetProtocolStatsTest.java, CHANGELOG.md
TCP and UDPv4 parsing uses named counters from /proc/net/snmp. UDPv6 parsing reads the full /proc/net/snmp6 map and reports outgoing, incoming, no-port, and error counters in that order. Missing counters default to zero. Tests check TCP and UDP counter mappings, including UDPv6 values distinct from UDP-Lite counters.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 58fbd

No actionable issue remains in the supplied review evidence; this change is mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 2 files. (1 skipped: … Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: reading Linux UDP statistics by counter name.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

I’m a rabbit; I read counters by name.
TCP and UDP now map values the same.
IPv6’s full map comes into view.
Missing counters return zero, too.
I hop through the tests with a soft little cheer.

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.71%. Comparing base (e989574) to head (58fbd0f).

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     
Flag Coverage Δ
freebsd 36.96% <66.66%> (+0.20%) ⬆️
linux 40.99% <100.00%> (-0.10%) ⬇️
macos 42.12% <66.66%> (+0.17%) ⬆️
netbsd 36.21% <66.66%> (+0.18%) ⬆️
openbsd 36.79% <66.66%> (+0.19%) ⬆️
solaris 38.12% <66.66%> (+0.20%) ⬆️
windows 48.39% <66.66%> (+0.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit 3d57a4d into oshi:master Oct 13, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the linux-udp6-stats branch October 31, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant