Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
3 changes: 3 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,9 @@ if(LLVM_LIBC_FULL_BUILD)

# sched.h entrypoints
libc.src.sched.__sched_getcpucount
libc.src.sched.__sched_setcpuzero
libc.src.sched.__sched_setcpuset
libc.src.sched.__sched_getcpuisset

# strings.h entrypoints
libc.src.strings.strcasecmp_l
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,9 @@ if(LLVM_LIBC_FULL_BUILD)

# sched.h entrypoints
libc.src.sched.__sched_getcpucount
libc.src.sched.__sched_setcpuzero
libc.src.sched.__sched_setcpuset
libc.src.sched.__sched_getcpuisset

# setjmp.h entrypoints
libc.src.setjmp.longjmp
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,9 @@ if(LLVM_LIBC_FULL_BUILD)

# sched.h entrypoints
libc.src.sched.__sched_getcpucount
libc.src.sched.__sched_setcpuzero
libc.src.sched.__sched_setcpuset
libc.src.sched.__sched_getcpuisset

# setjmp.h entrypoints
libc.src.setjmp.longjmp
Expand Down
9 changes: 9 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ add_proxy_header_library(
libc.include.fenv
)

add_proxy_header_library(
sched_macros
HDRS
sched_macros.h
FULL_BUILD_DEPENDS
libc.include.sched
libc.include.llvm-libc-macros.sched_macros
)

add_proxy_header_library(
signal_macros
HDRS
Expand Down
22 changes: 22 additions & 0 deletions libc/hdr/sched_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Definition of macros from sched.h ---------------------------------===//
//
// 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_HDR_SCHED_MACROS_H
#define LLVM_LIBC_HDR_SCHED_MACROS_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-macros/sched-macros.h"

#else // Overlay mode

#include <sched.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_SCHED_MACROS_H
8 changes: 8 additions & 0 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,11 @@ add_proxy_header_library(
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_pollfd
)

add_proxy_header_library(
cpu_set_t
HDRS
cpu_set_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.cpu_set_t
)
22 changes: 22 additions & 0 deletions libc/hdr/types/cpu_set_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Proxy for cpu_set_t -----------------------------------------------===//
//
// 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_HDR_TYPES_CPU_SET_T_H
#define LLVM_LIBC_HDR_TYPES_CPU_SET_T_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/cpu_set_t.h"

#else // Overlay mode

#include <sched.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_CPU_SET_T_H
8 changes: 8 additions & 0 deletions libc/include/llvm-libc-macros/linux/sched-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@
#define SCHED_IDLE 5
#define SCHED_DEADLINE 6

#define CPU_SETSIZE __CPU_SETSIZE
#define NCPUBITS __NCPUBITS
#define CPU_COUNT_S(setsize, set) __sched_getcpucount(setsize, set)
#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)
#define CPU_ZERO_S(setsize, set) __sched_setcpuzero(setsize, set)
#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set)
#define CPU_SET_S(cpu, setsize, set) __sched_setcpuset(cpu, setsize, set)
#define CPU_SET(cpu, setsize, set) CPU_SET_S(cpu, sizeof(cpt_set_t), set)
#define CPU_ISSET_S(cpu, setsize, set) __sched_getcpuisset(cpu, setsize, set)
#define CPU_ISSET(cpu, setsize, set) CPU_ISSET_S(cpu, sizeof(cpt_set_t), set)

#endif // LLVM_LIBC_MACROS_LINUX_SCHED_MACROS_H
3 changes: 3 additions & 0 deletions libc/include/llvm-libc-types/cpu_set_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#ifndef LLVM_LIBC_TYPES_CPU_SET_T_H
#define LLVM_LIBC_TYPES_CPU_SET_T_H

#define __CPU_SETSIZE 1024
#define __NCPUBITS 8 * sizeof(unsigned long)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need parens?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in c94712f1


typedef struct {
// If a processor with more than 1024 CPUs is to be supported in future,
// we need to adjust the size of this array.
Expand Down
21 changes: 21 additions & 0 deletions libc/src/sched/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,24 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.__sched_getcpucount
)

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

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

add_entrypoint_object(
__sched_getcpuisset
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.__sched_getcpuisset
)
37 changes: 36 additions & 1 deletion libc/src/sched/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ add_entrypoint_object(
../sched_getcpucount.h
DEPENDS
libc.include.sched
)
)

add_entrypoint_object(
sched_yield
Expand Down Expand Up @@ -135,3 +135,38 @@ add_entrypoint_object(
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
__sched_setcpuzero
SRCS
sched_setcpuzero.cpp
HDRS
../sched_setcpuzero.h
DEPENDS
libc.hdr.types.cpu_set_t
libc.hdr.types.size_t
)

add_entrypoint_object(
__sched_setcpuset
SRCS
sched_setcpuset.cpp
HDRS
../sched_setcpuset.h
DEPENDS
libc.hdr.sched_macros
libc.hdr.types.cpu_set_t
libc.hdr.types.size_t
)

add_entrypoint_object(
__sched_getcpuisset
SRCS
sched_getcpuisset.cpp
HDRS
../sched_getcpuisset.h
DEPENDS
libc.hdr.sched_macros
libc.hdr.types.cpu_set_t
libc.hdr.types.size_t
)
33 changes: 33 additions & 0 deletions libc/src/sched/linux/sched_getcpuisset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===-- Implementation of sched_getcpuisset -------------------------------===//
//
// 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/sched_getcpuisset.h"

#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL

#include "hdr/sched_macros.h" // NCPUBITS
#include "hdr/types/cpu_set_t.h"
#include "hdr/types/size_t.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, __sched_getcpuisset,
(int cpu, const size_t cpuset_size, cpu_set_t *set)) {
if (static_cast<size_t>(cpu) / 8 < cpuset_size) {
const size_t element_index = static_cast<size_t>(cpu) / NCPUBITS;
const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS;

const unsigned long mask = 1UL << bit_position;
return (set->__mask[element_index] & mask) != 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

What does the standard say when these pointers are 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.

these macros are glibc extensions, the man page itself doesn't define a way to deal with null pointers. i found an implementation of those in glibc's code and surprisingly even that doesn't handle null pointers. ref:

#define __CPU_ISSET_S(cpu, setsize, cpusetp) \
  (__extension__							      \
   ({ size_t __cpu = (cpu);						      \
      __cpu / 8 < (setsize)						      \
      ? ((((const __cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)]	      \
	  & __CPUMASK (__cpu))) != 0					      \
      : 0; }))

for implementing these macros (CPU_ZERO et al), i looked at our existing implementation for CPU_COUNT ref:

LLVM_LIBC_FUNCTION(int, __sched_getcpucount,
(size_t cpuset_size, const cpu_set_t *mask)) {
int result = 0;
for (size_t i = 0; i < cpuset_size / sizeof(long); ++i) {
result += __builtin_popcountl(mask->__mask[i]);
}
return result;
}

and even that doesn't handle null pointers!


should i implement a nullptr guard for these?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, can you add null check with https://github.com/llvm/llvm-project/blob/main/libc/src/__support/macros/null_check.h for these functions in this PR? Thanks,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done! 522c6697

}

return 0;
}

} // namespace LIBC_NAMESPACE_DECL
31 changes: 31 additions & 0 deletions libc/src/sched/linux/sched_setcpuset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===-- Implementation of sched_setcpuset ---------------------------------===//
//
// 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/sched_setcpuset.h"

#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL

#include "hdr/sched_macros.h" // NCPUBITS
#include "hdr/types/cpu_set_t.h"
#include "hdr/types/size_t.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void, __sched_setcpuset,
(int cpu, const size_t cpuset_size, cpu_set_t *set)) {
if (static_cast<size_t>(cpu) / 8 < cpuset_size) {
const size_t element_index = static_cast<size_t>(cpu) / NCPUBITS;
const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS;

const unsigned long mask = 1UL << bit_position;
set->__mask[element_index] |= mask;
}
}

} // namespace LIBC_NAMESPACE_DECL
24 changes: 24 additions & 0 deletions libc/src/sched/linux/sched_setcpuzero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Implementation of sched_setcpuzero --------------------------------===//
//
// 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/sched_setcpuzero.h"

#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL

#include "hdr/types/cpu_set_t.h"
#include "hdr/types/size_t.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(void, __sched_setcpuzero,
(const size_t cpuset_size, cpu_set_t *set)) {
__builtin_memset(set, 0, cpuset_size);
}

} // namespace LIBC_NAMESPACE_DECL
24 changes: 24 additions & 0 deletions libc/src/sched/sched_getcpuisset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Implementation header for sched_getcpuisset -------------*- 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_SCHED_GETCPUISSET_H
#define LLVM_LIBC_SRC_SCHED_SCHED_GETCPUISSET_H

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

#include "hdr/types/cpu_set_t.h"
#include "hdr/types/size_t.h"

namespace LIBC_NAMESPACE_DECL {

// for internal use in the CPU_ISSET macro
int __sched_getcpuisset(int cpu, const size_t cpuset_size, cpu_set_t *set);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_SCHED_SCHED_GETCPUISSET_H
24 changes: 24 additions & 0 deletions libc/src/sched/sched_setcpuset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Implementation header for sched_setcpuset ---------------*- 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_SCHED_SETCPUSET_H
#define LLVM_LIBC_SRC_SCHED_SCHED_SETCPUSET_H

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

#include "hdr/types/cpu_set_t.h"
#include "hdr/types/size_t.h"

namespace LIBC_NAMESPACE_DECL {

// for internal use in the CPU_SET macro
void __sched_setcpuset(int cpu, const size_t cpuset_size, cpu_set_t *set);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_SCHED_SCHED_SETCPUSET_H
24 changes: 24 additions & 0 deletions libc/src/sched/sched_setcpuzero.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===-- Implementation header for sched_setcpuzero --------------*- 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_SCHED_SETCPUZERO_H
#define LLVM_LIBC_SRC_SCHED_SCHED_SETCPUZERO_H

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

#include "hdr/types/cpu_set_t.h"
#include "hdr/types/size_t.h"

namespace LIBC_NAMESPACE_DECL {

// for internal use in the CPU_ZERO macro
void __sched_setcpuzero(const size_t cpuset_size, cpu_set_t *set);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_SCHED_SCHED_SETCPUZERO_H
Loading