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