git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD
@ 2025-05-09  6:13 Brad Smith
  2025-05-09  8:36 ` Patrick Steinhardt
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Brad Smith @ 2025-05-09  6:13 UTC (permalink / raw)
  To: git

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 <brad@comstyle.com>
---
 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;
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD
  2025-05-09  6:13 [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD Brad Smith
@ 2025-05-09  8:36 ` Patrick Steinhardt
  2025-05-31 20:18 ` Brad Smith
  2025-06-01  2:14 ` Collin Funk
  2 siblings, 0 replies; 4+ messages in thread
From: Patrick Steinhardt @ 2025-05-09  8:36 UTC (permalink / raw)
  To: Brad Smith; +Cc: git

On Fri, May 09, 2025 at 02:13:13AM -0400, Brad Smith wrote:
> 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 <brad@comstyle.com>
> ---
>  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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD
  2025-05-09  6:13 [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD Brad Smith
  2025-05-09  8:36 ` Patrick Steinhardt
@ 2025-05-31 20:18 ` Brad Smith
  2025-06-01  2:14 ` Collin Funk
  2 siblings, 0 replies; 4+ messages in thread
From: Brad Smith @ 2025-05-31 20:18 UTC (permalink / raw)
  To: git

ping.

On 2025-05-09 2:13 a.m., Brad Smith wrote:
> 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 <brad@comstyle.com>
> ---
>   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;

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD
  2025-05-09  6:13 [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD Brad Smith
  2025-05-09  8:36 ` Patrick Steinhardt
  2025-05-31 20:18 ` Brad Smith
@ 2025-06-01  2:14 ` Collin Funk
  2 siblings, 0 replies; 4+ messages in thread
From: Collin Funk @ 2025-06-01  2:14 UTC (permalink / raw)
  To: Brad Smith, git

Hi Brad,

You wrote:

> 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 <brad@comstyle.com>
> ---
>  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.

Reviewed-by: Collin Funk <collin.funk1@gmail.com>

Collin

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-06-01  2:14 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-09  6:13 [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD Brad Smith
2025-05-09  8:36 ` Patrick Steinhardt
2025-05-31 20:18 ` Brad Smith
2025-06-01  2:14 ` Collin Funk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).