diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td --- a/libc/config/linux/api.td +++ b/libc/config/linux/api.td @@ -193,6 +193,10 @@ ]; } +def SchedAPI : PublicAPI<"sched.h"> { + let Types = ["pid_t", "size_t", "cpu_set_t"]; +} + def SysMManAPI : PublicAPI<"sys/mman.h"> { let Types = ["off_t", "size_t"]; let Macros = [ diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -22,6 +22,10 @@ libc.src.fcntl.open libc.src.fcntl.openat + # sched.h entrypoints + libc.src.sched.sched_getaffinity + libc.src.sched.sched_setaffinity + # string.h entrypoints libc.src.string.bcmp libc.src.string.bzero diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt --- a/libc/include/CMakeLists.txt +++ b/libc/include/CMakeLists.txt @@ -195,6 +195,15 @@ .llvm-libc-types.pthread_t ) +add_gen_header( + sched + DEF_FILE sched.h.def + GEN_HDR sched.h + DEPENDS + .llvm_libc_common_h + .llvm-libc-types.cpu_set_t +) + # TODO: Not all platforms will have a include/sys directory. Add the sys # directory and the targets for sys/*.h files conditional to the OS requiring # them. diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt --- a/libc/include/llvm-libc-types/CMakeLists.txt +++ b/libc/include/llvm-libc-types/CMakeLists.txt @@ -15,6 +15,7 @@ add_header(clockid_t HDR clockid_t.h) add_header(cnd_t HDR cnd_t.h) add_header(cookie_io_functions_t HDR cookie_io_functions_t.h DEPENDS .off64_t) +add_header(cpu_set_t HDR cpu_set_t.h) add_header(double_t HDR double_t.h) add_header(DIR HDR DIR.h) add_header(dev_t HDR dev_t.h) diff --git a/libc/include/llvm-libc-types/cpu_set_t.h b/libc/include/llvm-libc-types/cpu_set_t.h new file mode 100644 --- /dev/null +++ b/libc/include/llvm-libc-types/cpu_set_t.h @@ -0,0 +1,18 @@ +//===-- Definition of a cpu_set_t type ------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef __LLVM_LIBC_TYPES_CPU_SET_T_H +#define __LLVM_LIBC_TYPES_CPU_SET_T_H + +typedef struct { + // If a processor with more than 1024 CPUs is to be supported in future, + // we need to adjust the size of this array. + unsigned long __mask[128 / sizeof(unsigned long)]; +} cpu_set_t; + +#endif // __LLVM_LIBC_TYPES_CPU_SET_T_H diff --git a/libc/include/sched.h.def b/libc/include/sched.h.def new file mode 100644 --- /dev/null +++ b/libc/include/sched.h.def @@ -0,0 +1,16 @@ +//===-- C standard library header sched.h ---------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SCHED_H +#define LLVM_LIBC_SCHED_H + +#include <__llvm-libc-common.h> + +%%public_api() + +#endif // LLVM_LIBC_SCHED_H diff --git a/libc/spec/gnu_ext.td b/libc/spec/gnu_ext.td --- a/libc/spec/gnu_ext.td +++ b/libc/spec/gnu_ext.td @@ -28,7 +28,29 @@ FunctionSpec<"exp10f", RetValSpec, [ArgSpec]>, ] >; - + + NamedType CpuSetT = NamedType<"cpu_set_t">; + PtrType CpuSetPtr = PtrType; + ConstType ConstCpuSetPtr = ConstType; + HeaderSpec Sched = HeaderSpec< + "sched.h", + [], // Macros + [PidT, SizeTType, CpuSetT], // Types + [], // Enumerations + [ + FunctionSpec< + "sched_getaffinity", + RetValSpec, + [ArgSpec, ArgSpec, ArgSpec] + >, + FunctionSpec< + "sched_setaffinity", + RetValSpec, + [ArgSpec, ArgSpec, ArgSpec] + >, + ] + >; + HeaderSpec String = HeaderSpec< "string.h", [], // Macros @@ -164,6 +186,7 @@ FEnv, Math, PThread, + Sched, SendFile, StdIO, String, diff --git a/libc/spec/posix.td b/libc/spec/posix.td --- a/libc/spec/posix.td +++ b/libc/spec/posix.td @@ -26,7 +26,6 @@ def BlkSizeT : NamedType<"blksize_t">; def BlkCntT : NamedType<"blkcnt_t">; def NLinkT : NamedType<"nlink_t">; -def PidT : NamedType<"pid_t">; def StatType : NamedType<"struct stat">; def StatTypePtr : PtrType; diff --git a/libc/spec/spec.td b/libc/spec/spec.td --- a/libc/spec/spec.td +++ b/libc/spec/spec.td @@ -113,6 +113,8 @@ def PThreadTType : NamedType<"pthread_t">; +def PidT : NamedType<"pid_t">; + //added because __assert_fail needs it. def UnsignedType : NamedType<"unsigned">; diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt --- a/libc/src/CMakeLists.txt +++ b/libc/src/CMakeLists.txt @@ -13,6 +13,7 @@ add_subdirectory(dirent) add_subdirectory(fcntl) add_subdirectory(pthread) + add_subdirectory(sched) add_subdirectory(sys) add_subdirectory(unistd) endif() diff --git a/libc/src/sched/CMakeLists.txt b/libc/src/sched/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/libc/src/sched/CMakeLists.txt @@ -0,0 +1,17 @@ +if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) +endif() + +add_entrypoint_object( + sched_getaffinity + ALIAS + DEPENDS + .${LIBC_TARGET_OS}.sched_getaffinity +) + +add_entrypoint_object( + sched_setaffinity + ALIAS + DEPENDS + .${LIBC_TARGET_OS}.sched_setaffinity +) diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/libc/src/sched/linux/CMakeLists.txt @@ -0,0 +1,25 @@ +add_entrypoint_object( + sched_getaffinity + SRCS + sched_getaffinity.cpp + HDRS + ../sched_getaffinity.h + DEPENDS + libc.include.errno + libc.include.sched + libc.src.__support.OSUtil.osutil + libc.src.errno.errno +) + +add_entrypoint_object( + sched_setaffinity + SRCS + sched_setaffinity.cpp + HDRS + ../sched_setaffinity.h + DEPENDS + libc.include.errno + libc.include.sched + libc.src.__support.OSUtil.osutil + libc.src.errno.errno +) diff --git a/libc/src/sched/linux/sched_getaffinity.cpp b/libc/src/sched/linux/sched_getaffinity.cpp new file mode 100644 --- /dev/null +++ b/libc/src/sched/linux/sched_getaffinity.cpp @@ -0,0 +1,39 @@ +//===-- Implementation of sched_getaffinity -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/sched/sched_getaffinity.h" + +#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/common.h" + +#include +#include +#include +#include // For syscall numbers. + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(int, sched_getaffinity, + (pid_t tid, size_t cpuset_size, cpu_set_t *mask)) { + long ret = + __llvm_libc::syscall(SYS_sched_getaffinity, tid, cpuset_size, mask); + if (ret < 0) { + errno = -ret; + return -1; + } + if (size_t(ret) < cpuset_size) { + // This means that only |ret| bytes in |mask| have been set. We will have to + // zero out the remaining bytes. + auto *mask_bytes = reinterpret_cast(mask); + for (size_t i = size_t(ret); i < cpuset_size; ++i) + mask_bytes[i] = 0; + } + return 0; +} + +} // namespace __llvm_libc diff --git a/libc/src/sched/linux/sched_setaffinity.cpp b/libc/src/sched/linux/sched_setaffinity.cpp new file mode 100644 --- /dev/null +++ b/libc/src/sched/linux/sched_setaffinity.cpp @@ -0,0 +1,31 @@ +//===-- Implementation of sched_setaffinity -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/sched/sched_setaffinity.h" + +#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/common.h" + +#include +#include +#include // For syscall numbers. + +namespace __llvm_libc { + +LLVM_LIBC_FUNCTION(int, sched_setaffinity, + (pid_t tid, size_t cpuset_size, const cpu_set_t *mask)) { + long ret = + __llvm_libc::syscall(SYS_sched_setaffinity, tid, cpuset_size, mask); + if (ret < 0) { + errno = -ret; + return -1; + } + return 0; +} + +} // namespace __llvm_libc diff --git a/libc/src/sched/sched_getaffinity.h b/libc/src/sched/sched_getaffinity.h new file mode 100644 --- /dev/null +++ b/libc/src/sched/sched_getaffinity.h @@ -0,0 +1,20 @@ +//===-- Implementation header for sched_getaffinity -------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_UNISTD_SCHED_GETAFFINITY_H +#define LLVM_LIBC_SRC_UNISTD_SCHED_GETAFFINITY_H + +#include + +namespace __llvm_libc { + +int sched_getaffinity(pid_t tid, size_t cpuset_size, cpu_set_t *mask); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_UNISTD_SCHED_GETAFFINITY_H diff --git a/libc/src/sched/sched_setaffinity.h b/libc/src/sched/sched_setaffinity.h new file mode 100644 --- /dev/null +++ b/libc/src/sched/sched_setaffinity.h @@ -0,0 +1,20 @@ +//===-- Implementation header for sched_setaffinity -------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC_UNISTD_SCHED_SETAFFINITY_H +#define LLVM_LIBC_SRC_UNISTD_SCHED_SETAFFINITY_H + +#include + +namespace __llvm_libc { + +int sched_setaffinity(pid_t pid, size_t cpuset_size, const cpu_set_t *mask); + +} // namespace __llvm_libc + +#endif // LLVM_LIBC_SRC_UNISTD_SCHED_SETAFFINITY_H diff --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt --- a/libc/test/src/CMakeLists.txt +++ b/libc/test/src/CMakeLists.txt @@ -38,6 +38,7 @@ if(${LIBC_TARGET_OS} STREQUAL "linux") add_subdirectory(fcntl) + add_subdirectory(sched) add_subdirectory(sys) add_subdirectory(unistd) endif() diff --git a/libc/test/src/sched/CMakeLists.txt b/libc/test/src/sched/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/libc/test/src/sched/CMakeLists.txt @@ -0,0 +1,17 @@ +add_libc_testsuite(libc_sched_unittests) + +add_libc_unittest( + affinity_test + SUITE + libc_sched_unittests + SRCS + affinity_test.cpp + DEPENDS + libc.include.errno + libc.include.sched + libc.include.sys_syscall + libc.src.__support.OSUtil.osutil + libc.src.sched.sched_getaffinity + libc.src.sched.sched_setaffinity + libc.test.errno_setter_matcher +) diff --git a/libc/test/src/sched/affinity_test.cpp b/libc/test/src/sched/affinity_test.cpp new file mode 100644 --- /dev/null +++ b/libc/test/src/sched/affinity_test.cpp @@ -0,0 +1,44 @@ +//===-- Unittests for sched_getaffinity and sched_setaffinity -------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "src/__support/OSUtil/syscall.h" +#include "src/sched/sched_getaffinity.h" +#include "src/sched/sched_setaffinity.h" +#include "test/ErrnoSetterMatcher.h" + +#include +#include +#include + +TEST(LlvmLibcSchedAffinityTest, SmokeTest) { + cpu_set_t mask; + errno = 0; + using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds; + pid_t tid = __llvm_libc::syscall(SYS_gettid); + ASSERT_GT(tid, pid_t(0)); + // We just get and set the same mask. + ASSERT_THAT(__llvm_libc::sched_getaffinity(tid, sizeof(cpu_set_t), &mask), + Succeeds(0)); + ASSERT_THAT(__llvm_libc::sched_setaffinity(tid, sizeof(cpu_set_t), &mask), + Succeeds(0)); +} + +TEST(LlvmLibcSchedAffinityTest, BadMask) { + using __llvm_libc::testing::ErrnoSetterMatcher::Fails; + pid_t tid = __llvm_libc::syscall(SYS_gettid); + + errno = 0; + ASSERT_THAT(__llvm_libc::sched_getaffinity(tid, sizeof(cpu_set_t), nullptr), + Fails(EFAULT)); + + errno = 0; + ASSERT_THAT(__llvm_libc::sched_setaffinity(tid, sizeof(cpu_set_t), nullptr), + Fails(EFAULT)); + + errno = 0; +}