From: David VaĊĦek Date: Thu, 10 Jun 2021 09:55:29 +0000 (+0200) Subject: dthreads: fix the detection of available CPU cores on some platforms (mainly *BSDs) X-Git-Tag: v3.1.0~68 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f19f25b6ef726226bc71dde463bf0d8ffc856a8;p=thirdparty%2Fknot-dns.git dthreads: fix the detection of available CPU cores on some platforms (mainly *BSDs) Found by Matthieu Guegan on OpenBSD. Thanks! On OpenBSD, cheloha@ describes the related change very clear: https://cvsweb.openbsd.org/src/sys/sys/sysctl.h#rev1.179 Some other sources: https://www.freebsd.org/cgi/man.cgi?smp https://man.netbsd.org/sysctl.7 https://man.openbsd.org/sysctl.2 https://man.dragonflybsd.org/?command=sysctl§ion=3 fixes #743 --- diff --git a/src/knot/server/dthreads.c b/src/knot/server/dthreads.c index f3d94e40c1..886a2c8088 100644 --- a/src/knot/server/dthreads.c +++ b/src/knot/server/dthreads.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2018 CZ.NIC, z.s.p.o. +/* Copyright (C) 2021 CZ.NIC, z.s.p.o. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -687,18 +687,28 @@ int dt_compact(dt_unit_t *unit) int dt_online_cpus(void) { int ret = -1; -/* Linux, Solaris, OS X 10.4+ */ +/* Linux, Solaris, macOS/OS X 10.4+ */ #ifdef _SC_NPROCESSORS_ONLN ret = (int) sysconf(_SC_NPROCESSORS_ONLN); #else /* FreeBSD, NetBSD, OpenBSD, OS X < 10.4 */ #if HAVE_SYSCTLBYNAME size_t rlen = sizeof(int); +#if defined(__OpenBSD__) || defined(__NetBSD__) + if (sysctlbyname("hw.ncpuonline", &ret, &rlen, NULL, 0) < 0) { + ret = -1; + } +#elif defined(__FreeBSD__) + if (sysctlbyname("kern.smp.cpus", &ret, &rlen, NULL, 0) < 0) { + ret = -1; + } +#else if (sysctlbyname("hw.ncpu", &ret, &rlen, NULL, 0) < 0) { ret = -1; } -#endif -#endif +#endif /* __OpenBSD__, __NetBSD__, __FreeBSD__, etc. */ +#endif /* HAVE_SYSCTLBYNAME */ +#endif /* _SC_NPROCESSORS_ONLN */ return ret; }