2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
#include "alloc-util.h"
|
|
|
|
#include "cpu-set-util.h"
|
|
|
|
#include "macro.h"
|
|
|
|
|
|
|
|
static void test_parse_cpu_set(void) {
|
2019-05-21 08:45:19 +02:00
|
|
|
CPUSet c = {};
|
2019-05-19 18:02:38 +02:00
|
|
|
_cleanup_free_ char *str = NULL;
|
2016-03-02 23:29:49 +01:00
|
|
|
int cpu;
|
|
|
|
|
|
|
|
/* Simple range (from CPUAffinity example) */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("1 2", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.set);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_ISSET_S(1, c.allocated, c.set));
|
|
|
|
assert_se(CPU_ISSET_S(2, c.allocated, c.set));
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 2);
|
|
|
|
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
2019-05-19 18:02:38 +02:00
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
2019-05-21 08:45:19 +02:00
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* A more interesting range */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0 1 2 3 8 9 10 11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 8);
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 0; cpu < 4; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 8; cpu < 12; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
2019-05-19 18:02:38 +02:00
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
2019-05-21 08:45:19 +02:00
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Quoted strings */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("8 '9' 10 \"11\"", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 4);
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 8; cpu < 12; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
2019-05-19 18:02:38 +02:00
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
2019-05-21 08:45:19 +02:00
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Use commas as separators */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0,1,2,3 8,9,10,11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 8);
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 0; cpu < 4; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 8; cpu < 12; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
2019-05-19 18:02:38 +02:00
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
2019-05-21 08:45:19 +02:00
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Commas with spaces (and trailing comma, space) */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0, 1, 2, 3, 4, 5, 6, 7, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 8);
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 0; cpu < 8; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
2019-05-19 18:02:38 +02:00
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
2019-05-21 08:45:19 +02:00
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Ranges */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0-3,8-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 8);
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 0; cpu < 4; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 8; cpu < 12; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
2019-05-19 18:02:38 +02:00
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
2019-05-21 08:45:19 +02:00
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Ranges with trailing comma, space */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0-3 8-11, ", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 8);
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 0; cpu < 4; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 8; cpu < 12; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
2019-05-19 18:02:38 +02:00
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
2019-05-21 08:45:19 +02:00
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Negative range (returns empty cpu_set) */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("3-0", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 0);
|
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Overlapping ranges */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0-7 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 12);
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 0; cpu < 12; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
2019-05-19 18:02:38 +02:00
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
2019-05-21 08:45:19 +02:00
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Mix ranges and individual CPUs */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0,1 4-11", &c, true, NULL, "fake", 1, "CPUAffinity") >= 0);
|
|
|
|
assert_se(c.allocated >= sizeof(__cpu_mask) / 8);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 10);
|
|
|
|
assert_se(CPU_ISSET_S(0, c.allocated, c.set));
|
|
|
|
assert_se(CPU_ISSET_S(1, c.allocated, c.set));
|
2016-03-02 23:29:49 +01:00
|
|
|
for (cpu = 4; cpu < 12; cpu++)
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(CPU_ISSET_S(cpu, c.allocated, c.set));
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
2019-05-19 18:02:38 +02:00
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
2019-05-21 08:45:19 +02:00
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Garbage */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0 1 2 3 garbage", &c, true, NULL, "fake", 1, "CPUAffinity") == -EINVAL);
|
|
|
|
assert_se(!c.set);
|
|
|
|
assert_se(c.allocated == 0);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Range with garbage */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0-3 8-garbage", &c, true, NULL, "fake", 1, "CPUAffinity") == -EINVAL);
|
|
|
|
assert_se(!c.set);
|
|
|
|
assert_se(c.allocated == 0);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
|
|
|
/* Empty string */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("", &c, true, NULL, "fake", 1, "CPUAffinity") == 0);
|
|
|
|
assert_se(!c.set); /* empty string returns NULL */
|
|
|
|
assert_se(c.allocated == 0);
|
2016-03-02 23:29:49 +01:00
|
|
|
|
2017-09-26 13:50:31 +02:00
|
|
|
/* Runaway quoted string */
|
2019-05-21 08:45:19 +02:00
|
|
|
assert_se(parse_cpu_set_full("0 1 2 3 \"4 5 6 7 ", &c, true, NULL, "fake", 1, "CPUAffinity") == -EINVAL);
|
|
|
|
assert_se(!c.set);
|
|
|
|
assert_se(c.allocated == 0);
|
|
|
|
|
|
|
|
/* Maximum allocation */
|
|
|
|
assert_se(parse_cpu_set_full("8000-8191", &c, true, NULL, "fake", 1, "CPUAffinity") == 0);
|
|
|
|
assert_se(CPU_COUNT_S(c.allocated, c.set) == 192);
|
|
|
|
assert_se(str = cpu_set_to_string(&c));
|
|
|
|
log_info("cpu_set_to_string: %s", str);
|
|
|
|
str = mfree(str);
|
|
|
|
cpu_set_reset(&c);
|
2016-03-02 23:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2019-05-21 08:45:19 +02:00
|
|
|
log_info("CPU_ALLOC_SIZE(1) = %zu", CPU_ALLOC_SIZE(1));
|
|
|
|
log_info("CPU_ALLOC_SIZE(9) = %zu", CPU_ALLOC_SIZE(9));
|
|
|
|
log_info("CPU_ALLOC_SIZE(64) = %zu", CPU_ALLOC_SIZE(64));
|
|
|
|
log_info("CPU_ALLOC_SIZE(65) = %zu", CPU_ALLOC_SIZE(65));
|
|
|
|
log_info("CPU_ALLOC_SIZE(1024) = %zu", CPU_ALLOC_SIZE(1024));
|
|
|
|
log_info("CPU_ALLOC_SIZE(1025) = %zu", CPU_ALLOC_SIZE(1025));
|
|
|
|
log_info("CPU_ALLOC_SIZE(8191) = %zu", CPU_ALLOC_SIZE(8191));
|
|
|
|
|
2016-03-02 23:29:49 +01:00
|
|
|
test_parse_cpu_set();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|