Thread (4 messages) 4 messages, 3 authors, 2025-06-01

Re: [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD

From: Patrick Steinhardt <hidden>
Date: 2025-05-09 08:36:46

On Fri, May 09, 2025 at 02:13:13AM -0400, Brad Smith wrote:
quoted hunk ↗ jump to hunk
OpenBSD / NetBSD use HW_NCPUONLINE to detect the online CPU
count. OpenBSD ships with SMT disabled on X86 systems so
HW_NCPU would provide double the number of CPUs as opposed
to the proper online count.

Signed-off-by: Brad Smith <redacted>
---
 thread-utils.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/thread-utils.c b/thread-utils.c
index 1f89ffab4c..374890e6b0 100644
--- a/thread-utils.c
+++ b/thread-utils.c
@@ -46,11 +46,11 @@ int online_cpus(void)
 	mib[0] = CTL_HW;
 #  ifdef HW_AVAILCPU
 	mib[1] = HW_AVAILCPU;
-	len = sizeof(cpucount);
-	if (!sysctl(mib, 2, &cpucount, &len, NULL, 0))
-		return cpucount;
-#  endif /* HW_AVAILCPU */
+#  elif defined(HW_NCPUONLINE)
+	mib[1] = HW_NCPUONLINE;
+#  else
 	mib[1] = HW_NCPU;
+#  endif /* HW_AVAILCPU */
 	len = sizeof(cpucount);
 	if (!sysctl(mib, 2, &cpucount, &len, NULL, 0))
 		return cpucount;
This change looks sensible to me and matches the documentation at [1].
Using the number of online CPUs instead of existing CPUs certainly
matches the expectation of what this function should reutrn.

Thanks!

Patrick

[1]: https://man.openbsd.org/sysctl.2#HW_NCPUONLINE~2
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help