[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:
Aiden Grossman
2026-04-30 14:57:44 -06:00
committed by GitHub
parent 76b5e40297
commit f863470262
10 changed files with 108 additions and 0 deletions

View File

@@ -39,6 +39,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sched.sched_get_priority_max
libc.src.sched.sched_get_priority_min
libc.src.sched.sched_getaffinity
libc.src.sched.sched_getcpu
libc.src.sched.sched_getparam
libc.src.sched.sched_getscheduler
libc.src.sched.sched_rr_get_interval

View File

@@ -39,6 +39,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sched.sched_get_priority_max
libc.src.sched.sched_get_priority_min
libc.src.sched.sched_getaffinity
libc.src.sched.sched_getcpu
libc.src.sched.sched_getparam
libc.src.sched.sched_getscheduler
libc.src.sched.sched_rr_get_interval

View File

@@ -41,6 +41,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sched.sched_get_priority_max
libc.src.sched.sched_get_priority_min
libc.src.sched.sched_getaffinity
libc.src.sched.sched_getcpu
libc.src.sched.sched_getparam
libc.src.sched.sched_getscheduler
libc.src.sched.sched_rr_get_interval

View File

@@ -75,6 +75,12 @@ functions:
- type: pid_t
- type: size_t
- type: cpu_set_t *
- name: sched_getcpu
standards:
- GNUExtensions
return_type: int
arguments:
- type: void
- name: sched_getparam
standards:
- POSIX

View File

@@ -16,6 +16,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.sched_getaffinity
)
add_entrypoint_object(
sched_getcpu
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.sched_getcpu
)
add_entrypoint_object(
sched_setaffinity
ALIAS

View File

@@ -24,6 +24,17 @@ add_entrypoint_object(
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(
sched_setaffinity
SRCS

View 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

View 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

View File

@@ -56,6 +56,18 @@ add_libc_unittest(
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(
scheduler_test
SUITE

View 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();
}