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