use crate::error::{Error, Result};
use rayon::prelude::*;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering};
use xxhash_rust::xxh3::xxh3_128;
pub const DIGEST_CHUNK: usize = 4 << 20;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Digest(pub Vec<u8>);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceRef {
pub path: PathBuf,
pub offset: u64,
pub len: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpillFile {
pub path: PathBuf,
pub len: u64,
pub digest: Digest,
}
fn combine(len: usize, parts: &[u128]) -> Digest {
let mut buf = Vec::with_capacity(8 + 16 * parts.len());
buf.extend_from_slice(&(len as u64).to_le_bytes());
for p in parts {
buf.extend_from_slice(&p.to_le_bytes());
}
Digest(xxh3_128(&buf).to_le_bytes().to_vec())
}
pub fn digest(data: &[u8]) -> Digest {
combine(data.len(), &digest_parts(data))
}
pub fn digest_parts(data: &[u8]) -> Vec<u128> {
data.par_chunks(DIGEST_CHUNK).map(xxh3_128).collect()
}
pub fn digest_combine(len: usize, parts: &[u128]) -> Digest {
combine(len, parts)
}
#[cfg(target_os = "macos")]
fn lower_io_priority() {
unsafe extern "C" {
fn setiopolicy_np(
iotype: libc::c_int,
scope: libc::c_int,
policy: libc::c_int,
) -> libc::c_int;
}
const IOPOL_TYPE_DISK: libc::c_int = 0;
const IOPOL_SCOPE_THREAD: libc::c_int = 1;
const IOPOL_UTILITY: libc::c_int = 4;
unsafe {
setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_THREAD, IOPOL_UTILITY);
}
}
#[cfg(target_os = "linux")]
fn lower_io_priority() {
const IOPRIO_WHO_PROCESS: libc::c_long = 1; const IOPRIO_CLASS_BE: libc::c_long = 2;
const LOWEST_BE_LEVEL: libc::c_long = 7;
unsafe {
libc::syscall(
libc::SYS_ioprio_set,
IOPRIO_WHO_PROCESS,
0,
(IOPRIO_CLASS_BE << 13) | LOWEST_BE_LEVEL,
);
}
}
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
fn lower_io_priority() {}
fn write_pool() -> Option<&'static rayon::ThreadPool> {
static POOL: OnceLock<Option<rayon::ThreadPool>> = OnceLock::new();
POOL.get_or_init(|| {
rayon::ThreadPoolBuilder::new()
.num_threads(std::thread::available_parallelism().map_or(2, |n| n.get().min(4)))
.thread_name(|i| format!("memopro-spill-{i}"))
.start_handler(|_| lower_io_priority())
.build()
.ok()
})
.as_ref()
}
#[cfg(unix)]
fn read_at(file: &File, buf: &mut [u8], offset: u64) -> std::io::Result<()> {
use std::os::unix::fs::FileExt;
file.read_exact_at(buf, offset)
}
#[cfg(windows)]
fn read_at(file: &File, mut buf: &mut [u8], mut offset: u64) -> std::io::Result<()> {
use std::os::windows::fs::FileExt;
while !buf.is_empty() {
match file.seek_read(buf, offset)? {
0 => return Err(std::io::ErrorKind::UnexpectedEof.into()),
n => {
buf = &mut buf[n..];
offset += n as u64;
}
}
}
Ok(())
}
#[cfg(unix)]
fn create_private(path: &Path) -> std::io::Result<File> {
use std::os::unix::fs::OpenOptionsExt;
std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.mode(0o600)
.open(path)
}
#[cfg(windows)]
fn create_private(path: &Path) -> std::io::Result<File> {
std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(path)
}
#[cfg(unix)]
fn write_at(file: &File, buf: &[u8], offset: u64) -> std::io::Result<()> {
use std::os::unix::fs::FileExt;
file.write_all_at(buf, offset)
}
#[cfg(windows)]
fn write_at(file: &File, mut buf: &[u8], mut offset: u64) -> std::io::Result<()> {
use std::os::windows::fs::FileExt;
while !buf.is_empty() {
match file.seek_write(buf, offset)? {
0 => return Err(std::io::ErrorKind::WriteZero.into()),
n => {
buf = &buf[n..];
offset += n as u64;
}
}
}
Ok(())
}
pub(crate) fn read_region(
file: &File,
offset: u64,
dst: &mut [u8],
expected: Option<&Digest>,
) -> Result<Digest> {
let parts: std::io::Result<Vec<u128>> = dst
.par_chunks_mut(DIGEST_CHUNK)
.enumerate()
.map(|(i, chunk)| {
read_at(file, chunk, offset + (i * DIGEST_CHUNK) as u64)?;
Ok(xxh3_128(chunk))
})
.collect();
let got = combine(dst.len(), &parts?);
match expected {
Some(want) if *want != got => Err(Error::Integrity(
"data read back does not match its digest".into(),
)),
_ => Ok(got),
}
}
static COUNTER: AtomicU64 = AtomicU64::new(0);
#[derive(Debug, Default, Clone, Copy)]
pub struct Engine;
impl Engine {
pub fn new() -> Self {
Engine
}
pub fn digest(&self, data: &[u8]) -> Digest {
digest(data)
}
pub fn read_source_into(
&self,
source: &SourceRef,
dst: &mut [u8],
expected: Option<&Digest>,
) -> Result<Digest> {
if dst.len() as u64 != source.len {
return Err(Error::InvalidArgument(format!(
"destination holds {} bytes, source region {}",
dst.len(),
source.len
)));
}
let file = File::open(&source.path)?;
let size = file.metadata()?.len();
if source.offset.saturating_add(source.len) > size {
return Err(Error::Integrity(format!(
"{} is {size} bytes, shorter than the recorded region",
source.path.display()
)));
}
read_region(&file, source.offset, dst, expected)
}
pub fn write(&self, src: &[u8], dir: &Path) -> Result<SpillFile> {
let name = format!(
"{}-{}-{}.mpspill",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_nanos()),
COUNTER.fetch_add(1, Ordering::Relaxed)
);
let path = dir.join(name);
let file = create_private(&path)?;
let job = || -> std::io::Result<Vec<u128>> {
file.set_len(src.len() as u64)?;
src.par_chunks(DIGEST_CHUNK)
.enumerate()
.map(|(i, chunk)| {
write_at(&file, chunk, (i * DIGEST_CHUNK) as u64)?;
Ok(xxh3_128(chunk))
})
.collect()
};
let parts = match write_pool() {
Some(pool) => pool.install(job),
None => job(),
};
match parts {
Ok(parts) => Ok(SpillFile {
path,
len: src.len() as u64,
digest: combine(src.len(), &parts),
}),
Err(e) => {
drop(file);
let _ = std::fs::remove_file(&path);
Err(e.into())
}
}
}
pub fn read_into(&self, file: &SpillFile, dst: &mut [u8]) -> Result<()> {
let source = SourceRef {
path: file.path.clone(),
offset: 0,
len: file.len,
};
self.read_source_into(&source, dst, Some(&file.digest))
.map(|_| ())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn data(n: usize) -> Vec<u8> {
(0..n).map(|i| (i * 31 + i / 7) as u8).collect()
}
fn tmpdir(tag: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("memopro-{tag}-{}", std::process::id()));
std::fs::create_dir_all(&d).unwrap();
d
}
#[test]
fn digest_is_deterministic_and_sensitive() {
let a = data(DIGEST_CHUNK * 2 + 5);
let d1 = digest(&a);
let d2 = rayon::ThreadPoolBuilder::new()
.num_threads(1)
.build()
.unwrap()
.install(|| digest(&a));
assert_eq!(d1, d2, "independent of thread count");
assert_eq!(d1.0.len(), 16);
let mut b = a.clone();
b[DIGEST_CHUNK + 3] ^= 1;
assert_ne!(d1, digest(&b));
assert_ne!(digest(&a[..10]), digest(&a[..11]));
}
#[test]
fn digest_parts_of_pieces_combine_to_the_whole() {
let a = data(DIGEST_CHUNK * 5 + 77);
let mut parts = digest_parts(&a[..DIGEST_CHUNK * 2]);
parts.extend(digest_parts(&a[DIGEST_CHUNK * 2..DIGEST_CHUNK * 4]));
parts.extend(digest_parts(&a[DIGEST_CHUNK * 4..]));
assert_eq!(digest_combine(a.len(), &parts), digest(&a));
}
#[test]
fn spill_roundtrip_is_bit_exact_and_private() {
let dir = tmpdir("spill");
let src = data(DIGEST_CHUNK * 2 + 123);
let engine = Engine::new();
let file = engine.write(&src, &dir).unwrap();
assert_eq!(file.len, src.len() as u64);
assert_eq!(file.digest, digest(&src));
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = std::fs::metadata(&file.path).unwrap().permissions().mode();
assert_eq!(mode & 0o777, 0o600);
}
let mut out = vec![0u8; src.len()];
engine.read_into(&file, &mut out).unwrap();
assert_eq!(out, src);
std::fs::remove_file(&file.path).unwrap();
}
#[test]
fn corrupted_or_changed_files_are_detected() {
let dir = tmpdir("corrupt");
let src = data(100_000);
let engine = Engine::new();
let file = engine.write(&src, &dir).unwrap();
let mut bytes = std::fs::read(&file.path).unwrap();
bytes[5000] ^= 0xff;
std::fs::write(&file.path, &bytes).unwrap();
let mut out = vec![0u8; src.len()];
assert!(matches!(
engine.read_into(&file, &mut out),
Err(Error::Integrity(_))
));
std::fs::write(&file.path, &bytes[..1000]).unwrap(); assert!(matches!(
engine.read_into(&file, &mut out),
Err(Error::Integrity(_))
));
std::fs::remove_file(&file.path).unwrap();
}
#[test]
fn source_region_read_lands_in_destination() {
let dir = tmpdir("source");
let path = dir.join("weights.bin");
let blob = data(DIGEST_CHUNK + 4096);
std::fs::write(&path, &blob).unwrap();
let region = SourceRef {
path: path.clone(),
offset: 100,
len: (DIGEST_CHUNK + 1000) as u64,
};
let mut dst = vec![0u8; region.len as usize];
let d = Engine::new()
.read_source_into(®ion, &mut dst, None)
.unwrap();
assert_eq!(&dst[..], &blob[100..100 + region.len as usize]);
assert_eq!(d, digest(&dst));
let mut short = vec![0u8; 10];
assert!(matches!(
Engine::new().read_source_into(®ion, &mut short, None),
Err(Error::InvalidArgument(_))
));
std::fs::remove_file(&path).unwrap();
}
}