NumberOfCores.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C) 2012 University of Szeged. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "NumberOfCores.h"
  27. #if OS(DARWIN) || OS(OPENBSD) || OS(NETBSD) || OS(FREEBSD)
  28. #include <sys/param.h>
  29. // sys/types.h must come before sys/sysctl.h because the latter uses
  30. // data types defined in the former. See sysctl(3) and style(9).
  31. #include <sys/types.h>
  32. #include <sys/sysctl.h>
  33. #elif OS(LINUX) || OS(AIX) || OS(SOLARIS)
  34. #include <unistd.h>
  35. #elif OS(WINDOWS)
  36. #include <windows.h>
  37. #elif OS(QNX)
  38. #include <sys/syspage.h>
  39. #endif
  40. namespace WTF {
  41. int numberOfProcessorCores()
  42. {
  43. const int defaultIfUnavailable = 1;
  44. static int s_numberOfCores = -1;
  45. if (s_numberOfCores > 0)
  46. return s_numberOfCores;
  47. #if OS(DARWIN) || OS(OPENBSD) || OS(NETBSD) || OS(FREEBSD) && !OS(ORBIS)
  48. unsigned result;
  49. size_t length = sizeof(result);
  50. int name[] = {
  51. CTL_HW,
  52. HW_NCPU
  53. };
  54. int sysctlResult = sysctl(name, sizeof(name) / sizeof(int), &result, &length, 0, 0);
  55. s_numberOfCores = sysctlResult < 0 ? defaultIfUnavailable : result;
  56. #elif OS(LINUX) || OS(AIX) || OS(SOLARIS)
  57. long sysconfResult = sysconf(_SC_NPROCESSORS_ONLN);
  58. s_numberOfCores = sysconfResult < 0 ? defaultIfUnavailable : static_cast<int>(sysconfResult);
  59. #elif OS(WINDOWS)
  60. UNUSED_PARAM(defaultIfUnavailable);
  61. SYSTEM_INFO sysInfo;
  62. GetSystemInfo(&sysInfo);
  63. s_numberOfCores = sysInfo.dwNumberOfProcessors;
  64. #elif OS(QNX)
  65. UNUSED_PARAM(defaultIfUnavailable);
  66. s_numberOfCores = _syspage_ptr->num_cpu;
  67. #else
  68. s_numberOfCores = defaultIfUnavailable;
  69. #endif
  70. return s_numberOfCores;
  71. }
  72. }