|
NUMA++ 0.11.0
|
Class representing a memory policy that can be modified and used to apply to the current thread or a memory range. More...
#include <numapp/mempolicy.hpp>
Public Types | |
| enum class | Mode : int { Default = MPOL_DEFAULT , Bind = MPOL_BIND , Interleave = MPOL_INTERLEAVE , Preferred = MPOL_PREFERRED } |
| Memory allocation modes. More... | |
Public Member Functions | |
| MemPolicy (Mode mode, Nodemask &&node_mask) noexcept | |
| Create memory policy. | |
| bool | operator== (MemPolicy const &rhs) const noexcept |
| Mode | GetMode () const noexcept |
| void | SetMode (Mode mode) noexcept |
| Set memory policy mode. | |
| Nodemask const & | GetNodemask () const noexcept |
Static Public Member Functions | |
| static MemPolicy | MakeBindNode (int node) |
Creates a strict policy (using MPOL_BIND) to allocate all memory to the specified node. | |
| static MemPolicy | MakeFromNodeString (Mode mode, char const *nodestring) |
Create MemPolicy from mode and nodestring while considering currently allowed nodes. | |
| static MemPolicy | MakeFromNodeStringAll (Mode mode, char const *nodestring) |
Create MemPolicy from mode and nodestring considering all nodes, whether they are allowed or not. | |
| static MemPolicy | MakeFromActive () |
| Make from active default policy of calling thread. | |
| static MemPolicy | MakeFromAddress (void *address) |
| Make from policy at address. | |
Related Symbols | |||||||||||||||||||||
(Note that these are not member symbols.) | |||||||||||||||||||||
| enum class | MemPolicyFlag : unsigned | ||||||||||||||||||||
| Flag that modify the behaviour of applying a memory policy to a range of memory. | |||||||||||||||||||||
| std::ostream & | operator<< (std::ostream &os, MemPolicy::Mode const &mode) | ||||||||||||||||||||
| Formats mode and inserts it to os. | |||||||||||||||||||||
| std::ostream & | operator<< (std::ostream &os, MemPolicy const &mempolicy) | ||||||||||||||||||||
| Formats mempolicy and inserts it to os. | |||||||||||||||||||||
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;
}
MemPolicy(Mode mode, Nodemask &&node_mask) noexcept Create memory policy. Definition mempolicy.hpp:229 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 * | Allocate (std::size_t size, MemPolicy const &policy, std::error_code &ec) noexcept | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void * | Allocate (std::size_t size, MemPolicy const &policy, MemPolicyFlag flags, std::error_code &ec) noexcept | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void * | Allocate (std::size_t size, MemPolicy const &policy) | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void * | Allocate (std::size_t size, MemPolicy const &policy, MemPolicyFlag flags) | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void | Free (void *ptr, std::size_t size, std::error_code &ec) noexcept | ||||||||||||||||||||
| See group for details. | |||||||||||||||||||||
| void | 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 * | AllocateHuge (std::size_t size, HugePageSize page_size, MemPolicy const &policy, MemPolicyFlag flags, std::error_code &ec) noexcept | ||||||||||||||||||||
| Non-throwing version of AllocateHuge() | |||||||||||||||||||||
| void * | AllocateHuge (std::size_t size, HugePageSize page_size, MemPolicy const &policy, MemPolicyFlag flags) | ||||||||||||||||||||
| Throwing version of AllocateHuge() | |||||||||||||||||||||
Apply Memory Policy to Current Thread | |||||||||||||||||||||
Applies default memory policy to calling thread or to thread stack memory. | |||||||||||||||||||||
| std::error_code | Apply (MemPolicy const &policy) noexcept | ||||||||||||||||||||
| Applies the default memory policy to the calling thread. | |||||||||||||||||||||
Apply Memory Policy to Memory Range | |||||||||||||||||||||
| std::error_code | Apply (void *address, std::size_t length, MemPolicy const &policy, MemPolicyFlag flags) noexcept | ||||||||||||||||||||
| Applies memory policy to the memory pages that spans the range [address, adress + length]. | |||||||||||||||||||||
Class representing a memory policy that can be modified and used to apply to the current thread or a memory range.
Uses numa APIs set_mempolicy and get_mempolicy.
To read the current memory policy use MemPolicy::MakeFromActive. To apply a policy use thisThread::Apply(MemPolicy const&).
For CPU policy see numapp::CpuAffinity.
malloc/std::allocator/new after a sucessfull call to MemPolicy::Apply does not in fact allocate a new physical page but may reuse recently freed memory or otherwise unused, but physically allocated memory.Definition at line 174 of file mempolicy.hpp.
|
strong |
Memory allocation modes.
Definition at line 179 of file mempolicy.hpp.
Create memory policy.
| mode | Policy mode |
| node_mask |
Definition at line 229 of file mempolicy.hpp.
|
inlinenodiscardnoexcept |
Definition at line 326 of file mempolicy.hpp.
|
inlinenoexcept |
Definition at line 342 of file mempolicy.hpp.
|
staticnodiscard |
Creates a strict policy (using MPOL_BIND) to allocate all memory to the specified node.
| node | Numa node. |
| std::system_error | if it fails. |
|
staticnodiscard |
Make from active default policy of calling thread.
| std::system_error | if it fails. |
|
staticnodiscard |
Make from policy at address.
| address | memory address to make policy from. |
| std::system_error | if it fails. |
|
inlinestaticnodiscard |
Create MemPolicy from mode and nodestring while considering currently allowed nodes.
Nodestring use patterns such as:
1-5,7,10!4-5+0-3all| mode | Memory policy mode such as MPOL_BIND |
| nodestring | Nodes to include such as 1-5,7,10 or all. |
| std::system_error | on failure. This can also happen if the nodestring references a disallowed node. Use MakeFromNodeStringAll to ignore this. |
Definition at line 269 of file mempolicy.hpp.
|
inlinestaticnodiscard |
Create MemPolicy from mode and nodestring considering all nodes, whether they are allowed or not.
Nodestring use patterns such as:
1-5,7,10!4-5+0-3all| mode | Memory policy mode such as MPOL_BIND |
| nodestring | Nodes to include such as 1-5,7,10 or all. |
| std::system_error | on failure. This can also happen if the nodestring references a disallowed node. Use MakeFromNodeStringAll to ignore this. |
Definition at line 294 of file mempolicy.hpp.
|
inlinenodiscardnoexcept |
Definition at line 316 of file mempolicy.hpp.
|
inlinenoexcept |
|
Applies memory policy to the memory pages that spans the range [address, adress + length].
Read related man-page of mbind() to understand the various behaviours if memory is mapped or shared.
| address | start of address range. |
| length | length of range. |
| policy | Policy to apply. |
| flags | combination of MemPolicyFlag values that modify the behaviour. |
|
Formats mempolicy and inserts it to os.
| os | output stream to insert into. |
| mempolicy | Memory policy to format. |
|
Formats mode and inserts it to os.
| os | output stream to insert into. |
| mode | Memory policy mode to format. |