Files
seaweedfs/weed/util/fadvise_linux.go
Chris Lu e1fa4ec756 perf(cache): drop OS page cache after disk cache reads (#9098)
* perf(cache): drop OS page cache after disk cache reads

After reading from the on-disk chunk cache, advise the kernel via
FADV_DONTNEED to release the corresponding page cache pages. This
prevents double-caching the same data in both user-space and kernel
page caches, freeing RAM for other uses on systems with large disk
caches.

* fix(cache): guard dropReadCache against zero length and invalid fd

A zero-length fadvise is interpreted as "to end of file" on Linux,
which would inadvertently drop the page cache for the entire remainder
of the cache volume. Also check fd >= 0 to avoid unnecessary syscalls
when the backend file is closed.

* perf(cache): only apply FADV_DONTNEED for reads >= 1 MiB

For small needle reads the syscall overhead outweighs the memory
savings, and the kernel page cache is more beneficial for warm data.
Restrict fadvise to reads of at least 1 MiB where the freed page
cache is meaningful.
2026-04-16 09:38:42 -07:00

14 lines
442 B
Go

//go:build linux
package util
import "golang.org/x/sys/unix"
// DropOSPageCache advises the kernel that the given byte range is no longer
// needed in the page cache. This is useful after reading from a user-space
// cache (e.g., on-disk chunk cache) to prevent the kernel from double-caching
// the same data.
func DropOSPageCache(fd int, offset int64, length int64) error {
return unix.Fadvise(fd, offset, length, unix.FADV_DONTNEED)
}