Skip to content

Commit

Permalink
chore(rust): Add force_populate_read for debugging pagefault performa…
Browse files Browse the repository at this point in the history
…nce problems (pola-rs#21054)
  • Loading branch information
orlp authored Feb 3, 2025
1 parent 480edbc commit 635a215
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub(super) use polars_utils::mem::{
madvise_populate_read, madvise_sequential, madvise_willneed, prefetch_l2,
force_populate_read, madvise_populate_read, madvise_sequential, madvise_willneed, prefetch_l2,
};
pub(super) fn no_prefetch(_: &[u8]) {}

Expand Down Expand Up @@ -51,6 +51,7 @@ pub(super) fn get_memory_prefetch_func(verbose: bool) -> fn(&[u8]) -> () {
);
}
},
Some("force_populate_read") => force_populate_read,
Some(v) => panic!("invalid value for POLARS_MEMORY_PREFETCH: {}", v),
};

Expand All @@ -61,6 +62,7 @@ pub(super) fn get_memory_prefetch_func(verbose: bool) -> fn(&[u8]) -> () {
v if v == madvise_sequential as usize => "madvise_sequential",
v if v == madvise_willneed as usize => "madvise_willneed",
v if v == madvise_populate_read as usize => "madvise_populate_read",
v if v == force_populate_read as usize => "force_populate_read",
_ => unreachable!(),
};

Expand Down
9 changes: 9 additions & 0 deletions crates/polars-utils/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ pub fn madvise_populate_read(#[allow(unused)] slice: &[u8]) {
madvise(slice, libc::MADV_POPULATE_READ);
}

/// Forcibly reads at least one byte each page.
pub fn force_populate_read(slice: &[u8]) {
for i in (0..slice.len()).step_by(*PAGE_SIZE) {
std::hint::black_box(slice[i]);
}

std::hint::black_box(slice.last().copied());
}

#[cfg(target_family = "unix")]
fn madvise(slice: &[u8], advice: libc::c_int) {
let ptr = slice.as_ptr();
Expand Down

0 comments on commit 635a215

Please sign in to comment.