/contrib/bind9/lib/isc/unix/os.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 94 lines · 59 code · 18 blank · 17 comment · 8 complexity · 6c272d1af00b5894927c6d97b3b2c404 MD5 · raw file

  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. /* $Id: os.c,v 1.18 2007/06/19 23:47:18 tbox Exp $ */
  18. #include <config.h>
  19. #include <isc/os.h>
  20. #ifdef HAVE_SYSCONF
  21. #include <unistd.h>
  22. #ifndef __hpux
  23. static inline long
  24. sysconf_ncpus(void) {
  25. #if defined(_SC_NPROCESSORS_ONLN)
  26. return sysconf((_SC_NPROCESSORS_ONLN));
  27. #elif defined(_SC_NPROC_ONLN)
  28. return sysconf((_SC_NPROC_ONLN));
  29. #else
  30. return (0);
  31. #endif
  32. }
  33. #endif
  34. #endif /* HAVE_SYSCONF */
  35. #ifdef __hpux
  36. #include <sys/pstat.h>
  37. static inline int
  38. hpux_ncpus(void) {
  39. struct pst_dynamic psd;
  40. if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) != -1)
  41. return (psd.psd_proc_cnt);
  42. else
  43. return (0);
  44. }
  45. #endif /* __hpux */
  46. #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
  47. #include <sys/types.h> /* for FreeBSD */
  48. #include <sys/param.h> /* for NetBSD */
  49. #include <sys/sysctl.h>
  50. static int
  51. sysctl_ncpus(void) {
  52. int ncpu, result;
  53. size_t len;
  54. len = sizeof(ncpu);
  55. result = sysctlbyname("hw.ncpu", &ncpu, &len , 0, 0);
  56. if (result != -1)
  57. return (ncpu);
  58. return (0);
  59. }
  60. #endif
  61. unsigned int
  62. isc_os_ncpus(void) {
  63. long ncpus = 0;
  64. #ifdef __hpux
  65. ncpus = hpux_ncpus();
  66. #elif defined(HAVE_SYSCONF)
  67. ncpus = sysconf_ncpus();
  68. #endif
  69. #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
  70. if (ncpus <= 0)
  71. ncpus = sysctl_ncpus();
  72. #endif
  73. if (ncpus <= 0)
  74. ncpus = 1;
  75. return ((unsigned int)ncpus);
  76. }