Skip to content

Commit

Permalink
Use MADV_FREE to prevent OOM
Browse files Browse the repository at this point in the history
  • Loading branch information
ellenhp committed Feb 23, 2024
1 parent 03f7ae7 commit 388a206
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
17 changes: 12 additions & 5 deletions airmail/src/directory/uffd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::{collections::HashSet, num::NonZeroUsize, os::raw::c_void, sync::Arc, t

use log::{debug, error, info, trace, warn};
use lru::LruCache;
use nix::sys::mman::{madvise, MmapAdvise};
use nix::libc::{madvise, MADV_COLD, MADV_DONTNEED, MADV_FREE};
use tokio::{
runtime::Runtime,
spawn,
sync::{
broadcast::{Receiver, Sender},
Mutex,
Expand Down Expand Up @@ -92,7 +93,10 @@ async fn fetch_and_resume(
4096,
true,
);
dont_need(dst_ptr as usize);
spawn(async move {
tokio::time::sleep(Duration::from_millis(1000)).await;
dont_need(dst_ptr as usize);
});
}
{
trace!("Locking recent chunks to insert new chunk");
Expand Down Expand Up @@ -127,8 +131,7 @@ async fn fetch_and_resume(
fn dont_need(page_start: usize) {
// Round down to page size.
unsafe {
madvise(page_start as *mut c_void, 4096, MmapAdvise::MADV_WILLNEED)
.expect("madvise failed");
madvise(page_start as *mut c_void, 4096, MADV_FREE);
}
}

Expand Down Expand Up @@ -178,7 +181,11 @@ pub(crate) fn handle_uffd(uffd: Uffd, mmap_start: usize, _len: usize, artifact_u
4096,
true,
);
dont_need(addr as usize);
let addr = addr as usize;
rt.spawn(async move {
tokio::time::sleep(Duration::from_millis(1000)).await;
dont_need(addr);
});
}
continue;
}
Expand Down
7 changes: 6 additions & 1 deletion airmail/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl AirmailIndex {
}
mandatory_queries.push(Box::new(BoostQuery::new(Box::new(query), boost)));
} else {
let query: Box<dyn Query> = if self.is_remote {
let query: Box<dyn Query> = if self.is_remote || !lenient {
Box::new(TermQuery::new(term, IndexRecordOption::Basic))
} else {
Box::new(FuzzyTermQuery::new_prefix(term, 0, false))
Expand Down Expand Up @@ -307,6 +307,11 @@ impl AirmailIndex {
)
.await;

#[cfg(feature = "invasive_logging")]
{
dbg!(&query);
}

let (top_docs, searcher) = spawn_blocking(move || {
(searcher.search(&query, &TopDocs::with_limit(10)), searcher)
})
Expand Down

0 comments on commit 388a206

Please sign in to comment.