From 47a191489e00e71473f2822e3d8d88006d9d16a5 Mon Sep 17 00:00:00 2001 From: Guilherme Lawless Date: Sun, 7 Nov 2021 20:37:48 +0000 Subject: [PATCH] cmake: sitl_gazebo build use memory information from the system to estimate the parallel jobs Using cmake_host_system_information, grabs AVAILABLE_PHYSICAL_MEMORY and adds another job for every 1.5GB of available memory. This is tested on a single system with 16 logical cores and 16GB RAM (~11.5GB available, reported correctly by cmake). --- platforms/posix/cmake/sitl_target.cmake | 36 ++++++++++++++++++++----- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/platforms/posix/cmake/sitl_target.cmake b/platforms/posix/cmake/sitl_target.cmake index e13d947462..ffae877cb0 100644 --- a/platforms/posix/cmake/sitl_target.cmake +++ b/platforms/posix/cmake/sitl_target.cmake @@ -21,15 +21,37 @@ px4_add_git_submodule(TARGET git_jmavsim PATH "${PX4_SOURCE_DIR}/Tools/jMAVSim") # Add support for external project building include(ExternalProject) -include(ProcessorCount) -set(build_cores 1) +# Estimate an appropriate number of parallel jobs +cmake_host_system_information(RESULT AVAILABLE_PHYSICAL_MEMORY QUERY AVAILABLE_PHYSICAL_MEMORY) +cmake_host_system_information(RESULT NUMBER_OF_LOGICAL_CORES QUERY NUMBER_OF_LOGICAL_CORES) -ProcessorCount(N) -if(N GREATER_EQUAL 4) - math(EXPR build_cores "${N} - 2") +set(parallel_jobs 1) + +if(NOT NUMBER_OF_LOGICAL_CORES) + include(ProcessorCount) + ProcessorCount(NUMBER_OF_LOGICAL_CORES) endif() +if(NOT AVAILABLE_PHYSICAL_MEMORY AND NUMBER_OF_LOGICAL_CORES GREATER_EQUAL 4) + # Memory estimate unavailable, use N-2 jobs + math(EXPR parallel_jobs "${NUMBER_OF_LOGICAL_CORES} - 2") +endif() + +if (AVAILABLE_PHYSICAL_MEMORY) + # Allow an additional job for every 1.5GB of available physical memory + math(EXPR parallel_jobs "${AVAILABLE_PHYSICAL_MEMORY}/(3*1024/2)") +else() + set(AVAILABLE_PHYSICAL_MEMORY "?") +endif() + +if(parallel_jobs GREATER NUMBER_OF_LOGICAL_CORES) + set(parallel_jobs ${NUMBER_OF_LOGICAL_CORES}) +endif() + +message(DEBUG "${NUMBER_OF_LOGICAL_CORES} logical cores detected and ${AVAILABLE_PHYSICAL_MEMORY} megabytes of memory available. + Limiting sitl_gazebo and simulation-ignition concurrent jobs to ${parallel_jobs}") + # project to build sitl_gazebo if necessary px4_add_git_submodule(TARGET git_gazebo PATH "${PX4_SOURCE_DIR}/Tools/sitl_gazebo") ExternalProject_Add(sitl_gazebo @@ -45,7 +67,7 @@ ExternalProject_Add(sitl_gazebo USES_TERMINAL_BUILD true EXCLUDE_FROM_ALL true BUILD_ALWAYS 1 - BUILD_COMMAND ${CMAKE_COMMAND} --build -- -j ${build_cores} + BUILD_COMMAND ${CMAKE_COMMAND} --build -- -j ${parallel_jobs} ) px4_add_git_submodule(TARGET git_ign_gazebo PATH "${PX4_SOURCE_DIR}/Tools/simulation-ignition") @@ -59,7 +81,7 @@ ExternalProject_Add(simulation-ignition USES_TERMINAL_BUILD true EXCLUDE_FROM_ALL true BUILD_ALWAYS 1 - BUILD_COMMAND ${CMAKE_COMMAND} --build -- -j ${build_cores} + BUILD_COMMAND ${CMAKE_COMMAND} --build -- -j ${parallel_jobs} ) ExternalProject_Add(mavsdk_tests