Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.poll.poll

# sched.h entrypoints
libc.src.sched.getcpu
libc.src.sched.sched_get_priority_max
libc.src.sched.sched_get_priority_min
libc.src.sched.sched_getaffinity
Expand Down
7 changes: 7 additions & 0 deletions libc/include/sched.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ functions:
arguments:
- type: size_t
- type: const cpu_set_t *
- name: getcpu
standards:
- POSIX
return_type: int
arguments:
- type: unsigned int *
- type: unsigned int *
- name: sched_get_priority_max
standards:
- POSIX
Expand Down
7 changes: 7 additions & 0 deletions libc/src/sched/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
endif()

add_entrypoint_object(
getcpu
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.getcpu
)

add_entrypoint_object(
sched_getaffinity
ALIAS
Expand Down
20 changes: 20 additions & 0 deletions libc/src/sched/getcpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for getcpu ------------------------*- 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_SCHED_GETCPU_H
#define LLVM_LIBC_SRC_SCHED_GETCPU_H

#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

int getcpu(unsigned int *cpu, unsigned int *node);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_SCHED_GETCPU_H
12 changes: 12 additions & 0 deletions libc/src/sched/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
add_entrypoint_object(
getcpu
SRCS
getcpu.cpp
HDRS
../getcpu.h
DEPENDS
libc.include.sched
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
sched_getaffinity
SRCS
Expand Down
29 changes: 29 additions & 0 deletions libc/src/sched/linux/getcpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===-- Implementation of getcpu ------------------------------------------===//
//
// 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/getcpu.h"

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"

#include <sys/syscall.h> // For syscall numbers.

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, getcpu, (unsigned int *cpu, unsigned int *node)) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_getcpu, cpu, node);
Copy link
Contributor

Choose a reason for hiding this comment

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

the man page mentions that there's a third argument that should be null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, yep. Looks like it's helpful to actually read the whole thing...

if (ret < 0) {
libc_errno = -ret;
return -1;
}
return 0;
}

} // namespace LIBC_NAMESPACE_DECL
13 changes: 13 additions & 0 deletions libc/test/src/sched/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ add_libc_unittest(
libc.src.sched.sched_get_priority_max
)

add_libc_unittest(
getcpu_test
SUITE
libc_sched_unittests
SRCS
getcpu_test.cpp
DEPENDS
libc.include.sched
libc.src.errno.errno
libc.src.sched.getcpu
libc.test.UnitTest.ErrnoCheckingTest
)

add_libc_unittest(
scheduler_test
SUITE
Expand Down
30 changes: 30 additions & 0 deletions libc/test/src/sched/getcpu_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===-- Unittests for getcpu ----------------------------------------------===//
//
// 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/__support/libc_errno.h"
#include "src/sched/getcpu.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"

using LlvmLibcSchedGetCpuTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;

TEST_F(LlvmLibcSchedGetCpuTest, SmokeTest) {
unsigned int current_cpu;
unsigned int current_node;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
ASSERT_THAT(LIBC_NAMESPACE::getcpu(&current_cpu, &current_node), Succeeds(0));
}

TEST_F(LlvmLibcSchedGetCpuTest, BadPointer) {
unsigned int current_cpu;
unsigned int *current_node = reinterpret_cast<unsigned int *>(-1);
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
ASSERT_THAT(LIBC_NAMESPACE::getcpu(&current_cpu, current_node),
Fails(EFAULT));
}
Loading