mirror of
https://github.com/google/benchmark.git
synced 2025-03-27 20:40:40 +08:00
Get number of CPUs with sysconf() on Linux (#1901)
* Get number of CPUs with sysconf() on Linux Avoid parsing the /proc/cpuinfo just to get number of CPUs. Instead use the portable function provided by glibc. * Update sysinfo.cc
This commit is contained in:
parent
39be87d300
commit
c24774dc4f
@ -492,14 +492,14 @@ int GetNumCPUsImpl() {
|
|||||||
GetSystemInfo(&sysinfo);
|
GetSystemInfo(&sysinfo);
|
||||||
// number of logical processors in the current group
|
// number of logical processors in the current group
|
||||||
return static_cast<int>(sysinfo.dwNumberOfProcessors);
|
return static_cast<int>(sysinfo.dwNumberOfProcessors);
|
||||||
#elif defined(BENCHMARK_OS_SOLARIS)
|
#elif defined(__linux__) || defined(BENCHMARK_OS_SOLARIS)
|
||||||
// Returns -1 in case of a failure.
|
// 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) {
|
if (num_cpu < 0) {
|
||||||
PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
|
PrintErrorAndDie("sysconf(_SC_NPROCESSORS_ONLN) failed with error: ",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
return (int)num_cpu;
|
return num_cpu;
|
||||||
#elif defined(BENCHMARK_OS_QNX)
|
#elif defined(BENCHMARK_OS_QNX)
|
||||||
return static_cast<int>(_syspage_ptr->num_cpu);
|
return static_cast<int>(_syspage_ptr->num_cpu);
|
||||||
#elif defined(BENCHMARK_OS_QURT)
|
#elif defined(BENCHMARK_OS_QURT)
|
||||||
@ -508,54 +508,6 @@ int GetNumCPUsImpl() {
|
|||||||
hardware_threads.max_hthreads = 1;
|
hardware_threads.max_hthreads = 1;
|
||||||
}
|
}
|
||||||
return hardware_threads.max_hthreads;
|
return hardware_threads.max_hthreads;
|
||||||
#else
|
|
||||||
int num_cpus = 0;
|
|
||||||
int max_id = -1;
|
|
||||||
std::ifstream f("/proc/cpuinfo");
|
|
||||||
if (!f.is_open()) {
|
|
||||||
std::cerr << "Failed to open /proc/cpuinfo\n";
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#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
|
#endif
|
||||||
BENCHMARK_UNREACHABLE();
|
BENCHMARK_UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user