Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memory page size alignment for madvise, mlock and munlock #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions src/mlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,50 @@

#![cfg(feature = "use_os")]

use libc::_SC_PAGESIZE;

/// Retrieve page size of the system
///
/// Used for alignment
unsafe fn page_size() -> usize {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the page size cache in alloc mod.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, shouldn't be doing syscalls everytime we mlock/munlock. I've initialized the alloc_init() function and returned the PAGE_SIZE from the alloc module instead

use libc::sysconf;
sysconf(_SC_PAGESIZE) as usize
}

/// Cross-platform `mlock`.
///
/// * Unix `mlock`.
/// * Windows `VirtualLock`.
pub unsafe fn mlock(addr: *mut u8, len: usize) -> bool {
#[cfg(unix)]
{
let page_size = page_size();

// start address of the page obtained from masked the value of the page size
// with the memory address
//
let start_addr = (addr as usize) & !(page_size - 1);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be put into a separate function and some tests added.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored it out to a separate function, and added a few tests to check whether it returns the right start and end page offsets


// End address of the page
let end_addr = ((addr as usize) + len + page_size - 1) & !(page_size - 1);

let aligned_len = end_addr - start_addr;

#[cfg(target_os = "linux")]
libc::madvise(addr as *mut libc::c_void, len, libc::MADV_DONTDUMP);
libc::madvise(
start_addr as *mut libc::c_void,
aligned_len,
libc::MADV_DONTDUMP,
);

#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
libc::madvise(addr as *mut libc::c_void, len, libc::MADV_NOCORE);
libc::madvise(
start_addr as *mut libc::c_void,
aligned_len,
libc::MADV_NOCORE,
);

libc::mlock(addr as *mut libc::c_void, len) == 0
libc::mlock(start_addr as *mut libc::c_void, aligned_len) == 0
}

#[cfg(windows)]
Expand All @@ -33,13 +63,32 @@ pub unsafe fn munlock(addr: *mut u8, len: usize) -> bool {

#[cfg(unix)]
{
let page_size = page_size();

// start address of the page obtained from masked the value of the page size
// with the memory address
//
let start_addr = (addr as usize) & !(page_size - 1);

// End address of the page
let end_addr = ((addr as usize) + len + page_size - 1) & !(page_size - 1);

let aligned_len = end_addr - start_addr;
#[cfg(target_os = "linux")]
libc::madvise(addr as *mut libc::c_void, len, libc::MADV_DODUMP);
libc::madvise(
start_addr as *mut libc::c_void,
aligned_len,
libc::MADV_DODUMP,
);

#[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
libc::madvise(addr as *mut libc::c_void, len, libc::MADV_CORE);
libc::madvise(
start_addr as *mut libc::c_void,
aligned_len,
libc::MADV_CORE,
);

libc::munlock(addr as *mut libc::c_void, len) == 0
libc::munlock(start_addr as *mut libc::c_void, aligned_len) == 0
}

#[cfg(windows)]
Expand Down