Skip to content

[libc] Implement sysinfo - #217471

Merged
michaelrj-google merged 3 commits into
llvm:mainfrom
michaelrj-google:libcSysinfo
Aug 21, 2026
Merged

[libc] Implement sysinfo#217471
michaelrj-google merged 3 commits into
llvm:mainfrom
michaelrj-google:libcSysinfo

Conversation

@michaelrj-google

Copy link
Copy Markdown
Member

Adds the public sysinfo function and tests. The internal wrapper already
existed. I decided to avoid defining struct sysinfo since it's a
kernel struct defined in the UAPI headers. Can revisit in future if
necessary.

Assisted-by: Automated tooling, human reviewed.

Adds the public sysinfo function and tests. The internal wrapper already
existed. I decided to avoid defining `struct sysinfo` since it's a
kernel struct defined in the UAPI headers. Can revisit in future if
necessary.

Assisted-by: Automated tooling, human reviewed.
@michaelrj-google
michaelrj-google requested a review from a team as a code owner August 19, 2026 21:55
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-libc

Author: Michael Jones (michaelrj-google)

Changes

Adds the public sysinfo function and tests. The internal wrapper already
existed. I decided to avoid defining struct sysinfo since it's a
kernel struct defined in the UAPI headers. Can revisit in future if
necessary.

Assisted-by: Automated tooling, human reviewed.


Full diff: https://github.com/llvm/llvm-project/pull/217471.diff

26 Files Affected:

  • (modified) libc/config/linux/aarch64/entrypoints.txt (+3)
  • (modified) libc/config/linux/aarch64/headers.txt (+1)
  • (modified) libc/config/linux/arm/entrypoints.txt (+3)
  • (modified) libc/config/linux/arm/headers.txt (+1)
  • (modified) libc/config/linux/riscv/entrypoints.txt (+3)
  • (modified) libc/config/linux/riscv/headers.txt (+1)
  • (modified) libc/config/linux/x86_64/entrypoints.txt (+3)
  • (modified) libc/config/linux/x86_64/headers.txt (+1)
  • (modified) libc/hdr/types/CMakeLists.txt (+9)
  • (added) libc/hdr/types/struct_sysinfo.h (+27)
  • (modified) libc/include/CMakeLists.txt (+9)
  • (modified) libc/include/llvm-libc-types/CMakeLists.txt (+1)
  • (added) libc/include/llvm-libc-types/struct_sysinfo.h (+22)
  • (added) libc/include/sys/sysinfo.yaml (+12)
  • (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+1)
  • (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h (+3-3)
  • (modified) libc/src/sys/CMakeLists.txt (+1)
  • (added) libc/src/sys/sysinfo/CMakeLists.txt (+10)
  • (added) libc/src/sys/sysinfo/linux/CMakeLists.txt (+11)
  • (added) libc/src/sys/sysinfo/linux/sysinfo.cpp (+33)
  • (added) libc/src/sys/sysinfo/sysinfo.h (+26)
  • (modified) libc/src/unistd/linux/CMakeLists.txt (+1)
  • (modified) libc/src/unistd/linux/sysconf.cpp (+1-1)
  • (modified) libc/test/src/sys/CMakeLists.txt (+1)
  • (added) libc/test/src/sys/sysinfo/CMakeLists.txt (+17)
  • (added) libc/test/src/sys/sysinfo/sysinfo_test.cpp (+51)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 3af5943717e84..1a1b09c526192 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -349,6 +349,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.statvfs.fstatvfs
     libc.src.sys.statvfs.statvfs
 
+    # sys/sysinfo.h entrypoints
+    libc.src.sys.sysinfo.sysinfo
+
     # sys/utsname.h entrypoints
     libc.src.sys.utsname.uname
 
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index ccc6cfd38fd6e..b2346c568c986 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -65,6 +65,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.sys_stat
     libc.include.sys_statfs
     libc.include.sys_statvfs
+    libc.include.sys_sysinfo
     libc.include.sys_syscall
     libc.include.sys_sysmacros
     libc.include.sys_time
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index b6a24f4488089..124bc861a25d4 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -206,6 +206,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/prctl.h entrypoints
     libc.src.sys.prctl.prctl
 
+    # sys/sysinfo.h entrypoints
+    libc.src.sys.sysinfo.sysinfo
+
     # sys/personality.h entrypoints
     libc.src.sys.personality.personality
 
diff --git a/libc/config/linux/arm/headers.txt b/libc/config/linux/arm/headers.txt
index 6ec36bc7f3511..f8426f32a3bfc 100644
--- a/libc/config/linux/arm/headers.txt
+++ b/libc/config/linux/arm/headers.txt
@@ -28,6 +28,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.wctype
     libc.include.sys_param
     libc.include.sys_personality
+    libc.include.sys_sysinfo
     libc.include.sys_unistd
     # Disabled due to epoll_wait syscalls not being available on this platform.
     # libc.include.sys_epoll
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2a40f55076178..fad2d7ca08091 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -383,6 +383,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/time.h entrypoints
     libc.src.sys.time.utimes
 
+    # sys/sysinfo.h entrypoints
+    libc.src.sys.sysinfo.sysinfo
+
     # sys/utsname.h entrypoints
     libc.src.sys.utsname.uname
 
diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index 54ae65b6943eb..3677ecd2b657d 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -66,6 +66,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.sys_stat
     libc.include.sys_statfs
     libc.include.sys_statvfs
+    libc.include.sys_sysinfo
     libc.include.sys_syscall
     libc.include.sys_time
     libc.include.sys_types
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index cef595fc67465..2bfcc711fec15 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -388,6 +388,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     # sys/time.h entrypoints
     libc.src.sys.time.utimes
 
+    # sys/sysinfo.h entrypoints
+    libc.src.sys.sysinfo.sysinfo
+
     # sys/utsname.h entrypoints
     libc.src.sys.utsname.uname
 
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 475883a18165f..ee41a988d1d7f 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -68,6 +68,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.sys_stat
     libc.include.sys_statfs
     libc.include.sys_statvfs
+    libc.include.sys_sysinfo
     libc.include.sys_syscall
     libc.include.sys_sysmacros
     libc.include.sys_time
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 4983109aa82f8..e182f2dc71142 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -711,6 +711,15 @@ add_proxy_header_library(
     libc.include.netinet_udp
 )
 
+add_proxy_header_library(
+  struct_sysinfo
+  HDRS
+    struct_sysinfo.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.struct_sysinfo
+    libc.include.sys_sysinfo
+)
+
 add_proxy_header_library(
   struct_utsname
   HDRS
diff --git a/libc/hdr/types/struct_sysinfo.h b/libc/hdr/types/struct_sysinfo.h
new file mode 100644
index 0000000000000..bd77ec127f145
--- /dev/null
+++ b/libc/hdr/types/struct_sysinfo.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy for struct sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_SYSINFO_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_SYSINFO_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_sysinfo.h"
+
+#else // Overlay mode
+
+#include <sys/sysinfo.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_SYSINFO_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 91d601d50a711..dd7e0d4fdbbe5 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -956,6 +956,15 @@ add_header_macro(
     .llvm-libc-types.ushort
 )
 
+add_header_macro(
+  sys_sysinfo
+  ../libc/include/sys/sysinfo.yaml
+  sys/sysinfo.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-types.struct_sysinfo
+)
+
 add_header_macro(
   sys_utsname
   ../libc/include/sys/utsname.yaml
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 1c999bfd0b857..bb871f505a606 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -214,6 +214,7 @@ add_header(
 )
 add_header(struct_tm HDR struct_tm.h)
 add_header(struct_passwd HDR struct_passwd.h DEPENDS .uid_t .gid_t)
+add_header(struct_sysinfo HDR struct_sysinfo.h)
 add_header(struct_utsname HDR struct_utsname.h)
 add_header(thrd_start_t HDR thrd_start_t.h)
 add_header(thrd_t HDR thrd_t.h DEPENDS .__thread_type)
diff --git a/libc/include/llvm-libc-types/struct_sysinfo.h b/libc/include/llvm-libc-types/struct_sysinfo.h
new file mode 100644
index 0000000000000..408d2f705883e
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_sysinfo.h
@@ -0,0 +1,22 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of struct sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_SYSINFO_H
+#define LLVM_LIBC_TYPES_STRUCT_SYSINFO_H
+
+// Kernel struct defined in the UAPI headers. Include it instead of defining it
+// ourselves.
+
+#include <linux/sysinfo.h>
+
+#endif // LLVM_LIBC_TYPES_STRUCT_SYSINFO_H
diff --git a/libc/include/sys/sysinfo.yaml b/libc/include/sys/sysinfo.yaml
new file mode 100644
index 0000000000000..fb621c4d88195
--- /dev/null
+++ b/libc/include/sys/sysinfo.yaml
@@ -0,0 +1,12 @@
+header: sys/sysinfo.h
+standards:
+  - linux
+types:
+  - type_name: struct_sysinfo
+functions:
+  - name: sysinfo
+    standards:
+      - linux
+    return_type: int
+    arguments:
+      - type: struct sysinfo *
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 997e199460ab7..7d536e4b91abb 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -894,6 +894,7 @@ add_header_library(
   HDRS
     sysinfo.h
   DEPENDS
+    libc.hdr.types.struct_sysinfo
     libc.src.__support.OSUtil.osutil
     libc.src.__support.common
     libc.src.__support.error_or
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h
index 7487d05353c5b..ef2b7a3f582e5 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h
@@ -14,17 +14,17 @@
 #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYSINFO_H
 #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYSINFO_H
 
+#include "hdr/types/struct_sysinfo.h"
 #include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
-#include <linux/sysinfo.h> // For struct sysinfo
-#include <sys/syscall.h>   // For syscall numbers
+#include <sys/syscall.h> // For syscall numbers
 
 namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
-LIBC_INLINE ErrorOr<int> sysinfo(struct ::sysinfo *info) {
+LIBC_INLINE ErrorOr<int> sysinfo(struct sysinfo *info) {
   return syscall_checked<int>(SYS_sysinfo, info);
 }
 
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 5917b91b33eaa..4249f72b4d78a 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -20,3 +20,4 @@ add_subdirectory(ptrace)
 add_subdirectory(uio)
 add_subdirectory(ioctl)
 add_subdirectory(sysmacros)
+add_subdirectory(sysinfo)
diff --git a/libc/src/sys/sysinfo/CMakeLists.txt b/libc/src/sys/sysinfo/CMakeLists.txt
new file mode 100644
index 0000000000000..eb19566892ecc
--- /dev/null
+++ b/libc/src/sys/sysinfo/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  sysinfo
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.sysinfo
+)
diff --git a/libc/src/sys/sysinfo/linux/CMakeLists.txt b/libc/src/sys/sysinfo/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..c29252482cc90
--- /dev/null
+++ b/libc/src/sys/sysinfo/linux/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_entrypoint_object(
+  sysinfo
+  SRCS
+    sysinfo.cpp
+  HDRS
+    ../sysinfo.h
+  DEPENDS
+    libc.hdr.types.struct_sysinfo
+    libc.src.__support.OSUtil.linux.syscall_wrappers.sysinfo
+    libc.src.errno.errno
+)
diff --git a/libc/src/sys/sysinfo/linux/sysinfo.cpp b/libc/src/sys/sysinfo/linux/sysinfo.cpp
new file mode 100644
index 0000000000000..1b9504e99f1a5
--- /dev/null
+++ b/libc/src/sys/sysinfo/linux/sysinfo.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/sysinfo/sysinfo.h"
+
+#include "hdr/types/struct_sysinfo.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/sysinfo.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, sysinfo, (struct sysinfo * info)) {
+  auto result = linux_syscalls::sysinfo(info);
+  if (!result) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/sysinfo/sysinfo.h b/libc/src/sys/sysinfo/sysinfo.h
new file mode 100644
index 0000000000000..154243823a3e3
--- /dev/null
+++ b/libc/src/sys/sysinfo/sysinfo.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_SYSINFO_SYSINFO_H
+#define LLVM_LIBC_SRC_SYS_SYSINFO_SYSINFO_H
+
+#include "hdr/types/struct_sysinfo.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int sysinfo(struct sysinfo *info);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_SYSINFO_SYSINFO_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 7a8b51e9514aa..a04c43ce784d5 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -606,6 +606,7 @@ add_entrypoint_object(
     libc.hdr.sys_auxv_macros
     libc.hdr.sys_resource_macros
     libc.hdr.types.struct_rlimit
+    libc.hdr.types.struct_sysinfo
     libc.src.__support.libc_errno
     libc.src.__support.macros.config
     libc.src.__support.OSUtil.linux.sysinfo
diff --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
index 4785a8bac478d..7382e513d9c9f 100644
--- a/libc/src/unistd/linux/sysconf.cpp
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -18,6 +18,7 @@
 #include "hdr/sys_auxv_macros.h"
 #include "hdr/sys_resource_macros.h"
 #include "hdr/types/struct_rlimit.h"
+#include "hdr/types/struct_sysinfo.h"
 #include "hdr/unistd_macros.h"
 #include "src/__support/OSUtil/linux/auxv.h"
 #include "src/__support/OSUtil/linux/syscall_wrappers/prlimit.h"
@@ -26,7 +27,6 @@
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 #include <linux/limits.h>
-#include <linux/sysinfo.h>
 
 // In overlay mode, system headers (like glibc's <bits/local_lim.h>) may
 // explicitly undefine ARG_MAX to indicate it is dynamic. We define a fallback
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 75b733d1c525c..17bf10f2b3d07 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -19,3 +19,4 @@ add_subdirectory(time)
 add_subdirectory(ioctl)
 add_subdirectory(sem)
 add_subdirectory(sysmacros)
+add_subdirectory(sysinfo)
diff --git a/libc/test/src/sys/sysinfo/CMakeLists.txt b/libc/test/src/sys/sysinfo/CMakeLists.txt
new file mode 100644
index 0000000000000..e975f58a7505e
--- /dev/null
+++ b/libc/test/src/sys/sysinfo/CMakeLists.txt
@@ -0,0 +1,17 @@
+add_custom_target(libc_sys_sysinfo_unittests)
+
+add_libc_test(
+  sysinfo_test
+  SUITE
+    libc_sys_sysinfo_unittests
+  SRCS
+    sysinfo_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.types.struct_sysinfo
+    libc.include.sys_sysinfo
+    libc.src.errno.errno
+    libc.src.__support.common
+    libc.src.sys.sysinfo.sysinfo
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/sysinfo/sysinfo_test.cpp b/libc/test/src/sys/sysinfo/sysinfo_test.cpp
new file mode 100644
index 0000000000000..1aa2482397b43
--- /dev/null
+++ b/libc/test/src/sys/sysinfo/sysinfo_test.cpp
@@ -0,0 +1,51 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for sysinfo.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/struct_sysinfo.h"
+#include "src/sys/sysinfo/sysinfo.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST(LlvmLibcSysinfoTest, ValidBuffer) {
+  struct sysinfo info;
+  ASSERT_THAT(LIBC_NAMESPACE::sysinfo(&info), Succeeds(0));
+
+  EXPECT_GT(info.uptime, 0l);
+  EXPECT_GE(info.loads[0], 0ul);
+  EXPECT_GE(info.loads[1], 0ul);
+  EXPECT_GE(info.loads[2], 0ul);
+  EXPECT_GT(info.totalram, 0ul);
+  EXPECT_GT(info.freeram, 0ul);
+  EXPECT_LE(info.freeram, info.totalram);
+  EXPECT_GE(info.sharedram, 0ul);
+  EXPECT_LE(info.sharedram, info.totalram);
+  EXPECT_GE(info.bufferram, 0ul);
+  EXPECT_LE(info.bufferram, info.totalram);
+  EXPECT_GE(info.totalswap, 0ul);
+  EXPECT_GE(info.freeswap, 0ul);
+  EXPECT_LE(info.freeswap, info.totalswap);
+  EXPECT_GT(info.procs, static_cast<unsigned short>(0));
+  EXPECT_GE(info.pad, static_cast<unsigned short>(0));
+  EXPECT_GE(info.totalhigh, 0ul);
+  EXPECT_GE(info.freehigh, 0ul);
+  EXPECT_LE(info.freehigh, info.totalhigh);
+  EXPECT_GT(info.mem_unit, 0u);
+}
+
+TEST(LlvmLibcSysinfoTest, NullptrBuffer) {
+  EXPECT_THAT(LIBC_NAMESPACE::sysinfo(nullptr), Fails(EFAULT));
+}

@vonosmas vonosmas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with minor notes

// Kernel struct defined in the UAPI headers. Include it instead of defining it
// ourselves.

#include <linux/sysinfo.h>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be in linux/ subdirectory under llvm-libc-types? That's what we do for another kernel-specific type: loff_t

Comment thread libc/include/sys/sysinfo.yaml Outdated
- type_name: struct_sysinfo
functions:
- name: sysinfo
standards:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be omitted if the entire header is linux-specific?

Comment on lines +31 to +46
EXPECT_GT(info.totalram, 0ul);
EXPECT_GT(info.freeram, 0ul);
EXPECT_LE(info.freeram, info.totalram);
EXPECT_GE(info.sharedram, 0ul);
EXPECT_LE(info.sharedram, info.totalram);
EXPECT_GE(info.bufferram, 0ul);
EXPECT_LE(info.bufferram, info.totalram);
EXPECT_GE(info.totalswap, 0ul);
EXPECT_GE(info.freeswap, 0ul);
EXPECT_LE(info.freeswap, info.totalswap);
EXPECT_GT(info.procs, static_cast<unsigned short>(0));
EXPECT_GE(info.pad, static_cast<unsigned short>(0));
EXPECT_GE(info.totalhigh, 0ul);
EXPECT_GE(info.freehigh, 0ul);
EXPECT_LE(info.freehigh, info.totalhigh);
EXPECT_GT(info.mem_unit, 0u);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly worried of potential flakiness for these, though the checks themselves are fine. We probably don't want to test kernel behavior here, so maybe only a handful of sanity checks would be enough?

)
add_header(struct_tm HDR struct_tm.h)
add_header(struct_passwd HDR struct_passwd.h DEPENDS .uid_t .gid_t)
if(TARGET libc.include.llvm-libc-types.${LIBC_TARGET_OS}.struct_sysinfo)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but jus' saying that we might want to add add_type_header here that would populate the OS-specific dependencies automatically, like add_macro_header does.

(in a separate, PR of course)

@michaelrj-google
michaelrj-google merged commit 75d5d50 into llvm:main Aug 21, 2026
45 checks passed
@michaelrj-google
michaelrj-google deleted the libcSysinfo branch August 21, 2026 20:17
vonosmas pushed a commit that referenced this pull request Aug 21, 2026
This fixes 75d5d50 (#217471).

Buildkite error link:
https://buildkite.com/llvm-project/upstream-bazel/builds?commit=75d5d50255a809604d1149ff377957ed9311ceb3

Co-authored-by: Google Bazel Bot <google-bazel-bot@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants