Debian Codegraph @

src/test/test-cpu-set-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "cpu-set-util.h"
5 #include "tests.h"
6
7 #define ASSERT_CPUSET_EMPTY(c) \
8 ASSERT_NULL(c.set); \
9 ASSERT_EQ(c.allocated, 0u)
10
11 #define ASSERT_CPUSET_COUNT(c, n) \
12 ASSERT_NOT_NULL(c.set); \
13 ASSERT_GE(c.allocated, CPU_ALLOC_SIZE(n)); \
14 ASSERT_EQ(CPU_COUNT_S(c.allocated, c.set), (n)); \
15 ASSERT_EQ(cpu_set_count(&c), (size_t) (n))
16
17 #define ASSERT_CPUSET_ISSET(c, i) \
18 ASSERT_TRUE(CPU_ISSET_S(i, c.allocated, c.set));
19
20 #define ASSERT_CPUSET_STRING(c, str, range, mask) \
21 { \
22 _cleanup_free_ char *s = NULL; \
23 ASSERT_NOT_NULL(s = cpu_set_to_string(&c)); \
24 log_info("cpu_set_to_string: %s", s); \
25 ASSERT_STREQ(s, str); \
26 s = mfree(s); \
27 ASSERT_NOT_NULL(s = cpu_set_to_range_string(&c)); \
28 log_info("cpu_set_to_range_string: %s", s); \
29 ASSERT_STREQ(s, range); \
30 s = mfree(s); \
31 ASSERT_NOT_NULL(s = cpu_set_to_mask_string(&c)); \
32 log_info("cpu_set_to_mask_string: %s", s); \
33 ASSERT_STREQ(s, mask); \
34 }
35
36 TEST(parse_cpu_set) {
37 CPUSet c = {};
38
39 /* empty */
40 ASSERT_CPUSET_EMPTY(c);
41 ASSERT_CPUSET_STRING(c, "", "", "0");
42 cpu_set_done(&c);
43
44 /* Single value */
45 ASSERT_OK(parse_cpu_set("0", &c));
46 ASSERT_CPUSET_COUNT(c, 1);
47 ASSERT_CPUSET_ISSET(c, 0);
48 ASSERT_CPUSET_STRING(c, "0", "0", "1");
49 cpu_set_done(&c);
50
51 /* Simple range (from CPUAffinity example) */
52 ASSERT_OK(parse_cpu_set("1 2 4", &c));
53 ASSERT_CPUSET_COUNT(c, 3);
54 ASSERT_CPUSET_ISSET(c, 1);
55 ASSERT_CPUSET_ISSET(c, 2);
56 ASSERT_CPUSET_ISSET(c, 4);
57 ASSERT_CPUSET_STRING(c, "1 2 4", "1-2 4", "16");
58 cpu_set_done(&c);
59
60 /* A more interesting range */
61 ASSERT_OK(parse_cpu_set("0 1 2 3 8 9 10 11", &c));
62 ASSERT_CPUSET_COUNT(c, 8);
63 for (unsigned i = 0; i < 4; i++)
64 ASSERT_CPUSET_ISSET(c, i);
65 for (unsigned i = 8; i < 12; i++)
66 ASSERT_CPUSET_ISSET(c, i);
67 ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
68 cpu_set_done(&c);
69
70 /* Quoted strings */
71 ASSERT_OK(parse_cpu_set("8 '9' 10 \"11\"", &c));
72 ASSERT_CPUSET_COUNT(c, 4);
73 for (unsigned i = 8; i < 12; i++)
74 ASSERT_CPUSET_ISSET(c, i);
75 ASSERT_CPUSET_STRING(c, "8 9 10 11", "8-11", "f00");
76 cpu_set_done(&c);
77
78 /* Use commas as separators */
79 ASSERT_OK(parse_cpu_set("0,1,2,3 8,9,10,11", &c));
80 ASSERT_CPUSET_COUNT(c, 8);
81 for (unsigned i = 0; i < 4; i++)
82 ASSERT_CPUSET_ISSET(c, i);
83 for (unsigned i = 8; i < 12; i++)
84 ASSERT_CPUSET_ISSET(c, i);
85 ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
86 cpu_set_done(&c);
87
88 /* Commas with spaces (and trailing comma, space) */
89 ASSERT_OK(parse_cpu_set("0, 1, 2, 3, 4, 5, 6, 7, 63, ", &c));
90 ASSERT_CPUSET_COUNT(c, 9);
91 for (unsigned i = 0; i < 8; i++)
92 ASSERT_CPUSET_ISSET(c, i);
93 ASSERT_CPUSET_ISSET(c, 63);
94 ASSERT_CPUSET_STRING(c, "0 1 2 3 4 5 6 7 63", "0-7 63", "80000000,000000ff");
95 cpu_set_done(&c);
96
97 /* Ranges */
98 ASSERT_OK(parse_cpu_set("0-3,8-11", &c));
99 ASSERT_CPUSET_COUNT(c, 8);
100 for (unsigned i = 0; i < 4; i++)
101 ASSERT_CPUSET_ISSET(c, i);
102 for (unsigned i = 8; i < 12; i++)
103 ASSERT_CPUSET_ISSET(c, i);
104 ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
105 cpu_set_done(&c);
106
107 ASSERT_OK(parse_cpu_set("36-39,44-47", &c));
108 ASSERT_CPUSET_COUNT(c, 8);
109 for (unsigned i = 36; i < 40; i++)
110 ASSERT_CPUSET_ISSET(c, i);
111 for (unsigned i = 44; i < 48; i++)
112 ASSERT_CPUSET_ISSET(c, i);
113 ASSERT_CPUSET_STRING(c, "36 37 38 39 44 45 46 47", "36-39 44-47", "f0f0,00000000");
114 cpu_set_done(&c);
115
116 ASSERT_OK(parse_cpu_set("64-71", &c));
117 ASSERT_CPUSET_COUNT(c, 8);
118 for (unsigned i = 64; i < 72; i++)
119 ASSERT_CPUSET_ISSET(c, i);
120 ASSERT_CPUSET_STRING(c, "64 65 66 67 68 69 70 71", "64-71", "ff,00000000,00000000");
121 cpu_set_done(&c);
122
123 /* Ranges with trailing comma, space */
124 ASSERT_OK(parse_cpu_set("0-3 8-11, ", &c));
125 ASSERT_CPUSET_COUNT(c, 8);
126 for (unsigned i = 0; i < 4; i++)
127 ASSERT_CPUSET_ISSET(c, i);
128 for (unsigned i = 8; i < 12; i++)
129 ASSERT_CPUSET_ISSET(c, i);
130 ASSERT_CPUSET_STRING(c, "0 1 2 3 8 9 10 11", "0-3 8-11", "f0f");
131 cpu_set_done(&c);
132
133 /* Overlapping ranges */
134 ASSERT_OK(parse_cpu_set("0-7 4-11", &c));
135 ASSERT_CPUSET_COUNT(c, 12);
136 for (unsigned i = 0; i < 12; i++)
137 ASSERT_CPUSET_ISSET(c, i);
138 ASSERT_CPUSET_STRING(c, "0 1 2 3 4 5 6 7 8 9 10 11", "0-11", "fff");
139 cpu_set_done(&c);
140
141 /* Mix ranges and individual CPUs */
142 ASSERT_OK(parse_cpu_set("0,2 4-11", &c));
143 ASSERT_CPUSET_COUNT(c, 10);
144 ASSERT_CPUSET_ISSET(c, 0);
145 ASSERT_CPUSET_ISSET(c, 2);
146 for (unsigned i = 4; i < 12; i++)
147 ASSERT_CPUSET_ISSET(c, i);
148 ASSERT_CPUSET_STRING(c, "0 2 4 5 6 7 8 9 10 11", "0 2 4-11", "ff5");
149 cpu_set_done(&c);
150
151 /* Negative range */
152 ASSERT_ERROR(parse_cpu_set("3-0", &c), EINVAL);
153 ASSERT_CPUSET_EMPTY(c);
154
155 /* Garbage */
156 ASSERT_ERROR(parse_cpu_set("0 1 2 3 garbage", &c), EINVAL);
157 ASSERT_CPUSET_EMPTY(c);
158
159 /* Range with garbage */
160 ASSERT_ERROR(parse_cpu_set("0-3 8-garbage", &c), EINVAL);
161 ASSERT_CPUSET_EMPTY(c);
162
163 /* Empty string */
164 ASSERT_OK(parse_cpu_set("", &c));
165 ASSERT_CPUSET_EMPTY(c); /* empty string returns NULL */
166
167 /* Runaway quoted string */
168 ASSERT_ERROR(parse_cpu_set("0 1 2 3 \"4 5 6 7 ", &c), EINVAL);
169 ASSERT_CPUSET_EMPTY(c);
170
171 /* Maximum allocation */
172 ASSERT_OK(parse_cpu_set("8000-8191", &c));
173 ASSERT_CPUSET_COUNT(c, 192);
174
175 _cleanup_free_ char *expected_str = NULL;
176 for (size_t i = 8000; i < 8192; i++)
177 ASSERT_OK(strextendf_with_separator(&expected_str, " ", "%zu", i));
178
179 _cleanup_free_ char *expected_mask = NULL;
180 for (size_t i = 0; i < 8192 / 32; i++)
181 ASSERT_NOT_NULL(strextend_with_separator(&expected_mask, ",", i < 6 ? "ffffffff" : "00000000"));
182
183 ASSERT_CPUSET_STRING(c, expected_str, "8000-8191", expected_mask);
184 cpu_set_done(&c);
185 }
186
187 #define parse(str, c) \
188 config_parse_cpu_set( \
189 "unit", \
190 "filename", \
191 /* line= */ 0, \
192 "[Section]", \
193 /* section_line= */ 0, \
194 "CPUAffinity", \
195 /* ltype= */ 0, \
196 str, \
197 c, \
198 /* userdata= */ NULL)
199
200 TEST(config_parse_cpu_set) {
201 CPUSet c = {};
202
203 ASSERT_OK_POSITIVE(parse("1 3", &c));
204 ASSERT_CPUSET_COUNT(c, 2);
205 ASSERT_CPUSET_STRING(c, "1 3", "1 3", "a");
206
207 ASSERT_OK_POSITIVE(parse("4", &c));
208 ASSERT_CPUSET_COUNT(c, 3);
209 ASSERT_CPUSET_STRING(c, "1 3 4", "1 3-4", "1a");
210
211 ASSERT_OK_POSITIVE(parse("", &c));
212 ASSERT_CPUSET_EMPTY(c);
213 }
214
215 TEST(cpu_set_to_from_dbus) {
216 _cleanup_(cpu_set_done) CPUSet c = {}, c2 = {};
217
218 ASSERT_OK(parse_cpu_set("1 3 8 100-200", &c));
219 ASSERT_CPUSET_COUNT(c, 104);
220
221 _cleanup_free_ char *expected_str = strdup("1 3 8");
222 ASSERT_NOT_NULL(expected_str);
223 for (size_t i = 100; i <= 200; i++)
224 ASSERT_OK(strextendf_with_separator(&expected_str, " ", "%zu", i));
225
226 ASSERT_CPUSET_STRING(c, expected_str, "1 3 8 100-200", "1ff,ffffffff,ffffffff,fffffff0,00000000,00000000,0000010a");
227
228 _cleanup_free_ uint8_t *array = NULL;
229 size_t allocated;
230 static const char expected[32] =
231 "\x0A\x01\x00\x00\x00\x00\x00\x00"
232 "\x00\x00\x00\x00\xF0\xFF\xFF\xFF"
233 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
234 "\xFF\x01";
235
236 ASSERT_OK(cpu_set_to_dbus(&c, &array, &allocated));
237 ASSERT_NOT_NULL(array);
238 ASSERT_EQ(allocated, c.allocated);
239
240 ASSERT_LE(allocated, sizeof expected);
241 ASSERT_GE(allocated, DIV_ROUND_UP(201u, 8u)); /* We need at least 201 bits for our mask */
242 ASSERT_EQ(memcmp(array, expected, allocated), 0);
243
244 ASSERT_OK(cpu_set_from_dbus(array, allocated, &c2));
245 ASSERT_CPUSET_COUNT(c2, 104);
246 ASSERT_EQ(memcmp_nn(c.set, c.allocated, c2.set, c2.allocated), 0);
247 }
248
249 TEST(cpus_in_affinity_mask) {
250 int r;
251
252 ASSERT_OK_POSITIVE(r = cpus_in_affinity_mask());
253 log_info("cpus_in_affinity_mask: %d", r);
254 }
255
256 TEST(print_cpu_alloc_size) {
257 log_info("CPU_ALLOC_SIZE(1) = %zu", CPU_ALLOC_SIZE(1));
258 log_info("CPU_ALLOC_SIZE(9) = %zu", CPU_ALLOC_SIZE(9));
259 log_info("CPU_ALLOC_SIZE(64) = %zu", CPU_ALLOC_SIZE(64));
260 log_info("CPU_ALLOC_SIZE(65) = %zu", CPU_ALLOC_SIZE(65));
261 log_info("CPU_ALLOC_SIZE(1024) = %zu", CPU_ALLOC_SIZE(1024));
262 log_info("CPU_ALLOC_SIZE(1025) = %zu", CPU_ALLOC_SIZE(1025));
263 log_info("CPU_ALLOC_SIZE(8191) = %zu", CPU_ALLOC_SIZE(8191));
264 }
265
266 TEST(cpu_set_add) {
267 _cleanup_(cpu_set_done) CPUSet c = {};
268
269 for (size_t i = 0; i < 8192; i++)
270 ASSERT_OK(cpu_set_add(&c, 8191));
271
272 ASSERT_ERROR(cpu_set_add(&c, 8192), ERANGE);
273 ASSERT_ERROR(cpu_set_add(&c, SIZE_MAX), ERANGE);
274 }
275
276 TEST(cpu_set_add_range) {
277 _cleanup_(cpu_set_done) CPUSet c = {};
278
279 ASSERT_ERROR(cpu_set_add_range(&c, 0, 8192), ERANGE);
280 ASSERT_ERROR(cpu_set_add_range(&c, 0, SIZE_MAX), ERANGE);
281 ASSERT_SIGNAL(cpu_set_add_range(&c, 100, 0), SIGABRT);
282
283 ASSERT_OK(cpu_set_add_range(&c, 0, 0));
284 ASSERT_OK(cpu_set_add_range(&c, 0, 8191));
285 }
286
287 TEST(cpus_online) {
288 unsigned n_cpus = 0;
289 ASSERT_OK(cpus_online(&n_cpus));
290 ASSERT_GE(n_cpus, 1U);
291 log_info("Number of CPUs currently online: %u", n_cpus);
292 }
293
294 DEFINE_TEST_MAIN(LOG_DEBUG);