1290001Sglebius/*
2290001Sglebius * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3290001Sglebius * Copyright (C) 2000, 2001  Internet Software Consortium.
4290001Sglebius *
5290001Sglebius * Permission to use, copy, modify, and/or distribute this software for any
6290001Sglebius * purpose with or without fee is hereby granted, provided that the above
7290001Sglebius * copyright notice and this permission notice appear in all copies.
8290001Sglebius *
9290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10290001Sglebius * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11290001Sglebius * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12290001Sglebius * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13290001Sglebius * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14290001Sglebius * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15290001Sglebius * PERFORMANCE OF THIS SOFTWARE.
16290001Sglebius */
17290001Sglebius
18290001Sglebius/* $Id: os.c,v 1.18 2007/06/19 23:47:18 tbox Exp $ */
19290001Sglebius
20290001Sglebius#include <config.h>
21290001Sglebius
22290001Sglebius#include <isc/os.h>
23290001Sglebius
24290001Sglebius
25290001Sglebius#ifdef HAVE_SYSCONF
26290001Sglebius
27290001Sglebius#include <unistd.h>
28290001Sglebius
29290001Sglebius#ifndef __hpux
30290001Sglebiusstatic inline long
31290001Sglebiussysconf_ncpus(void) {
32290001Sglebius#if defined(_SC_NPROCESSORS_ONLN)
33290001Sglebius	return sysconf((_SC_NPROCESSORS_ONLN));
34290001Sglebius#elif defined(_SC_NPROC_ONLN)
35290001Sglebius	return sysconf((_SC_NPROC_ONLN));
36290001Sglebius#else
37290001Sglebius	return (0);
38290001Sglebius#endif
39290001Sglebius}
40290001Sglebius#endif
41290001Sglebius#endif /* HAVE_SYSCONF */
42290001Sglebius
43290001Sglebius
44290001Sglebius#ifdef __hpux
45290001Sglebius
46290001Sglebius#include <sys/pstat.h>
47290001Sglebius
48290001Sglebiusstatic inline int
49290001Sglebiushpux_ncpus(void) {
50290001Sglebius	struct pst_dynamic psd;
51290001Sglebius	if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) != -1)
52290001Sglebius		return (psd.psd_proc_cnt);
53290001Sglebius	else
54290001Sglebius		return (0);
55290001Sglebius}
56290001Sglebius
57290001Sglebius#endif /* __hpux */
58290001Sglebius
59290001Sglebius#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
60290001Sglebius#include <sys/types.h>  /* for FreeBSD */
61290001Sglebius#include <sys/param.h>  /* for NetBSD */
62290001Sglebius#include <sys/sysctl.h>
63290001Sglebius
64290001Sglebiusstatic int
65290001Sglebiussysctl_ncpus(void) {
66290001Sglebius	int ncpu, result;
67290001Sglebius	size_t len;
68290001Sglebius
69290001Sglebius	len = sizeof(ncpu);
70290001Sglebius	result = sysctlbyname("hw.ncpu", &ncpu, &len , 0, 0);
71290001Sglebius	if (result != -1)
72290001Sglebius		return (ncpu);
73290001Sglebius	return (0);
74290001Sglebius}
75290001Sglebius#endif
76290001Sglebius
77290001Sglebiusunsigned int
78290001Sglebiusisc_os_ncpus(void) {
79290001Sglebius	long ncpus = 0;
80290001Sglebius
81290001Sglebius#ifdef __hpux
82290001Sglebius	ncpus = hpux_ncpus();
83290001Sglebius#elif defined(HAVE_SYSCONF)
84290001Sglebius	ncpus = sysconf_ncpus();
85290001Sglebius#endif
86290001Sglebius#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
87290001Sglebius	if (ncpus <= 0)
88290001Sglebius		ncpus = sysctl_ncpus();
89290001Sglebius#endif
90290001Sglebius	if (ncpus <= 0)
91290001Sglebius		ncpus = 1;
92290001Sglebius
93290001Sglebius	return ((unsigned int)ncpus);
94290001Sglebius}
95