Skip to content

Commit bf49b30

Browse files
boomanaiden154krishna2803
authored andcommitted
[libc] Add implementation of getcpu syscall wrapper (llvm#150871)
This patch adds the getcpu syscall wrapper. This has been supported in glibc since v2.29.
1 parent 23ca0bc commit bf49b30

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ set(TARGET_LIBC_ENTRYPOINTS
3636
libc.src.poll.poll
3737

3838
# sched.h entrypoints
39+
libc.src.sched.getcpu
3940
libc.src.sched.sched_get_priority_max
4041
libc.src.sched.sched_get_priority_min
4142
libc.src.sched.sched_getaffinity

libc/include/sched.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ functions:
1818
arguments:
1919
- type: size_t
2020
- type: const cpu_set_t *
21+
- name: getcpu
22+
standards:
23+
- POSIX
24+
return_type: int
25+
arguments:
26+
- type: unsigned int *
27+
- type: unsigned int *
2128
- name: sched_get_priority_max
2229
standards:
2330
- POSIX

libc/src/sched/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
22
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
33
endif()
44

5+
add_entrypoint_object(
6+
getcpu
7+
ALIAS
8+
DEPENDS
9+
.${LIBC_TARGET_OS}.getcpu
10+
)
11+
512
add_entrypoint_object(
613
sched_getaffinity
714
ALIAS

libc/src/sched/getcpu.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for getcpu ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_SCHED_GETCPU_H
10+
#define LLVM_LIBC_SRC_SCHED_GETCPU_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
int getcpu(unsigned int *cpu, unsigned int *node);
17+
18+
} // namespace LIBC_NAMESPACE_DECL
19+
20+
#endif // LLVM_LIBC_SRC_SCHED_GETCPU_H

libc/src/sched/linux/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
add_entrypoint_object(
2+
getcpu
3+
SRCS
4+
getcpu.cpp
5+
HDRS
6+
../getcpu.h
7+
DEPENDS
8+
libc.include.sched
9+
libc.src.__support.OSUtil.osutil
10+
libc.src.errno.errno
11+
)
12+
113
add_entrypoint_object(
214
sched_getaffinity
315
SRCS

libc/src/sched/linux/getcpu.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Implementation of getcpu ------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/sched/getcpu.h"
10+
11+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12+
#include "src/__support/common.h"
13+
#include "src/__support/libc_errno.h"
14+
#include "src/__support/macros/config.h"
15+
16+
#include <sys/syscall.h> // For syscall numbers.
17+
18+
namespace LIBC_NAMESPACE_DECL {
19+
20+
LLVM_LIBC_FUNCTION(int, getcpu, (unsigned int *cpu, unsigned int *node)) {
21+
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_getcpu, cpu, node, nullptr);
22+
if (ret < 0) {
23+
libc_errno = -ret;
24+
return -1;
25+
}
26+
return 0;
27+
}
28+
29+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/sched/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ add_libc_unittest(
4040
libc.src.sched.sched_get_priority_max
4141
)
4242

43+
add_libc_unittest(
44+
getcpu_test
45+
SUITE
46+
libc_sched_unittests
47+
SRCS
48+
getcpu_test.cpp
49+
DEPENDS
50+
libc.include.sched
51+
libc.src.errno.errno
52+
libc.src.sched.getcpu
53+
libc.test.UnitTest.ErrnoCheckingTest
54+
)
55+
4356
add_libc_unittest(
4457
scheduler_test
4558
SUITE

libc/test/src/sched/getcpu_test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Unittests for getcpu ----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/OSUtil/syscall.h"
10+
#include "src/__support/libc_errno.h"
11+
#include "src/sched/getcpu.h"
12+
#include "test/UnitTest/ErrnoCheckingTest.h"
13+
#include "test/UnitTest/ErrnoSetterMatcher.h"
14+
15+
using LlvmLibcSchedGetCpuTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
16+
17+
TEST_F(LlvmLibcSchedGetCpuTest, SmokeTest) {
18+
unsigned int current_cpu;
19+
unsigned int current_node;
20+
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
21+
ASSERT_THAT(LIBC_NAMESPACE::getcpu(&current_cpu, &current_node), Succeeds(0));
22+
}
23+
24+
TEST_F(LlvmLibcSchedGetCpuTest, BadPointer) {
25+
unsigned int current_cpu;
26+
unsigned int *current_node = reinterpret_cast<unsigned int *>(-1);
27+
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
28+
ASSERT_THAT(LIBC_NAMESPACE::getcpu(&current_cpu, current_node),
29+
Fails(EFAULT));
30+
}

0 commit comments

Comments
 (0)