JayBeams  0.1
Another project to have fun coding.
cpu_set.hpp
Go to the documentation of this file.
1 #ifndef jb_cpu_set_hpp
2 #define jb_cpu_set_hpp
3 
4 #include <iosfwd>
5 #include <sched.h>
6 #include <string>
7 
8 namespace jb {
9 
10 /**
11  * A wrapper for the Linux CPU_SET data structure.
12  *
13  * Setting CPU affinity can help improve the predictability of our
14  * system. Typically different threads are assigned to different
15  * processors and in extreme cases no threads share the same core.
16  * The cpu sets are also typically configured using the "List Format"
17  * as described in cpuset(7). Briefly this format is:
18  *
19  * cpuset ::= range[,cpuset]
20  * range ::= (number|number-number)
21  * number ::= a positive integer
22  *
23  * So one can specify a cpu_set as "1,2,3" (cpus 1, 2 and 3), or
24  * "1-15" (cpus from 1 to 15), or "0-4,10,12-14" (cpus 0 to 4, 10 and
25  * 12 to 14).
26  *
27  * The iostream operators (used for tests, and conversions everywhere)
28  * use this format.
29  *
30  * TODO(#151) this is a portability problem. Many platforms can
31  * specify cpu affinity, but they all use different constructs for
32  * it.
33  * TODO(#152) we only support up to CPU_SETSIZE cpus, it is possible
34  * to control more CPUs in Linux, but the application that has access
35  * to more than 1024 cpus is rare indeed.
36  */
37 class cpu_set {
38 public:
40  : set_() {
41  CPU_ZERO(&set_);
42  }
43 
44  /// Return the number of CPUs that can be stored in the cpu set.
45  std::size_t capacity() const {
46  return CPU_SETSIZE;
47  }
48 
49  /// Returns true if @a cpu is included in the cpu set.
50  bool status(int cpu) const {
51  return CPU_ISSET(cpu, &set_);
52  }
53 
54  /// Return the number of cpus included in the cpu set.
55  int count() const {
56  return CPU_COUNT(&set_);
57  }
58 
59  /// Remove all cpus from the cpu set.
61  CPU_ZERO(&set_);
62  return *this;
63  }
64 
65  /// Add @a cpu to the cpu set.
66  cpu_set& set(int cpu) {
67  check_range(cpu, "set");
68  CPU_SET(cpu, &set_);
69  return *this;
70  }
71 
72  /// Remove @a cpu from the cpu set.
73  cpu_set& clear(int cpu) {
74  check_range(cpu, "clear");
75  CPU_CLR(cpu, &set_);
76  return *this;
77  }
78 
79  /// Add all the cpus in the [cpulo,cpuhi] range to the set.
80  cpu_set& set(int cpulo, int cpuhi);
81 
82  /// Remove all the cpus in the [cpulo,cpuhi] range from the set.
83  cpu_set& clear(int cpulo, int cpuhi);
84 
85  //@{
86  /**
87  * @name Implement bitwise operations for cpu sets.
88  */
89  void operator&=(cpu_set const& rhs) {
90  (void)CPU_AND(&set_, &set_, &rhs.set_);
91  }
92  void operator|=(cpu_set const& rhs) {
93  (void)CPU_OR(&set_, &set_, &rhs.set_);
94  }
95  void operator^=(cpu_set const& rhs) {
96  (void)CPU_XOR(&set_, &set_, &rhs.set_);
97  }
98  //@}
99 
100  //@{
101  /**
102  * @name Comparison operators for cpu sets.
103  */
104  bool operator==(cpu_set const& rhs) const {
105  return CPU_EQUAL(&set_, &rhs.set_);
106  }
107 
108  bool operator!=(cpu_set const& rhs) const {
109  return not(*this == rhs);
110  }
111  //@}
112 
113  /**
114  * Interpret @a value as a cpu set in list format.
115  */
116  static cpu_set parse(std::string const& value);
117 
118  /**
119  * Return the set in the list format representation.
120  */
121  std::string as_list_format() const;
122 
123  //@{
124  /**
125  * @name Access the native implementation.
126  */
127  cpu_set_t* native_handle() {
128  return &set_;
129  }
130  cpu_set_t const* native_handle() const {
131  return &set_;
132  }
133  //@}
134 
135 private:
136  /**
137  * Check that @a cpu is in range
138  *
139  * @throw std::out_of_range if the argument is out of range.
140  */
141  void check_range(int cpu, char const* op) const;
142 
143  /**
144  * Check that @a [cpulo,cpuhi] is in range
145  *
146  * @throw std::out_of_range if the arguments are out of range.
147  */
148  void check_range(int cpulo, int cpuhi, char const* op) const;
149 
150  /**
151  * Raise an exception because the input to parse() is invalid.
152  */
153  static void parse_error(std::string const& value);
154 
155 private:
156  cpu_set_t set_;
157 };
158 
159 /// Stream a cpu set in list format.
160 std::ostream& operator<<(std::ostream&, cpu_set const&);
161 
162 /// Read a cpu set in list format.
163 std::istream& operator>>(std::istream&, cpu_set&);
164 
165 /// Bitwise AND operator for cpu sets
166 inline cpu_set operator&(cpu_set const& lhs, cpu_set const& rhs) {
167  cpu_set r = lhs;
168  r &= rhs;
169  return r;
170 }
171 
172 /// Bitwise OR operator for cpu sets
173 inline cpu_set operator|(cpu_set const& lhs, cpu_set const& rhs) {
174  cpu_set r = lhs;
175  r |= rhs;
176  return r;
177 }
178 
179 /// Bitwise XOR operator for cpu sets
180 inline cpu_set operator^(cpu_set const& lhs, cpu_set const& rhs) {
181  cpu_set r = lhs;
182  r ^= rhs;
183  return r;
184 }
185 
186 //@}
187 
188 } // namespace jb
189 
190 #endif // jb_cpu_set_hpp
void operator &=(cpu_set const &rhs)
Definition: cpu_set.hpp:89
cpu_set operator &(cpu_set const &lhs, cpu_set const &rhs)
Bitwise AND operator for cpu sets.
Definition: cpu_set.hpp:166
std::ostream & operator<<(std::ostream &os, as_hhmmssu const &x)
Format as_hhmmssu into an iostream.
Definition: as_hhmmss.cpp:8
std::size_t capacity() const
Return the number of CPUs that can be stored in the cpu set.
Definition: cpu_set.hpp:45
bool operator==(cpu_set const &rhs) const
Definition: cpu_set.hpp:104
cpu_set_t const * native_handle() const
Definition: cpu_set.hpp:130
bool status(int cpu) const
Returns true if cpu is included in the cpu set.
Definition: cpu_set.hpp:50
cpu_set_t set_
Definition: cpu_set.hpp:156
cpu_set & clear(int cpu)
Remove cpu from the cpu set.
Definition: cpu_set.hpp:73
std::string as_list_format() const
Return the set in the list format representation.
Definition: cpu_set.cpp:55
cpu_set operator|(cpu_set const &lhs, cpu_set const &rhs)
Bitwise OR operator for cpu sets.
Definition: cpu_set.hpp:173
void operator^=(cpu_set const &rhs)
Definition: cpu_set.hpp:95
static cpu_set parse(std::string const &value)
Interpret value as a cpu set in list format.
Definition: cpu_set.cpp:24
std::istream & operator>>(std::istream &, cpu_set &)
Read a cpu set in list format.
Definition: cpu_set.cpp:104
void check_range(int cpu, char const *op) const
Check that cpu is in range.
Definition: cpu_set.cpp:75
cpu_set & reset()
Remove all cpus from the cpu set.
Definition: cpu_set.hpp:60
cpu_set_t * native_handle()
Definition: cpu_set.hpp:127
cpu_set operator^(cpu_set const &lhs, cpu_set const &rhs)
Bitwise XOR operator for cpu sets.
Definition: cpu_set.hpp:180
static void parse_error(std::string const &value)
Raise an exception because the input to parse() is invalid.
Definition: cpu_set.cpp:94
A wrapper for the Linux CPU_SET data structure.
Definition: cpu_set.hpp:37
void operator|=(cpu_set const &rhs)
Definition: cpu_set.hpp:92
int count() const
Return the number of cpus included in the cpu set.
Definition: cpu_set.hpp:55
bool operator!=(cpu_set const &rhs) const
Definition: cpu_set.hpp:108
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7