];
}
+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 = [
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
.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.
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)
--- /dev/null
+//===-- 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
--- /dev/null
+//===-- 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
FunctionSpec<"exp10f", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
]
>;
-
+
+ NamedType CpuSetT = NamedType<"cpu_set_t">;
+ PtrType CpuSetPtr = PtrType<CpuSetT>;
+ ConstType ConstCpuSetPtr = ConstType<CpuSetPtr>;
+ HeaderSpec Sched = HeaderSpec<
+ "sched.h",
+ [], // Macros
+ [PidT, SizeTType, CpuSetT], // Types
+ [], // Enumerations
+ [
+ FunctionSpec<
+ "sched_getaffinity",
+ RetValSpec<IntType>,
+ [ArgSpec<PidT>, ArgSpec<SizeTType>, ArgSpec<CpuSetPtr>]
+ >,
+ FunctionSpec<
+ "sched_setaffinity",
+ RetValSpec<IntType>,
+ [ArgSpec<PidT>, ArgSpec<SizeTType>, ArgSpec<ConstCpuSetPtr>]
+ >,
+ ]
+ >;
+
HeaderSpec String = HeaderSpec<
"string.h",
[], // Macros
FEnv,
Math,
PThread,
+ Sched,
SendFile,
StdIO,
String,
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<StatType>;
def PThreadTType : NamedType<"pthread_t">;
+def PidT : NamedType<"pid_t">;
+
//added because __assert_fail needs it.
def UnsignedType : NamedType<"unsigned">;
add_subdirectory(dirent)
add_subdirectory(fcntl)
add_subdirectory(pthread)
+ add_subdirectory(sched)
add_subdirectory(sys)
add_subdirectory(unistd)
endif()
--- /dev/null
+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
+)
--- /dev/null
+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
+)
--- /dev/null
+//===-- 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 <errno.h>
+#include <sched.h>
+#include <stdint.h>
+#include <sys/syscall.h> // 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<uint8_t *>(mask);
+ for (size_t i = size_t(ret); i < cpuset_size; ++i)
+ mask_bytes[i] = 0;
+ }
+ return 0;
+}
+
+} // namespace __llvm_libc
--- /dev/null
+//===-- 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 <errno.h>
+#include <sched.h>
+#include <sys/syscall.h> // 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
--- /dev/null
+//===-- 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 <sched.h>
+
+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
--- /dev/null
+//===-- 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 <sched.h>
+
+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
if(${LIBC_TARGET_OS} STREQUAL "linux")
add_subdirectory(fcntl)
+ add_subdirectory(sched)
add_subdirectory(sys)
add_subdirectory(unistd)
endif()
--- /dev/null
+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
+)
--- /dev/null
+//===-- 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 <errno.h>
+#include <sched.h>
+#include <sys/syscall.h>
+
+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;
+}