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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
#define _GNU_SOURCE
#include <sched.h>
#include <ctype.h>
#include <haproxy/compat.h>
#include <haproxy/cpuset.h>
#include <haproxy/intops.h>
#include <haproxy/tools.h>
struct cpu_map *cpu_map;
void ha_cpuset_zero(struct hap_cpuset *set)
{
#if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
CPU_ZERO(&set->cpuset);
#elif defined(CPUSET_USE_ULONG)
set->cpuset = 0;
#endif
}
int ha_cpuset_set(struct hap_cpuset *set, int cpu)
{
if (cpu >= ha_cpuset_size())
return 1;
#if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
CPU_SET(cpu, &set->cpuset);
return 0;
#elif defined(CPUSET_USE_ULONG)
set->cpuset |= (0x1 << cpu);
return 0;
#endif
}
int ha_cpuset_clr(struct hap_cpuset *set, int cpu)
{
if (cpu >= ha_cpuset_size())
return 1;
#if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
CPU_CLR(cpu, &set->cpuset);
return 0;
#elif defined(CPUSET_USE_ULONG)
set->cpuset &= ~(0x1 << cpu);
return 0;
#endif
}
void ha_cpuset_and(struct hap_cpuset *dst, struct hap_cpuset *src)
{
#if defined(CPUSET_USE_CPUSET)
CPU_AND(&dst->cpuset, &dst->cpuset, &src->cpuset);
#elif defined(CPUSET_USE_FREEBSD_CPUSET)
CPU_AND(&dst->cpuset, &src->cpuset);
#elif defined(CPUSET_USE_ULONG)
dst->cpuset &= src->cpuset;
#endif
}
void ha_cpuset_or(struct hap_cpuset *dst, struct hap_cpuset *src)
{
#if defined(CPUSET_USE_CPUSET)
CPU_OR(&dst->cpuset, &dst->cpuset, &src->cpuset);
#elif defined(CPUSET_USE_FREEBSD_CPUSET)
CPU_OR(&dst->cpuset, &src->cpuset);
#elif defined(CPUSET_USE_ULONG)
dst->cpuset |= src->cpuset;
#endif
}
int ha_cpuset_isset(const struct hap_cpuset *set, int cpu)
{
if (cpu >= ha_cpuset_size())
return 0;
#if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
return CPU_ISSET(cpu, &set->cpuset);
#elif defined(CPUSET_USE_ULONG)
return !!(set->cpuset & (0x1 << cpu));
#else
return 0;
#endif
}
int ha_cpuset_count(const struct hap_cpuset *set)
{
#if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
return CPU_COUNT(&set->cpuset);
#elif defined(CPUSET_USE_ULONG)
return my_popcountl(set->cpuset);
#endif
}
int ha_cpuset_ffs(const struct hap_cpuset *set)
{
#if defined(CPUSET_USE_CPUSET)
int n;
if (!CPU_COUNT(&set->cpuset))
return 0;
for (n = 0; !CPU_ISSET(n, &set->cpuset); ++n)
;
return n + 1;
#elif defined(CPUSET_USE_FREEBSD_CPUSET)
return CPU_FFS(&set->cpuset);
#elif defined(CPUSET_USE_ULONG)
if (!set->cpuset)
return 0;
return my_ffsl(set->cpuset);
#endif
}
void ha_cpuset_assign(struct hap_cpuset *dst, struct hap_cpuset *src)
{
#if defined(CPUSET_USE_CPUSET)
CPU_ZERO(&dst->cpuset);
CPU_OR(&dst->cpuset, &dst->cpuset, &src->cpuset);
#elif defined(CPUSET_USE_FREEBSD_CPUSET)
CPU_COPY(&src->cpuset, &dst->cpuset);
#elif defined(CPUSET_USE_ULONG)
dst->cpuset = src->cpuset;
#endif
}
int ha_cpuset_size()
{
#if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
return CPU_SETSIZE;
#elif defined(CPUSET_USE_ULONG)
return LONGBITS;
#endif
}
/* Detects CPUs that are bound to the current process. Returns the number of
* CPUs detected or 0 if the detection failed.
*/
int ha_cpuset_detect_bound(struct hap_cpuset *set)
{
ha_cpuset_zero(set);
/* detect bound CPUs depending on the OS's API */
if (0
#if defined(__linux__)
|| sched_getaffinity(0, sizeof(set->cpuset), &set->cpuset) != 0
#elif defined(__FreeBSD__)
|| cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, sizeof(set->cpuset), &set->cpuset) != 0
#else
|| 1 // unhandled platform
#endif
) {
/* detection failed */
return 0;
}
return ha_cpuset_count(set);
}
/* Parse cpu sets. Each CPU set is either a unique number between 0 and
* ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash
* ('-'). Each CPU set can be a list of unique numbers or ranges separated by
* a comma. It is also possible to specify multiple cpu numbers or ranges in
* distinct argument in <args>. On success, it returns 0, otherwise it returns
* 1, optionally with an error message in <err> if <err> is not NULL.
*/
int parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err)
{
int cur_arg = 0;
const char *arg;
ha_cpuset_zero(cpu_set);
arg = args[cur_arg];
while (*arg) {
const char *dash, *comma;
unsigned int low, high;
if (!isdigit((unsigned char)*args[cur_arg])) {
memprintf(err, "'%s' is not a CPU range.", arg);
return 1;
}
low = high = str2uic(arg);
comma = strchr(arg, ',');
dash = strchr(arg, '-');
if (dash && (!comma || dash < comma))
high = *(dash+1) ? str2uic(dash + 1) : ha_cpuset_size() - 1;
if (high < low) {
unsigned int swap = low;
low = high;
high = swap;
}
if (high >= ha_cpuset_size()) {
memprintf(err, "supports CPU numbers from 0 to %d.",
ha_cpuset_size() - 1);
return 1;
}
while (low <= high)
ha_cpuset_set(cpu_set, low++);
/* if a comma is present, parse the rest of the arg, else
* skip to the next arg */
arg = comma ? comma + 1 : args[++cur_arg];
}
return 0;
}
/* Parse a linux cpu map string representing to a numeric cpu mask map
* The cpu map string is a list of 4-byte hex strings separated by commas, with
* most-significant byte first, one bit per cpu number.
*/
void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set)
{
unsigned long cpumap;
char *start, *endptr, *comma;
int i, j;
ha_cpuset_zero(cpu_set);
i = 0;
do {
/* reverse-search for a comma, parse the string after the comma
* or at the beginning if no comma found
*/
comma = strrchr(cpumap_str, ',');
start = comma ? comma + 1 : cpumap_str;
cpumap = strtoul(start, &endptr, 16);
for (j = 0; cpumap; cpumap >>= 1, ++j) {
if (cpumap & 0x1)
ha_cpuset_set(cpu_set, j + i * 32);
}
if (comma)
*comma = '\0';
++i;
} while (comma);
}
/* Returns true if at least one cpu-map directive was configured, otherwise
* false.
*/
int cpu_map_configured(void)
{
int grp, thr;
for (grp = 0; grp < MAX_TGROUPS; grp++) {
for (thr = 0; thr < MAX_THREADS_PER_GROUP; thr++)
if (ha_cpuset_count(&cpu_map[grp].thread[thr]))
return 1;
}
return 0;
}
/* Allocates everything needed to store CPU information at boot.
* Returns non-zero on success, zero on failure.
*/
static int cpuset_alloc(void)
{
/* allocate the structures used to store CPU topology info */
cpu_map = calloc(MAX_TGROUPS, sizeof(*cpu_map));
if (!cpu_map)
return 0;
return 1;
}
static void cpuset_deinit(void)
{
ha_free(&cpu_map);
}
INITCALL0(STG_ALLOC, cpuset_alloc);
REGISTER_POST_DEINIT(cpuset_deinit);
|