NUMA++ 0.12.0
Loading...
Searching...
No Matches
numapolicies.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup numapp
4 * @brief Contains declarations for NumaPolicies
5 * @copyright
6 * SPDX-FileCopyrightText: 2020-2023 European Southern Observatory (ESO)
7 *
8 * SPDX-License-Identifier: LGPL-3.0-only
9 */
10#ifndef NUMAPP_NUMAPOLICIES_HPP_
11#define NUMAPP_NUMAPOLICIES_HPP_
12#include <iosfwd>
13
15#include <numapp/mempolicy.hpp>
16#include <numapp/scheduler.hpp>
17
18namespace numapp {
19
20class NumaPolicies;
21
22namespace thisThread {
23/**
24 * @name Apply Policies to Current Thread
25 *
26 * Applies specified policies to calling thread.
27 */
28/// @{
29
30/**
31 * Apply the set policies to current thread.
32 *
33 * If any failure occurs the function will stop applying policies and return error.
34 * This means the policies can be partially applied.
35 *
36 * @param policies The set of policies to apply.
37 * @returns system error code if policies fails to apply.
38 *
39 * @relatesalso numapp::NumaPolicies
40 */
41std::error_code Apply(NumaPolicies const& policies) noexcept;
42/// @}
43
44} // namespace thisThread
45
46/**
47 * Combines the the available NUMA policy types in one object.
48 *
49 * @ingroup numapp
50 */
51class NumaPolicies {
52public:
53 NumaPolicies() noexcept = default;
54 NumaPolicies(MemPolicy const& policy) : m_mempolicy(policy) {};
55 NumaPolicies(CpuAffinity const& policy) : m_affinity(policy) {};
56 NumaPolicies(Scheduler const& policy) : m_scheduler(policy) {};
57 NumaPolicies(NumaPolicies&&) noexcept = default;
58 NumaPolicies& operator=(NumaPolicies&&) noexcept = default;
59
60 NumaPolicies(NumaPolicies const& rhs) = default;
61 NumaPolicies& operator=(NumaPolicies const& rhs) noexcept = default;
62
63 /**
64 * Set CPU affinity.
65 */
66 inline void SetCpuAffinity(std::optional<CpuAffinity> affinity) noexcept;
67
68 /**
69 * Get CPU affinity.
70 */
71 inline std::optional<CpuAffinity> GetCpuAffinity() const noexcept;
72
73 /**
74 * Swap CPU affinity.
75 */
76 inline void SwapCpuAffinity(std::optional<CpuAffinity>& affinity) noexcept;
77
78 /**
79 * Set scheduler.
80 */
81 inline void SetScheduler(std::optional<Scheduler> scheduler) noexcept;
82
83 /**
84 * Get scheduler.
85 */
86 inline std::optional<Scheduler> GetScheduler() const noexcept;
87
88 /**
89 * Swap scheduler.
90 */
91 inline void SwapScheduler(std::optional<Scheduler>& scheduler) noexcept;
92
93 /**
94 * Set memory policy.
95 */
96 inline void SetMemPolicy(std::optional<MemPolicy> policy) noexcept;
97
98
99 /**
100 * Get memory policy.
101 */
102 inline std::optional<MemPolicy> GetMemPolicy() const noexcept;
103
104 /**
105 * Swap memory policy.
106 */
107 inline void SwapMemPolicy(std::optional<MemPolicy>& mempol) noexcept ;
108
109private:
110 std::optional<CpuAffinity> m_affinity;
111 std::optional<MemPolicy> m_mempolicy;
112 std::optional<Scheduler> m_scheduler;
113};
114
115// Inlines
116void NumaPolicies::SetCpuAffinity(std::optional<CpuAffinity> affinity) noexcept {
117 m_affinity.swap(affinity);
118}
119
120std::optional<CpuAffinity> NumaPolicies::GetCpuAffinity() const noexcept {
121 return m_affinity;
122}
123
124void NumaPolicies::SwapCpuAffinity(std::optional<CpuAffinity>& affinity) noexcept {
125 m_affinity.swap(affinity);
126}
127
128void NumaPolicies::SetScheduler(std::optional<Scheduler> scheduler) noexcept {
129 m_scheduler.swap(scheduler);
130}
131
132std::optional<Scheduler> NumaPolicies::GetScheduler() const noexcept {
133 return m_scheduler;
134}
135
136void NumaPolicies::SwapScheduler(std::optional<Scheduler>& scheduler) noexcept {
137 m_scheduler.swap(scheduler);
138}
139
140void NumaPolicies::SetMemPolicy(std::optional<MemPolicy> policy) noexcept {
141 m_mempolicy.swap(policy);
142}
143
144std::optional<MemPolicy> NumaPolicies::GetMemPolicy() const noexcept {
145 return m_mempolicy;
146}
147
148void NumaPolicies::SwapMemPolicy(std::optional<MemPolicy>& mempol) noexcept {
149 m_mempolicy.swap(mempol);
150}
151
152/**
153 * Formats @a policies and inserts it to @a os.
154 *
155 * @param os output stream to insert into.
156 * @param policies policy to format.
157 * @returns @a os
158 */
159std::ostream& operator<<(std::ostream& os, NumaPolicies const& policies);
160
161} // namespace numapp
162#endif // NUMAPP_NUMAPOLICIES_HPP_
Create CPU affinity and apply to current thread.
Class representing a memory policy that can be modified and used to apply to the current thread or a ...
Combines the the available NUMA policy types in one object.
void SwapScheduler(std::optional< Scheduler > &scheduler) noexcept
Swap scheduler.
void SwapMemPolicy(std::optional< MemPolicy > &mempol) noexcept
Swap memory policy.
void SetCpuAffinity(std::optional< CpuAffinity > affinity) noexcept
Set CPU affinity.
std::optional< MemPolicy > GetMemPolicy() const noexcept
Get memory policy.
std::optional< Scheduler > GetScheduler() const noexcept
Get scheduler.
std::optional< CpuAffinity > GetCpuAffinity() const noexcept
Get CPU affinity.
void SwapCpuAffinity(std::optional< CpuAffinity > &affinity) noexcept
Swap CPU affinity.
void SetMemPolicy(std::optional< MemPolicy > policy) noexcept
Set memory policy.
void SetScheduler(std::optional< Scheduler > scheduler) noexcept
Set scheduler.
A sum-type of all supported schedulers.
Contains declarations for CpuAffinity.
std::error_code Apply(CpuAffinity const &affinity) noexcept
Apply policy to calling thread.
Contains declarations for numapp::MemPolicy.
Contains scheduler declarations.