JayBeams  0.1
Another project to have fun coding.
cpu_set.cpp
Go to the documentation of this file.
1 #include "jb/cpu_set.hpp"
2 #include <jb/strtonum.hpp>
3 
4 #include <iostream>
5 #include <sstream>
6 #include <stdexcept>
7 
8 jb::cpu_set& jb::cpu_set::set(int cpulo, int cpuhi) {
9  check_range(cpulo, cpuhi, "set");
10  for (int i = cpulo; i != cpuhi + 1; ++i) {
11  CPU_SET(i, &set_);
12  }
13  return *this;
14 }
15 
16 jb::cpu_set& jb::cpu_set::clear(int cpulo, int cpuhi) {
17  check_range(cpulo, cpuhi, "clear");
18  for (int i = cpulo; i != cpuhi + 1; ++i) {
19  CPU_CLR(i, &set_);
20  }
21  return *this;
22 }
23 
24 jb::cpu_set jb::cpu_set::parse(std::string const& value) {
25  cpu_set tmp;
26 
27  std::string element;
28  for (std::istringstream is(value);
29  is.good() and std::getline(is, element, ',');) {
30  std::istringstream el(element);
31  std::string lo, hi;
32  std::getline(el, lo, '-');
33  if (el.eof()) {
34  int cpu;
35  if (not jb::strtonum(lo, cpu)) {
36  parse_error(value);
37  }
38  tmp.set(cpu);
39  continue;
40  }
41  if (not std::getline(el, hi, '-') or not el.eof()) {
42  parse_error(value);
43  }
44  int cpulo, cpuhi;
45  if (jb::strtonum(lo, cpulo) and jb::strtonum(hi, cpuhi)) {
46  tmp.set(cpulo, cpuhi);
47  } else {
48  parse_error(value);
49  }
50  }
51 
52  return tmp;
53 }
54 
55 std::string jb::cpu_set::as_list_format() const {
56  std::ostringstream os;
57  char const* sep = "";
58  for (int i = 0; std::size_t(i) < capacity(); ++i) {
59  if (not status(i)) {
60  continue;
61  }
62  os << sep << i;
63  int end = i;
64  for (; std::size_t(end) < capacity() and status(end); ++end) {
65  }
66  if (end != i + 1 or std::size_t(end) == capacity()) {
67  i = end;
68  os << "-" << end - 1;
69  }
70  sep = ",";
71  }
72  return os.str();
73 }
74 
75 void jb::cpu_set::check_range(int cpu, char const* op) const {
76  if (cpu >= 0 and std::size_t(cpu) < capacity()) {
77  return;
78  }
79  std::ostringstream os;
80  os << "cpu_set::" << op << "(" << cpu << ") - argument out of range "
81  << "[0," << capacity() - 1 << "]";
82  throw std::out_of_range(os.str());
83 }
84 
85 void jb::cpu_set::check_range(int cpulo, int cpuhi, char const* op) const {
86  if (cpulo < 0 or std::size_t(cpuhi) >= capacity()) {
87  std::ostringstream os;
88  os << "cpu_set::" << op << "(" << cpulo << "," << cpuhi << ")"
89  << "- argument out of expected range [0," << capacity() - 1 << "]";
90  throw std::out_of_range(os.str());
91  }
92 }
93 
94 void jb::cpu_set::parse_error(std::string const& value) {
95  std::stringstream os;
96  os << "cpu_set::parse() - invalid argument (" << value << ")";
97  throw std::invalid_argument(os.str());
98 }
99 
100 std::ostream& jb::operator<<(std::ostream& os, jb::cpu_set const& rhs) {
101  return os << rhs.as_list_format();
102 }
103 
104 std::istream& jb::operator>>(std::istream& is, jb::cpu_set& rhs) {
105  std::string s;
106  is >> s;
108  rhs = std::move(tmp);
109  return is;
110 }
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 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
bool strtonum(std::string const &s, T &r)
Generic string to number conversion with validation.
Definition: strtonum.hpp:40
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 & set(int cpu)
Add cpu to the cpu set.
Definition: cpu_set.hpp:66
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