urna_runtime/mmap_cold.rs
1//! OS-level mmap hints, carved out of `mmap_file.rs`, which opens and
2//! validates the file.
3
4use crate::mmap_file::MmapUrnaFile;
5
6impl MmapUrnaFile {
7 /// Hint to the OS that the mmap pages won't be needed soon. The
8 /// next read will fault them back in from disk.
9 ///
10 /// **Caveat:** this is `posix_madvise(MADV_DONTNEED)` - an
11 /// approximation of cold cache, NOT a guarantee. The OS may
12 /// ignore the hint, keep pages around for prefetch, or return
13 /// them from the kernel's page cache anyway. Use it for
14 /// "madvise-cold" benchmarks (a useful upper bound on the warm
15 /// case) but don't claim it's equivalent to a fresh boot. Real
16 /// cold-cache benchmarks need `purge` (macOS) or `echo 3 >
17 /// /proc/sys/vm/drop_caches` (Linux), both of which require
18 /// privileged operations.
19 #[cfg(unix)]
20 pub fn madvise_cold(&self) {
21 use std::ffi::c_void;
22 // SAFETY: passing a valid mmap pointer + length. POSIX_MADV_DONTNEED
23 // does not invalidate or move the mapping; we still hold the Mmap
24 // and can read from it as before.
25 unsafe {
26 libc::posix_madvise(
27 self._mmap.as_ptr() as *mut c_void,
28 self._mmap.len(),
29 libc::POSIX_MADV_DONTNEED,
30 );
31 }
32 }
33
34 /// No-op on non-Unix platforms.
35 #[cfg(not(unix))]
36 pub fn madvise_cold(&self) {}
37}