Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 958c0df

Browse files
committed
Remove /proc/cpuinfo fallback path
AIX, WASM (fork of musl for libc) and a few others should now use the sysconf path. /proc is not portable and cpuinfo is Linux specific. It does not work anywhere else.
1 parent 9d8201e commit 958c0df

File tree

1 file changed

+2
-48
lines changed

1 file changed

+2
-48
lines changed

src/sysinfo.cc

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -517,54 +517,8 @@ int GetNumCPUsImpl() {
517517
}
518518
return num_cpu;
519519
#else
520-
// Fallback for platforms (such as WASM) that aren't covered above.
521-
int num_cpus = 0;
522-
int max_id = -1;
523-
std::ifstream f("/proc/cpuinfo");
524-
if (!f.is_open()) {
525-
std::cerr << "Failed to open /proc/cpuinfo\n";
526-
return -1;
527-
}
528-
#if defined(__alpha__)
529-
const std::string Key = "cpus detected";
530-
#else
531-
const std::string Key = "processor";
532-
#endif
533-
std::string ln;
534-
while (std::getline(f, ln)) {
535-
if (ln.empty()) continue;
536-
std::size_t split_idx = ln.find(':');
537-
std::string value;
538-
#if defined(__s390__)
539-
// s390 has another format in /proc/cpuinfo
540-
// it needs to be parsed differently
541-
if (split_idx != std::string::npos)
542-
value = ln.substr(Key.size() + 1, split_idx - Key.size() - 1);
543-
#else
544-
if (split_idx != std::string::npos) value = ln.substr(split_idx + 1);
545-
#endif
546-
if (ln.size() >= Key.size() && ln.compare(0, Key.size(), Key) == 0) {
547-
num_cpus++;
548-
if (!value.empty()) {
549-
const int cur_id = benchmark::stoi(value);
550-
max_id = std::max(cur_id, max_id);
551-
}
552-
}
553-
}
554-
if (f.bad()) {
555-
PrintErrorAndDie("Failure reading /proc/cpuinfo");
556-
}
557-
if (!f.eof()) {
558-
PrintErrorAndDie("Failed to read to end of /proc/cpuinfo");
559-
}
560-
f.close();
561-
562-
if ((max_id + 1) != num_cpus) {
563-
fprintf(stderr,
564-
"CPU ID assignments in /proc/cpuinfo seem messed up."
565-
" This is usually caused by a bad BIOS.\n");
566-
}
567-
return num_cpus;
520+
// Fallback, no other API exists.
521+
return -1;
568522
#endif
569523
BENCHMARK_UNREACHABLE();
570524
}

0 commit comments

Comments
 (0)