git: 66a762e5f685 - stable/13 - Add tests for affinity syscalls.
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 17 Jun 2022 19:40:20 UTC
The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=66a762e5f685da68475204b8029d38996c1a23d1 commit 66a762e5f685da68475204b8029d38996c1a23d1 Author: Dmitry Chagin <dchagin@FreeBSD.org> AuthorDate: 2022-05-11 07:39:18 +0000 Commit: Dmitry Chagin <dchagin@FreeBSD.org> CommitDate: 2022-06-17 19:35:15 +0000 Add tests for affinity syscalls. MFC after: 2 weeks (cherry picked from commit c8b5c478f66d155a092b8d0c6fd689546736dae7) --- tests/sys/kern/Makefile | 1 + tests/sys/kern/sched_affinity.c | 297 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 298 insertions(+) diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile index 36aad1ec797d..fb20f8b296b6 100644 --- a/tests/sys/kern/Makefile +++ b/tests/sys/kern/Makefile @@ -22,6 +22,7 @@ ATF_TESTS_C+= module_test ATF_TESTS_C+= ptrace_test TEST_METADATA.ptrace_test+= timeout="15" ATF_TESTS_C+= reaper +ATF_TESTS_C+= sched_affinity ATF_TESTS_C+= sigaltstack ATF_TESTS_C+= sigwait .if ${MACHINE_ARCH} != "i386" && ${MACHINE_ARCH:Mpowerpc*} == "" && \ diff --git a/tests/sys/kern/sched_affinity.c b/tests/sys/kern/sched_affinity.c new file mode 100644 index 000000000000..2237acf75f88 --- /dev/null +++ b/tests/sys/kern/sched_affinity.c @@ -0,0 +1,297 @@ +/*- + * Copyright (c) 2022 Dmitry Chagin <dchagin@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/types.h> +#include <sys/stdint.h> +#include <sys/sysctl.h> + +#include <errno.h> +#include <sched.h> + +#include <atf-c.h> + +static uint32_t maxcpuid; +static uint32_t maxcpus; +static uint32_t cpus; + +static uint32_t +support_getcpus(void) +{ + uint32_t val; + size_t sz = sizeof(val); + + ATF_REQUIRE(sysctlbyname("kern.smp.cpus", &val, &sz, NULL, 0) == 0); + return (val); +} + +static uint32_t +support_getmaxcpus(void) +{ + uint32_t val; + size_t sz = sizeof(val); + + ATF_REQUIRE(sysctlbyname("kern.smp.maxcpus", &val, &sz, NULL, 0) == 0); + return (val); +} + +static uint32_t +support_getmaxcpuid(void) +{ + cpuset_t *set; + int setsize, rv; + uint32_t i, id; + + for (i = 1; i < maxcpus; i++) { + setsize = CPU_ALLOC_SIZE(i); + set = CPU_ALLOC(i); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(i, set); + rv = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, setsize, set); + if (rv == 0) { + id = __BIT_FLS(i, set) - 1; + CPU_FREE(set); + break; + } + CPU_FREE(set); + } + ATF_REQUIRE(rv == 0); + return (id); +} + +ATF_TC_WITHOUT_HEAD(test_setinvalidcpu); +ATF_TC_BODY(test_setinvalidcpu, tc) +{ + size_t cpusetsize; + cpuset_t *set; + + cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1); + set = CPU_ALLOC(maxcpuid + 1); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(maxcpuid + 1, set); + CPU_SET_S(maxcpuid + 1, maxcpuid + 1, set); + CPU_SET_S(maxcpuid - 1, maxcpuid + 1, set); + ATF_REQUIRE(sched_setaffinity(0, cpusetsize, set) == 0); + CPU_FREE(set); + + cpusetsize = CPU_ALLOC_SIZE(maxcpus + 1); + set = CPU_ALLOC(maxcpus + 1); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(maxcpus + 1, set); + CPU_SET_S(maxcpuid + 1, maxcpus + 1, set); + CPU_SET_S(maxcpuid - 1, maxcpus + 1, set); + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == -1); + ATF_REQUIRE_EQ(errno, EINVAL); + CPU_FREE(set); +} + +ATF_TC_WITHOUT_HEAD(test_setvalidcpu); +ATF_TC_BODY(test_setvalidcpu, tc) +{ + size_t cpusetsize; + cpuset_t *set; + int cpu; + + ATF_REQUIRE(maxcpuid < maxcpus); + cpu = maxcpuid > 1 ? maxcpuid - 1 : 0; + + cpusetsize = CPU_ALLOC_SIZE(maxcpus + 1); + set = CPU_ALLOC(maxcpus + 1); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(maxcpus + 1, set); + CPU_SET_S(cpu, maxcpus + 1, set); + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == 0); + ATF_REQUIRE_EQ(cpu, sched_getcpu()); + CPU_FREE(set); +} + +ATF_TC_WITHOUT_HEAD(test_setzeroset1); +ATF_TC_BODY(test_setzeroset1, tc) +{ + size_t cpusetsize; + cpuset_t *set; + + cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1); + set = CPU_ALLOC(maxcpuid + 1); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(maxcpuid + 1, set); + ATF_REQUIRE(sched_setaffinity(0, cpusetsize, set) == -1); + ATF_REQUIRE_EQ(errno, EINVAL); + CPU_FREE(set); +} + +ATF_TC_WITHOUT_HEAD(test_setzeroset2); +ATF_TC_BODY(test_setzeroset2, tc) +{ + size_t cpusetsize; + cpuset_t *set; + + cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1); + set = CPU_ALLOC(maxcpuid + 1); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(maxcpuid + 1, set); + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == -1); + ATF_REQUIRE_EQ(errno, EDEADLK); + CPU_FREE(set); +} + +ATF_TC_WITHOUT_HEAD(test_setmaxsetsize); +ATF_TC_BODY(test_setmaxsetsize, tc) +{ + size_t cpusetsize; + cpuset_t *set; + + cpusetsize = CPU_ALLOC_SIZE(maxcpus * 2); + set = CPU_ALLOC(maxcpus * 2); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(maxcpus * 2, set); + ATF_REQUIRE(CPU_COUNT_S(maxcpus * 2, set) == 0); + CPU_SET_S(0, maxcpus * 2, set); + ATF_REQUIRE(CPU_COUNT_S(maxcpus * 2, set) == 1); + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == 0); + + CPU_ZERO_S(maxcpus * 2, set); + CPU_SET_S(maxcpuid, maxcpus * 2, set); + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == 0); + + CPU_ZERO_S(maxcpus * 2, set); + CPU_SET_S(maxcpuid + 1, maxcpus * 2, set); + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == -1); + ATF_REQUIRE_EQ(errno, EINVAL); + CPU_FREE(set); +} + +ATF_TC_WITHOUT_HEAD(test_setminsetsize); +ATF_TC_BODY(test_setminsetsize, tc) +{ + size_t cpusetsize = 1; + int8_t set; + + if (cpus <= 8) + return; + + set = 1; + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, (const cpuset_t *)&set) == 0); + set = 0; + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, (const cpuset_t *)&set) == -1); + ATF_REQUIRE_EQ(errno, EDEADLK); +} + +ATF_TC_WITHOUT_HEAD(test_getminsetsize); +ATF_TC_BODY(test_getminsetsize, tc) +{ + size_t cpusetsize = 1; + int8_t set = 0; + + if (cpus < 9) + return; + ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, (cpuset_t *)&set) == -1); + ATF_REQUIRE_EQ(errno, ERANGE); +} + +ATF_TC_WITHOUT_HEAD(test_getsetsize); +ATF_TC_BODY(test_getsetsize, tc) +{ + size_t cpusetsize; + cpuset_t *set; + + cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1); + set = CPU_ALLOC(maxcpuid + 1); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(maxcpuid + 1, set); + ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == 0); + CPU_FREE(set); +} + +ATF_TC_WITHOUT_HEAD(test_schedgetsetsize); +ATF_TC_BODY(test_schedgetsetsize, tc) +{ + cpuset_t *set; + int cpusetsize; + + cpusetsize = CPU_ALLOC_SIZE(maxcpuid + 1); + set = CPU_ALLOC(maxcpuid + 1); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(maxcpuid + 1, set); + ATF_REQUIRE(sched_getaffinity(0, cpusetsize, set) == cpusetsize); + CPU_FREE(set); + + set = CPU_ALLOC(CPU_SETSIZE); + ATF_REQUIRE(set != NULL); + cpusetsize = CPU_ALLOC_SIZE(CPU_SETSIZE); + CPU_ZERO(set); + ATF_REQUIRE(sched_getaffinity(0, cpusetsize, set) == cpusetsize); + CPU_FREE(set); +} + +ATF_TC_WITHOUT_HEAD(test_holes); +ATF_TC_BODY(test_holes, tc) +{ + cpuset_t *set; + int cpusetsize; + + cpusetsize = CPU_ALLOC_SIZE(maxcpus * 2); + set = CPU_ALLOC(maxcpus * 2); + ATF_REQUIRE(set != NULL); + CPU_ZERO_S(maxcpus * 2, set); + ATF_REQUIRE(CPU_COUNT_S(maxcpus * 2, set) == 0); + CPU_SET_S(maxcpuid, maxcpus * 2, set); + ATF_REQUIRE(CPU_COUNT_S(maxcpus * 2, set) == 1); + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == 0); + + CPU_ZERO_S(maxcpus * 2, set); + ATF_REQUIRE(CPU_COUNT_S(maxcpus * 2, set) == 0); + CPU_SET_S(maxcpuid + 1, maxcpus * 2, set); + ATF_REQUIRE(CPU_COUNT_S(maxcpus * 2, set) == 1); + ATF_REQUIRE(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == -1); + ATF_REQUIRE_EQ(errno, EINVAL); + + ATF_REQUIRE(CPU_COUNT_S(maxcpus * 2, set) == 1); + ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, + -1, cpusetsize, set) == 0); + ATF_REQUIRE(CPU_ISSET_S(maxcpuid + 1, maxcpus * 2, set) == false); + ATF_REQUIRE(CPU_ISSET_S(maxcpuid, maxcpus * 2, set) == true); + ATF_REQUIRE_EQ(maxcpuid, (uint32_t)sched_getcpu()); +} + +ATF_TP_ADD_TCS(tp) +{ + + cpus = support_getcpus(); + maxcpus = support_getmaxcpus(); + maxcpuid = support_getmaxcpuid(); + + ATF_TP_ADD_TC(tp, test_setinvalidcpu); + ATF_TP_ADD_TC(tp, test_setvalidcpu); + ATF_TP_ADD_TC(tp, test_setzeroset1); + ATF_TP_ADD_TC(tp, test_setzeroset2); + + ATF_TP_ADD_TC(tp, test_setminsetsize); + ATF_TP_ADD_TC(tp, test_setmaxsetsize); + + ATF_TP_ADD_TC(tp, test_getminsetsize); + ATF_TP_ADD_TC(tp, test_getsetsize); + + ATF_TP_ADD_TC(tp, test_schedgetsetsize); + + ATF_TP_ADD_TC(tp, test_holes); + + return (atf_no_error()); +}