-
Notifications
You must be signed in to change notification settings - Fork 18.3k
[libc] Implement sysinfo #217471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc] Implement sysinfo #217471
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| header: sys/sysinfo.h | ||
| standards: | ||
| - linux | ||
| types: | ||
| - type_name: struct_sysinfo | ||
| functions: | ||
| - name: sysinfo | ||
| standards: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| - linux | ||
| return_type: int | ||
| arguments: | ||
| - type: struct sysinfo * | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
|
Comment on lines
+28
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
| } | ||
|
|
||
| TEST(LlvmLibcSysinfoTest, NullptrBuffer) { | ||
| EXPECT_THAT(LIBC_NAMESPACE::sysinfo(nullptr), Fails(EFAULT)); | ||
| } | ||
There was a problem hiding this comment.
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