#[cfg(not(any(target_os = "linux", target_os = "macos")))]
use crate::error::{Error, Result};
#[derive(Debug, Clone)]
pub struct PagerConfig {
pub budget: u64,
pub chunk: usize,
pub elem: usize,
pub compress_level: i32,
pub min_saving: f64,
pub process_budget: Option<u64>,
}
impl PagerConfig {
pub fn new(budget: u64) -> Self {
PagerConfig {
budget,
chunk: 1 << 20,
elem: 4,
compress_level: 1,
min_saving: 0.15,
process_budget: None,
}
}
}
#[cfg(any(target_os = "macos", test))]
pub(crate) fn arena_bytes(physical: u64, chunk: usize) -> usize {
const FLOOR: u64 = 64 << 30;
const MAX_CHUNKS: u64 = 1 << 23;
let chunk = chunk.max(1) as u64;
let ceiling = FLOOR.max(chunk.saturating_mul(MAX_CHUNKS));
let len = physical.saturating_mul(8).clamp(FLOOR, ceiling);
let len = usize::try_from(len).unwrap_or(usize::MAX);
len - len % chunk as usize
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PagerStats {
pub budget: u64,
pub limit: u64,
pub used: u64,
pub peak_used: u64,
pub regions: u64,
pub mapped_bytes: u64,
pub resident_bytes: u64,
pub compressed_bytes: u64,
pub faults: u64,
pub zero_fills: u64,
pub restores: u64,
pub restore_seconds: f64,
pub evictions: u64,
pub compress_in: u64,
pub compress_out: u64,
pub compress_seconds: f64,
pub incompressible: u64,
pub overruns: u64,
pub spurious: u64,
pub outside_peak: u64,
pub limit_low: u64,
pub trims: u64,
}
#[cfg(target_os = "linux")]
mod imp;
#[cfg(target_os = "linux")]
pub use imp::{Pager, in_pager_thread};
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::{Pager, in_pager_thread};
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
pub fn in_pager_thread() -> bool {
false
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
pub struct Pager {
_never: std::convert::Infallible,
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn unsupported() -> Error {
Error::Unsupported(
"transparent paging needs Linux userfaultfd; on this system use rt::Runtime buffers \
(explicit pins)"
.into(),
)
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
impl Pager {
pub fn new(_config: PagerConfig) -> Result<Pager> {
Err(unsupported())
}
pub fn map(&self, _len: usize) -> Result<*mut u8> {
match self._never {}
}
pub unsafe fn unmap(&self, _addr: *mut u8) -> Result<usize> {
match self._never {}
}
pub fn owns(&self, _addr: *const u8) -> Option<usize> {
match self._never {}
}
pub fn contains(&self, _addr: *const u8) -> bool {
match self._never {}
}
pub fn limit(&self) -> u64 {
match self._never {}
}
pub fn stats(&self) -> PagerStats {
match self._never {}
}
}
#[cfg(test)]
#[cfg(any(target_os = "linux", target_os = "macos"))]
mod tests;
#[cfg(test)]
mod portable_tests {
use super::*;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use crate::error::Error;
#[test]
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn other_systems_say_why() {
let e = Pager::new(PagerConfig::new(64 << 20)).err().unwrap();
assert!(e.to_string().contains("userfaultfd"), "{e}");
}
#[test]
fn defaults_are_sane() {
let c = PagerConfig::new(1 << 30);
assert_eq!(c.chunk % 4096, 0);
assert!(c.chunk <= crate::codec::CHUNK);
let _ = Error::Unsupported(String::new());
}
#[test]
fn the_arena_scales_with_the_machine() {
const GIB: u64 = 1 << 30;
const MIB: usize = 1 << 20;
assert_eq!(arena_bytes(8 * GIB, MIB) as u64, 64 * GIB);
assert_eq!(arena_bytes(0, MIB) as u64, 64 * GIB);
assert_eq!(arena_bytes(128 * GIB, MIB) as u64, 1024 * GIB);
assert_eq!(arena_bytes(512 * GIB, MIB) as u64, 4096 * GIB);
assert_eq!(arena_bytes(4096 * GIB, MIB) as u64, 8192 * GIB);
assert_eq!(arena_bytes(128 * GIB, 16 * 1024) as u64, 128 * GIB);
assert_eq!(arena_bytes(512 * GIB, 4096) as u64, 64 * GIB);
assert_eq!(arena_bytes(8 * GIB, 3 * 4096) % (3 * 4096), 0);
assert_eq!(arena_bytes(u64::MAX, MIB) % MIB, 0);
}
}