[libc-commits] [libc] [libc] Implement getrusage and corresponding unit tests. (PR #217684)
via libc-commits
libc-commits at lists.llvm.org
Thu Aug 20 10:05:14 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Vadim Kotov (vadimkotov)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/217684.diff
11 Files Affected:
- (modified) libc/config/linux/x86_64/entrypoints.txt (+1)
- (modified) libc/include/llvm-libc-macros/linux/sys-resource-macros.h (+4)
- (modified) libc/include/sys/resource.yaml (+8)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+14)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/getrusage.h (+37)
- (modified) libc/src/sys/resource/CMakeLists.txt (+7)
- (added) libc/src/sys/resource/getrusage.h (+26)
- (modified) libc/src/sys/resource/linux/CMakeLists.txt (+14)
- (added) libc/src/sys/resource/linux/getrusage.cpp (+33)
- (modified) libc/test/src/sys/resource/CMakeLists.txt (+13)
- (added) libc/test/src/sys/resource/getrusage_test.cpp (+38)
``````````diff
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7ffb4f8d706f0..e5a332118a7f9 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -330,6 +330,7 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/resource.h entrypoints
libc.src.sys.resource.getrlimit
+ libc.src.sys.resource.getrusage
libc.src.sys.resource.setrlimit
# sys/sem.h entrypoints
diff --git a/libc/include/llvm-libc-macros/linux/sys-resource-macros.h b/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
index 419761e01c453..9f1e04a339c73 100644
--- a/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
@@ -30,4 +30,8 @@
#define RLIM_INFINITY ((rlim_t) - 1)
+#define RUSAGE_SELF 0
+#define RUSAGE_CHILDREN (-1)
+#define RUSAGE_THREAD 1
+
#endif // LLVM_LIBC_MACROS_LINUX_SYS_RESOURCE_MACROS_H
diff --git a/libc/include/sys/resource.yaml b/libc/include/sys/resource.yaml
index faf263d3d58e9..53e9a9dc27478 100644
--- a/libc/include/sys/resource.yaml
+++ b/libc/include/sys/resource.yaml
@@ -17,6 +17,14 @@ functions:
arguments:
- type: int
- type: struct rlimit *
+ - name: getrusage
+ standards:
+ # GNU because we support RUSAGE_THREAD, which is not in POSIX.
+ - gnu
+ return_type: int
+ arguments:
+ - type: int
+ - type: struct rusage *
- name: setrlimit
standards:
- posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 2b22bb8e733de..2756d9cf76943 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -940,3 +940,17 @@ add_header_library(
libc.src.__support.macros.config
libc.include.sys_syscall
)
+
+add_header_library(
+ getrusage
+ HDRS
+ getrusage.h
+ DEPENDS
+ libc.hdr.types.struct_timespec
+ libc.hdr.time_macros
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getrusage.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getrusage.h
new file mode 100644
index 0000000000000..2a4b1b0887969
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getrusage.h
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getrusage
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETRUSAGE_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETRUSAGE_H
+
+#include "hdr/types/struct_rusage.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 <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+// The wrapper returns an int (casting from `long` returned by the syscall)
+// because its libc counterpart returns an int and the set of possible return
+// values fits: 0, -EFAULT (-14) or -EINVAL (-22).
+LIBC_INLINE ErrorOr<int> getrusage(int who, struct rusage *ru) {
+ return syscall_checked<int>(SYS_getrusage, who, ru);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETRUSAGE_H
diff --git a/libc/src/sys/resource/CMakeLists.txt b/libc/src/sys/resource/CMakeLists.txt
index 8319b6399650c..ac5f676588994 100644
--- a/libc/src/sys/resource/CMakeLists.txt
+++ b/libc/src/sys/resource/CMakeLists.txt
@@ -9,6 +9,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.getrlimit
)
+add_entrypoint_object(
+ getrusage
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.getrusage
+)
+
add_entrypoint_object(
setrlimit
ALIAS
diff --git a/libc/src/sys/resource/getrusage.h b/libc/src/sys/resource/getrusage.h
new file mode 100644
index 0000000000000..40c13948e32a0
--- /dev/null
+++ b/libc/src/sys/resource/getrusage.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 of getrusage.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LVM_LIBC_SRC_SYS_RESOURCE_GETRUSAGE_H
+#define LVM_LIBC_SRC_SYS_RESOURCE_GETRUSAGE_H
+
+#include "hdr/types/struct_rusage.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int getrusage(int who, struct rusage *usage);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_RESOURCE_GETRUSAGE_H
diff --git a/libc/src/sys/resource/linux/CMakeLists.txt b/libc/src/sys/resource/linux/CMakeLists.txt
index 3d2aa3888887c..2a7d96c2eb365 100644
--- a/libc/src/sys/resource/linux/CMakeLists.txt
+++ b/libc/src/sys/resource/linux/CMakeLists.txt
@@ -10,6 +10,20 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ getrusage
+ SRCS
+ getrusage.cpp
+ HDRS
+ ../getrusage.h
+ DEPENDS
+ libc.hdr.types.struct_rusage
+ libc.src.__support.OSUtil.linux.syscall_wrappers.getrusage
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
setrlimit
SRCS
diff --git a/libc/src/sys/resource/linux/getrusage.cpp b/libc/src/sys/resource/linux/getrusage.cpp
new file mode 100644
index 0000000000000..4851cd7a522f6
--- /dev/null
+++ b/libc/src/sys/resource/linux/getrusage.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 getrusage.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/resource/getrusage.h"
+
+#include "hdr/types/struct_rusage.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/getrusage.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, getrusage, (int who, struct rusage *usage)) {
+ auto result = linux_syscalls::getrusage(who, usage);
+ if (!result) {
+ libc_errno = result.error();
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/sys/resource/CMakeLists.txt b/libc/test/src/sys/resource/CMakeLists.txt
index 876d93703e794..18196eaf1865c 100644
--- a/libc/test/src/sys/resource/CMakeLists.txt
+++ b/libc/test/src/sys/resource/CMakeLists.txt
@@ -20,3 +20,16 @@ add_libc_test(
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
+
+add_libc_test(
+ getrusage_test
+ SUITE
+ libc_sys_resource_unittests
+ SRCS
+ getrusage_test.cpp
+ DEPENDS
+ libc.hdr.sys_resource_macros
+ libc.hdr.types.struct_rusage
+ libc.src.sys.resource.getrusage
+ libc.test.UnitTest.ErrnoCheckingTest
+)
diff --git a/libc/test/src/sys/resource/getrusage_test.cpp b/libc/test/src/sys/resource/getrusage_test.cpp
new file mode 100644
index 0000000000000..1a3a52c90c238
--- /dev/null
+++ b/libc/test/src/sys/resource/getrusage_test.cpp
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 getrusage.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/sys_resource_macros.h"
+#include "hdr/types/struct_rusage.h"
+#include "src/sys/resource/getrusage.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcGetrusageTest, BasicTest) {
+ struct rusage usage = {};
+ int res = LIBC_NAMESPACE::getrusage(RUSAGE_SELF, &usage);
+ EXPECT_EQ(res, 0);
+ ASSERT_ERRNO_SUCCESS();
+}
+
+TEST(LlvmLibcGetrusageTest, TestWhoInvalid) {
+ struct rusage usage = {};
+ int res = LIBC_NAMESPACE::getrusage(99, &usage);
+ EXPECT_EQ(res, -1);
+ ASSERT_ERRNO_EQ(EINVAL);
+}
+
+TEST(LlvmLibcGetrusageTest, TestUsageBadPointer) {
+ int res = LIBC_NAMESPACE::getrusage(RUSAGE_SELF, (struct rusage *)-1L);
+ EXPECT_EQ(res, -1);
+ ASSERT_ERRNO_EQ(EFAULT);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/217684
More information about the libc-commits
mailing list