os.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000, 2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: os.c,v 1.18 2007/06/19 23:47:18 tbox Exp $ */
00019 
00020 #include <config.h>
00021 
00022 #include <isc/os.h>
00023 
00024 
00025 #ifdef HAVE_SYSCONF
00026 
00027 #include <unistd.h>
00028 
00029 #ifndef __hpux
00030 static inline long
00031 sysconf_ncpus(void) {
00032 #if defined(_SC_NPROCESSORS_ONLN)
00033         return sysconf((_SC_NPROCESSORS_ONLN));
00034 #elif defined(_SC_NPROC_ONLN)
00035         return sysconf((_SC_NPROC_ONLN));
00036 #else
00037         return (0);
00038 #endif
00039 }
00040 #endif
00041 #endif /* HAVE_SYSCONF */
00042 
00043 
00044 #ifdef __hpux
00045 
00046 #include <sys/pstat.h>
00047 
00048 static inline int
00049 hpux_ncpus(void) {
00050         struct pst_dynamic psd;
00051         if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) != -1)
00052                 return (psd.psd_proc_cnt);
00053         else
00054                 return (0);
00055 }
00056 
00057 #endif /* __hpux */
00058 
00059 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
00060 #include <sys/types.h>  /* for FreeBSD */
00061 #include <sys/param.h>  /* for NetBSD */
00062 #include <sys/sysctl.h>
00063 
00064 static int
00065 sysctl_ncpus(void) {
00066         int ncpu, result;
00067         size_t len;
00068 
00069         len = sizeof(ncpu);
00070         result = sysctlbyname("hw.ncpu", &ncpu, &len , 0, 0);
00071         if (result != -1)
00072                 return (ncpu);
00073         return (0);
00074 }
00075 #endif
00076 
00077 unsigned int
00078 isc_os_ncpus(void) {
00079         long ncpus = 0;
00080 
00081 #ifdef __hpux
00082         ncpus = hpux_ncpus();
00083 #elif defined(HAVE_SYSCONF)
00084         ncpus = sysconf_ncpus();
00085 #endif
00086 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
00087         if (ncpus <= 0)
00088                 ncpus = sysctl_ncpus();
00089 #endif
00090         if (ncpus <= 0)
00091                 ncpus = 1;
00092 
00093         return ((unsigned int)ncpus);
00094 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha