Skip to content

Add raw access to map structures in proc filesystem - #2854

Merged
dbwiddis merged 10 commits into
oshi:masterfrom
jdeppe-pivotal:add-raw-proc-netstat
Mar 21, 2025
Merged

dbwiddis merged 10 commits into
oshi:masterfrom
jdeppe-pivotal:add-raw-proc-netstat

Conversation

@jdeppe-pivotal

Copy link
Copy Markdown
Contributor

Provide methods to process and marshal the named /proc files into
a consistent map structure of { key: { stat: value }}.

@jdeppe-pivotal

Copy link
Copy Markdown
Contributor Author

Thanks for this library! I'd like to propose the provided additions which would be very useful in the project I work on. These 'raw' access methods simply provide a means to access various statistics without needing specific API calls/methods for each individual statistic.

@codecov

codecov Bot commented Mar 18, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 91.66667% with 3 lines in your changes missing coverage. Please review.

Project coverage is 85.73%. Comparing base (8be4558) to head (7aa9d25).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
oshi-core/src/main/java/oshi/util/ProcUtil.java 91.66% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2854      +/-   ##
==========================================
- Coverage   87.19%   85.73%   -1.47%     
==========================================
  Files          30       31       +1     
  Lines        1359     1395      +36     
  Branches      192      202      +10     
==========================================
+ Hits         1185     1196      +11     
- Misses        100      113      +13     
- Partials       74       86      +12     

☔ View full report in Codecov by Sentry.
📢 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.

@mprins mprins left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are some checkstyle errors:

[INFO] Starting audit...
Error:  /home/runner/work/oshi/oshi/oshi-core/src/main/java/oshi/software/os/linux/LinuxInternetProtocolStats.java:176:74: Expected @param tag for 'procFile'. [JavadocMethod]
Error:  /home/runner/work/oshi/oshi/oshi-core/src/main/java/oshi/software/os/linux/LinuxInternetProtocolStats.java:243:43: File contains tab characters (this is the first instance). [FileTabCharacter]
Error:  /home/runner/work/oshi/oshi/oshi-core/src/test/java/oshi/software/os/linux/LinuxInternetProtocolStatsTest.java:8:1: Disallowed import - java.net.URL. [ImportControlTest]
Audit done.
[INFO] There are 3 errors reported by Checkstyle 10.21.4 with config/checkstyle.xml ruleset.
Error:  src/main/java/oshi/software/os/linux/LinuxInternetProtocolStats.java:[176,74] (javadoc) JavadocMethod: Expected @param tag for 'procFile'.
Error:  src/main/java/oshi/software/os/linux/LinuxInternetProtocolStats.java:[243,43] (whitespace) FileTabCharacter: File contains tab characters (this is the first instance).
Error:  src/test/java/oshi/software/os/linux/LinuxInternetProtocolStatsTest.java:[8,1] (extension) ImportControlTest: Disallowed import - java.net.URL.

@dbwiddis

Copy link
Copy Markdown
Member

Hey @jdeppe-pivotal thanks for this contribution! I agree we should make it easier to expose "all" data from the proc filesystem available in generic key/value maps.

The one thing that gave me pause when initially reviewing this is exposing a platform-specific package as a JPMS module, as it somewhat breaks the "this is the API" model, particularly for the "hardware" and "software" package trees.

We do currently have FileUtil.getKeyValueMapFromFile() in oshi.util that is more generic but it seems we only really use it for Linux things, some in proc and some elsewhere. We could just have more variations on that.

WDYT about creating a ProcUtil class under oshi.util.linux and exposing that package to the module path? Or just put it in oshi.util since some other *nix systems use procfs.

You could probably just have more generic "getFooMap" methods there for the different styles such as a prefix with headers and values on successive lines which is essentially what you've written in processNetSnmpOrNetstat(String path), and a version of your getRawNetSnmp6() method that takes in a vararg list of strings for the prefixes to be used in the outer map.

@jdeppe-pivotal

Copy link
Copy Markdown
Contributor Author

@dbwiddis Thanks for the suggestions. I've:

  • Created ProcUtil as you suggested (happy to also just use FileUtil if you prefer).
  • Gave the method a more generic name (getMapFromHeaderProc, but it still sounds a bit weird. Naming is hard!
  • Since you pointed out FileUtil.getKeyValueMapFromFile() I think that should suffice to process /proc/net/snmp6 formatted files (at least for my purposes). So I dropped the snmp6 specific methods.

@dbwiddis

Copy link
Copy Markdown
Member
  • Created ProcUtil as you suggested (happy to also just use FileUtil if you prefer).

I think it depends on how many methods we're adding, but I'm leaning more toward a ProcUtil:

  • the FileUtil method just assumes Map<String, String> but we want Map<String, Long> for some stats.
  • The style "Foo: header1 header2" / "Foo: 123 456" seems uniquely procsys based
  • There are other proc formats that we may want to add in the future (/status has a variety of different formats, /maps looks useful, /smaps and /meminfo could benefit from parsing the memory units, etc.)
  • Gave the method a more generic name (getMapFromHeaderProc, but it still sounds a bit weird. Naming is hard!
  • I've found using LLMs to be useful for spurring creativity here. It gave several options but I thought parseNestedStatistics() was best for the Map<String, Map<String, Long>> data.
  • Since you pointed out FileUtil.getKeyValueMapFromFile() I think that should suffice to process /proc/net/snmp6 formatted files (at least for my purposes). So I dropped the snmp6 specific methods.

Well, it has a String value, so we could have a parseStatistics() method with a Map<String, Long> there.

@dbwiddis dbwiddis changed the title Add raw access to /proc/net/snmp, /proc/net/snmp6 and /proc/net/netstat Add raw access to map structures in proc filesystem Mar 20, 2025

@dbwiddis dbwiddis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good pending the string length check (can probably add parts[0].isEmpty() to the conditional on the line above).

Also please add a change log entry.

@dbwiddis
dbwiddis requested a review from mprins March 20, 2025 18:53

@dbwiddis dbwiddis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I'll merge when tests finish.

@dbwiddis
dbwiddis merged commit 058cb8c into oshi:master Mar 21, 2025
@jdeppe-pivotal

Copy link
Copy Markdown
Contributor Author

Thanks @dbwiddis !

N7ghtm4r3 added a commit to N7ghtm4r3/KInfo that referenced this pull request Apr 24, 2025
N7ghtm4r3 added a commit to N7ghtm4r3/KInfo that referenced this pull request Apr 24, 2025
dbwiddis added a commit that referenced this pull request Oct 13, 2026
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 #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.
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.

3 participants