Skip to content

Commit

Permalink
sysinfo: standardize on PrintErrorAndDie for handling fatal errors
Browse files Browse the repository at this point in the history
Signed-off-by: Sam James <sam@gentoo.org>
  • Loading branch information
thesamesam committed Feb 13, 2024
1 parent 561d3e8 commit 975fa24
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions src/sysinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -478,25 +478,27 @@ int GetNumCPUs() {
#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
// positives.
std::memset(&sysinfo, 0, sizeof(SYSTEM_INFO));
GetSystemInfo(&sysinfo);
if (sysinfo.dwNumberOfProcessors <= 0) {
PrintErrorAndDie("Failed to identify number of processors");
}
return sysinfo.dwNumberOfProcessors; // number of logical
// processors in the current
// group
#elif defined(BENCHMARK_OS_SOLARIS)
// 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;
return static_cast<int>(num_cpu);
#elif defined(BENCHMARK_OS_QNX)
return static_cast<int>(_syspage_ptr->num_cpu);
#elif defined(BENCHMARK_OS_QURT)
Expand Down Expand Up @@ -667,13 +669,11 @@ double GetCPUCyclesPerSecond(CPUInfo::Scaling scaling) {
return static_cast<double>(freq) * 1000.0;
}

const double error_value = -1;
double bogo_clock = error_value;
double bogo_clock = -1;

std::ifstream f("/proc/cpuinfo");
if (!f.is_open()) {
std::cerr << "failed to open /proc/cpuinfo\n";
return error_value;
PrintErrorAndDie("Failed to open /proc/cpuinfo");
}

auto StartsWithKey = [](std::string const& Value, std::string const& Key) {
Expand Down Expand Up @@ -701,17 +701,15 @@ double GetCPUCyclesPerSecond(CPUInfo::Scaling scaling) {
} else if (StartsWithKey(ln, "bogomips")) {
if (!value.empty()) {
bogo_clock = benchmark::stod(value) * 1000000.0;
if (bogo_clock < 0.0) bogo_clock = error_value;
if (bogo_clock < 0.0) bogo_clock = -1;
}
}
}
if (f.bad()) {
std::cerr << "Failure reading /proc/cpuinfo\n";
return error_value;
PrintErrorAndDie("Failure reading /proc/cpuinfo");
}
if (!f.eof()) {
std::cerr << "Failed to read to end of /proc/cpuinfo\n";
return error_value;
PrintErrorAndDie("Failed to read to end of /proc/cpuinfo");
}
f.close();
// If we found the bogomips clock, but nothing better, we'll use it (but
Expand Down Expand Up @@ -756,29 +754,24 @@ double GetCPUCyclesPerSecond(CPUInfo::Scaling scaling) {
#elif defined(BENCHMARK_OS_SOLARIS)
kstat_ctl_t* kc = kstat_open();
if (!kc) {
std::cerr << "failed to open /dev/kstat\n";
return -1;
PrintErrorAndDie("Failed to open /dev/kstat");
}
kstat_t* ksp = kstat_lookup(kc, const_cast<char*>("cpu_info"), -1,
const_cast<char*>("cpu_info0"));
if (!ksp) {
std::cerr << "failed to lookup in /dev/kstat\n";
return -1;
PrintErrorAndDie("Failed to lookup in /dev/kstat");
}
if (kstat_read(kc, ksp, NULL) < 0) {
std::cerr << "failed to read from /dev/kstat\n";
return -1;
PrintErrorAndDie("Failed to read from /dev/kstat");
}
kstat_named_t* knp = (kstat_named_t*)kstat_data_lookup(
ksp, const_cast<char*>("current_clock_Hz"));
if (!knp) {
std::cerr << "failed to lookup data in /dev/kstat\n";
return -1;
PrintErrorAndDie("Failed to lookup data in /dev/kstat");
}
if (knp->data_type != KSTAT_DATA_UINT64) {
std::cerr << "current_clock_Hz is of unexpected data type: "
<< knp->data_type << "\n";
return -1;
PrintErrorAndDie("Current_clock_Hz is of unexpected data type: ",
knp->data_type);
}
double clock_hz = knp->value.ui64;
kstat_close(kc);
Expand Down

0 comments on commit 975fa24

Please sign in to comment.