1258945Sroberto/*
2258945Sroberto * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 2000, 2001  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18258945Sroberto/* $Id: os.c,v 1.18 2007/06/19 23:47:18 tbox Exp $ */
19258945Sroberto
20258945Sroberto#include <config.h>
21258945Sroberto
22258945Sroberto#include <isc/os.h>
23258945Sroberto
24258945Sroberto
25258945Sroberto#ifdef HAVE_SYSCONF
26258945Sroberto
27258945Sroberto#include <unistd.h>
28258945Sroberto
29258945Sroberto#ifndef __hpux
30258945Srobertostatic inline long
31258945Srobertosysconf_ncpus(void) {
32258945Sroberto#if defined(_SC_NPROCESSORS_ONLN)
33258945Sroberto	return sysconf((_SC_NPROCESSORS_ONLN));
34258945Sroberto#elif defined(_SC_NPROC_ONLN)
35258945Sroberto	return sysconf((_SC_NPROC_ONLN));
36258945Sroberto#else
37258945Sroberto	return (0);
38258945Sroberto#endif
39258945Sroberto}
40258945Sroberto#endif
41258945Sroberto#endif /* HAVE_SYSCONF */
42258945Sroberto
43258945Sroberto
44258945Sroberto#ifdef __hpux
45258945Sroberto
46258945Sroberto#include <sys/pstat.h>
47258945Sroberto
48258945Srobertostatic inline int
49258945Srobertohpux_ncpus(void) {
50258945Sroberto	struct pst_dynamic psd;
51258945Sroberto	if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) != -1)
52258945Sroberto		return (psd.psd_proc_cnt);
53258945Sroberto	else
54258945Sroberto		return (0);
55258945Sroberto}
56258945Sroberto
57258945Sroberto#endif /* __hpux */
58258945Sroberto
59258945Sroberto#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
60258945Sroberto#include <sys/types.h>  /* for FreeBSD */
61258945Sroberto#include <sys/param.h>  /* for NetBSD */
62258945Sroberto#include <sys/sysctl.h>
63258945Sroberto
64258945Srobertostatic int
65258945Srobertosysctl_ncpus(void) {
66258945Sroberto	int ncpu, result;
67258945Sroberto	size_t len;
68258945Sroberto
69258945Sroberto	len = sizeof(ncpu);
70258945Sroberto	result = sysctlbyname("hw.ncpu", &ncpu, &len , 0, 0);
71258945Sroberto	if (result != -1)
72258945Sroberto		return (ncpu);
73258945Sroberto	return (0);
74258945Sroberto}
75258945Sroberto#endif
76258945Sroberto
77258945Srobertounsigned int
78258945Srobertoisc_os_ncpus(void) {
79258945Sroberto	long ncpus = 0;
80258945Sroberto
81258945Sroberto#ifdef __hpux
82258945Sroberto	ncpus = hpux_ncpus();
83258945Sroberto#elif defined(HAVE_SYSCONF)
84258945Sroberto	ncpus = sysconf_ncpus();
85258945Sroberto#endif
86258945Sroberto#if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
87258945Sroberto	if (ncpus <= 0)
88258945Sroberto		ncpus = sysctl_ncpus();
89258945Sroberto#endif
90258945Sroberto	if (ncpus <= 0)
91258945Sroberto		ncpus = 1;
92258945Sroberto
93258945Sroberto	return ((unsigned int)ncpus);
94258945Sroberto}
95