diff --git a/configure.ac b/configure.ac index a6f0c002f0..17d8a48a5a 100644 --- a/configure.ac +++ b/configure.ac @@ -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 - #endif - #ifdef HAVE_SYS_SOCKET_H - # include - #endif + #include + #include ]) # @@ -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 - #endif - ]) +AC_CHECK_FUNCS([sched_getaffinity cpuset_getaffinity]) # # Do we want to use pthread rwlock? diff --git a/lib/isc/meminfo.c b/lib/isc/meminfo.c index 6d68afec6e..570154ce71 100644 --- a/lib/isc/meminfo.c +++ b/lib/isc/meminfo.c @@ -12,12 +12,15 @@ */ #include +#include +#include #include -#include -#if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__) +#if HAVE_SYS_SYSCTL_H && !defined(__linux__) #include -#endif /* if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__) */ +#endif + +#include uint64_t isc_meminfo_totalphys(void) { diff --git a/lib/isc/net.c b/lib/isc/net.c index 549e7a94fb..6133664bf1 100644 --- a/lib/isc/net.c +++ b/lib/isc/net.c @@ -11,21 +11,19 @@ * information regarding copyright ownership. */ -#include -#include - -#if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__) -#if defined(HAVE_SYS_PARAM_H) -#include -#endif /* if defined(HAVE_SYS_PARAM_H) */ -#include -#endif /* if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux__) */ #include #include #include +#include +#include +#include #include #include +#if HAVE_SYS_SYSCTL_H && !defined(__linux__) +#include +#endif + #include #include #include diff --git a/lib/isc/os.c b/lib/isc/os.c index 97b5cac024..2a8c62402d 100644 --- a/lib/isc/os.c +++ b/lib/isc/os.c @@ -39,46 +39,42 @@ ncpus_initialize(void) { #else /* UV_VERSION_HEX >= UV_VERSION(1, 44, 0) */ -#ifdef HAVE_SYSCONF - +#include /* for NetBSD */ +#if HAVE_SYS_SYSCTL_H && !defined(__linux__) +#include +#endif +#include /* for OpenBSD */ #include -static long -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) */ -} -#endif /* HAVE_SYSCONF */ - -#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) -#include /* for NetBSD */ -#include -#include /* for FreeBSD */ - 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); - } - return (0); +sysconf_ncpus(void) { + long ncpus = sysconf((_SC_NPROCESSORS_ONLN)); + return ((int)ncpus); } -#endif /* if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) */ + +#if HAVE_SYSCTLBYNAME +static int +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 (-1); +} +#endif /* HAVE_SYSCTLBYNAME */ #if defined(HAVE_SCHED_GETAFFINITY) - -#if defined(HAVE_SCHED_H) #include -#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 #include 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 = sysctlbyname_ncpus(); + } +#endif if (isc__os_ncpus <= 0) { isc__os_ncpus = sysconf_ncpus(); } -#endif /* if defined(HAVE_SYSCONF) */ -#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) - if (isc__os_ncpus <= 0) { - isc__os_ncpus = sysctl_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;