Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8329256

Offer options to call posix_fadvise() when opening files or using a FileChannel

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • None
    • core-libs
    • other, linux, windows

      After discussions on JDK-8292771 I will open the new issue.

      Use case: When implementing databases in Java it is often needed advise how the files are used to allow OS kernel to improve paging and readahead. POSIX has the posix_fadvise() libc function for that; it should be exposed to Java (optionally).

      Related: When using FileChannel#map() it would also be nice to use posix_madvise(), but since Java 22 there's official support to do the syscall using project Panama using the MemorySegment which was mapped before. Nevertheless, when we implement posix_fadvise, it would be good to also apply the same setting to mapped memory regions (see below for details).

      Proposal:
      - Add a new set of OpenOptions (like done for O_DIRECT, can be a separate class) that tells on opening the file how it will be used. If the OpenOption is given it will call posix_fadvise(fd, 0, 0, POSIX_FADV_xxxx) which will advise the kernel to apply the setting for the whole file (len == 0 means whole file).
      - In FileChannel add additional possibilities to only advise for a slice of the file. This could be a separate method.

      On Windows there's FILE_FLAG_SEQUENTIAL_SCAN and FILE_FLAG_RANDOM_ACCESS as option when opening files. So this can be implemented much more straight-forward direcly when opening the file (it maps directly to those proposed OpenOptions).

      If the call is not supported by the underlying platform, the OpenOption should be ignored. As it is an advice, it can be ignored if not supported.

            Unassigned Unassigned
            uschindler Uwe Schindler
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: