Skip to contentSkip to conversation

thread-utils.c: detect online CPU count on OpenBSD / NetBSD

PATCH In review Brad Smith 3 participants 4 messages May 9, 2025 – Jun 1, 2025

lore
Share & tools
lore

Brad Smith

author
#0 lore raw

PATCH 1/1 thread-utils.c: detect online CPU count on OpenBSD / NetBSD

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.

---
 thread-utils.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
thread-utils.c +4 -4
index 1f89ffab4c..374890e6b0 100644
@@ -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;
Signature · 1 line
2.49.0
#1 lore raw
Quoted 20 lines · Brad Smith

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.

---
 thread-utils.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
thread-utils.c +0 -2
index 1f89ffab4c..374890e6b0 100644
@@ -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))

thread-utils.c int online_cpus(void) · line 56

Quoted 10 lines
thread-utils.c +4 -2
-		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

author
#2 lore raw
In reply to Brad Smith

ping.

Quoted 30 lines · Brad Smith

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.

---
  thread-utils.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
thread-utils.c +4 -4
index 1f89ffab4c..374890e6b0 100644
@@ -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;
Reviewed-by on v1 1/1
#3 lore raw
In reply to Brad Smith

Hi Brad,

Quoted 9 lines · You

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.

---
 thread-utils.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Looks good to me. I tested the patch on OpenBSD 7.7 and NetBSD 10.0.

Collin