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

Retrieve the number of online CPUs on OpenBSD and NetBSD #1916

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions src/sysinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ ValueUnion GetSysctlImp(std::string const& name) {
int mib[2];

mib[0] = CTL_HW;
if ((name == "hw.ncpu") || (name == "hw.cpuspeed")) {
if ((name == "hw.ncpuonline") || (name == "hw.cpuspeed")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not ncpufound?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sysctl name is hw.ncpuonline.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://man.openbsd.org/sysctl.2 includes ncpufound which seems to be more predictable in some cases (from brief reading i did). i'm not an expert but i want to make sure that we're picking the right one.

Copy link
Contributor Author

@brad0 brad0 Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ncpufound is the same as ncpu. On OpenBSD X86 systems have SMT disabled by default so the number of CPUs would be double what is actually online and available. This is functionally equivalent to what is used on other OS's with sysconf(_SC_NPROCESSORS_ONLN) instead of _SC_NPROCESSORS_CONF.

ValueUnion buff(sizeof(int));

if (name == "hw.ncpu") {
mib[1] = HW_NCPU;
if (name == "hw.ncpuonline") {
mib[1] = HW_NCPUONLINE;
} else {
mib[1] = HW_CPUSPEED;
}
Expand Down Expand Up @@ -482,7 +482,13 @@ std::string GetSystemName() {
int GetNumCPUsImpl() {
#ifdef BENCHMARK_HAS_SYSCTL
int num_cpu = -1;
if (GetSysctl("hw.ncpu", &num_cpu)) return num_cpu;
constexpr auto* hwncpu =
#ifdef HW_NCPUONLINE
"hw.ncpuonline";
#else
"hw.ncpu";
#endif
if (GetSysctl(hwncpu, &num_cpu)) return num_cpu;
PrintErrorAndDie("Err: ", strerror(errno));
#elif defined(BENCHMARK_OS_WINDOWS)
SYSTEM_INFO sysinfo;
Expand Down
Loading