14#ifndef NUMAPP_MEMPOLICY_HPP_
15#define NUMAPP_MEMPOLICY_HPP_
22#include <system_error>
51 Strict = MPOL_MF_STRICT,
63 MoveAll = MPOL_MF_MOVE_ALL,
107[[nodiscard]] std::error_code
109 MemPolicyFlag flags = MemPolicyFlag::Move | MemPolicyFlag::Strict)
noexcept;
140[[nodiscard]] std::error_code
Apply(
void* address,
230 : m_mode(mode), m_nodes(std::move(node_mask)) {
235 MemPolicy(MemPolicy&&) noexcept = default;
236 MemPolicy& operator=(MemPolicy&&) noexcept = default;
237 MemPolicy(MemPolicy const& rhs) = default;
238 MemPolicy& operator=(MemPolicy const& rhs) noexcept = default;
317 return m_mode == rhs.m_mode && m_nodes == rhs.m_nodes;
319 [[nodiscard]]
bool operator!=(
MemPolicy const& rhs)
const noexcept {
320 return !(*
this == rhs);
393 : m_old_policy(
MemPolicy::MakeFromActive()) {
396 throw std::system_error(ec);
417 m_old_policy.swap(rhs.m_old_policy);
425 [[nodiscard]] std::error_code
Restore() noexcept;
Class representing a memory policy that can be modified and used to apply to the current thread or a ...
std::ostream & operator<<(std::ostream &os, MemPolicy const &mempolicy)
Formats mempolicy and inserts it to os.
Mode
Memory allocation modes.
@ Bind
The Bind mode defines a strict policy that restricts memory allocation to the nodes specified in node...
@ Default
The Default mode specifies that any nondefault process memory policy be removed, so that the memory p...
@ Preferred
Preferred sets the preferred node for allocation.
@ Interleave
Interleave interleaves page allocations across the nodes specified in nodemask in numeric node ID ord...
MemPolicy(Mode mode, Nodemask &&node_mask) noexcept
Create memory policy.
Nodemask const & GetNodemask() const noexcept
bool operator==(MemPolicy const &rhs) const noexcept
static MemPolicy MakeFromActive()
Make from active default policy of calling thread.
static MemPolicy MakeFromNodeString(Mode mode, char const *nodestring)
Create MemPolicy from mode and nodestring while considering currently allowed nodes.
Mode GetMode() const noexcept
void SetMode(Mode mode) noexcept
Set memory policy mode.
static MemPolicy MakeFromAddress(void *address)
Make from policy at address.
static MemPolicy MakeBindNode(int node)
Creates a strict policy (using MPOL_BIND) to allocate all memory to the specified node.
std::ostream & operator<<(std::ostream &os, MemPolicy::Mode const &mode)
Formats mode and inserts it to os.
static MemPolicy MakeFromNodeStringAll(Mode mode, char const *nodestring)
Create MemPolicy from mode and nodestring considering all nodes, whether they are allowed or not.
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].
ScopedMemPolicy() noexcept=default
Initialized a scoped memory policy that does nothing and holds no state about previous policy.
void Swap(ScopedMemPolicy &rhs) noexcept
Swap state with another ScopedMemPolicy.
ScopedMemPolicy(MemPolicy const &new_policy)
Constructs an object that applies the provided policy during construction and restores it during dest...
std::error_code Restore() noexcept
Restores memory policy that was read during construction.
~ScopedMemPolicy() noexcept
Restores old policy, if any.
Contains definitions for bitwise operators for enums.
#define NUMAPP_ENABLE_BITFLAG(enum)
Enables numapp bitwise operators in overload set for the specified enumeration enum.
std::error_code Apply(CpuAffinity const &affinity) noexcept
Apply policy to calling thread.
MemPolicyFlag
Flag that modify the behaviour of applying a memory policy to a range of memory.
std::error_code ApplyStack(MemPolicy const &policy, MemPolicyFlag flags=MemPolicyFlag::Move|MemPolicyFlag::Strict) noexcept
Convenience function that applies a memory policy to current thread stack memory.
std::error_code Apply(MemPolicy const &policy) noexcept
Applies the default memory policy to the calling thread.
Contains declarations for Nodemask.
Type-safe NUMA node mask.
static Nodemask MakeFromNodestringAll(char const *nodestring)
Construct Nodemask from nodestring that considers does not consider current cpuset.
static Nodemask MakeFromNodestring(char const *nodestring)
Construct Nodemask from nodestring that considers current cpuset.