Description
Problem
OSFileStore.updateAttributes() on all platforms re-enumerates every mounted filesystem just to update space/inode numbers for a single known mount point. When called in a monitoring loop for N file stores, this results in O(N²) work.
Per-platform behavior
| Platform | What updateAttributes() does |
Impact |
|---|---|---|
| macOS | Calls getfsstat64 for all mounts + opens a DiskArbitration session |
Heavy — DA session is expensive |
| Linux | Parses /proc/mounts + builds UUID map from /dev/disk/by-uuid/ |
Moderate — file I/O per call |
| Windows | Enumerates volumes via FindFirstVolume/FindNextVolume, but skips expensive calls for non-matches |
Lighter — filters early |
| FreeBSD/OpenBSD/Solaris/AIX | Creates a new *FileSystem() and calls getFileStores() |
Heavy — full enumeration |
JMH benchmark results (macOS, JNA vs FFM)
The benchmark calls updateAttributes() on every file store each iteration:
| Benchmark | Time |
|---|---|
FileStoreBenchmark.jna |
~16 ms |
FileStoreBenchmark.ffm |
~13 ms |
For comparison, a single getFileStores() call (which enumerates once) takes ~2 ms.
Proposed fix
Since updateAttributes() only needs to refresh space and inode numbers (volume name, UUID, description, fs type don't change), add a fast path that calls the stat syscall directly on the known mount path:
- macOS/Linux/Unix:
statvfs(mount)directly - Windows:
GetDiskFreeSpaceEx(mount)directly
Fall back to the current full-enumeration path only if the direct stat fails (e.g., mount was removed).
This would reduce updateAttributes() from O(N) enumeration to O(1) per store, and the monitoring loop from O(N²) to O(N).
Activity
coderabbitai commentedon Apr 22, 2026
🔗 Related PRs
#3158 - Fix getOpenFileDescriptors() to use _Total PDH counter instance [merged]
📝 Issue Planner
Check the box below or use the
@coderabbitai plancommand to generate an implementation plan and prompts that you can use with your favorite coding assistant.✅ Successfully generated implementation plan.
💡 Use
@coderabbitai <feedback>to replan🧪 Issue enrichment is currently in open beta.
To disable automatic issue enrichment, add the following to your
.coderabbit.yaml:💬 Have feedback or questions? Drop into our discord!
coderabbitai commentedon Apr 22, 2026
Coding Plan
Summary
queryStatvfs()infrastructure; macOS requires newstatfs64bindings in both JNA and FFM; Windows uses the existingGetDiskFreeSpaceExbinding but bypasses volume enumeration*OSFileStoreclass gets a new protected helper method for partial field updates, preserving encapsulation while enabling the fast pathDesign Choices
Design Choice 1: BSD/Unix platforms excluded from this change
Options Considered:
statvfsJNA bindings for all four BSD/Unix platforms (comprehensive but high effort)java.io.Filefor space-only fast path, accepting inode updates remain slowChosen Option: 3
Rationale: Focus on Linux, macOS, and Windows where infrastructure is ready; BSD/Unix is a follow-up effort with its own ticket.
💡 User Tips
Regenerate the plan with different choices with
@coderabbitai <feedback>.Implementation Steps
Phase 1: Linux Fast Path
Linux already has the
queryStatvfs(path)infrastructure inLinuxFileSystem. The change is to bypass/proc/mountsparsing and UUID map building by callingqueryStatvfsdirectly on the known mount point.Task 1: Add Protected Space/Inode Update Helper to LinuxOSFileStore
Add a protected method that updates only the 5 space/inode fields directly, bypassing the full
getFileStoreMatching()loop.updateSpaceAndInodes(long[] vfs)inoshi-common/src/main/java/oshi/software/common/os/linux/LinuxOSFileStore.javathat maps the 5-element array to the corresponding fields[totalInodes, freeInodes, totalSpace, usableSpace, freeSpace]per existing convention inLinuxFileSystemTask 2: Implement Fast Path in LinuxOSFileStore.updateAttributes()
Modify
updateAttributes()to attempt a directstatvfscall first before falling back to full enumeration.LinuxOSFileStore.updateAttributes(), first callfs.queryStatvfs(getMount())directlytruegetFileStoreMatching()pathLinuxOSFileStoreis inoshi-commonand calls the abstractqueryStatvfs()which is implemented polymorphically🤖 Prompt for AI agents
Phase 2: macOS Fast Path
macOS currently uses
getfsstat64(bulk enumeration) plus DiskArbitration session for everyupdateAttributes()call. This phase adds a per-pathstatfs64binding and uses it directly.Task 1: Add statfs64 JNA Binding
Add the per-path
statfs64method to the macOS JNA interface.oshi-core/src/main/java/oshi/jna/platform/mac/SystemB.java, add method declaration:int statfs64(String path, com.sun.jna.platform.mac.SystemB.Statfs buf)Stringtoconst char*and handles the struct pointer; no additional struct definition needed sinceStatfsalready exists in the JNA platform libraryTask 2: Add statfs64 FFM Binding
Add the per-path
statfs64downcall handle to the macOS FFM bindings.oshi-core-ffm/src/main/java/oshi/ffm/mac/MacSystemFunctions.java, add aMethodHandleforstatfs64with signatureint statfs64(const char *path, struct statfs *buf)oshi-core-ffm/src/main/java/oshi/ffm/mac/MacSystem.java, addPathElementconstants for space fields:F_BSIZE,F_BLOCKS,F_BFREE,F_BAVAIL(inode fieldsF_FILES,F_FFREEalready exist)STATFSstruct layout can be reusedTask 3: Add Protected Space/Inode Update Helper to MacOSFileStore
Add a method to the base class that allows subclasses to update just the 5 space/inode fields.
oshi-common/src/main/java/oshi/software/common/os/mac/MacOSFileStore.java, add a protected method likeupdateSpaceAndInodes(long freeSpace, long usableSpace, long totalSpace, long freeInodes, long totalInodes)logicalVolume,description, andfsTypeunchangedTask 4: Implement Fast Path in MacOSFileStoreJNA.updateAttributes()
Modify the JNA implementation to attempt direct
statfs64first.oshi-core/src/main/java/oshi/software/os/mac/MacOSFileStoreJNA.java, updateupdateAttributes()to:Statfsstruct and callSystemB.INSTANCE.statfs64(getMount(), stat)f_ffree,f_filesfrom the struct for inodes, and usejava.io.Filemethods for space (matching existing pattern)updateSpaceAndInodes()helperstatfs64fails, fall back to the existinggetFileStoreMatching()enumeration pathTask 5: Implement Fast Path in MacOSFileStoreFFM.updateAttributes()
Modify the FFM implementation to attempt direct
statfs64first.oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSFileStoreFFM.java, updateupdateAttributes()to:MemorySegmentfor theSTATFSstruct, callMacSystemFunctions.statfs64(pathSeg, buf)java.io.Filefor spaceupdateSpaceAndInodes()helper🤖 Prompt for AI agents
Phase 3: Windows Fast Path
Windows already has
GetDiskFreeSpaceExbindings in both JNA and FFM, butupdateAttributes()still iterates through all volumes viaFindFirstVolume/FindNextVolume. This phase callsGetDiskFreeSpaceExdirectly on the known mount path.Task 1: Add Protected Space Update Helper to WindowsOSFileStore
Add a method to update only the 3 space fields (Windows has no meaningful inode counts).
oshi-common/src/main/java/oshi/software/common/os/windows/WindowsOSFileStore.java, add a protected method likeupdateSpace(long freeSpace, long usableSpace, long totalSpace)Task 2: Implement Fast Path in WindowsOSFileStoreJNA.updateAttributes()
Modify the JNA implementation to call
GetDiskFreeSpaceExdirectly.oshi-core/src/main/java/oshi/software/os/windows/WindowsOSFileStoreJNA.java, updateupdateAttributes()to:Kernel32.INSTANCE.GetDiskFreeSpaceEx(getMount(), userFreeBytes, totalBytes, systemFreeBytes)directlyupdateSpace()helper with the retrieved valuesgetLocalVolumes()/getWmiVolumes()enumeration path for local/network drives respectivelyTask 3: Implement Fast Path in WindowsOSFileStoreFFM.updateAttributes()
Modify the FFM implementation to call
GetDiskFreeSpaceExdirectly.oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOSFileStoreFFM.java, updateupdateAttributes()to:Kernel32FFM.GetDiskFreeSpaceEx(...)with the known mount pathupdateSpace()helper🤖 Prompt for AI agents
Research
OSHI has parallel implementations for JNA (
oshi-core) and FFM (oshi-core-ffm) across all platforms. Each platform has a*FileSystemclass that handles enumeration and a*OSFileStoreclass that represents individual mounts. Linux already has aqueryStatvfs(path)protected method inLinuxFileSystemthat can be called directly. macOS hasgetfsstat64(bulk) but lacks a per-pathstatfs64binding in both JNA and FFM. Windows hasGetDiskFreeSpaceExavailable in both modules butupdateAttributes()still iterates through all volumes before calling it. Base store classes (MacOSFileStore,WindowsOSFileStore) have private mutable fields with anupdateFrom()method that copies all 8 fields atomically—a new helper is needed for partial (space-only) updates.🚀 Next Steps
🤖 All AI agent prompts combined
💡 Iterate on the plan with:
@coderabbitai <feedback>💬 Have feedback or questions? Drop into our discord!
Add fast path to OSFileStore.updateAttributes() for Linux, macOS, and…