{"thread":{"id":"63426","subject":"[PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD","startedAt":"2025-05-09T06:13:15Z","lastAt":"2025-06-01T02:14:48Z","messageCount":4,"participants":["Brad Smith","Patrick Steinhardt","Collin Funk"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"517623","messageId":"aB2c-W0V83aVDpZr@humpty.home.comstyle.com","threadId":"63426","inReplyTo":null,"subject":"[PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD","fromName":"Brad Smith","fromEmail":"brad@comstyle.com","sentAt":"2025-05-09T06:13:13Z","receivedAt":"2025-05-09T06:13:15Z","isPatch":true,"sender":{"key":"brad@comstyle.com","avatar":"https://avatars.githubusercontent.com/u/1129902?v=4"},"body":"OpenBSD / NetBSD use HW_NCPUONLINE to detect the online CPU\ncount. OpenBSD ships with SMT disabled on X86 systems so\nHW_NCPU would provide double the number of CPUs as opposed\nto the proper online count.\n\nSigned-off-by: Brad Smith <brad@comstyle.com>\n---\n thread-utils.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/thread-utils.c b/thread-utils.c\nindex 1f89ffab4c..374890e6b0 100644\n--- a/thread-utils.c\n+++ b/thread-utils.c\n@@ -46,11 +46,11 @@ int online_cpus(void)\n \tmib[0] = CTL_HW;\n #  ifdef HW_AVAILCPU\n \tmib[1] = HW_AVAILCPU;\n-\tlen = sizeof(cpucount);\n-\tif (!sysctl(mib, 2, &cpucount, &len, NULL, 0))\n-\t\treturn cpucount;\n-#  endif /* HW_AVAILCPU */\n+#  elif defined(HW_NCPUONLINE)\n+\tmib[1] = HW_NCPUONLINE;\n+#  else\n \tmib[1] = HW_NCPU;\n+#  endif /* HW_AVAILCPU */\n \tlen = sizeof(cpucount);\n \tif (!sysctl(mib, 2, &cpucount, &len, NULL, 0))\n \t\treturn cpucount;\n-- \n2.49.0\n\n"},{"id":"517635","messageId":"aB2-lwKPOnj3EH0b@pks.im","threadId":"63426","inReplyTo":"aB2c-W0V83aVDpZr@humpty.home.comstyle.com","subject":"Re: [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2025-05-09T08:36:39Z","receivedAt":"2025-05-09T08:36:46Z","isPatch":true,"sender":{"key":"ps@pks.im","avatar":"https://avatars.githubusercontent.com/u/4056630?v=4"},"body":"On Fri, May 09, 2025 at 02:13:13AM -0400, Brad Smith wrote:\n> OpenBSD / NetBSD use HW_NCPUONLINE to detect the online CPU\n> count. OpenBSD ships with SMT disabled on X86 systems so\n> HW_NCPU would provide double the number of CPUs as opposed\n> to the proper online count.\n> \n> Signed-off-by: Brad Smith <brad@comstyle.com>\n> ---\n>  thread-utils.c | 8 ++++----\n>  1 file changed, 4 insertions(+), 4 deletions(-)\n> \n> diff --git a/thread-utils.c b/thread-utils.c\n> index 1f89ffab4c..374890e6b0 100644\n> --- a/thread-utils.c\n> +++ b/thread-utils.c\n> @@ -46,11 +46,11 @@ int online_cpus(void)\n>  \tmib[0] = CTL_HW;\n>  #  ifdef HW_AVAILCPU\n>  \tmib[1] = HW_AVAILCPU;\n> -\tlen = sizeof(cpucount);\n> -\tif (!sysctl(mib, 2, &cpucount, &len, NULL, 0))\n> -\t\treturn cpucount;\n> -#  endif /* HW_AVAILCPU */\n> +#  elif defined(HW_NCPUONLINE)\n> +\tmib[1] = HW_NCPUONLINE;\n> +#  else\n>  \tmib[1] = HW_NCPU;\n> +#  endif /* HW_AVAILCPU */\n>  \tlen = sizeof(cpucount);\n>  \tif (!sysctl(mib, 2, &cpucount, &len, NULL, 0))\n>  \t\treturn cpucount;\n\nThis change looks sensible to me and matches the documentation at [1].\nUsing the number of online CPUs instead of existing CPUs certainly\nmatches the expectation of what this function should reutrn.\n\nThanks!\n\nPatrick\n\n[1]: https://man.openbsd.org/sysctl.2#HW_NCPUONLINE~2\n"},{"id":"519311","messageId":"f929a4b5-1011-42dd-ad86-bb654d170db2@comstyle.com","threadId":"63426","inReplyTo":"aB2c-W0V83aVDpZr@humpty.home.comstyle.com","subject":"Re: [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD","fromName":"Brad Smith","fromEmail":"brad@comstyle.com","sentAt":"2025-05-31T20:18:03Z","receivedAt":"2025-05-31T20:18:11Z","isPatch":true,"sender":{"key":"brad@comstyle.com","avatar":"https://avatars.githubusercontent.com/u/1129902?v=4"},"body":"ping.\n\nOn 2025-05-09 2:13 a.m., Brad Smith wrote:\n> OpenBSD / NetBSD use HW_NCPUONLINE to detect the online CPU\n> count. OpenBSD ships with SMT disabled on X86 systems so\n> HW_NCPU would provide double the number of CPUs as opposed\n> to the proper online count.\n>\n> Signed-off-by: Brad Smith <brad@comstyle.com>\n> ---\n>   thread-utils.c | 8 ++++----\n>   1 file changed, 4 insertions(+), 4 deletions(-)\n>\n> diff --git a/thread-utils.c b/thread-utils.c\n> index 1f89ffab4c..374890e6b0 100644\n> --- a/thread-utils.c\n> +++ b/thread-utils.c\n> @@ -46,11 +46,11 @@ int online_cpus(void)\n>   \tmib[0] = CTL_HW;\n>   #  ifdef HW_AVAILCPU\n>   \tmib[1] = HW_AVAILCPU;\n> -\tlen = sizeof(cpucount);\n> -\tif (!sysctl(mib, 2, &cpucount, &len, NULL, 0))\n> -\t\treturn cpucount;\n> -#  endif /* HW_AVAILCPU */\n> +#  elif defined(HW_NCPUONLINE)\n> +\tmib[1] = HW_NCPUONLINE;\n> +#  else\n>   \tmib[1] = HW_NCPU;\n> +#  endif /* HW_AVAILCPU */\n>   \tlen = sizeof(cpucount);\n>   \tif (!sysctl(mib, 2, &cpucount, &len, NULL, 0))\n>   \t\treturn cpucount;\n"},{"id":"519320","messageId":"87msas5ht5.fsf@gmail.com","threadId":"63426","inReplyTo":"aB2c-W0V83aVDpZr@humpty.home.comstyle.com","subject":"Re: [PATCH] thread-utils.c: detect online CPU count on OpenBSD / NetBSD","fromName":"Collin Funk","fromEmail":"collin.funk1@gmail.com","sentAt":"2025-06-01T02:14:46Z","receivedAt":"2025-06-01T02:14:48Z","isPatch":true,"sender":{"key":"collin.funk1@gmail.com","avatar":"https://avatars.githubusercontent.com/u/65689063?v=4"},"body":"Hi Brad,\n\nYou wrote:\n\n> OpenBSD / NetBSD use HW_NCPUONLINE to detect the online CPU\n> count. OpenBSD ships with SMT disabled on X86 systems so\n> HW_NCPU would provide double the number of CPUs as opposed\n> to the proper online count.\n> \n> Signed-off-by: Brad Smith <brad@comstyle.com>\n> ---\n>  thread-utils.c | 8 ++++----\n>  1 file changed, 4 insertions(+), 4 deletions(-)\n\nLooks good to me. I tested the patch on OpenBSD 7.7 and NetBSD 10.0.\n\nReviewed-by: Collin Funk <collin.funk1@gmail.com>\n\nCollin\n"}]}