-
Notifications
You must be signed in to change notification settings - Fork 175
/
fadvise_linux.go
42 lines (37 loc) · 1.65 KB
/
fadvise_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//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.
//nolint:unused,deadcode
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)
}