Cleanup the sysctlbyname and friends configure checks and ifdefs

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.
This commit is contained in:
Ondřej Surý 2024-09-20 08:45:46 +02:00 committed by Ondřej Surý
parent 26e7358b16
commit 3a91c0a4e3
No known key found for this signature in database
GPG key ID: 2820F37E873DEA41
4 changed files with 62 additions and 101 deletions

View 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?

View file

@ -12,12 +12,15 @@
*/
#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) {

View file

@ -11,21 +11,19 @@
* 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>

View 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
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 <sys/param.h> /* for NetBSD */
#include <sys/sysctl.h>
#include <sys/types.h> /* 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 <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 = 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;