Skip to content
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

sysinfo.cc: Always abort on GetNumCPUs failure #1756

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/sysinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,11 @@ std::string GetSystemName() {
#endif // Catch-all POSIX block.
}

int GetNumCPUs() {
int GetNumCPUsImpl() {
#ifdef BENCHMARK_HAS_SYSCTL
int num_cpu = -1;
if (GetSysctl("hw.ncpu", &num_cpu)) return num_cpu;
fprintf(stderr, "Err: %s\n", strerror(errno));
std::exit(EXIT_FAILURE);
PrintErrorAndDie("Err: ", strerror(errno));
#elif defined(BENCHMARK_OS_WINDOWS)
SYSTEM_INFO sysinfo;
// Use memset as opposed to = {} to avoid GCC missing initializer false
Expand All @@ -493,8 +492,8 @@ int GetNumCPUs() {
// Returns -1 in case of a failure.
long num_cpu = sysconf(_SC_NPROCESSORS_ONLN);
if (num_cpu < 0) {
fprintf(stderr, "sysconf(_SC_NPROCESSORS_ONLN) failed with error: %s\n",
strerror(errno));
PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
strerror(errno));
}
return (int)num_cpu;
#elif defined(BENCHMARK_OS_QNX)
Expand All @@ -510,8 +509,7 @@ int GetNumCPUs() {
int max_id = -1;
std::ifstream f("/proc/cpuinfo");
if (!f.is_open()) {
std::cerr << "failed to open /proc/cpuinfo\n";
return -1;
PrintErrorAndDie("Failed to open /proc/cpuinfo");
}
#if defined(__alpha__)
const std::string Key = "cpus detected";
Expand Down Expand Up @@ -540,12 +538,10 @@ int GetNumCPUs() {
}
}
if (f.bad()) {
std::cerr << "Failure reading /proc/cpuinfo\n";
return -1;
PrintErrorAndDie("Failure reading /proc/cpuinfo");
}
if (!f.eof()) {
std::cerr << "Failed to read to end of /proc/cpuinfo\n";
return -1;
PrintErrorAndDie("Failed to read to end of /proc/cpuinfo");
}
f.close();

Expand All @@ -559,6 +555,16 @@ int GetNumCPUs() {
BENCHMARK_UNREACHABLE();
}

int GetNumCPUs() {
const int num_cpus = GetNumCPUsImpl();
if (num_cpus < 1) {
PrintErrorAndDie(
"Unable to extract number of CPUs. If your platform uses "
"/proc/cpuinfo, custom support may need to be added.");
}
return num_cpus;
}

class ThreadAffinityGuard final {
public:
ThreadAffinityGuard() : reset_affinity(SetAffinity()) {
Expand Down
Loading