diff --git a/lib/isc/os.c b/lib/isc/os.c index 2a8c62402d..7f0d7cced5 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();