[libc-commits] [libc] 545b954 - [libc] Add GNU extension functions sched_getaffinity and sched_setaffinity.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Thu Sep 29 13:32:02 PDT 2022


Author: Siva Chandra Reddy
Date: 2022-09-29T20:31:46Z
New Revision: 545b954251f0ee756f4218248d8107bbd4fc705e

URL: https://github.com/llvm/llvm-project/commit/545b954251f0ee756f4218248d8107bbd4fc705e
DIFF: https://github.com/llvm/llvm-project/commit/545b954251f0ee756f4218248d8107bbd4fc705e.diff

LOG: [libc] Add GNU extension functions sched_getaffinity and sched_setaffinity.

Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D134858

Added: 
    libc/include/llvm-libc-types/cpu_set_t.h
    libc/include/sched.h.def
    libc/src/sched/CMakeLists.txt
    libc/src/sched/linux/CMakeLists.txt
    libc/src/sched/linux/sched_getaffinity.cpp
    libc/src/sched/linux/sched_setaffinity.cpp
    libc/src/sched/sched_getaffinity.h
    libc/src/sched/sched_setaffinity.h
    libc/test/src/sched/CMakeLists.txt
    libc/test/src/sched/affinity_test.cpp

Modified: 
    libc/config/linux/api.td
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/CMakeLists.txt
    libc/include/llvm-libc-types/CMakeLists.txt
    libc/spec/gnu_ext.td
    libc/spec/posix.td
    libc/spec/spec.td
    libc/src/CMakeLists.txt
    libc/test/src/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/api.td b/libc/config/linux/api.td
index 28c86241d0a2b..414b874b86663 100644
--- a/libc/config/linux/api.td
+++ b/libc/config/linux/api.td
@@ -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 = [

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 529779ee26491..bf46da228d59b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -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

diff  --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 544dccc52d7d5..5cee537c429a0 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -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.

diff  --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 78f54380e38f3..399fe396594c3 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -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)

diff  --git a/libc/include/llvm-libc-types/cpu_set_t.h b/libc/include/llvm-libc-types/cpu_set_t.h
new file mode 100644
index 0000000000000..79f694aeda60f
--- /dev/null
+++ b/libc/include/llvm-libc-types/cpu_set_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

diff  --git a/libc/include/sched.h.def b/libc/include/sched.h.def
new file mode 100644
index 0000000000000..73f51788ba061
--- /dev/null
+++ b/libc/include/sched.h.def
@@ -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 

diff  --git a/libc/spec/gnu_ext.td b/libc/spec/gnu_ext.td
index f5b712db3f1d7..1a763db8677c3 100644
--- a/libc/spec/gnu_ext.td
+++ b/libc/spec/gnu_ext.td
@@ -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,

diff  --git a/libc/spec/posix.td b/libc/spec/posix.td
index f691001b3ce94..28d9813cb37f8 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -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>;

diff  --git a/libc/spec/spec.td b/libc/spec/spec.td
index 7a2237f82957c..10c34b9c58431 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -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">;
 

diff  --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index a44203eb5ee0e..c8f801b03d0be 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -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()

diff  --git a/libc/src/sched/CMakeLists.txt b/libc/src/sched/CMakeLists.txt
new file mode 100644
index 0000000000000..012fa419759e5
--- /dev/null
+++ b/libc/src/sched/CMakeLists.txt
@@ -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
+)

diff  --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..08813e4729413
--- /dev/null
+++ b/libc/src/sched/linux/CMakeLists.txt
@@ -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
+)

diff  --git a/libc/src/sched/linux/sched_getaffinity.cpp b/libc/src/sched/linux/sched_getaffinity.cpp
new file mode 100644
index 0000000000000..5d45d15e303db
--- /dev/null
+++ b/libc/src/sched/linux/sched_getaffinity.cpp
@@ -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

diff  --git a/libc/src/sched/linux/sched_setaffinity.cpp b/libc/src/sched/linux/sched_setaffinity.cpp
new file mode 100644
index 0000000000000..4f22aaaa508cf
--- /dev/null
+++ b/libc/src/sched/linux/sched_setaffinity.cpp
@@ -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

diff  --git a/libc/src/sched/sched_getaffinity.h b/libc/src/sched/sched_getaffinity.h
new file mode 100644
index 0000000000000..9b0c7a29a3822
--- /dev/null
+++ b/libc/src/sched/sched_getaffinity.h
@@ -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

diff  --git a/libc/src/sched/sched_setaffinity.h b/libc/src/sched/sched_setaffinity.h
new file mode 100644
index 0000000000000..ad630b13ac224
--- /dev/null
+++ b/libc/src/sched/sched_setaffinity.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

diff  --git a/libc/test/src/CMakeLists.txt b/libc/test/src/CMakeLists.txt
index 6d41c77b3b056..18253ec9d9ced 100644
--- a/libc/test/src/CMakeLists.txt
+++ b/libc/test/src/CMakeLists.txt
@@ -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()

diff  --git a/libc/test/src/sched/CMakeLists.txt b/libc/test/src/sched/CMakeLists.txt
new file mode 100644
index 0000000000000..3d96949587e8c
--- /dev/null
+++ b/libc/test/src/sched/CMakeLists.txt
@@ -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
+)

diff  --git a/libc/test/src/sched/affinity_test.cpp b/libc/test/src/sched/affinity_test.cpp
new file mode 100644
index 0000000000000..860a479cd16a5
--- /dev/null
+++ b/libc/test/src/sched/affinity_test.cpp
@@ -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;
+}


        


More information about the libc-commits mailing list