Skip to content

Commit bda87e0

Browse files
authored
[libc][sched] Implement CPU_ZERO, CPU_ISSET, CPU_SET macros (llvm#131524)
This PR implements the following macros for `sched.h`: - `CPU_ZERO` - `CPU_ISSET` - `CPU_SET` Fixes llvm#124642 --------- Signed-off-by: krishna2803 <[email protected]>
1 parent 0260bcb commit bda87e0

17 files changed

+314
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

+3
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,9 @@ if(LLVM_LIBC_FULL_BUILD)
910910

911911
# sched.h entrypoints
912912
libc.src.sched.__sched_getcpucount
913+
libc.src.sched.__sched_setcpuzero
914+
libc.src.sched.__sched_setcpuset
915+
libc.src.sched.__sched_getcpuisset
913916

914917
# strings.h entrypoints
915918
libc.src.strings.strcasecmp_l

libc/config/linux/riscv/entrypoints.txt

+3
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,9 @@ if(LLVM_LIBC_FULL_BUILD)
858858

859859
# sched.h entrypoints
860860
libc.src.sched.__sched_getcpucount
861+
libc.src.sched.__sched_setcpuzero
862+
libc.src.sched.__sched_setcpuset
863+
libc.src.sched.__sched_getcpuisset
861864

862865
# setjmp.h entrypoints
863866
libc.src.setjmp.longjmp

libc/config/linux/x86_64/entrypoints.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,9 @@ if(LLVM_LIBC_FULL_BUILD)
10281028

10291029
# sched.h entrypoints
10301030
libc.src.sched.__sched_getcpucount
1031+
libc.src.sched.__sched_setcpuzero
1032+
libc.src.sched.__sched_setcpuset
1033+
libc.src.sched.__sched_getcpuisset
10311034

10321035
# setjmp.h entrypoints
10331036
libc.src.setjmp.longjmp

libc/hdr/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ add_proxy_header_library(
7272
libc.include.fenv
7373
)
7474

75+
add_proxy_header_library(
76+
sched_macros
77+
HDRS
78+
sched_macros.h
79+
FULL_BUILD_DEPENDS
80+
libc.include.sched
81+
libc.include.llvm-libc-macros.sched_macros
82+
)
83+
7584
add_proxy_header_library(
7685
signal_macros
7786
HDRS

libc/hdr/sched_macros.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Definition of macros from sched.h ---------------------------------===//
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_HDR_SCHED_MACROS_H
10+
#define LLVM_LIBC_HDR_SCHED_MACROS_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-macros/sched-macros.h"
15+
16+
#else // Overlay mode
17+
18+
#include <sched.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_SCHED_MACROS_H

libc/hdr/types/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,11 @@ add_proxy_header_library(
357357
FULL_BUILD_DEPENDS
358358
libc.include.llvm-libc-types.struct_pollfd
359359
)
360+
361+
add_proxy_header_library(
362+
cpu_set_t
363+
HDRS
364+
cpu_set_t.h
365+
FULL_BUILD_DEPENDS
366+
libc.include.llvm-libc-types.cpu_set_t
367+
)

libc/hdr/types/cpu_set_t.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Proxy for cpu_set_t -----------------------------------------------===//
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_HDR_TYPES_CPU_SET_T_H
10+
#define LLVM_LIBC_HDR_TYPES_CPU_SET_T_H
11+
12+
#ifdef LIBC_FULL_BUILD
13+
14+
#include "include/llvm-libc-types/cpu_set_t.h"
15+
16+
#else // Overlay mode
17+
18+
#include <sched.h>
19+
20+
#endif // LLVM_LIBC_FULL_BUILD
21+
22+
#endif // LLVM_LIBC_HDR_TYPES_CPU_SET_T_H

libc/include/llvm-libc-macros/linux/sched-macros.h

+8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@
2323
#define SCHED_IDLE 5
2424
#define SCHED_DEADLINE 6
2525

26+
#define CPU_SETSIZE __CPU_SETSIZE
27+
#define NCPUBITS __NCPUBITS
2628
#define CPU_COUNT_S(setsize, set) __sched_getcpucount(setsize, set)
2729
#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set)
30+
#define CPU_ZERO_S(setsize, set) __sched_setcpuzero(setsize, set)
31+
#define CPU_ZERO(set) CPU_ZERO_S(sizeof(cpu_set_t), set)
32+
#define CPU_SET_S(cpu, setsize, set) __sched_setcpuset(cpu, setsize, set)
33+
#define CPU_SET(cpu, setsize, set) CPU_SET_S(cpu, sizeof(cpt_set_t), set)
34+
#define CPU_ISSET_S(cpu, setsize, set) __sched_getcpuisset(cpu, setsize, set)
35+
#define CPU_ISSET(cpu, setsize, set) CPU_ISSET_S(cpu, sizeof(cpt_set_t), set)
2836

2937
#endif // LLVM_LIBC_MACROS_LINUX_SCHED_MACROS_H

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

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#ifndef LLVM_LIBC_TYPES_CPU_SET_T_H
1010
#define LLVM_LIBC_TYPES_CPU_SET_T_H
1111

12+
#define __CPU_SETSIZE 1024
13+
#define __NCPUBITS (8 * sizeof(unsigned long))
14+
1215
typedef struct {
1316
// If a processor with more than 1024 CPUs is to be supported in future,
1417
// we need to adjust the size of this array.

libc/src/sched/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,24 @@ add_entrypoint_object(
7878
DEPENDS
7979
.${LIBC_TARGET_OS}.__sched_getcpucount
8080
)
81+
82+
add_entrypoint_object(
83+
__sched_setcpuzero
84+
ALIAS
85+
DEPENDS
86+
.${LIBC_TARGET_OS}.__sched_setcpuzero
87+
)
88+
89+
add_entrypoint_object(
90+
__sched_setcpuset
91+
ALIAS
92+
DEPENDS
93+
.${LIBC_TARGET_OS}.__sched_setcpuset
94+
)
95+
96+
add_entrypoint_object(
97+
__sched_getcpuisset
98+
ALIAS
99+
DEPENDS
100+
.${LIBC_TARGET_OS}.__sched_getcpuisset
101+
)

libc/src/sched/linux/CMakeLists.txt

+45-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ add_entrypoint_object(
3030
../sched_getcpucount.h
3131
DEPENDS
3232
libc.include.sched
33-
)
33+
)
3434

3535
add_entrypoint_object(
3636
sched_yield
@@ -135,3 +135,47 @@ add_entrypoint_object(
135135
libc.src.__support.OSUtil.osutil
136136
libc.src.errno.errno
137137
)
138+
139+
add_entrypoint_object(
140+
__sched_setcpuzero
141+
SRCS
142+
sched_setcpuzero.cpp
143+
HDRS
144+
../sched_setcpuzero.h
145+
DEPENDS
146+
libc.src.__support.common
147+
libc.src.__support.macros.config
148+
libc.src.__support.macros.null_check
149+
libc.hdr.types.cpu_set_t
150+
libc.hdr.types.size_t
151+
)
152+
153+
add_entrypoint_object(
154+
__sched_setcpuset
155+
SRCS
156+
sched_setcpuset.cpp
157+
HDRS
158+
../sched_setcpuset.h
159+
DEPENDS
160+
libc.src.__support.common
161+
libc.src.__support.macros.config
162+
libc.src.__support.macros.null_check
163+
libc.hdr.sched_macros
164+
libc.hdr.types.cpu_set_t
165+
libc.hdr.types.size_t
166+
)
167+
168+
add_entrypoint_object(
169+
__sched_getcpuisset
170+
SRCS
171+
sched_getcpuisset.cpp
172+
HDRS
173+
../sched_getcpuisset.h
174+
DEPENDS
175+
libc.src.__support.common
176+
libc.src.__support.macros.config
177+
libc.src.__support.macros.null_check
178+
libc.hdr.sched_macros
179+
libc.hdr.types.cpu_set_t
180+
libc.hdr.types.size_t
181+
)
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===-- Implementation of sched_getcpuisset -------------------------------===//
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/sched_getcpuisset.h"
10+
11+
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
12+
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
13+
#include "src/__support/macros/null_check.h" // LIBC_CRASH_ON_NULLPTR
14+
15+
#include "hdr/sched_macros.h" // NCPUBITS
16+
#include "hdr/types/cpu_set_t.h"
17+
#include "hdr/types/size_t.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
LLVM_LIBC_FUNCTION(int, __sched_getcpuisset,
22+
(int cpu, const size_t cpuset_size, cpu_set_t *set)) {
23+
LIBC_CRASH_ON_NULLPTR(set);
24+
25+
if (static_cast<size_t>(cpu) / 8 < cpuset_size) {
26+
const size_t element_index = static_cast<size_t>(cpu) / NCPUBITS;
27+
const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS;
28+
29+
const unsigned long mask = 1UL << bit_position;
30+
return (set->__mask[element_index] & mask) != 0;
31+
}
32+
33+
return 0;
34+
}
35+
36+
} // namespace LIBC_NAMESPACE_DECL
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- Implementation of sched_setcpuset ---------------------------------===//
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/sched_setcpuset.h"
10+
11+
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
12+
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
13+
#include "src/__support/macros/null_check.h" // LIBC_CRASH_ON_NULLPTR
14+
15+
#include "hdr/sched_macros.h" // NCPUBITS
16+
#include "hdr/types/cpu_set_t.h"
17+
#include "hdr/types/size_t.h"
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
21+
LLVM_LIBC_FUNCTION(void, __sched_setcpuset,
22+
(int cpu, const size_t cpuset_size, cpu_set_t *set)) {
23+
LIBC_CRASH_ON_NULLPTR(set);
24+
if (static_cast<size_t>(cpu) / 8 < cpuset_size) {
25+
const size_t element_index = static_cast<size_t>(cpu) / NCPUBITS;
26+
const size_t bit_position = static_cast<size_t>(cpu) % NCPUBITS;
27+
28+
const unsigned long mask = 1UL << bit_position;
29+
set->__mask[element_index] |= mask;
30+
}
31+
}
32+
33+
} // namespace LIBC_NAMESPACE_DECL
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===-- Implementation of sched_setcpuzero --------------------------------===//
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/sched_setcpuzero.h"
10+
11+
#include "src/__support/common.h" // LLVM_LIBC_FUNCTION
12+
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
13+
#include "src/__support/macros/null_check.h" // LIBC_CRASH_ON_NULLPTR
14+
15+
#include "hdr/types/cpu_set_t.h"
16+
#include "hdr/types/size_t.h"
17+
18+
namespace LIBC_NAMESPACE_DECL {
19+
20+
LLVM_LIBC_FUNCTION(void, __sched_setcpuzero,
21+
(const size_t cpuset_size, cpu_set_t *set)) {
22+
LIBC_CRASH_ON_NULLPTR(set);
23+
__builtin_memset(set, 0, cpuset_size);
24+
}
25+
26+
} // namespace LIBC_NAMESPACE_DECL

libc/src/sched/sched_getcpuisset.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation header for sched_getcpuisset -------------*- 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_SCHED_GETCPUISSET_H
10+
#define LLVM_LIBC_SRC_SCHED_SCHED_GETCPUISSET_H
11+
12+
#include "src/__support/macros/config.h" // LIBC_NAMESPACE_DECL
13+
14+
#include "hdr/types/cpu_set_t.h"
15+
#include "hdr/types/size_t.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
// for internal use in the CPU_ISSET macro
20+
int __sched_getcpuisset(int cpu, const size_t cpuset_size, cpu_set_t *set);
21+
22+
} // namespace LIBC_NAMESPACE_DECL
23+
24+
#endif // LLVM_LIBC_SRC_SCHED_SCHED_GETCPUISSET_H

libc/src/sched/sched_setcpuset.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation header for sched_setcpuset ---------------*- 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_SCHED_SETCPUSET_H
10+
#define LLVM_LIBC_SRC_SCHED_SCHED_SETCPUSET_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
#include "hdr/types/cpu_set_t.h"
15+
#include "hdr/types/size_t.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
// for internal use in the CPU_SET macro
20+
void __sched_setcpuset(int cpu, const size_t cpuset_size, cpu_set_t *set);
21+
22+
} // namespace LIBC_NAMESPACE_DECL
23+
24+
#endif // LLVM_LIBC_SRC_SCHED_SCHED_SETCPUSET_H

libc/src/sched/sched_setcpuzero.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Implementation header for sched_setcpuzero --------------*- 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_SCHED_SETCPUZERO_H
10+
#define LLVM_LIBC_SRC_SCHED_SCHED_SETCPUZERO_H
11+
12+
#include "src/__support/macros/config.h"
13+
14+
#include "hdr/types/cpu_set_t.h"
15+
#include "hdr/types/size_t.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
// for internal use in the CPU_ZERO macro
20+
void __sched_setcpuzero(const size_t cpuset_size, cpu_set_t *set);
21+
22+
} // namespace LIBC_NAMESPACE_DECL
23+
24+
#endif // LLVM_LIBC_SRC_SCHED_SCHED_SETCPUZERO_H

0 commit comments

Comments
 (0)