ProcessorCount.cmake 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. ProcessorCount
  5. --------------
  6. ProcessorCount(var)
  7. Determine the number of processors/cores and save value in ${var}
  8. Sets the variable named ${var} to the number of physical cores
  9. available on the machine if the information can be determined.
  10. Otherwise it is set to 0. Currently this functionality is implemented
  11. for AIX, cygwin, FreeBSD, HPUX, Linux, macOS, QNX, Sun and
  12. Windows.
  13. .. versionchanged:: 3.15
  14. On Linux, returns the container CPU count instead of the host CPU count.
  15. This function is guaranteed to return a positive integer (>=1) if it
  16. succeeds. It returns 0 if there's a problem determining the processor
  17. count.
  18. More generally accurate physical CPU count can be obtained via
  19. :command:`cmake_host_system_information`:
  20. .. code-block:: cmake
  21. cmake_host_system_information(RESULT N
  22. QUERY NUMBER_OF_PHYSICAL_CORES)
  23. Example use, in a ctest -S dashboard script:
  24. .. code-block:: cmake
  25. include(ProcessorCount)
  26. ProcessorCount(N)
  27. if(NOT N EQUAL 0)
  28. set(CTEST_BUILD_FLAGS -j${N})
  29. set(ctest_test_args ${ctest_test_args} PARALLEL_LEVEL ${N})
  30. endif()
  31. This function is intended to offer an approximation of the value of
  32. the number of compute cores available on the current machine, such
  33. that you may use that value for parallel building and parallel
  34. testing. It is meant to help utilize as much of the machine as seems
  35. reasonable. Of course, knowledge of what else might be running on the
  36. machine simultaneously should be used when deciding whether to request
  37. a machine's full capacity all for yourself.
  38. #]=======================================================================]
  39. # A more reliable way might be to compile a small C program that uses the CPUID
  40. # instruction, but that again requires compiler support or compiling assembler
  41. # code.
  42. function(ProcessorCount var)
  43. # Unknown:
  44. set(count 0)
  45. if(WIN32)
  46. # Windows:
  47. set(count "$ENV{NUMBER_OF_PROCESSORS}")
  48. #message("ProcessorCount: WIN32, trying environment variable")
  49. endif()
  50. if(NOT count)
  51. # Mac, FreeBSD, OpenBSD (systems with sysctl):
  52. find_program(ProcessorCount_cmd_sysctl sysctl
  53. PATHS /usr/sbin /sbin)
  54. mark_as_advanced(ProcessorCount_cmd_sysctl)
  55. if(ProcessorCount_cmd_sysctl)
  56. execute_process(COMMAND ${ProcessorCount_cmd_sysctl} -n hw.ncpu
  57. ERROR_QUIET
  58. OUTPUT_STRIP_TRAILING_WHITESPACE
  59. OUTPUT_VARIABLE count)
  60. #message("ProcessorCount: trying sysctl '${ProcessorCount_cmd_sysctl}'")
  61. endif()
  62. endif()
  63. if(NOT count)
  64. # Linux (systems with nproc):
  65. # Prefer nproc to getconf if available as getconf may return the host CPU count in Linux containers
  66. find_program(ProcessorCount_cmd_nproc nproc)
  67. mark_as_advanced(ProcessorCount_cmd_nproc)
  68. if(ProcessorCount_cmd_nproc)
  69. execute_process(COMMAND ${ProcessorCount_cmd_nproc}
  70. ERROR_QUIET
  71. OUTPUT_STRIP_TRAILING_WHITESPACE
  72. OUTPUT_VARIABLE count)
  73. #message("ProcessorCount: trying nproc '${ProcessorCount_cmd_nproc}'")
  74. endif()
  75. endif()
  76. if(NOT count)
  77. # Linux (systems with getconf):
  78. find_program(ProcessorCount_cmd_getconf getconf)
  79. mark_as_advanced(ProcessorCount_cmd_getconf)
  80. if(ProcessorCount_cmd_getconf)
  81. execute_process(COMMAND ${ProcessorCount_cmd_getconf} _NPROCESSORS_ONLN
  82. ERROR_QUIET
  83. OUTPUT_STRIP_TRAILING_WHITESPACE
  84. OUTPUT_VARIABLE count)
  85. #message("ProcessorCount: trying getconf '${ProcessorCount_cmd_getconf}'")
  86. endif()
  87. endif()
  88. if(NOT count)
  89. # HPUX (systems with machinfo):
  90. find_program(ProcessorCount_cmd_machinfo machinfo
  91. PATHS /usr/contrib/bin)
  92. mark_as_advanced(ProcessorCount_cmd_machinfo)
  93. if(ProcessorCount_cmd_machinfo)
  94. execute_process(COMMAND ${ProcessorCount_cmd_machinfo}
  95. ERROR_QUIET
  96. OUTPUT_STRIP_TRAILING_WHITESPACE
  97. OUTPUT_VARIABLE machinfo_output)
  98. string(REGEX MATCHALL "Number of CPUs = ([0-9]+)" procs "${machinfo_output}")
  99. set(count "${CMAKE_MATCH_1}")
  100. if(NOT count)
  101. string(REGEX MATCHALL "([0-9]+) logical processors" procs "${machinfo_output}")
  102. set(count "${CMAKE_MATCH_1}")
  103. endif()
  104. #message("ProcessorCount: trying machinfo '${ProcessorCount_cmd_machinfo}'")
  105. else()
  106. find_program(ProcessorCount_cmd_mpsched mpsched)
  107. mark_as_advanced(ProcessorCount_cmd_mpsched)
  108. if(ProcessorCount_cmd_mpsched)
  109. execute_process(COMMAND ${ProcessorCount_cmd_mpsched} -s
  110. OUTPUT_QUIET
  111. ERROR_STRIP_TRAILING_WHITESPACE
  112. ERROR_VARIABLE mpsched_output)
  113. string(REGEX MATCHALL "Processor Count *: *([0-9]+)" procs "${mpsched_output}")
  114. set(count "${CMAKE_MATCH_1}")
  115. #message("ProcessorCount: trying mpsched -s '${ProcessorCount_cmd_mpsched}'")
  116. endif()
  117. endif()
  118. endif()
  119. if(NOT count)
  120. # AIX (systems with lsconf):
  121. find_program(ProcessorCount_cmd_lsconf lsconf
  122. PATHS /usr/sbin)
  123. mark_as_advanced(ProcessorCount_cmd_lsconf)
  124. if(ProcessorCount_cmd_lsconf)
  125. execute_process(COMMAND ${ProcessorCount_cmd_lsconf}
  126. ERROR_QUIET
  127. OUTPUT_STRIP_TRAILING_WHITESPACE
  128. OUTPUT_VARIABLE lsconf_output)
  129. string(REGEX MATCHALL "Number Of Processors: ([0-9]+)" procs "${lsconf_output}")
  130. set(count "${CMAKE_MATCH_1}")
  131. #message("ProcessorCount: trying lsconf '${ProcessorCount_cmd_lsconf}'")
  132. endif()
  133. endif()
  134. if(NOT count)
  135. # QNX (systems with pidin):
  136. find_program(ProcessorCount_cmd_pidin pidin)
  137. mark_as_advanced(ProcessorCount_cmd_pidin)
  138. if(ProcessorCount_cmd_pidin)
  139. execute_process(COMMAND ${ProcessorCount_cmd_pidin} info
  140. ERROR_QUIET
  141. OUTPUT_STRIP_TRAILING_WHITESPACE
  142. OUTPUT_VARIABLE pidin_output)
  143. string(REGEX MATCHALL "Processor[0-9]+: " procs "${pidin_output}")
  144. list(LENGTH procs count)
  145. #message("ProcessorCount: trying pidin '${ProcessorCount_cmd_pidin}'")
  146. endif()
  147. endif()
  148. if(NOT count)
  149. # Sun (systems where psrinfo tool is available)
  150. find_program(ProcessorCount_cmd_psrinfo psrinfo PATHS /usr/sbin /sbin)
  151. mark_as_advanced(ProcessorCount_cmd_psrinfo)
  152. if (ProcessorCount_cmd_psrinfo)
  153. execute_process(COMMAND ${ProcessorCount_cmd_psrinfo} -p -v
  154. ERROR_QUIET
  155. OUTPUT_STRIP_TRAILING_WHITESPACE
  156. OUTPUT_VARIABLE psrinfo_output)
  157. string(REGEX MATCHALL "has [0-9]+ virtual processor" procs "${psrinfo_output}")
  158. set(count "")
  159. foreach(proc ${procs})
  160. string(REGEX MATCH "has ([0-9]+) virtual" res ${proc})
  161. math(EXPR count "${count} + ${CMAKE_MATCH_1}")
  162. endforeach()
  163. #message("ProcessorCount: trying '${ProcessorCount_cmd_psrinfo}' -p -v")
  164. else()
  165. # Sun (systems where uname -X emits "NumCPU" in its output):
  166. find_program(ProcessorCount_cmd_uname uname)
  167. mark_as_advanced(ProcessorCount_cmd_uname)
  168. if(ProcessorCount_cmd_uname)
  169. execute_process(COMMAND ${ProcessorCount_cmd_uname} -X
  170. ERROR_QUIET
  171. OUTPUT_STRIP_TRAILING_WHITESPACE
  172. OUTPUT_VARIABLE uname_X_output)
  173. string(REGEX MATCHALL "NumCPU = ([0-9]+)" procs "${uname_X_output}")
  174. set(count "${CMAKE_MATCH_1}")
  175. #message("ProcessorCount: trying uname -X '${ProcessorCount_cmd_uname}'")
  176. endif()
  177. endif()
  178. endif()
  179. # Execute this code when all previously attempted methods return empty
  180. # output:
  181. #
  182. if(NOT count)
  183. # Systems with /proc/cpuinfo:
  184. set(cpuinfo_file /proc/cpuinfo)
  185. if(EXISTS "${cpuinfo_file}")
  186. file(STRINGS "${cpuinfo_file}" procs REGEX "^processor.: [0-9]+$")
  187. list(LENGTH procs count)
  188. #message("ProcessorCount: trying cpuinfo '${cpuinfo_file}'")
  189. endif()
  190. endif()
  191. if(NOT count)
  192. # Haiku
  193. find_program(ProcessorCount_cmd_sysinfo sysinfo)
  194. if(ProcessorCount_cmd_sysinfo)
  195. execute_process(COMMAND ${ProcessorCount_cmd_sysinfo}
  196. ERROR_QUIET
  197. OUTPUT_STRIP_TRAILING_WHITESPACE
  198. OUTPUT_VARIABLE sysinfo_X_output)
  199. string(REGEX MATCHALL "\nCPU #[0-9]+:" procs "\n${sysinfo_X_output}")
  200. list(LENGTH procs count)
  201. #message("ProcessorCount: trying sysinfo '${ProcessorCount_cmd_sysinfo}'")
  202. endif()
  203. endif()
  204. # Since cygwin builds of CMake do not define WIN32 anymore, but they still
  205. # run on Windows, and will still have this env var defined:
  206. #
  207. if(NOT count)
  208. set(count "$ENV{NUMBER_OF_PROCESSORS}")
  209. #message("ProcessorCount: last fallback, trying environment variable")
  210. endif()
  211. # Ensure an integer return (avoid inadvertently returning an empty string
  212. # or an error string)... If it's not a decimal integer, return 0:
  213. #
  214. if(NOT count MATCHES "^[0-9]+$")
  215. set(count 0)
  216. endif()
  217. set(${var} ${count} PARENT_SCOPE)
  218. endfunction()