-
Notifications
You must be signed in to change notification settings - Fork 969
Expand file tree
/
Copy pathcpuset.c
More file actions
314 lines (257 loc) · 7.03 KB
/
Copy pathcpuset.c
File metadata and controls
314 lines (257 loc) · 7.03 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#define _GNU_SOURCE
#include <haproxy/compat.h>
#include <haproxy/cpuset.h>
#include <haproxy/intops.h>
#include <haproxy/tools.h>
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 < 0 || 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;
#else
return 1;
#endif
}
int ha_cpuset_clr(struct hap_cpuset *set, int cpu)
{
if (cpu < 0 || 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;
#else
return 1;
#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 < 0 || cpu >= ha_cpuset_size())
return 0;
#if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
/* Turn to boolean because musl directly returns the mask as a
* a long instead of an int, hence loses bits 32+.
*/
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);
#else
return 0;
#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);
#else
return 0;
#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
}
/* returns true if the sets are equal */
int ha_cpuset_isequal(const struct hap_cpuset *dst, const struct hap_cpuset *src)
{
#if defined(CPUSET_USE_CPUSET)
return CPU_EQUAL(&dst->cpuset, &src->cpuset);
#elif defined(CPUSET_USE_FREEBSD_CPUSET)
return !CPU_CMP(&src->cpuset, &dst->cpuset);
#elif defined(CPUSET_USE_ULONG)
return dst->cpuset == src->cpuset;
#else
return 0;
#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;
#else
return 0;
#endif
}
/* 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;
}
/* Print a cpu-set as compactly as possible and returns the output length.
* Returns >size if it cannot emit anything due to length constraints, in which
* case it will match what is at least needed to go further, and may return 0
* for an empty set. It will emit series of comma-delimited ranges in the form
* "beg[-end]".
*/
int print_cpu_set(char *output, size_t size, const struct hap_cpuset *cpu_set)
{
struct hap_cpuset set = *cpu_set;
int cpus = ha_cpuset_size();
int first = -1;
int len = 0;
int cpu;
for (cpu = 0; cpu < cpus; cpu++) {
if (!ha_cpuset_isset(&set, cpu))
continue;
ha_cpuset_clr(&set, cpu);
/* check if first of a series*/
if (first < 0) {
first = cpu;
len += snprintf(output + len, size - len, "%d", cpu);
if (len >= size)
return len + 1;
/* check if belongs to a range */
if (cpu < cpus - 1 && ha_cpuset_isset(&set, cpu + 1)) {
if (len + 1 >= size)
return len + 2;
output[len++] = '-';
output[len] = 0;
} else
first = -1;
}
else if (cpu >= cpus - 1 || !ha_cpuset_isset(&set, cpu + 1)) {
/* end of a series and not first */
len += snprintf(output + len, size - len, "%d", cpu);
if (len >= size)
return len + 1;
first = -1;
}
if (first < 0 && ha_cpuset_count(&set) > 0) {
if (len + 1 >= size)
return len + 2;
output[len++] = ',';
output[len] = 0;
}
}
return len;
}
/* 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);
}