From: Ondřej Surý Date: Fri, 20 Sep 2024 06:53:01 +0000 (+0200) Subject: Add support to read number of online CPUs on OpenBSD X-Git-Tag: v9.21.2~23^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=31458d405adcc967e314d757024dc1aef346c7be;p=thirdparty%2Fbind9.git Add support to read number of online CPUs on OpenBSD 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]. --- diff --git a/lib/isc/os.c b/lib/isc/os.c index 2a8c62402d1..7f0d7cced55 100644 --- a/lib/isc/os.c +++ b/lib/isc/os.c @@ -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 @@ -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();