|
NUMA++ 0.11.0
|
Contains memory function declarations. More...
#include <numapp/config.hpp>#include <sys/mman.h>#include <cstddef>#include <memory_resource>#include <optional>#include <system_error>#include <numapp/flags.hpp>#include <numapp/mempolicy.hpp>#include <numapp/nodemask.hpp>Go to the source code of this file.
Classes | |
| class | numapp::PageResource |
| Polymorphic memory resource allocating full system pages with specified NUMA policy. More... | |
| class | numapp::LockResource |
| Lock memory allocated from upstream memory resource using specified LockFlag. More... | |
| class | numapp::HugePageSize |
Describes a huge page size and maintains the invariant that page size is an integral power of 2, compatible with mmap() expectations. More... | |
| class | numapp::HugePageResource |
| Polymorphic memory resource allocating huge pages with specified NUMA policy. More... | |
Namespaces | |
| namespace | numapp |
Enumerations | |
| enum class | numapp::HugePagePreset : std::size_t { numapp::HugePagePreset::Huge8k = 8 * 1024 , numapp::HugePagePreset::Huge16k = 16 * 1024 , numapp::HugePagePreset::Huge64k = 64 * 1024 , numapp::HugePagePreset::Huge256k = 256 * 1024 , numapp::HugePagePreset::Huge1M = 1 * 1024 * 1024 , numapp::HugePagePreset::Huge2M = 2 * 1024 * 1024 , numapp::HugePagePreset::Huge4M = 4 * 1024 * 1024 , numapp::HugePagePreset::Huge16M = 16 * 1024 * 1024 , numapp::HugePagePreset::Huge256M = 256 * 1024 * 1024 , numapp::HugePagePreset::Huge1G = 1ul * 1024 * 1024 * 1024 } |
| Preset huge page sizes. More... | |
Functions | |||||||||||||||||||||
| auto | numapp::EncodeMmapFlags (HugePageSize page_size) noexcept -> int | ||||||||||||||||||||
Encodes page size into bit representation expected by mmap(). | |||||||||||||||||||||
Hardware Queries | |||||||||||||||||||||
Query host system/hardware. | |||||||||||||||||||||
| std::size_t | numapp::GetPageSize () noexcept | ||||||||||||||||||||
| Fast query of system page size. | |||||||||||||||||||||
| int | numapp::GetNumNodes () noexcept | ||||||||||||||||||||
| Query number of configured NUMA nodes. | |||||||||||||||||||||
| std::optional< int > | numapp::GetNodeDistance (int node1, int node2) noexcept | ||||||||||||||||||||
| Get NUMA distance between two nodes. | |||||||||||||||||||||
| std::optional< int > | numapp::GetNodeOfCpu (int cpu) noexcept | ||||||||||||||||||||
| Get NUMA node of the given CPU. | |||||||||||||||||||||
Allocate and free memory with specified memory policy | |||||||||||||||||||||
(1) Overloads without
Example allocating with bind policy and then lock and pre-fault the newly allocated memory: #include <numapp/memory.hpp>
#include <numapp/mempolicy.hpp>
void* BindAlloc(std::size_t size, int node, std::error_code& ec) {
using namespace numapp;
MemPolicy policy = MemPolicy::MakeBindNode(node);
void* ptr = Allocate(size, policy, MemPolicyFlag::Strict, ec);
if (ec) {
return nullptr;
}
ec = MemLock(ptr, size, LockFlag::PreFault);
if (ec) {
return nullptr;
}
return ptr;
}
Class representing a memory policy that can be modified and used to apply to the current thread or a ... Definition mempolicy.hpp:174 static MemPolicy MakeBindNode(int node) Creates a strict policy (using MPOL_BIND) to allocate all memory to the specified node. std::error_code MemLock(void const *addr, std::size_t len, LockFlag flag) noexcept Lock memory pages in the specified address range. Definition memory.cpp:58 void * Allocate(std::size_t size, MemPolicy const &policy, std::error_code &ec) noexcept See group for details. Definition memory.cpp:87 Contains memory function declarations. Contains declarations for numapp::MemPolicy. Definition bitmask.cpp:11 | |||||||||||||||||||||
| void * | numapp::Allocate (std::size_t size, MemPolicy const &policy, std::error_code &ec) noexcept | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void * | numapp::Allocate (std::size_t size, MemPolicy const &policy, MemPolicyFlag flags, std::error_code &ec) noexcept | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void * | numapp::Allocate (std::size_t size, MemPolicy const &policy) | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void * | numapp::Allocate (std::size_t size, MemPolicy const &policy, MemPolicyFlag flags) | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void | numapp::Free (void *ptr, std::size_t size, std::error_code &ec) noexcept | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void | numapp::Free (void *ptr, std::size_t size) | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
Allocate and free huge pages | |||||||||||||||||||||
Allocation is similar to Allocate and free but with the additional argument specifying the (huge) page size. Like non-huge allocations the pages are not pre-faulted by the functions so to avoid page fault failurs it is recommended to do that before use. #include <numapp/memory.hpp>
void Example() {
// Allocate 16MiB from NUMA node 1 in 2MiB pages.
auto const size = 16 * 1024 * 1024;
// 1) AllocateHuge will allocate size, rounding up to nearest page_size
// multiple
void* ptr = numapp::AllocateHuge(size,
page_size,
numapp::MemPolicyFlag::Strict);
// 2) It is particularly important to pre-fault huge-pages as the likelihood
// for failure is higher. If page-fault would fail when memory is accessed
// the result is a segmentation fault.
// Page-fault failed; this would have caused a segmentation fault if not
// for `numapp::MemLock()`.
throw std::system_error(ec, "Page fault failed");
}
// 3) FreeHuge will free memory, rounding up to nearest page_size multiple
// as required by mmap.
numapp::FreeHuge(ptr, size, page_size);
}
void FreeHuge(void *ptr, std::size_t size, HugePageSize page_size, std::error_code &ec) noexcept Free huge pages previously allocated with AllocateHuge. Definition memory.cpp:205 void * AllocateHuge(std::size_t size, HugePageSize page_size, MemPolicy const &policy, MemPolicyFlag flags, std::error_code &ec) noexcept Non-throwing version of AllocateHuge() Definition memory.cpp:165
| |||||||||||||||||||||
| void * | numapp::AllocateHuge (std::size_t size, HugePageSize page_size, MemPolicy const &policy, MemPolicyFlag flags, std::error_code &ec) noexcept | ||||||||||||||||||||
| Non-throwing version of AllocateHuge() | |||||||||||||||||||||
| void * | numapp::AllocateHuge (std::size_t size, HugePageSize page_size, MemPolicy const &policy, MemPolicyFlag flags) | ||||||||||||||||||||
| Throwing version of AllocateHuge() | |||||||||||||||||||||
| void | numapp::FreeHuge (void *ptr, std::size_t size, HugePageSize page_size, std::error_code &ec) noexcept | ||||||||||||||||||||
| Free huge pages previously allocated with AllocateHuge. | |||||||||||||||||||||
| void | numapp::FreeHuge (void *ptr, std::size_t size, HugePageSize page_size) | ||||||||||||||||||||
Throwing version of FreeHuge(). | |||||||||||||||||||||
Memory Locking | |
These functions provide the means to prevent memory from being paged to the swap area.
Locking may require additional permissions if it exceeds resource limits (- See also page section Memory Locking. | |
| enum class | numapp::LockFlag : unsigned int { numapp::LockFlag::PreFault = 0u , numapp::LockFlag::OnFault = MLOCK_ONFAULT } |
| Mutually exclusive flags that modifies behaviour of MemLock(). More... | |
| enum class | numapp::LockAllFlag : int { numapp::LockAllFlag::Current = MCL_CURRENT , numapp::LockAllFlag::Future = MCL_FUTURE , numapp::LockAllFlag::OnFault = MCL_ONFAULT } |
| Flags that are combined to modify behaviour of MemLockAll(). More... | |
| std::error_code | numapp::MemLock (void const *addr, std::size_t len, LockFlag flag) noexcept |
| Lock memory pages in the specified address range. | |
| std::error_code | numapp::MemUnlock (void const *addr, std::size_t len) noexcept |
| Unlock memory pages in the specified address range. | |
| std::error_code | numapp::MemLockAll (LockAllFlag flags) noexcept |
| Lock all memory pages as specified by provided flags. | |
| std::error_code | numapp::MemUnlockAll () noexcept |
| Unlock all locked memory in this process. | |
Contains memory function declarations.
Definition in file memory.hpp.