-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc] add CPU_COUNT macro and backing function
Add the macro CPU_COUNT as well as a backing function to implement the functionality. Reviewed By: sivachandra Differential Revision: https://reviews.llvm.org/D135179
- Loading branch information
1 parent
8c02ca1
commit 0b790af
Showing
16 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//===-- 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_MACROS_LINUX_SCHED_MACROS_H | ||
#define __LLVM_LIBC_MACROS_LINUX_SCHED_MACROS_H | ||
|
||
#define CPU_COUNT_S(setsize, set) __sched_getcpucount(setsize, set) | ||
#define CPU_COUNT(set) CPU_COUNT_S(sizeof(cpu_set_t), set) | ||
|
||
#endif // __LLVM_LIBC_MACROS_LINUX_SCHED_MACROS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//===-- Macros defined in sched.h header file -----------------------------===// | ||
// | ||
// 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_MACROS_SCHED_MACROS_H | ||
#define __LLVM_LIBC_MACROS_SCHED_MACROS_H | ||
|
||
#ifdef __unix__ | ||
#include "linux/sched-macros.h" | ||
#endif | ||
|
||
#endif // __LLVM_LIBC_MACROS_SCHED_MACROS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===-- Implementation of sched_getcpucount -------------------------------===// | ||
// | ||
// 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_getcpucount.h" | ||
|
||
#include "src/__support/common.h" | ||
|
||
#include <sched.h> | ||
#include <stddef.h> | ||
|
||
namespace __llvm_libc { | ||
|
||
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; | ||
} | ||
|
||
} // namespace __llvm_libc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//===-- Implementation header for sched_getcpucount -------------*- 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_UNISTD_SCHED_GETCPUCOUNT_H | ||
#define LLVM_LIBC_SRC_UNISTD_SCHED_GETCPUCOUNT_H | ||
|
||
#include <sched.h> | ||
#include <stddef.h> | ||
|
||
namespace __llvm_libc { | ||
|
||
// This function is for internal use in the CPU_COUNT macro, but since that's a | ||
// macro and will be applied to client files, this must be a public entrypoint. | ||
int __sched_getcpucount(size_t cpuset_size, const cpu_set_t *mask); | ||
|
||
} // namespace __llvm_libc | ||
|
||
#endif // LLVM_LIBC_SRC_UNISTD_SCHED_GETCPUCOUNT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//===-- Unittests for sched_getaffinity and sched_setaffinity -------------===// | ||
// | ||
// 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/sched/sched_getaffinity.h" | ||
#include "src/sched/sched_getcpucount.h" | ||
#include "test/ErrnoSetterMatcher.h" | ||
|
||
#include <errno.h> | ||
#include <sched.h> | ||
#include <sys/syscall.h> | ||
|
||
TEST(LlvmLibcSchedAffinityTest, SmokeTest) { | ||
cpu_set_t mask; | ||
errno = 0; | ||
using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds; | ||
pid_t tid = __llvm_libc::syscall_impl(SYS_gettid); | ||
ASSERT_GT(tid, pid_t(0)); | ||
ASSERT_THAT(__llvm_libc::sched_getaffinity(tid, sizeof(cpu_set_t), &mask), | ||
Succeeds(0)); | ||
|
||
// CPU_COUNT is a macro, but it expands to an LLVM-libc internal function that | ||
// needs to be in the appropriate namespace for the test. | ||
int num_cpus = __llvm_libc::CPU_COUNT(&mask); | ||
ASSERT_GT(num_cpus, 0); | ||
ASSERT_LE(num_cpus, int(sizeof(cpu_set_t) * sizeof(unsigned long))); | ||
} |