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