ProcessorCount.cmake 8.4 KB

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