|
| 1 | +//! The `numa` API. |
| 2 | +//! |
| 3 | +//! # Safety |
| 4 | +//! |
| 5 | +//! `mbind` and related functions manipulate raw pointers and have special |
| 6 | +//! semantics and are wildly unsafe. |
| 7 | +#![allow(unsafe_code)] |
| 8 | + |
| 9 | +use crate::{backend, io}; |
| 10 | +use core::ffi::c_void; |
| 11 | + |
| 12 | +pub use backend::numa::types::{Mode, ModeFlags}; |
| 13 | + |
| 14 | +/// `mbind(addr, len, mode, nodemask)`-Set memory policy for a memory range. |
| 15 | +/// |
| 16 | +/// # Safety |
| 17 | +/// |
| 18 | +/// This function operates on raw pointers, but it should only be used |
| 19 | +/// on memory which the caller owns. |
| 20 | +/// |
| 21 | +/// # References |
| 22 | +/// - [Linux] |
| 23 | +/// |
| 24 | +/// [Linux]: https://man7.org/linux/man-pages/man2/mbind.2.html |
| 25 | +#[cfg(linux_kernel)] |
| 26 | +#[inline] |
| 27 | +pub unsafe fn mbind(addr: *mut c_void, len: usize, mode: Mode, nodemask: &[u64], flags: ModeFlags) -> io::Result<()> { |
| 28 | + backend::numa::syscalls::mbind(addr, len, mode, nodemask, flags) |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +/// `set_mempolicy(mode, nodemask)`-Set default NUMA memory policy for |
| 33 | +/// a thread and its children. |
| 34 | +/// |
| 35 | +/// # Safety |
| 36 | +/// |
| 37 | +/// This function operates on raw pointers, but it should only be used |
| 38 | +/// on memory which the caller owns. |
| 39 | +/// |
| 40 | +/// # References |
| 41 | +/// - [Linux] |
| 42 | +/// |
| 43 | +/// [Linux]: https://man7.org/linux/man-pages/man2/set_mempolicy.2.html |
| 44 | +#[cfg(linux_kernel)] |
| 45 | +#[inline] |
| 46 | +pub unsafe fn set_mempolicy(mode: Mode, nodemask: &[u64]) -> io::Result<()> { |
| 47 | + backend::numa::syscalls::set_mempolicy(mode, nodemask) |
| 48 | +} |
| 49 | + |
| 50 | +/// `get_mempolicy_node(addr)`-Return the node ID of the node on which |
| 51 | +/// the address addr is allocated. |
| 52 | +/// |
| 53 | +/// If flags specifies both MPOL_F_NODE and MPOL_F_ADDR, |
| 54 | +/// get_mempolicy() will return the node ID of the node on which the |
| 55 | +/// address addr is allocated into the location pointed to by mode. |
| 56 | +/// If no page has yet been allocated for the specified address, |
| 57 | +/// get_mempolicy() will allocate a page as if the thread had |
| 58 | +/// performed a read (load) access to that address, and return the ID |
| 59 | +/// of the node where that page was allocated. |
| 60 | +/// |
| 61 | +/// # Safety |
| 62 | +/// |
| 63 | +/// This function operates on raw pointers, but it should only be used |
| 64 | +/// on memory which the caller owns. |
| 65 | +/// |
| 66 | +/// # References |
| 67 | +/// - [Linux] |
| 68 | +/// |
| 69 | +/// [Linux]: https://man7.org/linux/man-pages/man2/get_mempolicy.2.html |
| 70 | +#[cfg(linux_kernel)] |
| 71 | +#[inline] |
| 72 | +pub unsafe fn get_mempolicy_node(addr: *mut c_void) -> io::Result<usize> { |
| 73 | + backend::numa::syscalls::get_mempolicy_node(addr) |
| 74 | +} |
| 75 | + |
| 76 | +/// `get_mempolicy_next_node(addr)`-Return node ID of the next node |
| 77 | +/// that will be used for interleaving of internal kernel pages |
| 78 | +/// allocated on behalf of the thread. |
| 79 | +/// |
| 80 | +/// If flags specifies MPOL_F_NODE, but not MPOL_F_ADDR, and the |
| 81 | +/// thread's current policy is MPOL_INTERLEAVE, then get_mempolicy() |
| 82 | +/// will return in the location pointed to by a non-NULL mode |
| 83 | +/// argument, the node ID of the next node that will be used for |
| 84 | +/// interleaving of internal kernel pages allocated on behalf of the |
| 85 | +/// thread. These allocations include pages for memory-mapped files |
| 86 | +/// in process memory ranges mapped using the mmap(2) call with the |
| 87 | +/// MAP_PRIVATE flag for read accesses, and in memory ranges mapped |
| 88 | +/// with the MAP_SHARED flag for all accesses. |
| 89 | +/// |
| 90 | +/// # Safety |
| 91 | +/// |
| 92 | +/// This function operates on raw pointers, but it should only be used |
| 93 | +/// on memory which the caller owns. |
| 94 | +/// |
| 95 | +/// # References |
| 96 | +/// - [Linux] |
| 97 | +/// |
| 98 | +/// [Linux]: https://man7.org/linux/man-pages/man2/get_mempolicy.2.html |
| 99 | +#[cfg(linux_kernel)] |
| 100 | +#[inline] |
| 101 | +pub unsafe fn get_mempolicy_next_node() -> io::Result<usize> { |
| 102 | + backend::numa::syscalls::get_mempolicy_next_node() |
| 103 | +} |
| 104 | + |
0 commit comments