xref: /aosp_15_r20/external/llvm-libc/test/src/sched/affinity_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for sched_getaffinity and sched_setaffinity -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/__support/OSUtil/syscall.h"
10 #include "src/errno/libc_errno.h"
11 #include "src/sched/sched_getaffinity.h"
12 #include "src/sched/sched_setaffinity.h"
13 #include "test/UnitTest/ErrnoSetterMatcher.h"
14 
15 #include <sched.h>
16 #include <sys/syscall.h>
17 
TEST(LlvmLibcSchedAffinityTest,SmokeTest)18 TEST(LlvmLibcSchedAffinityTest, SmokeTest) {
19   cpu_set_t mask;
20   LIBC_NAMESPACE::libc_errno = 0;
21   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
22   pid_t tid = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_gettid);
23   ASSERT_GT(tid, pid_t(0));
24   // We just get and set the same mask.
25   ASSERT_THAT(LIBC_NAMESPACE::sched_getaffinity(tid, sizeof(cpu_set_t), &mask),
26               Succeeds(0));
27   ASSERT_THAT(LIBC_NAMESPACE::sched_setaffinity(tid, sizeof(cpu_set_t), &mask),
28               Succeeds(0));
29 }
30 
TEST(LlvmLibcSchedAffinityTest,BadMask)31 TEST(LlvmLibcSchedAffinityTest, BadMask) {
32   using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
33   pid_t tid = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_gettid);
34 
35   LIBC_NAMESPACE::libc_errno = 0;
36   ASSERT_THAT(
37       LIBC_NAMESPACE::sched_getaffinity(tid, sizeof(cpu_set_t), nullptr),
38       Fails(EFAULT));
39 
40   LIBC_NAMESPACE::libc_errno = 0;
41   ASSERT_THAT(
42       LIBC_NAMESPACE::sched_setaffinity(tid, sizeof(cpu_set_t), nullptr),
43       Fails(EFAULT));
44 
45   LIBC_NAMESPACE::libc_errno = 0;
46 }
47