Add abstraction over mmap

This commit is contained in:
guillaume didier 2020-04-03 11:24:54 +02:00
parent bebbf379c1
commit 811842e142
4 changed files with 54 additions and 14 deletions

View File

@ -151,14 +151,6 @@ const CFLUSH_BUCKET_NUMBER: usize = 500;
const CFLUSH_NUM_ITER: u32 = 1 << 11; const CFLUSH_NUM_ITER: u32 = 1 << 11;
/* TODO Code cleanup :
- change type back to a slice OK
- change return type to return thresholds per cache line ?
- change iteration to be per cache line OK
- take the cache line size as a parameter OK
- parametrize 4k vs 2M ? Or just use the slice length ? OK
*/
pub fn calibrate_flush( pub fn calibrate_flush(
array: &[u8], array: &[u8],
cache_line_size: usize, cache_line_size: usize,

View File

@ -12,6 +12,8 @@ assert_cfg!(
pub mod cache_info; pub mod cache_info;
pub mod calibration; pub mod calibration;
#[cfg(feature = "std")]
pub mod mmap;
pub mod prefetcher; pub mod prefetcher;
use core::arch::x86_64 as arch_x86; use core::arch::x86_64 as arch_x86;

View File

@ -14,8 +14,7 @@ use nix::sched::{sched_getaffinity, sched_setaffinity, CpuSet};
use nix::unistd::Pid; use nix::unistd::Pid;
use nix::Error::Sys; use nix::Error::Sys;
use nix::sys::mman; use cache_utils::mmap::MMappedMemory;
use static_assertions::_core::ptr::{null_mut, slice_from_raw_parts};
/* from linux kernel headers. /* from linux kernel headers.
#define HUGETLB_FLAG_ENCODE_SHIFT 26 #define HUGETLB_FLAG_ENCODE_SHIFT 26
@ -35,7 +34,7 @@ struct Page {
} }
*/ */
pub fn main() { pub fn main() {
let m: &[u8] = unsafe { /*let array: &[u8] = unsafe {
let p: *mut u8 = mman::mmap( let p: *mut u8 = mman::mmap(
null_mut(), null_mut(),
SIZE, SIZE,
@ -55,7 +54,9 @@ pub fn main() {
offset: off_t*/ offset: off_t*/
&*slice_from_raw_parts(p, SIZE) &*slice_from_raw_parts(p, SIZE)
}; };*/
let m = unsafe { MMappedMemory::new(SIZE) };
let array = m.slice();
/* /*
let p = Box::new(Page { mem: [0; 4096] }); let p = Box::new(Page { mem: [0; 4096] });
@ -71,8 +72,8 @@ pub fn main() {
match sched_setaffinity(Pid::from_raw(0), &core) { match sched_setaffinity(Pid::from_raw(0), &core) {
Ok(()) => { Ok(()) => {
calibrate_flush(m, 64, Verbosity::NoOutput); calibrate_flush(array, 64, Verbosity::NoOutput);
calibrate_flush(m, 64, Verbosity::Thresholds); calibrate_flush(array, 64, Verbosity::Thresholds);
sched_setaffinity(Pid::from_raw(0), &old).unwrap(); sched_setaffinity(Pid::from_raw(0), &old).unwrap();
println!("Iteration {}...ok ", i); println!("Iteration {}...ok ", i);
eprintln!("Iteration {}...ok ", i); eprintln!("Iteration {}...ok ", i);

45
cache_utils/src/mmap.rs Normal file
View File

@ -0,0 +1,45 @@
#![cfg(feature = "std")]
use core::ptr::null_mut;
use core::slice::{from_raw_parts, from_raw_parts_mut};
use nix::sys::mman;
/* from linux kernel headers.
#define HUGETLB_FLAG_ENCODE_SHIFT 26
#define HUGETLB_FLAG_ENCODE_MASK 0x3f
#define HUGETLB_FLAG_ENCODE_64KB (16 << HUGETLB_FLAG_ENCODE_SHIFT)
#define HUGETLB_FLAG_ENCODE_512KB (19 << HUGETLB_FLAG_ENCODE_SHIFT)
#define HUGETLB_FLAG_ENCODE_1MB (20 << HUGETLB_FLAG_ENCODE_SHIFT)
#define HUGETLB_FLAG_ENCODE_2MB (21 << HUGETLB_FLAG_ENCODE_SHIFT)
*/
pub struct MMappedMemory {
pointer: *mut u8,
size: usize,
}
impl MMappedMemory {
pub unsafe fn new(size: usize) -> MMappedMemory {
let p: *mut u8 = mman::mmap(
null_mut(),
size,
mman::ProtFlags::PROT_READ | mman::ProtFlags::PROT_WRITE,
mman::MapFlags::MAP_PRIVATE
| mman::MapFlags::MAP_ANONYMOUS
| mman::MapFlags::MAP_HUGETLB,
-1,
0,
)
.unwrap() as *mut u8;
MMappedMemory { pointer: p, size }
}
pub fn slice(&self) -> &[u8] {
unsafe { from_raw_parts(self.pointer, self.size) }
}
pub fn slice_mut(&self) -> &mut [u8] {
unsafe { from_raw_parts_mut(self.pointer, self.size) }
}
}