]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Cleanup the sysctlbyname and friends configure checks and ifdefs
authorOndřej Surý <ondrej@isc.org>
Fri, 20 Sep 2024 06:45:46 +0000 (08:45 +0200)
committerOndřej Surý <ondrej@isc.org>
Sat, 21 Sep 2024 10:38:33 +0000 (12:38 +0200)
Cleanup various checks and cleanups that are available on the all
platforms like sysctlbyname() and various related <sys/*.h> headers
that are either defined in POSIX or available on Linux and all BSDs.

configure.ac
lib/isc/meminfo.c
lib/isc/net.c
lib/isc/os.c

index a6f0c002f02e700c03bc86701763ad1dc181d37b..17d8a48a5ad5b356bc5b7da55125009119bc2805 100644 (file)
@@ -339,15 +339,10 @@ AS_CASE([$host],
                  ])
        ])
 
-AC_CHECK_HEADERS([sys/param.h sys/socket.h])
 AC_CHECK_HEADERS([fcntl.h regex.h sys/time.h unistd.h sys/mman.h sys/sockio.h sys/select.h sys/sysctl.h net/if6.h net/route.h linux/netlink.h linux/rtnetlink.h], [], [],
                 [$ac_includes_default
-                 #ifdef HAVE_SYS_PARAM_H
-                 # include <sys/param.h>
-                 #endif
-                 #ifdef HAVE_SYS_SOCKET_H
-                 # include <sys/socket.h>
-                 #endif
+                 #include <sys/param.h>
+                 #include <sys/socket.h>
                 ])
 
 #
@@ -549,8 +544,6 @@ AC_COMPILE_IFELSE(
 
 AC_CHECK_FUNCS([pthread_attr_getstacksize pthread_attr_setstacksize pthread_barrier_init pthread_spin_init])
 
-AC_CHECK_HEADERS([sched.h])
-
 AC_SEARCH_LIBS([sched_yield],[rt])
 AC_CHECK_FUNCS([sched_yield pthread_yield pthread_yield_np])
 
@@ -614,16 +607,9 @@ AM_CONDITIONAL([HAVE_LIBNGHTTP2], [test -n "$LIBNGHTTP2_LIBS"])
 AC_CHECK_FUNCS([flockfile getc_unlocked])
 
 #
-# Look for sysconf or other ways to allow detection of the number of processors.
+# Look for other ways to allow detection of the number of processors.
 #
-AC_CHECK_FUNCS([sysconf])
-AC_CHECK_FUNCS(sysconf sched_getaffinity cpuset_getaffinity)
-AC_CHECK_HEADERS([sys/cpuset.h], [], [],
-                [$ac_includes_default
-                 #ifdef HAVE_SYS_PARAM_H
-                 # include <sys/param.h>
-                 #endif
-                ])
+AC_CHECK_FUNCS([sched_getaffinity cpuset_getaffinity])
 
 #
 # Do we want to use pthread rwlock?
index 6d68afec6ef92474e1813356e985591821871e40..570154ce718d931ba061d696569973d5f7240168 100644 (file)
  */
 
 #include <inttypes.h>
+#include <sys/param.h>
+#include <sys/types.h>
 #include <unistd.h>
 
-#include <isc/meminfo.h>
-#if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__)
+#if HAVE_SYS_SYSCTL_H && !defined(__linux__)
 #include <sys/sysctl.h>
-#endif /* if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__) */
+#endif
+
+#include <isc/meminfo.h>
 
 uint64_t
 isc_meminfo_totalphys(void) {
index 549e7a94fb54d514ba579765c8740be796fefc97..6133664bf16bfc4c51d668d428d6e79dc1fa6700 100644 (file)
  * information regarding copyright ownership.
  */
 
-#include <stdbool.h>
-#include <sys/types.h>
-
-#if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__)
-#if defined(HAVE_SYS_PARAM_H)
-#include <sys/param.h>
-#endif /* if defined(HAVE_SYS_PARAM_H) */
-#include <sys/sysctl.h>
-#endif /* if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__) */
 #include <errno.h>
 #include <fcntl.h>
 #include <netdb.h>
+#include <stdbool.h>
+#include <sys/param.h>
+#include <sys/types.h>
 #include <sys/uio.h>
 #include <unistd.h>
 
+#if HAVE_SYS_SYSCTL_H && !defined(__linux__)
+#include <sys/sysctl.h>
+#endif
+
 #include <isc/log.h>
 #include <isc/net.h>
 #include <isc/once.h>
index 97b5cac02467aa408999632dc1a4448b33a42ac0..2a8c62402d1831c98651dcbb3fd1467e1dee7c46 100644 (file)
@@ -39,46 +39,42 @@ ncpus_initialize(void) {
 
 #else /* UV_VERSION_HEX >= UV_VERSION(1, 44, 0) */
 
-#ifdef HAVE_SYSCONF
-
+#include <sys/param.h> /* for NetBSD */
+#if HAVE_SYS_SYSCTL_H && !defined(__linux__)
+#include <sys/sysctl.h>
+#endif
+#include <sys/types.h> /* for OpenBSD */
 #include <unistd.h>
 
-static long
+static int
 sysconf_ncpus(void) {
-#if defined(_SC_NPROCESSORS_ONLN)
-       return (sysconf((_SC_NPROCESSORS_ONLN)));
-#elif defined(_SC_NPROC_ONLN)
-       return (sysconf((_SC_NPROC_ONLN)));
-#else  /* if defined(_SC_NPROCESSORS_ONLN) */
-       return (0);
-#endif /* if defined(_SC_NPROCESSORS_ONLN) */
+       long ncpus = sysconf((_SC_NPROCESSORS_ONLN));
+       return ((int)ncpus);
 }
-#endif /* HAVE_SYSCONF */
-
-#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
-#include <sys/param.h> /* for NetBSD */
-#include <sys/sysctl.h>
-#include <sys/types.h> /* for FreeBSD */
 
+#if HAVE_SYSCTLBYNAME
 static int
-sysctl_ncpus(void) {
-       int ncpu, result;
-       size_t len;
-
-       len = sizeof(ncpu);
-       result = sysctlbyname("hw.ncpu", &ncpu, &len, 0, 0);
-       if (result != -1) {
-               return (ncpu);
+sysctlbyname_ncpus(void) {
+       int ncpu;
+       size_t len = sizeof(ncpu);
+       static const char *mib[] = {
+               "hw.activecpu",
+               "hw.logicalcpu",
+               "hw.ncpu",
+       };
+
+       for (size_t i = 0; i < ARRAY_SIZE(mib); i++) {
+               int r = sysctlbyname(mib[i], &ncpu, &len, NULL, 0);
+               if (r != -1) {
+                       return (ncpu);
+               }
        }
-       return (0);
+       return (-1);
 }
-#endif /* if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) */
+#endif /* HAVE_SYSCTLBYNAME */
 
 #if defined(HAVE_SCHED_GETAFFINITY)
-
-#if defined(HAVE_SCHED_H)
 #include <sched.h>
-#endif
 
 /*
  * Administrators may wish to constrain the set of cores that BIND runs
@@ -92,56 +88,36 @@ sysctl_ncpus(void) {
 static int
 sched_affinity_ncpus(void) {
        cpu_set_t cpus;
-       int result;
-
-       result = sched_getaffinity(0, sizeof(cpus), &cpus);
-       if (result != -1) {
-#ifdef CPU_COUNT
+       int r = sched_getaffinity(0, sizeof(cpus), &cpus);
+       if (r != -1) {
                return (CPU_COUNT(&cpus));
-#else
-               int i, n = 0;
-
-               for (i = 0; i < CPU_SETSIZE; ++i) {
-                       if (CPU_ISSET(i, &cpus))
-                               ++n;
-               }
-               return (n);
-#endif
        }
-       return (0);
+       return (-1);
 }
 #endif
 
 /*
  * Affinity detecting variant of sched_affinity_cpus() for FreeBSD
  */
-
-#if defined(HAVE_SYS_CPUSET_H) && defined(HAVE_CPUSET_GETAFFINITY)
+#if defined(HAVE_CPUSET_GETAFFINITY)
 #include <sys/cpuset.h>
 #include <sys/param.h>
 
 static int
 cpuset_affinity_ncpus(void) {
        cpuset_t cpus;
-       int result;
-
-       result = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
-                                   sizeof(cpus), &cpus);
-       if (result != -1) {
-               int i, n = 0;
-               for (i = 0; i < CPU_SETSIZE; ++i) {
-                       if (CPU_ISSET(i, &cpus))
-                               ++n;
-               }
-               return (n);
+       int r = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
+                                  sizeof(cpus), &cpus);
+       if (r != -1) {
+               return (CPU_COUNT(&cpus));
        }
-       return (0);
+       return (-1);
 }
 #endif
 
 static void
 ncpus_initialize(void) {
-#if defined(HAVE_SYS_CPUSET_H) && defined(HAVE_CPUSET_GETAFFINITY)
+#if defined(HAVE_CPUSET_GETAFFINITY)
        if (isc__os_ncpus <= 0) {
                isc__os_ncpus = cpuset_affinity_ncpus();
        }
@@ -151,16 +127,14 @@ ncpus_initialize(void) {
                isc__os_ncpus = sched_affinity_ncpus();
        }
 #endif
-#if defined(HAVE_SYSCONF)
+#if HAVE_SYSCTLBYNAME
        if (isc__os_ncpus <= 0) {
-               isc__os_ncpus = sysconf_ncpus();
+               isc__os_ncpus = sysctlbyname_ncpus();
        }
-#endif /* if defined(HAVE_SYSCONF) */
-#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
+#endif
        if (isc__os_ncpus <= 0) {
-               isc__os_ncpus = sysctl_ncpus();
+               isc__os_ncpus = sysconf_ncpus();
        }
-#endif /* if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) */
        if (isc__os_ncpus <= 0) {
                isc__os_ncpus = 1;
        }
@@ -193,7 +167,7 @@ void
 isc__os_initialize(void) {
        umask_initialize();
        ncpus_initialize();
-#if defined(HAVE_SYSCONF) && defined(_SC_LEVEL1_DCACHE_LINESIZE)
+#if defined(_SC_LEVEL1_DCACHE_LINESIZE)
        long s = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
        if (s > 0 && (unsigned long)s > isc__os_cacheline) {
                isc__os_cacheline = s;