StephanvanSchaik / mmap-rs

A cross-platform and safe Rust API to create and manage memory mappings in the virtual address space of the calling process.
Apache License 2.0
59 stars 17 forks source link

Backing a vector/boxed array/slice using memory mapping #7

Closed vigna closed 1 year ago

vigna commented 1 year ago

Great create. What if I wanted to allocate memory using memory mapping and see it as slice of, say, u64 instead of u8? Would that require substantial implementation or is there an easy way (I'm a Rust newbie, so the answer could be trivial, sorry).

vigna commented 1 year ago

I'm actually trying this

   let mut mapping = MmapOptions::new(points * size_of::<u64>())
        .with_flags(MmapFlags::HUGE_PAGES)
        .map_mut()
        .unwrap();

    let mut v: &mut [u64] = bytemuck::try_cast_slice_mut(&mut mapping).unwrap();

and it works but under Linux I cannot allocate with the HUGE_PAGES flag (error Nix(ENOMEM)).

StephanvanSchaik commented 1 year ago

What if I wanted to allocate memory using memory mapping and see it as slice of, say, u64 instead of u8?

My recommendation would be to use the bytemuck crate. It provides bytemuck::cast_slice() and bytemuck::cast_slice_mut() that would allow you to interpret &[u8] as &[u64], or in general as &[T] as long as T implements the bytemuck::Pod and bytemuck::Zeroable traits (among other things I might be forgetting about).

StephanvanSchaik commented 1 year ago

I'm actually trying this

   let mut mapping = MmapOptions::new(points * size_of::<u64>())
        .with_flags(MmapFlags::HUGE_PAGES)
        .map_mut()
        .unwrap();

    let mut v: &mut [u64] = bytemuck::try_cast_slice_mut(&mut mapping).unwrap();

and it works but under Linux I cannot allocate with the HUGE_PAGES flag (error Nix(ENOMEM)).

Are you sure you reserved some huge pages using the following command?

echo 4 > /proc/sys/vm/nr_hugepages

or alternatively by having something like the following kernel command line?

hugepages=4

The snippet you linked works for me with some huge pages reserved.

vigna commented 1 year ago

No, I didn't reserve in advance huge pages and now I understand the problem. I'm used to transparent huge pages and I was trying to replicate the behavior.