Skip to content

Commit

Permalink
Use build tag requiring Linux on EN for fadvise
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Apr 22, 2022
1 parent bb354a8 commit 15cdac5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
37 changes: 9 additions & 28 deletions ledger/complete/wal/fadvise.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,25 @@
package wal

import (
"runtime"
//go:build !linux
// +build !linux

"golang.org/x/sys/unix"
)
package wal

// fadviseNoLinuxPageCache advises Linux to evict
// fadviseNoLinuxPageCache does nothing if GOOS != "linux".
// Otherwise, fadviseNoLinuxPageCache advises Linux to evict
// a file from Linux page cache. This calls unix.Fadvise which
// in turn calls posix_fadvise with POSIX_FADV_DONTNEED.
// If GOOS != "linux" then this function does nothing.
// CAUTION: If fsync=true, this will call unix.Fsync which
// can cause performance hit especially when used inside a loop.
func fadviseNoLinuxPageCache(fd uintptr, fsync bool) error {
return fadviseSegmentNoLinuxPageCache(fd, 0, 0, fsync)
return nil
}

// fadviseSegmentNoLinuxPageCache advises Linux to evict the
// fadviseNoLinuxPageCache does nothing if GOOS != "linux".
// Otherwise, fadviseSegmentNoLinuxPageCache advises Linux to evict
// file segment from Linux page cache. This calls unix.Fadvise
// which in turn calls posix_fadvise with POSIX_FADV_DONTNEED.
// If GOOS != "linux" then this function does nothing.
// CAUTION: If fsync=true, this will call unix.Fsync which
// can cause performance hit especially when used inside a loop.
func fadviseSegmentNoLinuxPageCache(fd uintptr, off, len int64, fsync bool) error {
if runtime.GOOS != "linux" {
return nil
}

// Optionally call fsync because dirty pages won't be evicted.
if fsync {
_ = unix.Fsync(int(fd)) // ignore error because this is optional
}

// Fadvise under the hood calls posix_fadvise.
// posix_fadvise doc from man page says:
// "posix_fadvise - predeclare an access pattern for file data"
// "The advice applies to a (not necessarily existent) region
// starting at offset and extending for len bytes (or until
// the end of the file if len is 0) within the file referred
// to by fd. The advice is not binding; it merely constitutes
// an expectation on behalf of the application."
return unix.Fadvise(int(fd), off, len, unix.FADV_DONTNEED)
return nil
}
41 changes: 41 additions & 0 deletions ledger/complete/wal/fadvise_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build linux
// +build linux

package wal

import (
"golang.org/x/sys/unix"
)

// fadviseNoLinuxPageCache advises Linux to evict
// a file from Linux page cache. This calls unix.Fadvise which
// in turn calls posix_fadvise with POSIX_FADV_DONTNEED.
// If GOOS != "linux" then this function does nothing.
// CAUTION: If fsync=true, this will call unix.Fsync which
// can cause performance hit especially when used inside a loop.
func fadviseNoLinuxPageCache(fd uintptr, fsync bool) error {
return fadviseSegmentNoLinuxPageCache(fd, 0, 0, fsync)
}

// fadviseSegmentNoLinuxPageCache advises Linux to evict the
// file segment from Linux page cache. This calls unix.Fadvise
// which in turn calls posix_fadvise with POSIX_FADV_DONTNEED.
// If GOOS != "linux" then this function does nothing.
// CAUTION: If fsync=true, this will call unix.Fsync which
// can cause performance hit especially when used inside a loop.
func fadviseSegmentNoLinuxPageCache(fd uintptr, off, len int64, fsync bool) error {
// Optionally call fsync because dirty pages won't be evicted.
if fsync {
_ = unix.Fsync(int(fd)) // ignore error because this is optional
}

// Fadvise under the hood calls posix_fadvise.
// posix_fadvise doc from man page says:
// "posix_fadvise - predeclare an access pattern for file data"
// "The advice applies to a (not necessarily existent) region
// starting at offset and extending for len bytes (or until
// the end of the file if len is 0) within the file referred
// to by fd. The advice is not binding; it merely constitutes
// an expectation on behalf of the application."
return unix.Fadvise(int(fd), off, len, unix.FADV_DONTNEED)
}

0 comments on commit 15cdac5

Please sign in to comment.