[libc] Implement sched_getcpu (#195001)
This is extremely similar to getcpu, but was available in a much earlier glibc, so a lot more code depends on it. Do a similar implementation. We can only have a simple smoke test as the only documented failure mode in the man page is running on a kernel that does not support the system call, and such kernels (<2.6) are ancient at this point.
This commit is contained in:
@@ -39,6 +39,7 @@ set(TARGET_LIBC_ENTRYPOINTS
|
|||||||
libc.src.sched.sched_get_priority_max
|
libc.src.sched.sched_get_priority_max
|
||||||
libc.src.sched.sched_get_priority_min
|
libc.src.sched.sched_get_priority_min
|
||||||
libc.src.sched.sched_getaffinity
|
libc.src.sched.sched_getaffinity
|
||||||
|
libc.src.sched.sched_getcpu
|
||||||
libc.src.sched.sched_getparam
|
libc.src.sched.sched_getparam
|
||||||
libc.src.sched.sched_getscheduler
|
libc.src.sched.sched_getscheduler
|
||||||
libc.src.sched.sched_rr_get_interval
|
libc.src.sched.sched_rr_get_interval
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ set(TARGET_LIBC_ENTRYPOINTS
|
|||||||
libc.src.sched.sched_get_priority_max
|
libc.src.sched.sched_get_priority_max
|
||||||
libc.src.sched.sched_get_priority_min
|
libc.src.sched.sched_get_priority_min
|
||||||
libc.src.sched.sched_getaffinity
|
libc.src.sched.sched_getaffinity
|
||||||
|
libc.src.sched.sched_getcpu
|
||||||
libc.src.sched.sched_getparam
|
libc.src.sched.sched_getparam
|
||||||
libc.src.sched.sched_getscheduler
|
libc.src.sched.sched_getscheduler
|
||||||
libc.src.sched.sched_rr_get_interval
|
libc.src.sched.sched_rr_get_interval
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ set(TARGET_LIBC_ENTRYPOINTS
|
|||||||
libc.src.sched.sched_get_priority_max
|
libc.src.sched.sched_get_priority_max
|
||||||
libc.src.sched.sched_get_priority_min
|
libc.src.sched.sched_get_priority_min
|
||||||
libc.src.sched.sched_getaffinity
|
libc.src.sched.sched_getaffinity
|
||||||
|
libc.src.sched.sched_getcpu
|
||||||
libc.src.sched.sched_getparam
|
libc.src.sched.sched_getparam
|
||||||
libc.src.sched.sched_getscheduler
|
libc.src.sched.sched_getscheduler
|
||||||
libc.src.sched.sched_rr_get_interval
|
libc.src.sched.sched_rr_get_interval
|
||||||
|
|||||||
@@ -75,6 +75,12 @@ functions:
|
|||||||
- type: pid_t
|
- type: pid_t
|
||||||
- type: size_t
|
- type: size_t
|
||||||
- type: cpu_set_t *
|
- type: cpu_set_t *
|
||||||
|
- name: sched_getcpu
|
||||||
|
standards:
|
||||||
|
- GNUExtensions
|
||||||
|
return_type: int
|
||||||
|
arguments:
|
||||||
|
- type: void
|
||||||
- name: sched_getparam
|
- name: sched_getparam
|
||||||
standards:
|
standards:
|
||||||
- POSIX
|
- POSIX
|
||||||
|
|||||||
@@ -16,6 +16,13 @@ add_entrypoint_object(
|
|||||||
.${LIBC_TARGET_OS}.sched_getaffinity
|
.${LIBC_TARGET_OS}.sched_getaffinity
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_entrypoint_object(
|
||||||
|
sched_getcpu
|
||||||
|
ALIAS
|
||||||
|
DEPENDS
|
||||||
|
.${LIBC_TARGET_OS}.sched_getcpu
|
||||||
|
)
|
||||||
|
|
||||||
add_entrypoint_object(
|
add_entrypoint_object(
|
||||||
sched_setaffinity
|
sched_setaffinity
|
||||||
ALIAS
|
ALIAS
|
||||||
|
|||||||
@@ -24,6 +24,17 @@ add_entrypoint_object(
|
|||||||
libc.src.errno.errno
|
libc.src.errno.errno
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_entrypoint_object(
|
||||||
|
sched_getcpu
|
||||||
|
SRCS
|
||||||
|
sched_getcpu.cpp
|
||||||
|
HDRS
|
||||||
|
../sched_getcpu.h
|
||||||
|
DEPENDS
|
||||||
|
libc.src.__support.OSUtil.osutil
|
||||||
|
libc.src.errno.errno
|
||||||
|
)
|
||||||
|
|
||||||
add_entrypoint_object(
|
add_entrypoint_object(
|
||||||
sched_setaffinity
|
sched_setaffinity
|
||||||
SRCS
|
SRCS
|
||||||
|
|||||||
31
libc/src/sched/linux/sched_getcpu.cpp
Normal file
31
libc/src/sched/linux/sched_getcpu.cpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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_getcpu.h"
|
||||||
|
|
||||||
|
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
|
||||||
|
#include "src/__support/common.h"
|
||||||
|
#include "src/__support/libc_errno.h"
|
||||||
|
#include "src/__support/macros/config.h"
|
||||||
|
|
||||||
|
#include <sys/syscall.h> // For syscall numbers.
|
||||||
|
|
||||||
|
namespace LIBC_NAMESPACE_DECL {
|
||||||
|
|
||||||
|
LLVM_LIBC_FUNCTION(int, sched_getcpu, ()) {
|
||||||
|
unsigned int cpu = 0;
|
||||||
|
int ret =
|
||||||
|
LIBC_NAMESPACE::syscall_impl<int>(SYS_getcpu, &cpu, nullptr, nullptr);
|
||||||
|
if (ret < 0) {
|
||||||
|
libc_errno = -ret;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return cpu;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace LIBC_NAMESPACE_DECL
|
||||||
20
libc/src/sched/sched_getcpu.h
Normal file
20
libc/src/sched/sched_getcpu.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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_SCHED_SCHED_GETCPU_H
|
||||||
|
#define LLVM_LIBC_SRC_SCHED_SCHED_GETCPU_H
|
||||||
|
|
||||||
|
#include "src/__support/macros/config.h"
|
||||||
|
|
||||||
|
namespace LIBC_NAMESPACE_DECL {
|
||||||
|
|
||||||
|
int sched_getcpu(void);
|
||||||
|
|
||||||
|
} // namespace LIBC_NAMESPACE_DECL
|
||||||
|
|
||||||
|
#endif // LLVM_LIBC_SRC_SCHED_SCHED_GETCPU_H
|
||||||
@@ -56,6 +56,18 @@ add_libc_unittest(
|
|||||||
libc.test.UnitTest.ErrnoCheckingTest
|
libc.test.UnitTest.ErrnoCheckingTest
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_libc_unittest(
|
||||||
|
sched_getcpu_test
|
||||||
|
SUITE
|
||||||
|
libc_sched_unittests
|
||||||
|
SRCS
|
||||||
|
sched_getcpu_test.cpp
|
||||||
|
DEPENDS
|
||||||
|
libc.src.errno.errno
|
||||||
|
libc.src.sched.sched_getcpu
|
||||||
|
libc.test.UnitTest.ErrnoCheckingTest
|
||||||
|
)
|
||||||
|
|
||||||
add_libc_unittest(
|
add_libc_unittest(
|
||||||
scheduler_test
|
scheduler_test
|
||||||
SUITE
|
SUITE
|
||||||
|
|||||||
18
libc/test/src/sched/sched_getcpu_test.cpp
Normal file
18
libc/test/src/sched/sched_getcpu_test.cpp
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// 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_getcpu.h"
|
||||||
|
#include "test/UnitTest/ErrnoCheckingTest.h"
|
||||||
|
#include "test/UnitTest/ErrnoSetterMatcher.h"
|
||||||
|
|
||||||
|
using LlvmLibcSchedSchedGetCpuTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
|
||||||
|
|
||||||
|
TEST_F(LlvmLibcSchedSchedGetCpuTest, SmokeTest) {
|
||||||
|
ASSERT_GE(LIBC_NAMESPACE::sched_getcpu(), 0);
|
||||||
|
ASSERT_ERRNO_SUCCESS();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user