diff --git a/pkg/file/file_linux.go b/pkg/file/file_linux.go new file mode 100644 index 0000000..484749c --- /dev/null +++ b/pkg/file/file_linux.go @@ -0,0 +1,30 @@ +//go:build linux + +package file + +import ( + "fmt" + "os" + + "golang.org/x/sys/unix" +) + +// OpenFileFADVDONTNEED opens file with FADV_DONTNEED +func OpenFileFADVDONTNEED(path string) (*os.File, error) { + // Open the file + file, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("error opening file: %w", err) + } + + // File descriptor + fd := int(file.Fd()) + + // Use FADV_DONTNEED to suggest not caching pages + err = unix.Fadvise(fd, 0, 0, unix.FADV_DONTNEED) + if err != nil { + return nil, fmt.Errorf("error setting FADV_DONTNEED: %w", err) + } + + return file, nil +} diff --git a/pkg/file/file_notlinux.go b/pkg/file/file_notlinux.go new file mode 100644 index 0000000..0173f73 --- /dev/null +++ b/pkg/file/file_notlinux.go @@ -0,0 +1,12 @@ +//go:build !linux + +package file + +import ( + "os" +) + +// OpenFileFADVDONTNEED opens file with FADV_DONTNEED +func OpenFileFADVDONTNEED(path string) (*os.File, error) { + return os.Open(path) +}