[libc-commits] [libc] [libc] Implement sched_getcpu (PR #195001)

Aiden Grossman via libc-commits libc-commits at lists.llvm.org
Wed Apr 29 21:40:13 PDT 2026


https://github.com/boomanaiden154 created https://github.com/llvm/llvm-project/pull/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.

>From 03de0d4992caee0fae864e761668f7a74d099cd9 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Thu, 30 Apr 2026 04:38:21 +0000
Subject: [PATCH] [libc] Implement sched_getcpu

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.
---
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/include/sched.yaml                   |  6 +++++
 libc/src/sched/CMakeLists.txt             |  7 +++++
 libc/src/sched/linux/CMakeLists.txt       | 11 ++++++++
 libc/src/sched/linux/sched_getcpu.cpp     | 31 +++++++++++++++++++++++
 libc/src/sched/sched_getcpu.h             | 20 +++++++++++++++
 libc/test/src/sched/CMakeLists.txt        | 12 +++++++++
 libc/test/src/sched/sched_getcpu_test.cpp | 19 ++++++++++++++
 8 files changed, 107 insertions(+)
 create mode 100644 libc/src/sched/linux/sched_getcpu.cpp
 create mode 100644 libc/src/sched/sched_getcpu.h
 create mode 100644 libc/test/src/sched/sched_getcpu_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index d1c1d9496af67..54f2280a8d9a3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -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
diff --git a/libc/include/sched.yaml b/libc/include/sched.yaml
index b75dee3000449..d08b875aaa6ce 100644
--- a/libc/include/sched.yaml
+++ b/libc/include/sched.yaml
@@ -52,6 +52,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
diff --git a/libc/src/sched/CMakeLists.txt b/libc/src/sched/CMakeLists.txt
index d1d1de0718c56..39ac8c8c6f247 100644
--- a/libc/src/sched/CMakeLists.txt
+++ b/libc/src/sched/CMakeLists.txt
@@ -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
diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt
index ceb755f8e1d1a..6cc41e97baa00 100644
--- a/libc/src/sched/linux/CMakeLists.txt
+++ b/libc/src/sched/linux/CMakeLists.txt
@@ -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
diff --git a/libc/src/sched/linux/sched_getcpu.cpp b/libc/src/sched/linux/sched_getcpu.cpp
new file mode 100644
index 0000000000000..2c58440e3c73f
--- /dev/null
+++ b/libc/src/sched/linux/sched_getcpu.cpp
@@ -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, ()) {
+  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
diff --git a/libc/src/sched/sched_getcpu.h b/libc/src/sched/sched_getcpu.h
new file mode 100644
index 0000000000000..6e6857cc33b4f
--- /dev/null
+++ b/libc/src/sched/sched_getcpu.h
@@ -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
diff --git a/libc/test/src/sched/CMakeLists.txt b/libc/test/src/sched/CMakeLists.txt
index 93752ed26108d..5c6c90f40194f 100644
--- a/libc/test/src/sched/CMakeLists.txt
+++ b/libc/test/src/sched/CMakeLists.txt
@@ -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
diff --git a/libc/test/src/sched/sched_getcpu_test.cpp b/libc/test/src/sched/sched_getcpu_test.cpp
new file mode 100644
index 0000000000000..7953294c3459e
--- /dev/null
+++ b/libc/test/src/sched/sched_getcpu_test.cpp
@@ -0,0 +1,19 @@
+//===-- Unittests for getcpu ----------------------------------------------===//
+//
+// 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_getcpu.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+
+using LlvmLibcSchedSchedGetCpuTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcSchedSchedGetCpuTest, SmokeTest) {
+  using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+  ASSERT_THAT(LIBC_NAMESPACE::sched_getcpu(), Succeeds());
+}



More information about the libc-commits mailing list