1 /* $NetBSD: os.c,v 1.1 2024/02/18 20:57:57 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 #include <isc/os.h> 17 18 #ifdef HAVE_SYSCONF 19 20 #include <unistd.h> 21 22 static long sysconf_ncpus(void)23sysconf_ncpus(void) { 24 #if defined(_SC_NPROCESSORS_ONLN) 25 return (sysconf((_SC_NPROCESSORS_ONLN))); 26 #elif defined(_SC_NPROC_ONLN) 27 return (sysconf((_SC_NPROC_ONLN))); 28 #else /* if defined(_SC_NPROCESSORS_ONLN) */ 29 return (0); 30 #endif /* if defined(_SC_NPROCESSORS_ONLN) */ 31 } 32 #endif /* HAVE_SYSCONF */ 33 34 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) 35 #include <sys/param.h> /* for NetBSD */ 36 #include <sys/sysctl.h> 37 #include <sys/types.h> /* for FreeBSD */ 38 39 static int sysctl_ncpus(void)40sysctl_ncpus(void) { 41 int ncpu, result; 42 size_t len; 43 44 len = sizeof(ncpu); 45 result = sysctlbyname("hw.ncpu", &ncpu, &len, 0, 0); 46 if (result != -1) { 47 return (ncpu); 48 } 49 return (0); 50 } 51 #endif /* if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) */ 52 53 unsigned int isc_os_ncpus(void)54isc_os_ncpus(void) { 55 long ncpus = 0; 56 57 #if defined(HAVE_SYSCONF) 58 ncpus = sysconf_ncpus(); 59 #endif /* if defined(HAVE_SYSCONF) */ 60 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) 61 if (ncpus <= 0) { 62 ncpus = sysctl_ncpus(); 63 } 64 #endif /* if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME) */ 65 if (ncpus <= 0) { 66 ncpus = 1; 67 } 68 69 return ((unsigned int)ncpus); 70 } 71