Skip to main content

pagers_core/ops/
evict.rs

1use crate::mincore::PageMap;
2
3use super::{FileContext, Op, ResidencyEffect};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct Evict;
8
9impl Op for Evict {
10    const LABEL: &str = "evicted";
11    const EFFECT: ResidencyEffect = ResidencyEffect::EvictAdvisory;
12    type Output = ();
13
14    fn execute<PM: PageMap + Sync>(&self, ctx: &FileContext<'_, PM>) -> crate::Result<()> {
15        tracing::debug!("Evicting {}", ctx.path().display());
16
17        #[cfg(target_os = "linux")]
18        {
19            use std::os::unix::io::AsFd;
20            nix::fcntl::posix_fadvise(
21                ctx.file().as_fd(),
22                ctx.offset() as libc::off_t,
23                ctx.len() as libc::off_t,
24                nix::fcntl::PosixFadviseAdvice::POSIX_FADV_DONTNEED,
25            )?;
26        }
27
28        #[cfg(target_os = "macos")]
29        {
30            use nix::sys::mman::{MsFlags, msync};
31            use std::ptr::NonNull;
32
33            unsafe {
34                let ptr = NonNull::new(ctx.mmap().as_ptr() as *mut _)
35                    .expect("mmap pointer should be non-null");
36                msync(ptr, ctx.len(), MsFlags::MS_INVALIDATE)?;
37            }
38        }
39
40        Ok(())
41    }
42}