NUMA++ 0.11.0
Loading...
Searching...
No Matches
mempolicy.cpp
1/**
2 * @cond Impl
3 * @copyright ESO 2024 - European Southern Observatory
4 *
5 * @brief Definition of memory policy classes.
6 */
8
9#include <iostream>
10
11#include <numapp/lowlevel.hpp>
12
13namespace numapp {
14namespace thisThread {
15std::error_code Apply(MemPolicy const& policy) noexcept {
16 if (auto err = ll::SetMemPolicy(static_cast<int>(policy.GetMode()), policy.GetNodemask());
17 err) {
18 return err;
19 } else {
20 return {};
21 }
22}
23
24std::error_code ApplyStack(MemPolicy const& policy, MemPolicyFlag flags) noexcept {
25 pthread_attr_t attr;
26 void* stack_addr = nullptr;
27 std::size_t stack_size = 0;
28 if (auto err = pthread_getattr_np(pthread_self(), &attr); err != 0) {
29 return std::make_error_code(static_cast<std::errc>(err));
30 }
31 if (auto err = pthread_attr_getstack(&attr, &stack_addr, &stack_size); err != 0) {
32 return std::make_error_code(static_cast<std::errc>(err));
33 }
34 return ::numapp::Apply(stack_addr, stack_size, policy, flags);
35}
36
37} // namespace thisThread
38
39std::error_code Apply(void* address,
40 std::size_t length,
41 MemPolicy const& policy,
42 MemPolicyFlag flags) NUMAPP_NOEXCEPT {
43 return ll::SetMemPolicy(address,
44 length,
45 static_cast<int>(policy.GetMode()),
46 policy.GetNodemask(),
47 static_cast<std::underlying_type<MemPolicyFlag>::type>(flags));
48}
49
51 auto node_mask = Nodemask();
52 // setbit does not return error if node is greater than node mask
53 (void)numa_bitmask_setbit(node_mask.GetNative(), node);
54 // So verify that bit is set and throw otherwise
55 if (!numa_bitmask_isbitset(node_mask.GetNative(), node)) {
56 throw std::system_error(std::make_error_code(std::errc::invalid_argument));
57 }
58
59 return MemPolicy(Mode::Bind, std::move(node_mask));
60}
61
63 int mode = 0;
64 Nodemask mask;
65 if (auto err = ll::GetMemPolicy(mode, mask, nullptr, 0); err) {
66 throw std::system_error(err);
67 }
68 return MemPolicy(static_cast<Mode>(mode), std::move(mask));
69}
70
72 int mode = 0;
73 Nodemask mask;
74 if (auto err = ll::GetMemPolicy(mode, mask, address, MPOL_F_ADDR); err) {
75 throw std::system_error(err);
76 }
77 return MemPolicy(static_cast<Mode>(mode), std::move(mask));
78}
79
80[[nodiscard]] std::error_code ScopedMemPolicy::Restore() noexcept {
81 if (m_old_policy) {
82 return thisThread::Apply(*m_old_policy);
83 }
84 return {};
85}
86
87std::ostream& operator<<(std::ostream& os, MemPolicy::Mode const& mode) {
88 switch (mode) {
90 os << "Default";
91 break;
93 os << "Bind";
94 break;
96 os << "Interleave";
97 break;
99 os << "Preferred";
100 break;
101 };
102 return os;
103}
104
105std::ostream& operator<<(std::ostream& os, MemPolicy const& mempolicy) {
106 os << "MemPolicy {mode=" << mempolicy.GetMode() << ", mask=" << mempolicy.GetNodemask() << "}";
107 return os;
108}
109} // namespace numapp
110/// @endcond Impl
Class representing a memory policy that can be modified and used to apply to the current thread or a ...
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.
static MemPolicy MakeFromActive()
Make from active default policy of calling thread.
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::error_code Restore() noexcept
Restores memory policy that was read during construction.
std::error_code Apply(CpuAffinity const &affinity) noexcept
Apply policy to calling thread.
std::error_code Apply(pid_t thread, CpuAffinity const &affinity) noexcept
Apply policy to specified thread.
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.
Contains low-level functions.
Contains declarations for numapp::MemPolicy.
std::error_code SetMemPolicy(int mode, Nodemask const &mask) noexcept
Set active memory policy for calling thread.
std::error_code GetMemPolicy(int &mode, Nodemask &mask, void *addr, unsigned flags) noexcept
Get memory policy for calling thread or an address.