]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add support to read number of online CPUs on OpenBSD
authorOndřej Surý <ondrej@isc.org>
Fri, 20 Sep 2024 06:53:01 +0000 (08:53 +0200)
committerOndřej Surý <ondrej@isc.org>
Sat, 21 Sep 2024 10:38:33 +0000 (12:38 +0200)
The OpenBSD doesn't have sysctlbyname(), but sysctl() can be used to
read the number of online/available CPUs by reading following MIB(s):
[CTL_HW, HW_NCPUONLINE] with fallback to [CTL_HW, HW_NCPU].

lib/isc/os.c

index 2a8c62402d1831c98651dcbb3fd1467e1dee7c46..7f0d7cced55265c7515fdc23dce32f75329d7f64 100644 (file)
@@ -73,6 +73,29 @@ sysctlbyname_ncpus(void) {
 }
 #endif /* HAVE_SYSCTLBYNAME */
 
+#if HAVE_SYS_SYSCTL_H && !defined(__linux__)
+static int
+sysctl_ncpus(void) {
+       int ncpu;
+       size_t len = sizeof(ncpu);
+       static int mib[][2] = {
+#ifdef HW_NCPUONLINE
+               { CTL_HW, HW_NCPUONLINE },
+#endif
+               { CTL_HW, HW_NCPU },
+       };
+
+       for (size_t i = 0; i < ARRAY_SIZE(mib); i++) {
+               int r = sysctl(mib[i], ARRAY_SIZE(mib[i]), &ncpu, &len, NULL,
+                              0);
+               if (r != -1) {
+                       return (ncpu);
+               }
+       }
+       return (-1);
+}
+#endif /* HAVE_SYS_SYSCTL_H */
+
 #if defined(HAVE_SCHED_GETAFFINITY)
 #include <sched.h>
 
@@ -131,6 +154,11 @@ ncpus_initialize(void) {
        if (isc__os_ncpus <= 0) {
                isc__os_ncpus = sysctlbyname_ncpus();
        }
+#endif
+#if HAVE_SYS_SYSCTL_H && !defined(__linux__)
+       if (isc__os_ncpus <= 0) {
+               isc__os_ncpus = sysctl_ncpus();
+       }
 #endif
        if (isc__os_ncpus <= 0) {
                isc__os_ncpus = sysconf_ncpus();