-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[benchmark] Get number of CPUs with sysconf() on Linux #125603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or 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
(cherry picked from commit c24774dc4f4402c3ad150363321cc972ed2669e7)
cc @rorth |
@llvm/pr-subscribers-third-party-benchmark Author: Brad Smith (brad0) Changes(cherry picked from commit c24774dc4f4402c3ad150363321cc972ed2669e7) Full diff: https://github.com/llvm/llvm-project/pull/125603.diff 1 Files Affected:
diff --git a/third-party/benchmark/src/sysinfo.cc b/third-party/benchmark/src/sysinfo.cc
index 2bed1663af2e95..8283a081ee80b4 100644
--- a/third-party/benchmark/src/sysinfo.cc
+++ b/third-party/benchmark/src/sysinfo.cc
@@ -495,14 +495,14 @@ int GetNumCPUsImpl() {
return sysinfo.dwNumberOfProcessors; // number of logical
// processors in the current
// group
-#elif defined(BENCHMARK_OS_SOLARIS)
+#elif defined(__linux__) || defined(BENCHMARK_OS_SOLARIS)
// Returns -1 in case of a failure.
- long num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
+ int num_cpu = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
if (num_cpu < 0) {
PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
strerror(errno));
}
- return (int)num_cpu;
+ return num_cpu;
#elif defined(BENCHMARK_OS_QNX)
return static_cast<int>(_syspage_ptr->num_cpu);
#elif defined(BENCHMARK_OS_QURT)
@@ -511,53 +511,6 @@ int GetNumCPUsImpl() {
hardware_threads.max_hthreads = 1;
}
return hardware_threads.max_hthreads;
-#else
- int num_cpus = 0;
- int max_id = -1;
- std::ifstream f("/proc/cpuinfo");
- if (!f.is_open()) {
- PrintErrorAndDie("Failed to open /proc/cpuinfo");
- }
-#if defined(__alpha__)
- const std::string Key = "cpus detected";
-#else
- const std::string Key = "processor";
-#endif
- std::string ln;
- while (std::getline(f, ln)) {
- if (ln.empty()) continue;
- std::size_t split_idx = ln.find(':');
- std::string value;
-#if defined(__s390__)
- // s390 has another format in /proc/cpuinfo
- // it needs to be parsed differently
- if (split_idx != std::string::npos)
- value = ln.substr(Key.size() + 1, split_idx - Key.size() - 1);
-#else
- if (split_idx != std::string::npos) value = ln.substr(split_idx + 1);
-#endif
- if (ln.size() >= Key.size() && ln.compare(0, Key.size(), Key) == 0) {
- num_cpus++;
- if (!value.empty()) {
- const int cur_id = benchmark::stoi(value);
- max_id = std::max(cur_id, max_id);
- }
- }
- }
- if (f.bad()) {
- PrintErrorAndDie("Failure reading /proc/cpuinfo");
- }
- if (!f.eof()) {
- PrintErrorAndDie("Failed to read to end of /proc/cpuinfo");
- }
- f.close();
-
- if ((max_id + 1) != num_cpus) {
- fprintf(stderr,
- "CPU ID assignments in /proc/cpuinfo seem messed up."
- " This is usually caused by a bad BIOS.\n");
- }
- return num_cpus;
#endif
BENCHMARK_UNREACHABLE();
}
|
mtrofin
approved these changes
Feb 4, 2025
/cherry-pick fbe470c |
/pull-request #125669 |
swift-ci
pushed a commit
to swiftlang/llvm-project
that referenced
this pull request
Feb 11, 2025
(cherry picked from commit c24774dc4f4402c3ad150363321cc972ed2669e7) (cherry picked from commit fbe470c)
Icohedron
pushed a commit
to Icohedron/llvm-project
that referenced
this pull request
Feb 11, 2025
(cherry picked from commit c24774dc4f4402c3ad150363321cc972ed2669e7)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(cherry picked from commit c24774dc4f4402c3ad150363321cc972ed2669e7)