From nobody Fri Jun 17 19:40:20 2022 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 6956D85CE4B; Fri, 17 Jun 2022 19:40:21 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LPqBr6xjGz3hVV; Fri, 17 Jun 2022 19:40:20 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655494821; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6atkC1EH57fMxg8NWCSb+QXZwU6xxfnRg3g2alN39Uc=; b=NU8d9aEKN6YtFjsl5TmB0c4kXWD29fMrrVBApeonG6LOTPg5kXrY9U5XFMpbIk2V/mOAVL uoXMZjBebFkKas5RyAvy0CPWbHsm8O+pQytLB/tNfYtbaRaDbWVN+1MziN1skFlmm/aHEC W1YnpkzAkRoGrjqwJHhBIyu66ZpFcZmquperKRPTixpfPi18Wscn2hsTndKbWqFyBR2v/B aiZ7DDWC6Yo4DR/DdMkey9YY+QG6b1U9NfP909L6SjUBNTnMcZOe+6yAm2kTiIxHQmdXU8 2qwsk8R/vgS00wiHLMgvVnSKMJfY9CWcw+ZpStxgmjQb6EPhzljlEai/hOi9iQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 26A3E25A50; Fri, 17 Jun 2022 19:40:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 25HJeKtR024680; Fri, 17 Jun 2022 19:40:20 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 25HJeKiF024674; Fri, 17 Jun 2022 19:40:20 GMT (envelope-from git) Date: Fri, 17 Jun 2022 19:40:20 GMT Message-Id: <202206171940.25HJeKiF024674@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dmitry Chagin Subject: git: 66a762e5f685 - stable/13 - Add tests for affinity syscalls. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 66a762e5f685da68475204b8029d38996c1a23d1 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1655494821; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=6atkC1EH57fMxg8NWCSb+QXZwU6xxfnRg3g2alN39Uc=; b=Uu8L7YTPXxhEMv5cBAlyzhXflybyxO6mw73onMT6+kJkciDeFcnw/ktGnddoumkzqqe+ak KE1Krw+fx5HyjgqQNTs1q/gKHid8QDctqPlG2Fi+yCE68VbpBW5nS3rI84EvvI9osKqUYS 7+Mt8h4UutlRupSZzhzsGAqSSkuGtCKoN9KT4JFeE+CyH3MmXN0xlTcApLVP8PYswH4UE2 OXMvtHUw71Ha4u0kcfGeBw5wymrJ/0wo1r+exxJIbTKyCJKG2d2QznVo/JlKPDMsTs9pTn j/a3q9e289em4k3COHEftqXeHrX9oWGRAWVUcuMciWSEE7PCMD/9YukGwaw9Lg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1655494821; a=rsa-sha256; cv=none; b=egh3nhY+VHZasNQR5cNdoDqQ7EQM/g4kEYyK3KhmD4pGPacbExh0LhBMd2TRKFiL60dk1w is/d8XVfZE+/AbKc0z4WoXj73fxwM9gUtu5X4KLZFBcTCTD+pqwHw5ASjmnM5U7zDCGZXa zr+XGhbAcvQ1WymSDGzt99do4mksX+eq3WzRRTnIfGEe0XcBcDl6Tgeo+BFn6+oxxzmt7H jmzmoV/sdPx/qi/Yzn0uz1sfJ2qRDcV+u5Rtt5w17DYtRwYGxRw17r0jlSuajK2i4t4+Me //euTvQXm/ZEKzv8oLooW/+8KPbBbg/eJZMLqDQT7lPCGKcpJXKJp89S7xnWNg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=66a762e5f685da68475204b8029d38996c1a23d1 commit 66a762e5f685da68475204b8029d38996c1a23d1 Author: Dmitry Chagin AuthorDate: 2022-05-11 07:39:18 +0000 Commit: Dmitry Chagin 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 + * + * SPDX-License-Identifier: BSD-2-Clause + */ +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include + +#include +#include + +#include + +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()); +}