Skip to content

Commit 5563240

Browse files
authored
[benchmark] Sync a few commits from upstream to help with CPU count (llvm#126410)
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) Update error message now that /proc/cpuinfo is no longer in use (cherry picked from commit c35af58b61daa111c93924e0e7b65022871fadac) Fix runtime crash when parsing /proc/cpuinfo fails (cherry picked from commit 39be87d3004ff9ff4cdf736651af80c3d15e2497) another reversal of something that breaks on wasm (cherry picked from commit 44507bc91ff9a23ad8ad4120cfc6b0d9bd27e2ca)
1 parent 161cfc6 commit 5563240

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

third-party/benchmark/src/sysinfo.cc

Lines changed: 30 additions & 23 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,16 +498,36 @@ 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
}
517524

518525
int GetNumCPUs() {
519-
const int num_cpus = GetNumCPUsImpl();
526+
int num_cpus = GetNumCPUsImpl();
520527
if (num_cpus < 1) {
521-
PrintErrorAndDie(
522-
"Unable to extract number of CPUs. If your platform uses "
523-
"/proc/cpuinfo, custom support may need to be added.");
528+
std::cerr << "Unable to extract number of CPUs.\n";
529+
/* There is at least one CPU which we run on. */
530+
num_cpus = 1;
524531
}
525532
return num_cpus;
526533
}

0 commit comments

Comments
 (0)