Skip to content

Commit 15cdac5

Browse files
committed
Use build tag requiring Linux on EN for fadvise
1 parent bb354a8 commit 15cdac5

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

ledger/complete/wal/fadvise.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,25 @@
1-
package wal
2-
3-
import (
4-
"runtime"
1+
//go:build !linux
2+
// +build !linux
53

6-
"golang.org/x/sys/unix"
7-
)
4+
package wal
85

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

19-
// fadviseSegmentNoLinuxPageCache advises Linux to evict the
16+
// fadviseNoLinuxPageCache does nothing if GOOS != "linux".
17+
// Otherwise, fadviseSegmentNoLinuxPageCache advises Linux to evict
2018
// file segment from Linux page cache. This calls unix.Fadvise
2119
// which in turn calls posix_fadvise with POSIX_FADV_DONTNEED.
2220
// If GOOS != "linux" then this function does nothing.
2321
// CAUTION: If fsync=true, this will call unix.Fsync which
2422
// can cause performance hit especially when used inside a loop.
2523
func fadviseSegmentNoLinuxPageCache(fd uintptr, off, len int64, fsync bool) error {
26-
if runtime.GOOS != "linux" {
27-
return nil
28-
}
29-
30-
// Optionally call fsync because dirty pages won't be evicted.
31-
if fsync {
32-
_ = unix.Fsync(int(fd)) // ignore error because this is optional
33-
}
34-
35-
// Fadvise under the hood calls posix_fadvise.
36-
// posix_fadvise doc from man page says:
37-
// "posix_fadvise - predeclare an access pattern for file data"
38-
// "The advice applies to a (not necessarily existent) region
39-
// starting at offset and extending for len bytes (or until
40-
// the end of the file if len is 0) within the file referred
41-
// to by fd. The advice is not binding; it merely constitutes
42-
// an expectation on behalf of the application."
43-
return unix.Fadvise(int(fd), off, len, unix.FADV_DONTNEED)
24+
return nil
4425
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
func fadviseSegmentNoLinuxPageCache(fd uintptr, off, len int64, fsync bool) error {
27+
// Optionally call fsync because dirty pages won't be evicted.
28+
if fsync {
29+
_ = unix.Fsync(int(fd)) // ignore error because this is optional
30+
}
31+
32+
// Fadvise under the hood calls posix_fadvise.
33+
// posix_fadvise doc from man page says:
34+
// "posix_fadvise - predeclare an access pattern for file data"
35+
// "The advice applies to a (not necessarily existent) region
36+
// starting at offset and extending for len bytes (or until
37+
// the end of the file if len is 0) within the file referred
38+
// to by fd. The advice is not binding; it merely constitutes
39+
// an expectation on behalf of the application."
40+
return unix.Fadvise(int(fd), off, len, unix.FADV_DONTNEED)
41+
}

0 commit comments

Comments
 (0)