github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/ledger/complete/wal/fadvise_linux.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  package wal
     5  
     6  import (
     7  	"golang.org/x/sys/unix"
     8  )
     9  
    10  // fadviseNoLinuxPageCache advises Linux to evict
    11  // a file from Linux page cache.  This calls unix.Fadvise which
    12  // in turn calls posix_fadvise with POSIX_FADV_DONTNEED.
    13  // If GOOS != "linux" then this function does nothing.
    14  // CAUTION: If fsync=true, this will call unix.Fsync which
    15  // can cause performance hit especially when used inside a loop.
    16  func fadviseNoLinuxPageCache(fd uintptr, fsync bool) error {
    17  	return fadviseSegmentNoLinuxPageCache(fd, 0, 0, fsync)
    18  }
    19  
    20  // fadviseSegmentNoLinuxPageCache advises Linux to evict the
    21  // file segment from Linux page cache.  This calls unix.Fadvise
    22  // which in turn calls posix_fadvise with POSIX_FADV_DONTNEED.
    23  // If GOOS != "linux" then this function does nothing.
    24  // CAUTION: If fsync=true, this will call unix.Fsync which
    25  // can cause performance hit especially when used inside a loop.
    26  //
    27  //nolint:unused,deadcode
    28  func fadviseSegmentNoLinuxPageCache(fd uintptr, off, len int64, fsync bool) error {
    29  	// Optionally call fsync because dirty pages won't be evicted.
    30  	if fsync {
    31  		_ = unix.Fsync(int(fd)) // ignore error because this is optional
    32  	}
    33  
    34  	// Fadvise under the hood calls posix_fadvise.
    35  	// posix_fadvise doc from man page says:
    36  	// "posix_fadvise - predeclare an access pattern for file data"
    37  	// "The advice applies to a (not necessarily existent) region
    38  	// starting at offset and extending for len bytes (or until
    39  	// the end of the file if len is 0) within the file referred
    40  	// to by fd.  The advice is not binding; it merely constitutes
    41  	// an expectation on behalf of the application."
    42  	return unix.Fadvise(int(fd), off, len, unix.FADV_DONTNEED)
    43  }