You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Optimize calls to netstat by using /proc files #2826
I am curious if there has ever been any discussion about optimizing the Linux network metrics by utilizing the various /proc files rather than executing netstat and parsing its output? It looks like for the most part on Linux that netstat is parsing the /proc files itself and just reformatting it. The execution of netstat results in a noticeable latency in sampling these metrics that is not seem direct parsing of /proc files.
If it is just a matter of lack of contribution I would love to contribute in any way but don't want to tread on something that may be intentionally implemented in a particular way.
Activity
rohan-coder02 commentedon Mar 8, 2025
Hi @dbwiddis,
Is it ok if I could present my analysis and code changes done to optimize calls to netstat by using /proc files?
Below is my analysis and code changes.
OSHI Existing Code Analysis:
Linux /proc Analysis for Optimized Data Retrieval:
Sample Output of /proc/net/snmp (TCPv4 & UDPv4):
Sample Output of /proc/net/snmp6 (UDPv6):
The above output of /proc/net/snmp6 is trimmed to consist of only variables that is stored in the class of UdpStats.
Refer code in https://github.com/oshi/oshi/blob/master/oshi-core/src/main/java/oshi/software/os/InternetProtocolStats.java
Key Observations:
Implemented Code Changes
Added the following constants in oshi.util.platform.linux.ProcPath
In oshi.software.os.linux.LinuxInternetProtocolStats add the below code
Optimized getTCPv4Stats(), getUDPv4Stats() and getUDPv6Stats() Implementation
Testing & Integration
Build Verification:
Integration in Java Application:
Below is the simple Java App
Sample Output:
Please let me know if any queries/issues regarding the above.
dbwiddis commentedon Mar 8, 2025
This looks great! Can you submit a PR with these changes?
Only change I'd suggest is eliminating the duplication of the string constants (they are in both a HashSet and HashMap). You might consider using an enum (and thus EnumMap). See
ParseUtil.stringToEnumMap()and various implementations using it: https://github.com/search?q=repo%3Aoshi%2Foshi%20stringtoenummap&type=codedbwiddis commentedon Mar 8, 2025
Also your BufferedReader code can be replaced by
FileUtil.readAllBytes().Optimize netstat calls using /proc files (Issue oshi#2826)
rohan-coder02 commentedon Mar 9, 2025
Hi @dbwiddis
Thanks for your feedback.
I've incorporated your suggestions and updated the code accordingly. The PR has been raised here: #2846.
Please let me know if you have any questions or require further changes.
Optimize netstat calls using /proc files (Issue #2826) (#2846)