NUMA++ 0.11.0
Loading...
Searching...
No Matches
cpuaffinity.hpp
Go to the documentation of this file.
1/**
2 * @file
3 * @ingroup numapp_affinity
4 * @copyright ESO 2024 - European Southern Observatory
5 *
6 * @brief Contains declarations for CpuAffinity
7 *
8 * @defgroup numapp_affinity CPU Affinity APIs
9 * @ingroup numapp
10 * @brief NUMA++ CPU affinity APIs
11 *
12 * Use header file <tt>\#include <numapp/cpuaffinity.hpp></tt>
13 *
14 * See @ref affinity-api for an overview.
15 *
16 */
17#ifndef NUMAPP_CPUAFFINITY_HPP_
18#define NUMAPP_CPUAFFINITY_HPP_
19
20#include <bitset>
21#include <cstring>
22#include <iosfwd>
23#include <optional>
24#include <system_error>
25
26#include "cpumask.hpp"
27
28namespace numapp {
29
30class CpuAffinity;
31
32namespace thisThread {
33
34/**
35 * @name Apply CPU Affinity to Current Thread
36 *
37 * Applies specified policy to calling thread.
38 */
39/// @{
40/**
41 * Apply policy to calling thread.
42 *
43 * @param affinity The CPU affinity to apply.
44 *
45 * @relatesalso ::numapp::CpuAffinity
46 * @ingroup numapp_affinity
47 * @headerfile <> <numapp/cpuaffinity.hpp>
48 */
49[[nodiscard]] std::error_code Apply(CpuAffinity const& affinity) noexcept;
50/// @}
51
52} // namespace thisThread
53
54/**
55 * @name Apply CPU Affinity to Specific Thread
56 *
57 * Applies specified policy to calling thread.
58 */
59/// @{
60
61/**
62 * Apply policy to specified thread.
63 *
64 * @param thread Thread id.
65 * @param affinity The CPU affinity to apply.
66 *
67 * @relatesalso CpuAffinity
68 * @ingroup numapp_affinity
69 * @headerfile <> <numapp/cpuaffinity.hpp>
70 */
71[[nodiscard]] std::error_code Apply(pid_t thread, CpuAffinity const& affinity) noexcept;
72/// @}
73
74/**
75 * Create CPU affinity and apply to current thread
76 *
77 * For memory policy see numapp::MemPolicy.
78 *
79 * @manpages
80 * @manpage{numa_sched_setaffinity,3}
81 *
82 * @sa numapp::MemPolicy
83 *
84 * @ingroup numapp_affinity
85 * @headerfile <> <numapp/cpuaffinity.hpp>
86 */
88public:
89 /**
90 * Create affinity from Cpumask.
91 *
92 * @param mask The CPU-mask to use.
93 */
94 explicit CpuAffinity(Cpumask&& mask) noexcept;
95 explicit CpuAffinity(Cpumask const& mask) noexcept;
96 CpuAffinity(CpuAffinity&&) noexcept = default;
97 CpuAffinity& operator=(CpuAffinity&&) noexcept = default;
98 CpuAffinity(CpuAffinity const& rhs) = default;
99 CpuAffinity& operator=(CpuAffinity const& rhs) = default;
100
101 [[nodiscard]] bool operator==(CpuAffinity const& rhs) const noexcept {
102 return m_mask == rhs.m_mask;
103 }
104 [[nodiscard]] bool operator!=(CpuAffinity const& rhs) const noexcept {
105 return !(*this == rhs);
106 }
107
108 /**
109 * Create current affinity settings.
110 *
111 * @throws std::system_error on failure.
112 */
113 [[nodiscard]] static CpuAffinity MakeFromActive();
114
115 /**
116 * Create CpuAffinity from @a @c `cpustring` while considering allowed CPUs from current cpuset.
117 *
118 * Example patterns:
119 * - <tt>1-5,7,10</tt>
120 * - <tt>!4-5</tt>
121 * - <tt>+0-3</tt>
122 * - <tt>all</tt>
123 *
124 * c.f. numa_parse_cpustring for how the format is specified.
125 *
126 * @note numa_parse_cpustring
127 *
128 *
129 * @param cpustring CPUs to include such as <tt>1-5,7,10</tt> or <tt>all</tt>.
130 * @throws std::system_error on failure. Method will fail if cpustring references a CPU outside
131 * current cpuset. Use @ref MakeFromCpuStringAll when current cpuset should not be considered.
132 *
133 * @sa MakeFromCpuStringAll
134 */
135 [[nodiscard]] static CpuAffinity MakeFromCpuString(char const* cpustring);
136
137 /**
138 * Create CpuAffinity from @a @c `cpustring` @a without considering current cpuset.
139 *
140 * Example patterns:
141 * - <tt>1-5,7,10</tt>
142 * - <tt>!4-5</tt>
143 * - <tt>+0-3</tt>
144 * - <tt>all</tt>
145 *
146 * c.f. numa_parse_cpustring for how the format is specified.
147 *
148 * @note The @a cpustring is parsed using numa_parse_cpustring_all which does not consider
149 * current cpuset. This method must be used when referencing CPUs isolated with isolcpus.
150 *
151 * @param cpustring CPUs to include such as <tt>1-5,7,10</tt> or <tt>all</tt>.
152 * @throws std::system_error on failure.
153 *
154 * @sa MakeFromCpuString
155 */
156 static CpuAffinity MakeFromCpuStringAll(char const* cpustring);
157
158 /**
159 * Create CpuAffinity bound to the specified NUMA node.
160 *
161 * @throws std::system_error on failure.
162 */
163 [[nodiscard]] static CpuAffinity MakeBindNode(int node);
164
165 /**
166 * Create CpuAffinity bound to the specified CPU.
167 *
168 * @throws std::system_error on failure.
169 */
170 [[nodiscard]] static CpuAffinity MakeBindCpu(int cpu);
171
172 /**
173 * @return Underlying CPU mask.
174 */
175 Cpumask const& GetMask() const noexcept;
176
177private:
178 explicit CpuAffinity(struct bitmask* cpumask) {
179 }
180 Cpumask m_mask;
181};
182
183/**
184 * Formats @a affinity and inserts it to @a os.
185 *
186 * @param os output stream to insert into.
187 * @param affinity CPU affinity to format.
188 * @returns @a os
189 *
190 * @relates CpuAffinity
191 * @headerfile <> <numapp/cpuaffinity.hpp>
192 */
193std::ostream& operator<<(std::ostream& os, CpuAffinity const& affinity);
194
195} // namespace numapp
196#endif // #ifndef NUMAPP_CPUAFFINITY_HPP_
Create CPU affinity and apply to current thread.
Cpumask const & GetMask() const noexcept
static CpuAffinity MakeFromCpuStringAll(char const *cpustring)
Create CpuAffinity from `cpustring` without considering current cpuset.
static CpuAffinity MakeBindCpu(int cpu)
Create CpuAffinity bound to the specified CPU.
static CpuAffinity MakeBindNode(int node)
Create CpuAffinity bound to the specified NUMA node.
CpuAffinity(Cpumask &&mask) noexcept
Create affinity from Cpumask.
static CpuAffinity MakeFromActive()
Create current affinity settings.
static CpuAffinity MakeFromCpuString(char const *cpustring)
Create CpuAffinity from `cpustring` while considering allowed CPUs from current cpuset.
Type-safe CPU mask.
Definition cpumask.hpp:25
Contains declarations for CpumaskTrait.
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.