NUMA++ 0.11.0
Loading...
Searching...
No Matches
numapp::HugePageSize Class Reference

Describes a huge page size and maintains the invariant that page size is an integral power of 2, compatible with mmap() expectations. More...

#include <memory.hpp>

Public Member Functions

 HugePageSize (HugePagePreset preset) noexcept
 Construct from preset page size value.
 
 HugePageSize (std::size_t bytes)
 Construct with given page size.
 
auto operator<=> (HugePageSize const &) const noexcept -> std::strong_ordering=default
 Three-way comparison operator.
 
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.

void Example() {
// Allocate 16MiB from NUMA node 1 in 2MiB pages.
auto const size = 16 * 1024 * 1024;
auto const page_size = numapp::HugePagePreset::Huge2M;
// 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.
if (auto ec = numapp::MemLock(ptr, size, numapp::LockFlag::PreFault); ec) {
// 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);
}
static MemPolicy MakeBindNode(int node)
Creates a strict policy (using MPOL_BIND) to allocate all memory to the specified node.
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
@ Huge2M
2 MiB page size.
Definition memory.hpp:505
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
@ PreFault
Locks pages whether they are resident or not.
Definition memory.hpp:126
Contains memory function declarations.
Parameters
Parameters
[in]sizeNumber of bytes to allocate/free, will be rounded up to multiples of page_size.
[in]page_sizePage size.
[in]policyMemory policy to apply.
[in]flagsControls behaviour if memory have to be moved because it was already paged-in due to a numapp::MemLockAll policy.
[in]ptrPage-aligned pointer to memory previously allocated with numapp::AllocateHuge().
[out]ecError code for non-throwing overloads. If there is an out of memory situation the error code std::errc::not_enough_memory is used.
Exceptions
std::system_erroron failure (only applicable to throwing-overloads without std::error_code return value).
Returns
numapp::AllocateHuge() returns pointer to memory block that is at least size big or nullptr on failure.
Related man-page(s):
constexpr auto Size () const noexcept -> std::size_t
 Query size in bytes.
 

Detailed Description

Describes a huge page size and maintains the invariant that page size is an integral power of 2, compatible with mmap() expectations.

Definition at line 534 of file memory.hpp.

Constructor & Destructor Documentation

◆ HugePageSize() [1/2]

numapp::HugePageSize::HugePageSize ( HugePagePreset preset)
noexcept

Construct from preset page size value.

Parameters
presetpage size preset.

Definition at line 145 of file memory.cpp.

◆ HugePageSize() [2/2]

numapp::HugePageSize::HugePageSize ( std::size_t bytes)
explicit

Construct with given page size.

Provided size bytes must an integer power of two and its base-2 logarithm must not be 0.

Parameters
bytesSize in bytes.
Exceptions
std::invalid_argumentif bytes is not an integer power of two or its base-2 logarithm is 0.

Definition at line 149 of file memory.cpp.

Member Function Documentation

◆ Size()

auto numapp::HugePageSize::Size ( ) const -> std::size_t
constexprnoexcept

Query size in bytes.

Returns
Size in bytes, is always an integer power of two and its base-2 logarithm is > 0.

Definition at line 728 of file memory.hpp.


The documentation for this class was generated from the following files: