Skip to content

OSFileStore.updateAttributes() re-enumerates all mounts instead of querying the known path #3185

Closed

Description

@dbwiddis

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

  1. coderabbitai commented on Apr 22, 2026

    @coderabbitai
    Contributor
    🔗 Related PRs

    #3158 - Fix getOpenFileDescriptors() to use _Total PDH counter instance [merged]


    📝 Issue Planner

    Check the box below or use the @coderabbitai plan command 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:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  2. self-assigned this
    on Apr 22, 2026
  3. coderabbitai commented on Apr 22, 2026

    @coderabbitai
    Contributor

    Coding Plan

    Summary

    • Adds platform-specific fast paths that call stat syscalls directly on the known mount point instead of re-enumerating all filesystems
    • Linux leverages the existing queryStatvfs() infrastructure; macOS requires new statfs64 bindings in both JNA and FFM; Windows uses the existing GetDiskFreeSpaceEx binding but bypasses volume enumeration
    • Each platform's base *OSFileStore class gets a new protected helper method for partial field updates, preserving encapsulation while enabling the fast path
    • Fallback to full enumeration is preserved when the direct syscall fails (e.g., mount was removed), ensuring backward compatibility
    Design Choices
    Design Choice 1: BSD/Unix platforms excluded from this change

    Options Considered:

    1. Add statvfs JNA bindings for all four BSD/Unix platforms (comprehensive but high effort)
    2. Use java.io.File for space-only fast path, accepting inode updates remain slow
    3. Exclude from this change and address in a follow-up ticket

    Chosen 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 in LinuxFileSystem. The change is to bypass /proc/mounts parsing and UUID map building by calling queryStatvfs directly 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.

    • Add a new method updateSpaceAndInodes(long[] vfs) in oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOSFileStore.java that maps the 5-element array to the corresponding fields
    • The array layout is: [totalInodes, freeInodes, totalSpace, usableSpace, freeSpace] per existing convention in LinuxFileSystem
    Task 2: Implement Fast Path in LinuxOSFileStore.updateAttributes()

    Modify updateAttributes() to attempt a direct statvfs call first before falling back to full enumeration.

    • In LinuxOSFileStore.updateAttributes(), first call fs.queryStatvfs(getMount()) directly
    • If it returns non-null, update only the 5 space/inode fields and return true
    • If it fails (returns null) or the mount no longer exists, fall back to the existing full getFileStoreMatching() path
    • This change applies to both JNA and FFM since LinuxOSFileStore is in oshi-common and calls the abstract queryStatvfs() which is implemented polymorphically
    🤖 Prompt for AI agents
    This phase adds a Linux fast path to `LinuxOSFileStore.updateAttributes()` so
    that it calls `queryStatvfs` directly on the known mount point instead of
    re-enumerating all filesystems.
    
    **Task 1: Add `updateSpaceAndInodes(long[] vfs)` helper**
    - File:
    `oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOSFileStore.java`
    - Add a new `protected` method `updateSpaceAndInodes(long[] vfs)` that maps the
    5-element array to the corresponding space/inode fields
    - Array layout convention (from `LinuxFileSystem`): `[totalInodes, freeInodes,
    totalSpace, usableSpace, freeSpace]`
    - This method should update only these 5 fields and nothing else, bypassing the
    full `getFileStoreMatching()` loop
    
    **Task 2: Implement fast path in `LinuxOSFileStore.updateAttributes()`**
    - In `LinuxOSFileStore.updateAttributes()`, attempt a direct
    `fs.queryStatvfs(getMount())` call first
    - If the result is non-null, call `updateSpaceAndInodes()` with the returned
    array and return `true`
    - If the result is null (mount no longer exists or call failed), fall back to
    the existing full `getFileStoreMatching()` enumeration path
    - No changes are needed to the JNA or FFM subclasses since `LinuxOSFileStore` is
    in `oshi-common` and `queryStatvfs()` is resolved polymorphically
    

    Phase 2: macOS Fast Path

    macOS currently uses getfsstat64 (bulk enumeration) plus DiskArbitration session for every updateAttributes() call. This phase adds a per-path statfs64 binding and uses it directly.

    Task 1: Add statfs64 JNA Binding

    Add the per-path statfs64 method to the macOS JNA interface.

    • In 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)
    • JNA auto-maps String to const char* and handles the struct pointer; no additional struct definition needed since Statfs already exists in the JNA platform library
    Task 2: Add statfs64 FFM Binding

    Add the per-path statfs64 downcall handle to the macOS FFM bindings.

    • In oshi-core-ffm/src/main/java/oshi/ffm/mac/MacSystemFunctions.java, add a MethodHandle for statfs64 with signature int statfs64(const char *path, struct statfs *buf)
    • In oshi-core-ffm/src/main/java/oshi/ffm/mac/MacSystem.java, add PathElement constants for space fields: F_BSIZE, F_BLOCKS, F_BFREE, F_BAVAIL (inode fields F_FILES, F_FFREE already exist)
    • The existing STATFS struct layout can be reused
    Task 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.

    • In oshi-common/src/main/java/oshi/software/common/os/mac/MacOSFileStore.java, add a protected method like updateSpaceAndInodes(long freeSpace, long usableSpace, long totalSpace, long freeInodes, long totalInodes)
    • This updates only the 5 space/inode fields, leaving logicalVolume, description, and fsType unchanged
    Task 4: Implement Fast Path in MacOSFileStoreJNA.updateAttributes()

    Modify the JNA implementation to attempt direct statfs64 first.

    • In oshi-core/src/main/java/oshi/software/os/mac/MacOSFileStoreJNA.java, update updateAttributes() to:
      • Create a Statfs struct and call SystemB.INSTANCE.statfs64(getMount(), stat)
      • If successful (returns 0), extract f_ffree, f_files from the struct for inodes, and use java.io.File methods for space (matching existing pattern)
      • Call the new updateSpaceAndInodes() helper
      • If statfs64 fails, fall back to the existing getFileStoreMatching() enumeration path
    Task 5: Implement Fast Path in MacOSFileStoreFFM.updateAttributes()

    Modify the FFM implementation to attempt direct statfs64 first.

    • In oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSFileStoreFFM.java, update updateAttributes() to:
      • Allocate a MemorySegment for the STATFS struct, call MacSystemFunctions.statfs64(pathSeg, buf)
      • If successful, read inode fields from the struct, use java.io.File for space
      • Call the new updateSpaceAndInodes() helper
      • Fall back to full enumeration if the call fails
    🤖 Prompt for AI agents
    This phase adds a macOS fast path by introducing a per-path `statfs64` binding
    (in both JNA and FFM) and using it directly in `updateAttributes()` instead of
    bulk enumeration.
    
    **Task 1: Add `statfs64` JNA Binding**
    - File: `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)`
    - JNA auto-maps `String` to `const char*`; the `Statfs` struct already exists in
    the JNA platform library, so no new struct definition is needed
    
    **Task 2: Add `statfs64` FFM Binding**
    - File: `oshi-core-ffm/src/main/java/oshi/ffm/mac/MacSystemFunctions.java` — add
    a `MethodHandle` for `statfs64` with signature `int statfs64(const char *path,
    struct statfs *buf)`
    - File: `oshi-core-ffm/src/main/java/oshi/ffm/mac/MacSystem.java` — add
    `PathElement` constants for space fields: `F_BSIZE`, `F_BLOCKS`, `F_BFREE`,
    `F_BAVAIL` (inode fields `F_FILES`, `F_FFREE` already exist)
    - Reuse the existing `STATFS` struct layout
    
    **Task 3: Add `updateSpaceAndInodes()` helper to `MacOSFileStore`**
    - File:
    `oshi-common/src/main/java/oshi/software/common/os/mac/MacOSFileStore.java`
    - Add a `protected` method `updateSpaceAndInodes(long freeSpace, long
    usableSpace, long totalSpace, long freeInodes, long totalInodes)`
    - Update only the 5 space/inode fields; leave `logicalVolume`, `description`,
    and `fsType` unchanged
    
    **Task 4: Implement fast path in `MacOSFileStoreJNA.updateAttributes()`**
    - File: `oshi-core/src/main/java/oshi/software/os/mac/MacOSFileStoreJNA.java`
    - Create a `Statfs` struct and call `SystemB.INSTANCE.statfs64(getMount(),
    stat)`
    - On success (return value 0): extract `f_ffree` and `f_files` for inodes; use
    `java.io.File` methods for space values (matching existing pattern); call
    `updateSpaceAndInodes()`
    - On failure: fall back to the existing `getFileStoreMatching()` enumeration
    path
    
    **Task 5: Implement fast path in `MacOSFileStoreFFM.updateAttributes()`**
    - File:
    `oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSFileStoreFFM.java`
    - Allocate a `MemorySegment` for the `STATFS` struct and call
    `MacSystemFunctions.statfs64(pathSeg, buf)`
    - On success: read inode fields from the struct; use `java.io.File` for space
    values; call `updateSpaceAndInodes()`
    - On failure: fall back to full enumeration
    

    Phase 3: Windows Fast Path

    Windows already has GetDiskFreeSpaceEx bindings in both JNA and FFM, but updateAttributes() still iterates through all volumes via FindFirstVolume/FindNextVolume. This phase calls GetDiskFreeSpaceEx directly 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).

    • In oshi-common/src/main/java/oshi/software/common/os/windows/WindowsOSFileStore.java, add a protected method like updateSpace(long freeSpace, long usableSpace, long totalSpace)
    • This updates only the 3 space fields, leaving other mutable fields unchanged
    • Note: Windows inode fields are always -1 and don't need updating
    Task 2: Implement Fast Path in WindowsOSFileStoreJNA.updateAttributes()

    Modify the JNA implementation to call GetDiskFreeSpaceEx directly.

    • In oshi-core/src/main/java/oshi/software/os/windows/WindowsOSFileStoreJNA.java, update updateAttributes() to:
      • Call Kernel32.INSTANCE.GetDiskFreeSpaceEx(getMount(), userFreeBytes, totalBytes, systemFreeBytes) directly
      • If successful (returns non-zero), call the new updateSpace() helper with the retrieved values
      • If it fails, fall back to the existing getLocalVolumes()/getWmiVolumes() enumeration path for local/network drives respectively
    Task 3: Implement Fast Path in WindowsOSFileStoreFFM.updateAttributes()

    Modify the FFM implementation to call GetDiskFreeSpaceEx directly.

    • In oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOSFileStoreFFM.java, update updateAttributes() to:
      • Call Kernel32FFM.GetDiskFreeSpaceEx(...) with the known mount path
      • If successful, call the new updateSpace() helper
      • Fall back to full enumeration if the call fails
    🤖 Prompt for AI agents
    This phase adds a Windows fast path so that `updateAttributes()` calls
    `GetDiskFreeSpaceEx` directly on the known mount path instead of iterating
    through all volumes via `FindFirstVolume`/`FindNextVolume`.
    
    **Task 1: Add `updateSpace()` helper to `WindowsOSFileStore`**
    - File:
    `oshi-common/src/main/java/oshi/software/common/os/windows/WindowsOSFileStore.java`
    - Add a `protected` method `updateSpace(long freeSpace, long usableSpace, long
    totalSpace)`
    - Update only the 3 space fields; leave all other mutable fields unchanged
    - Windows inode fields are always -1 and do not need updating
    
    **Task 2: Implement fast path in `WindowsOSFileStoreJNA.updateAttributes()`**
    - File:
    `oshi-core/src/main/java/oshi/software/os/windows/WindowsOSFileStoreJNA.java`
    - Call `Kernel32.INSTANCE.GetDiskFreeSpaceEx(getMount(), userFreeBytes,
    totalBytes, systemFreeBytes)` directly
    - On success (non-zero return): call the new `updateSpace()` helper with the
    retrieved values and return
    - On failure: fall back to the existing `getLocalVolumes()`/`getWmiVolumes()`
    enumeration path for local/network drives respectively
    
    **Task 3: Implement fast path in `WindowsOSFileStoreFFM.updateAttributes()`**
    - File:
    `oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOSFileStoreFFM.java`
    - Call `Kernel32FFM.GetDiskFreeSpaceEx(...)` with the known mount path
    - On success: call the new `updateSpace()` helper
    - On failure: fall back to full enumeration
    
    Research

    OSHI has parallel implementations for JNA (oshi-core) and FFM (oshi-core-ffm) across all platforms. Each platform has a *FileSystem class that handles enumeration and a *OSFileStore class that represents individual mounts. Linux already has a queryStatvfs(path) protected method in LinuxFileSystem that can be called directly. macOS has getfsstat64 (bulk) but lacks a per-path statfs64 binding in both JNA and FFM. Windows has GetDiskFreeSpaceEx available in both modules but updateAttributes() still iterates through all volumes before calling it. Base store classes (MacOSFileStore, WindowsOSFileStore) have private mutable fields with an updateFrom() method that copies all 8 fields atomically—a new helper is needed for partial (space-only) updates.


    🚀 Next Steps

    🤖 All AI agent prompts combined
    Task: 1
    
    This phase adds a Linux fast path to `LinuxOSFileStore.updateAttributes()` so
    that it calls `queryStatvfs` directly on the known mount point instead of
    re-enumerating all filesystems.
    
    **Task 1: Add `updateSpaceAndInodes(long[] vfs)` helper**
    - File:
    `oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOSFileStore.java`
    - Add a new `protected` method `updateSpaceAndInodes(long[] vfs)` that maps the
    5-element array to the corresponding space/inode fields
    - Array layout convention (from `LinuxFileSystem`): `[totalInodes, freeInodes,
    totalSpace, usableSpace, freeSpace]`
    - This method should update only these 5 fields and nothing else, bypassing the
    full `getFileStoreMatching()` loop
    
    **Task 2: Implement fast path in `LinuxOSFileStore.updateAttributes()`**
    - In `LinuxOSFileStore.updateAttributes()`, attempt a direct
    `fs.queryStatvfs(getMount())` call first
    - If the result is non-null, call `updateSpaceAndInodes()` with the returned
    array and return `true`
    - If the result is null (mount no longer exists or call failed), fall back to
    the existing full `getFileStoreMatching()` enumeration path
    - No changes are needed to the JNA or FFM subclasses since `LinuxOSFileStore` is
    in `oshi-common` and `queryStatvfs()` is resolved polymorphically
    ===============================================================================
    
    Task: 2
    
    This phase adds a macOS fast path by introducing a per-path `statfs64` binding
    (in both JNA and FFM) and using it directly in `updateAttributes()` instead of
    bulk enumeration.
    
    **Task 1: Add `statfs64` JNA Binding**
    - File: `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)`
    - JNA auto-maps `String` to `const char*`; the `Statfs` struct already exists in
    the JNA platform library, so no new struct definition is needed
    
    **Task 2: Add `statfs64` FFM Binding**
    - File: `oshi-core-ffm/src/main/java/oshi/ffm/mac/MacSystemFunctions.java` — add
    a `MethodHandle` for `statfs64` with signature `int statfs64(const char *path,
    struct statfs *buf)`
    - File: `oshi-core-ffm/src/main/java/oshi/ffm/mac/MacSystem.java` — add
    `PathElement` constants for space fields: `F_BSIZE`, `F_BLOCKS`, `F_BFREE`,
    `F_BAVAIL` (inode fields `F_FILES`, `F_FFREE` already exist)
    - Reuse the existing `STATFS` struct layout
    
    **Task 3: Add `updateSpaceAndInodes()` helper to `MacOSFileStore`**
    - File:
    `oshi-common/src/main/java/oshi/software/common/os/mac/MacOSFileStore.java`
    - Add a `protected` method `updateSpaceAndInodes(long freeSpace, long
    usableSpace, long totalSpace, long freeInodes, long totalInodes)`
    - Update only the 5 space/inode fields; leave `logicalVolume`, `description`,
    and `fsType` unchanged
    
    **Task 4: Implement fast path in `MacOSFileStoreJNA.updateAttributes()`**
    - File: `oshi-core/src/main/java/oshi/software/os/mac/MacOSFileStoreJNA.java`
    - Create a `Statfs` struct and call `SystemB.INSTANCE.statfs64(getMount(),
    stat)`
    - On success (return value 0): extract `f_ffree` and `f_files` for inodes; use
    `java.io.File` methods for space values (matching existing pattern); call
    `updateSpaceAndInodes()`
    - On failure: fall back to the existing `getFileStoreMatching()` enumeration
    path
    
    **Task 5: Implement fast path in `MacOSFileStoreFFM.updateAttributes()`**
    - File:
    `oshi-core-ffm/src/main/java/oshi/software/os/mac/MacOSFileStoreFFM.java`
    - Allocate a `MemorySegment` for the `STATFS` struct and call
    `MacSystemFunctions.statfs64(pathSeg, buf)`
    - On success: read inode fields from the struct; use `java.io.File` for space
    values; call `updateSpaceAndInodes()`
    - On failure: fall back to full enumeration
    ===============================================================================
    
    Task: 3
    
    This phase adds a Windows fast path so that `updateAttributes()` calls
    `GetDiskFreeSpaceEx` directly on the known mount path instead of iterating
    through all volumes via `FindFirstVolume`/`FindNextVolume`.
    
    **Task 1: Add `updateSpace()` helper to `WindowsOSFileStore`**
    - File:
    `oshi-common/src/main/java/oshi/software/common/os/windows/WindowsOSFileStore.java`
    - Add a `protected` method `updateSpace(long freeSpace, long usableSpace, long
    totalSpace)`
    - Update only the 3 space fields; leave all other mutable fields unchanged
    - Windows inode fields are always -1 and do not need updating
    
    **Task 2: Implement fast path in `WindowsOSFileStoreJNA.updateAttributes()`**
    - File:
    `oshi-core/src/main/java/oshi/software/os/windows/WindowsOSFileStoreJNA.java`
    - Call `Kernel32.INSTANCE.GetDiskFreeSpaceEx(getMount(), userFreeBytes,
    totalBytes, systemFreeBytes)` directly
    - On success (non-zero return): call the new `updateSpace()` helper with the
    retrieved values and return
    - On failure: fall back to the existing `getLocalVolumes()`/`getWmiVolumes()`
    enumeration path for local/network drives respectively
    
    **Task 3: Implement fast path in `WindowsOSFileStoreFFM.updateAttributes()`**
    - File:
    `oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOSFileStoreFFM.java`
    - Call `Kernel32FFM.GetDiskFreeSpaceEx(...)` with the known mount path
    - On success: call the new `updateSpace()` helper
    - On failure: fall back to full enumeration
    
    💡 Iterate on the plan with: @coderabbitai <feedback>
    Example Feedback
    - `@coderabbitai` You can skip phase 3. Add a simple unit test case for phase 2.
    - `@coderabbitai` For design choice 1 go ahead with option 3 and replan.
    

    💬 Have feedback or questions? Drop into our discord!

  4. added a commit that references this issue on May 3, 2026
    48fbcf2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions