]> git.ipfire.org Git - thirdparty/knot-dns.git/commitdiff
dthreads: fix the detection of available CPU cores on some platforms (mainly *BSDs)
authorDavid Vašek <david.vasek@nic.cz>
Thu, 10 Jun 2021 09:55:29 +0000 (11:55 +0200)
committerDaniel Salzman <daniel.salzman@nic.cz>
Thu, 10 Jun 2021 11:08:47 +0000 (13:08 +0200)
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&section=3

fixes #743

src/knot/server/dthreads.c

index f3d94e40c10ca8691e0edd8d36a0a6b77e598ed6..886a2c808814245c7f4f5bfc013d1f981ce31083 100644 (file)
@@ -1,4 +1,4 @@
-/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
+/*  Copyright (C) 2021 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
 
     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;
 }