]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
618234a5 | 2 | |
11c3a366 TA |
3 | #include <errno.h> |
4 | #include <stddef.h> | |
a832893f | 5 | #include <stdio.h> |
11c3a366 TA |
6 | #include <syslog.h> |
7 | ||
b5efdb8a | 8 | #include "alloc-util.h" |
6bedfcbb | 9 | #include "cpu-set-util.h" |
84ac7bea | 10 | #include "extract-word.h" |
11c3a366 TA |
11 | #include "log.h" |
12 | #include "macro.h" | |
0985c7c4 | 13 | #include "memory-util.h" |
93cc7779 | 14 | #include "parse-util.h" |
b11d6a7b | 15 | #include "string-util.h" |
618234a5 | 16 | |
0985c7c4 | 17 | char* cpu_set_to_string(const CPUSet *a) { |
a832893f ZJS |
18 | _cleanup_free_ char *str = NULL; |
19 | size_t allocated = 0, len = 0; | |
20 | int i, r; | |
21 | ||
0985c7c4 ZJS |
22 | for (i = 0; (size_t) i < a->allocated * 8; i++) { |
23 | if (!CPU_ISSET_S(i, a->allocated, a->set)) | |
a832893f ZJS |
24 | continue; |
25 | ||
26 | if (!GREEDY_REALLOC(str, allocated, len + 1 + DECIMAL_STR_MAX(int))) | |
27 | return NULL; | |
28 | ||
29 | r = sprintf(str + len, len > 0 ? " %d" : "%d", i); | |
30 | assert_se(r > 0); | |
31 | len += r; | |
32 | } | |
33 | ||
34 | return TAKE_PTR(str) ?: strdup(""); | |
35 | } | |
36 | ||
167a776d | 37 | int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) { |
0985c7c4 ZJS |
38 | size_t need; |
39 | ||
40 | assert(cpu_set); | |
41 | ||
42 | need = CPU_ALLOC_SIZE(ncpus); | |
43 | if (need > cpu_set->allocated) { | |
44 | cpu_set_t *t; | |
45 | ||
46 | t = realloc(cpu_set->set, need); | |
47 | if (!t) | |
48 | return -ENOMEM; | |
49 | ||
50 | memzero((uint8_t*) t + cpu_set->allocated, need - cpu_set->allocated); | |
51 | ||
52 | cpu_set->set = t; | |
53 | cpu_set->allocated = need; | |
54 | } | |
55 | ||
56 | return 0; | |
57 | } | |
58 | ||
59 | static int cpu_set_add(CPUSet *cpu_set, unsigned cpu) { | |
60 | int r; | |
61 | ||
62 | if (cpu >= 8192) | |
63 | /* As of kernel 5.1, CONFIG_NR_CPUS can be set to 8192 on PowerPC */ | |
64 | return -ERANGE; | |
65 | ||
66 | r = cpu_set_realloc(cpu_set, cpu + 1); | |
67 | if (r < 0) | |
68 | return r; | |
69 | ||
70 | CPU_SET_S(cpu, cpu_set->allocated, cpu_set->set); | |
71 | return 0; | |
72 | } | |
73 | ||
74 | int cpu_set_add_all(CPUSet *a, const CPUSet *b) { | |
75 | int r; | |
76 | ||
77 | /* Do this backwards, so if we fail, we fail before changing anything. */ | |
78 | for (unsigned cpu_p1 = b->allocated * 8; cpu_p1 > 0; cpu_p1--) | |
79 | if (CPU_ISSET_S(cpu_p1 - 1, b->allocated, b->set)) { | |
80 | r = cpu_set_add(a, cpu_p1 - 1); | |
81 | if (r < 0) | |
82 | return r; | |
83 | } | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
88 | int parse_cpu_set_full( | |
618234a5 | 89 | const char *rvalue, |
0985c7c4 | 90 | CPUSet *cpu_set, |
6d8a29b2 | 91 | bool warn, |
618234a5 LP |
92 | const char *unit, |
93 | const char *filename, | |
94 | unsigned line, | |
95 | const char *lvalue) { | |
96 | ||
0985c7c4 | 97 | _cleanup_(cpu_set_reset) CPUSet c = {}; |
6d8a29b2 | 98 | const char *p = rvalue; |
618234a5 | 99 | |
0985c7c4 | 100 | assert(p); |
618234a5 LP |
101 | |
102 | for (;;) { | |
103 | _cleanup_free_ char *word = NULL; | |
0985c7c4 | 104 | unsigned cpu_lower, cpu_upper; |
618234a5 LP |
105 | int r; |
106 | ||
6d8a29b2 YW |
107 | r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_QUOTES); |
108 | if (r == -ENOMEM) | |
109 | return warn ? log_oom() : -ENOMEM; | |
a26662ce | 110 | if (r < 0) |
6d8a29b2 | 111 | return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r; |
618234a5 LP |
112 | if (r == 0) |
113 | break; | |
114 | ||
a26662ce FB |
115 | r = parse_range(word, &cpu_lower, &cpu_upper); |
116 | if (r < 0) | |
6d8a29b2 | 117 | return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r; |
032cf8e4 | 118 | |
6d8a29b2 YW |
119 | if (cpu_lower > cpu_upper) { |
120 | if (warn) | |
0985c7c4 ZJS |
121 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring.", |
122 | word, cpu_lower, cpu_upper); | |
123 | ||
124 | /* Make sure something is allocated, to distinguish this from the empty case */ | |
125 | r = cpu_set_realloc(&c, 1); | |
126 | if (r < 0) | |
127 | return r; | |
032cf8e4 YW |
128 | } |
129 | ||
0985c7c4 ZJS |
130 | for (unsigned cpu_p1 = MIN(cpu_upper, UINT_MAX-1) + 1; cpu_p1 > cpu_lower; cpu_p1--) { |
131 | r = cpu_set_add(&c, cpu_p1 - 1); | |
132 | if (r < 0) | |
133 | return warn ? log_syntax(unit, LOG_ERR, filename, line, r, | |
134 | "Cannot add CPU %u to set: %m", cpu_p1 - 1) : r; | |
135 | } | |
032cf8e4 YW |
136 | } |
137 | ||
0985c7c4 ZJS |
138 | /* On success, transfer ownership to the output variable */ |
139 | *cpu_set = c; | |
140 | c = (CPUSet) {}; | |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | int parse_cpu_set_extend( | |
146 | const char *rvalue, | |
147 | CPUSet *old, | |
148 | bool warn, | |
149 | const char *unit, | |
150 | const char *filename, | |
151 | unsigned line, | |
152 | const char *lvalue) { | |
153 | ||
154 | _cleanup_(cpu_set_reset) CPUSet cpuset = {}; | |
155 | int r; | |
156 | ||
157 | r = parse_cpu_set_full(rvalue, &cpuset, true, unit, filename, line, lvalue); | |
158 | if (r < 0) | |
159 | return r; | |
160 | ||
161 | if (!cpuset.set) { | |
162 | /* An empty assignment resets the CPU list */ | |
163 | cpu_set_reset(old); | |
164 | return 0; | |
165 | } | |
166 | ||
167 | if (!old->set) { | |
168 | *old = cpuset; | |
169 | cpuset = (CPUSet) {}; | |
170 | return 0; | |
171 | } | |
032cf8e4 | 172 | |
0985c7c4 | 173 | return cpu_set_add_all(old, &cpuset); |
032cf8e4 | 174 | } |
f44b3035 ZJS |
175 | |
176 | int cpus_in_affinity_mask(void) { | |
177 | size_t n = 16; | |
178 | int r; | |
179 | ||
180 | for (;;) { | |
181 | cpu_set_t *c; | |
182 | ||
183 | c = CPU_ALLOC(n); | |
184 | if (!c) | |
185 | return -ENOMEM; | |
186 | ||
187 | if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) { | |
188 | int k; | |
189 | ||
190 | k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c); | |
191 | CPU_FREE(c); | |
192 | ||
193 | if (k <= 0) | |
194 | return -EINVAL; | |
195 | ||
196 | return k; | |
197 | } | |
198 | ||
199 | r = -errno; | |
200 | CPU_FREE(c); | |
201 | ||
202 | if (r != -EINVAL) | |
203 | return r; | |
204 | if (n > SIZE_MAX/2) | |
205 | return -ENOMEM; | |
206 | n *= 2; | |
207 | } | |
208 | } |