diff --git a/Cargo.lock b/Cargo.lock index 675a63a..b3b56d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -84,7 +84,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "collect" -version = "1.0.1" +version = "1.0.2" dependencies = [ "bitflags", "bytes", diff --git a/src/memfile.rs b/src/memfile.rs index a0e7431..50b6b5f 100644 --- a/src/memfile.rs +++ b/src/memfile.rs @@ -16,6 +16,9 @@ use std::{ pub mod fd; pub mod error; mod map; +//TODO: #[cfg(feature="hugetlb")] +mod hp; + /// Flags passed to `memfd_create()` when used in this module const MEMFD_CREATE_FLAGS: libc::c_uint = libc::MFD_CLOEXEC; diff --git a/src/memfile/hp.rs b/src/memfile/hp.rs new file mode 100644 index 0000000..61e2235 --- /dev/null +++ b/src/memfile/hp.rs @@ -0,0 +1,35 @@ +//! # Huge pages +//! Calculates available huge page sizes, and creates `memfd_create()` flag masks for a `MFD_HUGETLB` fd. +use super::*; +use std::{ + path::Path, +}; +use libc::{ + MFD_HUGETLB, + MAP_HUGE_SHIFT, +}; + +/// The location in the kernel's API which shows all valid huge-page sizes. +/// +/// This is a directory, and its subdirectories' *names* will contain the size in this format: `hugepages-(\d+)kB` (where the first capture-group is the size in kB.) +/// The contents of those subdirectories themselves are irrelevent for our purpose. +pub const HUGEPAGE_SIZES_LOCATION: &'static str = "/sys/kernel/mm/hugepages"; + +/// Take a directory path and try to parse the hugepage size from it. +/// +/// All subdirectories from `HUGEPAGE_SIZES_LOCATION` should be passed to this, and the correct system-valid hugepage size will be returned for that specific hugepage. +fn find_size_bytes(path: impl AsRef) -> Option +{ + let path= path.as_ref(); + if !path.is_dir() { + return None; + } + + let dir_name = path.file_name()?; + // location of the `-` in the dir name + let split_loc = memchr::memchr(b'-', dir_name.as_bytes())?; + + //TODO: find the `k` (XXX: Is it always in kB? Or do we have to find the last non-digit byte instead?) For now, we can just memchr('k') I think -- look into kernel spec for this later. + + None +}