]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
tuklib_cpucores: Use HW_NCPUONLINE on OpenBSD.
authorLasse Collin <lasse.collin@tukaani.org>
Thu, 20 Oct 2022 17:22:50 +0000 (20:22 +0300)
committerLasse Collin <lasse.collin@tukaani.org>
Fri, 11 Nov 2022 11:28:56 +0000 (13:28 +0200)
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.

m4/tuklib_cpucores.m4
src/common/tuklib_cpucores.c

index a2b09a72380cb1b92f3d64d38ab51950cc870a09..c5cee23e1822f536f19146b4b313f386c3852646 100644 (file)
@@ -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);
index cc968dd25efbfac996ed1d9ae0d54347e9825fa9..bb3f2f752b1b6dccf3cf6c61e798e50707cbd6c0 100644 (file)
@@ -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