Skip to content

Commit 1bd060e

Browse files
committed
[benchmark] Sync a few commits from upstream to help with CPU count
Try to use the _SC_NPROCESSORS_ONLN sysconf elsewhere (cherry picked from commit edb1e76d8cb080a396c7c992e5d4023e1a777bd1) Replace usage of deprecated sysctl on macOS (cherry picked from commit faaa266d33ff203e28b31dd31be9f90c29f28d04) Retrieve the number of online CPUs on OpenBSD and NetBSD (cherry picked from commit 41e81b1ca4bbb41d234f2d0f2c56591db78ebb83)
1 parent 7b348f9 commit 1bd060e

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

third-party/benchmark/src/sysinfo.cc

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,11 @@ ValueUnion GetSysctlImp(std::string const& name) {
160160
int mib[2];
161161

162162
mib[0] = CTL_HW;
163-
if ((name == "hw.ncpu") || (name == "hw.cpuspeed")) {
163+
if ((name == "hw.ncpuonline") || (name == "hw.cpuspeed")) {
164164
ValueUnion buff(sizeof(int));
165165

166-
if (name == "hw.ncpu") {
167-
mib[1] = HW_NCPU;
166+
if (name == "hw.ncpuonline") {
167+
mib[1] = HW_NCPUONLINE;
168168
} else {
169169
mib[1] = HW_CPUSPEED;
170170
}
@@ -482,27 +482,14 @@ std::string GetSystemName() {
482482
}
483483

484484
int GetNumCPUsImpl() {
485-
#ifdef BENCHMARK_HAS_SYSCTL
486-
int num_cpu = -1;
487-
if (GetSysctl("hw.ncpu", &num_cpu)) return num_cpu;
488-
PrintErrorAndDie("Err: ", strerror(errno));
489-
#elif defined(BENCHMARK_OS_WINDOWS)
485+
#ifdef BENCHMARK_OS_WINDOWS
490486
SYSTEM_INFO sysinfo;
491487
// Use memset as opposed to = {} to avoid GCC missing initializer false
492488
// positives.
493489
std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO));
494490
GetSystemInfo(&sysinfo);
495-
return sysinfo.dwNumberOfProcessors; // number of logical
496-
// processors in the current
497-
// group
498-
#elif defined(__linux__) || defined(BENCHMARK_OS_SOLARIS)
499-
// Returns -1 in case of a failure.
500-
int num_cpu = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
501-
if (num_cpu < 0) {
502-
PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
503-
strerror(errno));
504-
}
505-
return num_cpu;
491+
// number of logical processors in the current group
492+
return static_cast<int>(sysinfo.dwNumberOfProcessors);
506493
#elif defined(BENCHMARK_OS_QNX)
507494
return static_cast<int>(_syspage_ptr->num_cpu);
508495
#elif defined(BENCHMARK_OS_QURT)
@@ -511,6 +498,26 @@ int GetNumCPUsImpl() {
511498
hardware_threads.max_hthreads = 1;
512499
}
513500
return hardware_threads.max_hthreads;
501+
#elif defined(BENCHMARK_HAS_SYSCTL)
502+
int num_cpu = -1;
503+
constexpr auto* hwncpu =
504+
#if defined BENCHMARK_OS_MACOSX
505+
"hw.logicalcpu";
506+
#elif defined(HW_NCPUONLINE)
507+
"hw.ncpuonline";
508+
#else
509+
"hw.ncpu";
510+
#endif
511+
if (GetSysctl(hwncpu, &num_cpu)) return num_cpu;
512+
PrintErrorAndDie("Err: ", strerror(errno));
513+
#elif defined(_SC_NPROCESSORS_ONLN)
514+
// Returns -1 in case of a failure.
515+
int num_cpu = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
516+
if (num_cpu < 0) {
517+
PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
518+
strerror(errno));
519+
}
520+
return num_cpu;
514521
#endif
515522
BENCHMARK_UNREACHABLE();
516523
}

0 commit comments

Comments
 (0)