Skip to content

Commit f1ba3de

Browse files
committed
Add open with FADV_DONTNEED
1 parent 4e55473 commit f1ba3de

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pkg/directfile/file_linux.go

+19
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,22 @@ func OpenFileWithODirect(path string, blockSize uint32) (*DirectFile, error) {
2525

2626
return &DirectFile{file: file, blockSize: blockSize, Direct: true}, nil
2727
}
28+
29+
// OpenFileFADV_DONTNEED opens file with FADV_DONTNEED
30+
func OpenFileFADV_DONTNEED(path string) (*os.File, error) {
31+
// Open the file
32+
file, err := os.Open(path)
33+
if err != nil {
34+
return nil, fmt.Errorf("error opening file: %w", err)
35+
}
36+
37+
// File descriptor
38+
fd := int(file.Fd())
39+
40+
// Use FADV_DONTNEED to suggest not caching pages
41+
err = unix.Fadvise(fd, 0, 0, unix.FADV_DONTNEED)
42+
if err != nil {
43+
fmt.Println("Error setting FADV_DONTNEED:", err)
44+
return
45+
}
46+
}

pkg/directfile/file_nodirect.go

+9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
package directfile
44

5+
import (
6+
"os"
7+
)
8+
59
// OpenFileWithODirect Opens a file without system cache (DIRECT)
610
func OpenFileWithODirect(path string, blockSize uint32) (*DirectFile, error) {
711
// O_DIRECT is not supported on this platform, fallback to normal open
812
return openNodirect(path)
913
}
14+
15+
// OpenFileFADV_DONTNEED opens file with FADV_DONTNEED
16+
func OpenFileFADV_DONTNEED(path string) (*os.File, error) {
17+
return os.Open(path)
18+
}

0 commit comments

Comments
 (0)