1 /* $NetBSD: os.c,v 1.4 2014/12/10 04:38:01 christos Exp $ */
2
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /* Id: os.c,v 1.18 2007/06/19 23:47:18 tbox Exp */
21
22 #include <config.h>
23
24 #include <isc/os.h>
25
26
27 #ifdef HAVE_SYSCONF
28
29 #include <unistd.h>
30
31 #ifndef __hpux
32 static inline long
sysconf_ncpus(void)33 sysconf_ncpus(void) {
34 #if defined(_SC_NPROCESSORS_ONLN)
35 return sysconf((_SC_NPROCESSORS_ONLN));
36 #elif defined(_SC_NPROC_ONLN)
37 return sysconf((_SC_NPROC_ONLN));
38 #else
39 return (0);
40 #endif
41 }
42 #endif
43 #endif /* HAVE_SYSCONF */
44
45
46 #ifdef __hpux
47
48 #include <sys/pstat.h>
49
50 static inline int
hpux_ncpus(void)51 hpux_ncpus(void) {
52 struct pst_dynamic psd;
53 if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) != -1)
54 return (psd.psd_proc_cnt);
55 else
56 return (0);
57 }
58
59 #endif /* __hpux */
60
61 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
62 #include <sys/types.h> /* for FreeBSD */
63 #include <sys/param.h> /* for NetBSD */
64 #include <sys/sysctl.h>
65
66 static int
sysctl_ncpus(void)67 sysctl_ncpus(void) {
68 int ncpu, result;
69 size_t len;
70
71 len = sizeof(ncpu);
72 result = sysctlbyname("hw.ncpu", &ncpu, &len , 0, 0);
73 if (result != -1)
74 return (ncpu);
75 return (0);
76 }
77 #endif
78
79 unsigned int
isc_os_ncpus(void)80 isc_os_ncpus(void) {
81 long ncpus = 0;
82
83 #ifdef __hpux
84 ncpus = hpux_ncpus();
85 #elif defined(HAVE_SYSCONF)
86 ncpus = sysconf_ncpus();
87 #endif
88 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
89 if (ncpus <= 0)
90 ncpus = sysctl_ncpus();
91 #endif
92 if (ncpus <= 0)
93 ncpus = 1;
94
95 return ((unsigned int)ncpus);
96 }
97