forked from OSchip/llvm-project
[libc] Add GNU extension functions sched_getaffinity and sched_setaffinity.
Reviewed By: michaelrj Differential Revision: https://reviews.llvm.org/D134858
This commit is contained in:
parent
cecb0e98d4
commit
545b954251
|
@ -193,6 +193,10 @@ def ErrnoAPI : PublicAPI<"errno.h"> {
|
|||
];
|
||||
}
|
||||
|
||||
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 = [
|
||||
|
|
|
@ -22,6 +22,10 @@ set(TARGET_LIBC_ENTRYPOINTS
|
|||
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
|
||||
|
|
|
@ -195,6 +195,15 @@ add_gen_header(
|
|||
.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.
|
||||
|
|
|
@ -15,6 +15,7 @@ add_header(blksize_t HDR blksize_t.h)
|
|||
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)
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -28,7 +28,29 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
|
|||
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
|
||||
|
@ -164,6 +186,7 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
|
|||
FEnv,
|
||||
Math,
|
||||
PThread,
|
||||
Sched,
|
||||
SendFile,
|
||||
StdIO,
|
||||
String,
|
||||
|
|
|
@ -26,7 +26,6 @@ def ClockIdT : NamedType<"clockid_t">;
|
|||
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>;
|
||||
|
|
|
@ -113,6 +113,8 @@ def FILERestrictedPtr : RestrictedPtrType<FILE>;
|
|||
|
||||
def PThreadTType : NamedType<"pthread_t">;
|
||||
|
||||
def PidT : NamedType<"pid_t">;
|
||||
|
||||
//added because __assert_fail needs it.
|
||||
def UnsignedType : NamedType<"unsigned">;
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
|
|||
add_subdirectory(dirent)
|
||||
add_subdirectory(fcntl)
|
||||
add_subdirectory(pthread)
|
||||
add_subdirectory(sched)
|
||||
add_subdirectory(sys)
|
||||
add_subdirectory(unistd)
|
||||
endif()
|
||||
|
|
|
@ -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
|
||||
)
|
|
@ -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
|
||||
)
|
|
@ -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 <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
|
|
@ -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 <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
|
|
@ -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 <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
|
|
@ -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 <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
|
|
@ -38,6 +38,7 @@ add_subdirectory(stdio)
|
|||
|
||||
if(${LIBC_TARGET_OS} STREQUAL "linux")
|
||||
add_subdirectory(fcntl)
|
||||
add_subdirectory(sched)
|
||||
add_subdirectory(sys)
|
||||
add_subdirectory(unistd)
|
||||
endif()
|
||||
|
|
|
@ -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
|
||||
)
|
|
@ -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 <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;
|
||||
}
|
Loading…
Reference in New Issue