-
Notifications
You must be signed in to change notification settings - Fork 5
/
cpuset.hpp
164 lines (135 loc) · 3.07 KB
/
cpuset.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#pragma once
#include <cerrno>
#include <cstddef>
#include <cstring>
#include <iomanip>
#include <system_error>
#include <unistd.h>
#include <sched.h>
#include <sys/syscall.h>
class cpuset
{
public:
cpuset()
{
clear();
}
cpuset(int cpu)
{
clear();
insert(cpu);
}
cpuset(const cpuset &other)
{
copy_cpuset(other);
}
cpuset &operator=(const cpuset &other)
{
copy_cpuset(other);
return *this;
}
void clear()
{
CPU_ZERO(&cpuset_);
}
void insert(int cpu)
{
CPU_SET(cpu, &cpuset_);
}
void remove(int cpu)
{
CPU_CLR(cpu, &cpuset_);
}
bool find(int cpu) const
{
return CPU_ISSET(cpu, &cpuset_);
}
std::size_t size() const
{
return CPU_COUNT(&cpuset_);
}
std::size_t max_cpus() const
{
return CPU_SETSIZE - 1;
}
friend void set_cpu_affinity(const cpuset &cpuset);
friend cpuset get_cpu_affinity();
cpuset &operator&=(const cpuset &rhs)
{
CPU_AND(&cpuset_, &rhs.cpuset_, &cpuset_);
return *this;
}
cpuset &operator|=(const cpuset &rhs)
{
CPU_OR(&cpuset_, &rhs.cpuset_, &cpuset_);
return *this;
}
cpuset &operator^=(const cpuset &rhs)
{
CPU_XOR(&cpuset_, &rhs.cpuset_, &cpuset_);
return *this;
}
friend bool operator==(const cpuset &lhs, const cpuset &rhs);
friend cpuset operator&(const cpuset &rhs, const cpuset &lhs);
friend cpuset operator|(const cpuset &rhs, const cpuset &lhs);
friend cpuset operator^(const cpuset &rhs, const cpuset &lhs);
private:
void copy_cpuset(const cpuset &other)
{
std::memcpy(&cpuset_, &other.cpuset_, sizeof(cpu_set_t));
}
cpu_set_t cpuset_;
};
bool operator==(const cpuset &lhs, const cpuset &rhs)
{
return CPU_EQUAL(&lhs.cpuset_, &rhs.cpuset_);
}
cpuset operator&(const cpuset &rhs, const cpuset &lhs)
{
cpuset ret;
CPU_AND(&ret.cpuset_, &rhs.cpuset_, &lhs.cpuset_);
return ret;
}
cpuset operator|(const cpuset &rhs, const cpuset &lhs)
{
cpuset ret;
CPU_OR(&ret.cpuset_, &rhs.cpuset_, &lhs.cpuset_);
return ret;
}
cpuset operator^(const cpuset &rhs, const cpuset &lhs)
{
cpuset ret;
CPU_XOR(&ret.cpuset_, &rhs.cpuset_, &lhs.cpuset_);
return ret;
}
std::ostream &operator<<(std::ostream &os, const cpuset &cpuset)
{
os << "[";
for (std::size_t i = 0; i < cpuset.max_cpus(); ++i) {
if (cpuset.find(i)) {
os << std::setw(4) << i;
}
}
os << "]";
return os;
}
int get_current_cpu()
{
return sched_getcpu();
}
void set_cpu_affinity(const cpuset &cpuset)
{
auto err = sched_setaffinity(0, sizeof(cpuset.cpuset_), &cpuset.cpuset_);
if (err < 0) {
throw std::system_error(errno, std::system_category());
}
}
cpuset get_cpu_affinity()
{
cpuset ret;
auto err = sched_getaffinity(0, sizeof(ret.cpuset_), &ret.cpuset_);
if (err < 0) {
throw std::system_error(errno, std::system_category());
}
return ret;
}