summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2022-10-20 20:22:50 +0300
committerLasse Collin <lasse.collin@tukaani.org>2022-11-11 13:28:56 +0200
commit5daa40454b63c4eb04ab5caeb339eddc95c94bb1 (patch)
treef541d54a2f0842572db81a16db095b6fff658ed4
parent0af861050f375678eafc4e1136ca589ae47d31c0 (diff)
downloadxz-5daa40454b63c4eb04ab5caeb339eddc95c94bb1.tar.gz
tuklib_cpucores: Use HW_NCPUONLINE on OpenBSD.
On OpenBSD the number of cores online is often less than what HW_NCPU would return because OpenBSD disables simultaneous multi-threading (SMT) by default. Thanks to Christian Weisgerber.
-rw-r--r--m4/tuklib_cpucores.m45
-rw-r--r--src/common/tuklib_cpucores.c9
2 files changed, 14 insertions, 0 deletions
diff --git a/m4/tuklib_cpucores.m4 b/m4/tuklib_cpucores.m4
index a2b09a7..c5cee23 100644
--- a/m4/tuklib_cpucores.m4
+++ b/m4/tuklib_cpucores.m4
@@ -103,7 +103,12 @@ compile error
int
main(void)
{
+#ifdef HW_NCPUONLINE
+ /* This is preferred on OpenBSD, see tuklib_cpucores.c. */
+ int name[2] = { CTL_HW, HW_NCPUONLINE };
+#else
int name[2] = { CTL_HW, HW_NCPU };
+#endif
int cpus;
size_t cpus_size = sizeof(cpus);
sysctl(name, 2, &cpus, &cpus_size, NULL, 0);
diff --git a/src/common/tuklib_cpucores.c b/src/common/tuklib_cpucores.c
index cc968dd..bb3f2f7 100644
--- a/src/common/tuklib_cpucores.c
+++ b/src/common/tuklib_cpucores.c
@@ -72,7 +72,16 @@ tuklib_cpucores(void)
}
#elif defined(TUKLIB_CPUCORES_SYSCTL)
+ // On OpenBSD HW_NCPUONLINE tells the number of processor cores that
+ // are online so it is preferred over HW_NCPU which also counts cores
+ // that aren't currently available. The number of cores online is
+ // often less than HW_NCPU because OpenBSD disables simultaneous
+ // multi-threading (SMT) by default.
+# ifdef HW_NCPUONLINE
+ int name[2] = { CTL_HW, HW_NCPUONLINE };
+# else
int name[2] = { CTL_HW, HW_NCPU };
+# endif
int cpus;
size_t cpus_size = sizeof(cpus);
if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1