Skip to content

Commit

Permalink
Add support for testing if a portion of code allocates memory via Mem…
Browse files Browse the repository at this point in the history
…oryAllocationMonitor (ami-iit#768)
  • Loading branch information
traversaro authored Apr 8, 2024
1 parent b711a64 commit 5ae4e0a
Show file tree
Hide file tree
Showing 21 changed files with 593 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project are documented in this file.
- Add getJointTorques to python binding in SensorBridge (https://github.com/ami-iit/bipedal-locomotion-framework/pull/825)
- Add `System::TimeProfiler` class (https://github.com/ami-iit/bipedal-locomotion-framework/pull/826)
- Add the possibility to programmatically build a `QPTSID` problem from the content of a `ParametersHandler` (https://github.com/ami-iit/bipedal-locomotion-framework/pull/828)
- Add support for testing if a portion of code allocates memory via `MemoryAllocationMonitor`` (https://github.com/ami-iit/bipedal-locomotion-framework/pull/768)

### Changed
- 🤖 [ergoCubSN001] Add logging of the wrist and fix the name of the waist imu (https://github.com/ami-iit/bipedal-locomotion-framework/pull/810)
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ install_basic_package_files(${PROJECT_NAME}
NAMESPACE BipedalLocomotion::
VERSION ${BipedalLocomotionFramework_VERSION}
COMPATIBILITY AnyNewerVersion
TARGETS_PROPERTY BipedalLocomotionFramework_TARGETS
TARGETS_PROPERTY BipedalLocomotionFramework_INSTALLED_TARGETS
VARS_PREFIX ${PROJECT_NAME}
NO_CHECK_REQUIRED_COMPONENTS_MACRO
DEPENDENCIES ${FRAMEWORK_PUBLIC_DEPENDENCIES}
Expand Down
4 changes: 2 additions & 2 deletions cmake/AddBipedalLocomotionFrameworkTarget.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# BSD-3-Clause license.

get_property(umbrella_includes_list GLOBAL PROPERTY umbrella_includes)
get_property(target_list GLOBAL PROPERTY BipedalLocomotionFramework_TARGETS)
get_property(target_list GLOBAL PROPERTY BipedalLocomotionFramework_INSTALLED_TARGETS)

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/Framework.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/Autogenerated/BipedalLocomotion/Framework.h" @ONLY)
Expand All @@ -21,4 +21,4 @@ install(TARGETS Framework
COMPONENT runtime)

add_library(BipedalLocomotion::Framework ALIAS Framework)
set_property(GLOBAL APPEND PROPERTY BipedalLocomotionFramework_TARGETS Framework)
set_property(GLOBAL APPEND PROPERTY BipedalLocomotionFramework_INSTALLED_TARGETS Framework)
33 changes: 20 additions & 13 deletions cmake/AddBipedalLocomotionLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

function(add_bipedal_locomotion_library)

set(options IS_INTERFACE SKIP_INSTALL_CHECK)
set(options IS_INTERFACE SKIP_INSTALL_CHECK SKIP_INSTALL)
set(oneValueArgs NAME INSTALLATION_FOLDER)
set(multiValueArgs
SOURCES
Expand All @@ -26,6 +26,7 @@ function(add_bipedal_locomotion_library)
set(installation_folder ${${prefix}_INSTALLATION_FOLDER})
set(is_interface ${${prefix}_IS_INTERFACE})
set(skip_install_check ${${prefix}_SKIP_INSTALL_CHECK})
set(skip_install ${${prefix}_SKIP_INSTALL})
set(sources ${${prefix}_SOURCES})
set(public_headers ${${prefix}_PUBLIC_HEADERS})
set(private_headers ${${prefix}_PRIVATE_HEADERS})
Expand Down Expand Up @@ -58,8 +59,10 @@ function(add_bipedal_locomotion_library)
EXPORT ${PROJECT_NAME}
COMPONENT runtime)

install(FILES ${public_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}")
install(FILES ${private_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}/impl")
if (NOT ${skip_install})
install(FILES ${public_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}")
install(FILES ${private_headers} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}/impl")
endif()

else()

Expand All @@ -86,15 +89,17 @@ function(add_bipedal_locomotion_library)
target_include_directories(${name} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")

# Specify installation targets, typology and destination folders.
install(TARGETS ${name}
EXPORT ${PROJECT_NAME}
COMPONENT runtime
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT lib
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}" COMPONENT dev
PRIVATE_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}/impl" COMPONENT dev)
if (NOT ${skip_install})
# Specify installation targets, typology and destination folders.
install(TARGETS ${name}
EXPORT ${PROJECT_NAME}
COMPONENT runtime
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT lib
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}" COMPONENT dev
PRIVATE_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}/impl" COMPONENT dev)
endif()

endif()

Expand Down Expand Up @@ -129,7 +134,9 @@ function(add_bipedal_locomotion_library)
add_subdirectory(${subdir})
endforeach()

set_property(GLOBAL APPEND PROPERTY BipedalLocomotionFramework_TARGETS ${name})
if (NOT ${skip_install})
set_property(GLOBAL APPEND PROPERTY BipedalLocomotionFramework_INSTALLED_TARGETS ${name})
endif()

message(STATUS "Created target ${name} for export ${PROJECT_NAME}.")

Expand Down
24 changes: 24 additions & 0 deletions cmake/AddBipedalLocomotionUnitTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,32 @@ function(add_bipedal_test)

add_test(NAME ${targetname} COMMAND ${targetname})

if(FRAMEWORK_RUN_MemoryAllocationMonitor_tests)
# When possible use `path_list_prepend` to permit to set other LD_PRELOAD
if (CMAKE_MINIMUM_REQUIRED_VERSION VERSION_GREATER_EQUAL 3.22)
message(AUTHOR_WARNING "cmake_minimum_required is now 3.22, cleanup the if following this message command.")
endif()
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.22)
set_tests_properties(${targetname} PROPERTIES
ENVIRONMENT_MODIFICATION "LD_PRELOAD=path_list_prepend:$<TARGET_FILE:BipedalLocomotion::MemoryAllocationMonitorPreload>")
else()
set_tests_properties(${targetname} PROPERTIES
ENVIRONMENT "LD_PRELOAD=$<TARGET_FILE:BipedalLocomotion::MemoryAllocationMonitorPreload>")
endif()
endif()

if(FRAMEWORK_RUN_Valgrind_tests)
add_test(NAME memcheck_${targetname} COMMAND ${MEMCHECK_COMMAND_COMPLETE} $<TARGET_FILE:${targetname}>)

if(FRAMEWORK_RUN_MemoryAllocationMonitor_tests)
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.22)
set_tests_properties(memcheck_${targetname} PROPERTIES
ENVIRONMENT_MODIFICATION "LD_PRELOAD=path_list_prepend:$<TARGET_FILE:BipedalLocomotion::MemoryAllocationMonitorPreload>")
else()
set_tests_properties(memcheck_${targetname} PROPERTIES
ENVIRONMENT "LD_PRELOAD=$<TARGET_FILE:BipedalLocomotion::MemoryAllocationMonitorPreload>")
endif()
endif()
endif()

endif()
Expand Down
2 changes: 1 addition & 1 deletion cmake/AddBipedalLocomotionYARPThrift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function(add_bipedal_locomotion_yarp_thrift)
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}" COMPONENT dev
PRIVATE_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/BipedalLocomotion/${installation_folder}/impl" COMPONENT dev)

set_property(GLOBAL APPEND PROPERTY BipedalLocomotionFramework_TARGETS ${name})
set_property(GLOBAL APPEND PROPERTY BipedalLocomotionFramework_INSTALLED_TARGETS ${name})

message(STATUS "Created target ${name} for export ${PROJECT_NAME}.")

Expand Down
20 changes: 19 additions & 1 deletion cmake/BipedalLocomotionFrameworkDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,29 @@ checkandset_dependency(UnicyclePlanner)
find_package(onnxruntime QUIET)
checkandset_dependency(onnxruntime)

########################## Components ##############################
########################## Test-related options ##############################

# MemoryAllocationMonitor require glibc >= 2.35
set(FRAMEWORK_GLIBC_GEQ_2_35 OFF)
if(BUILD_TESTING AND UNIX AND NOT APPLE)
execute_process(COMMAND ldd --version
OUTPUT_VARIABLE FRAMEWORK_LDD_VERSION_OUTPUT)
string(REGEX MATCH "GLIBC ([0-9]+.[0-9]+)" FRAMEWORK_LDD_REGEX_OUTPUT ${FRAMEWORK_LDD_VERSION_OUTPUT})
set(FRAMEWORK_LDD_GLIBC_VERSION ${CMAKE_MATCH_1})
if(FRAMEWORK_LDD_GLIBC_VERSION VERSION_GREATER_EQUAL "2.35")
set(FRAMEWORK_GLIBC_GEQ_2_35 ON)
endif()
endif()
framework_dependent_option(FRAMEWORK_RUN_MemoryAllocationMonitor_tests
"Run MemoryAllocationMonitor tests?" ON
"BUILD_TESTING;UNIX;NOT APPLE;FRAMEWORK_GLIBC_GEQ_2_35" OFF)

framework_dependent_option(FRAMEWORK_RUN_Valgrind_tests
"Run Valgrind tests?" OFF
"BUILD_TESTING;VALGRIND_FOUND" OFF)

########################## Components ##############################

framework_dependent_option(FRAMEWORK_COMPILE_YarpUtilities
"Compile YarpHelper library?" ON
"FRAMEWORK_USE_YARP" OFF)
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# This software may be modified and distributed under the terms of the
# BSD-3-Clause license.

# Not-installed library
add_subdirectory(TestUtils)

add_subdirectory(ContinuousDynamicalSystem)
add_subdirectory(TextLogging)
add_subdirectory(GenericContainer)
Expand Down
12 changes: 8 additions & 4 deletions src/ContinuousDynamicalSystem/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
add_bipedal_test(
NAME IntegratorLinearSystemTest
SOURCES IntegratorLinearSystem.cpp
LINKS BipedalLocomotion::ContinuousDynamicalSystem Eigen3::Eigen)
LINKS BipedalLocomotion::ContinuousDynamicalSystem
BipedalLocomotion::TestUtils Eigen3::Eigen)


add_bipedal_test(
NAME IntegratorFloatingBaseSystemKinematicsTest
SOURCES IntegratorFloatingBaseSystemKinematics.cpp
LINKS BipedalLocomotion::ContinuousDynamicalSystem Eigen3::Eigen)
LINKS BipedalLocomotion::ContinuousDynamicalSystem
BipedalLocomotion::TestUtils Eigen3::Eigen)

add_bipedal_test(
NAME FirstOrderSmootherTest
SOURCES FirstOrderSmoother.cpp
LINKS BipedalLocomotion::ContinuousDynamicalSystem Eigen3::Eigen)
LINKS BipedalLocomotion::ContinuousDynamicalSystem
BipedalLocomotion::TestUtils Eigen3::Eigen)

add_bipedal_test(
NAME MultiStateWeightProviderTest
SOURCES MultiStateWeightProvider.cpp
LINKS BipedalLocomotion::ContinuousDynamicalSystem Eigen3::Eigen)
LINKS BipedalLocomotion::ContinuousDynamicalSystem
BipedalLocomotion::TestUtils Eigen3::Eigen)
13 changes: 13 additions & 0 deletions src/ContinuousDynamicalSystem/tests/FirstOrderSmoother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

#include <BipedalLocomotion/ContinuousDynamicalSystem/FirstOrderSmoother.h>
#include <BipedalLocomotion/ParametersHandler/StdImplementation.h>
#include <BipedalLocomotion/TestUtils/MemoryAllocationMonitor.h>

using namespace BipedalLocomotion::ContinuousDynamicalSystem;
using namespace BipedalLocomotion::TestUtils;

TEST_CASE("First order smoother")
{
Expand Down Expand Up @@ -89,7 +91,18 @@ TEST_CASE("First order smoother")
}

// advance the smoother

// We only test no memory allocation from the second step,
// as FirstOrderSmoother allocates memory at the first step
if (i >= 1)
{
MemoryAllocationMonitor::startMonitor();
}
REQUIRE(smoother.advance());
if (i >= 1)
{
REQUIRE(MemoryAllocationMonitor::endMonitorAndCheckNoMemoryAllocationInLastMonitor());
}
}

// check the settling time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

#include <BipedalLocomotion/ContinuousDynamicalSystem/FloatingBaseSystemKinematics.h>
#include <BipedalLocomotion/ContinuousDynamicalSystem/ForwardEuler.h>
#include <BipedalLocomotion/TestUtils/MemoryAllocationMonitor.h>

using namespace BipedalLocomotion::ContinuousDynamicalSystem;
using namespace BipedalLocomotion::TestUtils;

TEST_CASE("Integrator - Linear system")
{
Expand Down Expand Up @@ -73,6 +75,14 @@ TEST_CASE("Integrator - Linear system")
REQUIRE(basePosition.isApprox(basePositionExact, tolerance));
REQUIRE(jointPosition.isApprox(jointPositionExact, tolerance));

if (i >= 1)
{
MemoryAllocationMonitor::startMonitor();
}
REQUIRE(integrator.integrate(0s, dT));
if (i >= 1)
{
REQUIRE(MemoryAllocationMonitor::endMonitorAndCheckNoMemoryAllocationInLastMonitor());
}
}
}
24 changes: 24 additions & 0 deletions src/ContinuousDynamicalSystem/tests/IntegratorLinearSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include <BipedalLocomotion/ContinuousDynamicalSystem/ForwardEuler.h>
#include <BipedalLocomotion/ContinuousDynamicalSystem/LinearTimeInvariantSystem.h>
#include <BipedalLocomotion/ContinuousDynamicalSystem/RK4.h>
#include <BipedalLocomotion/TestUtils/MemoryAllocationMonitor.h>

using namespace BipedalLocomotion::ContinuousDynamicalSystem;
using namespace BipedalLocomotion::TestUtils;

TEST_CASE("Integrator - Linear system")
{
Expand Down Expand Up @@ -76,7 +78,18 @@ TEST_CASE("Integrator - Linear system")
REQUIRE(
solution.isApprox(closeFormSolution(std::chrono::duration<double>(dT * i).count()),
tolerance));

// We only test no memory allocation from the second step,
// as ForwardEuler allocates memory at the first step
if (i >= 1)
{
MemoryAllocationMonitor::startMonitor();
}
REQUIRE(integrator.integrate(0s, dT));
if (i >= 1)
{
REQUIRE(MemoryAllocationMonitor::endMonitorAndCheckNoMemoryAllocationInLastMonitor());
}
}
}

Expand All @@ -94,7 +107,18 @@ TEST_CASE("Integrator - Linear system")
REQUIRE(
solution.isApprox(closeFormSolution(std::chrono::duration<double>(dT * i).count()),
tolerance));

// We only test no memory allocation from the second step,
// as RK4 allocates memory at the first step
if (i >= 1)
{
MemoryAllocationMonitor::startMonitor();
}
REQUIRE(integrator.integrate(0s, dT));
if (i >=1)
{
REQUIRE(MemoryAllocationMonitor::endMonitorAndCheckNoMemoryAllocationInLastMonitor());
}
}
}
}
26 changes: 26 additions & 0 deletions src/ContinuousDynamicalSystem/tests/MultiStateWeightProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

#include <BipedalLocomotion/ContinuousDynamicalSystem/MultiStateWeightProvider.h>
#include <BipedalLocomotion/ParametersHandler/StdImplementation.h>
#include <BipedalLocomotion/TestUtils/MemoryAllocationMonitor.h>

using namespace BipedalLocomotion::ContinuousDynamicalSystem;
using namespace BipedalLocomotion::TestUtils;

TEST_CASE("Multistate weight provider")
{
Expand Down Expand Up @@ -58,7 +60,15 @@ TEST_CASE("Multistate weight provider")

for (unsigned int i = 0; i < 50; i++)
{
if (i >= 1)
{
MemoryAllocationMonitor::startMonitor();
}
REQUIRE(provider.advance());
if (i >= 1)
{
REQUIRE(MemoryAllocationMonitor::endMonitorAndCheckNoMemoryAllocationInLastMonitor());
}
}

REQUIRE(provider.getOutput().isApprox(stanceWeight, tolerance));
Expand All @@ -74,7 +84,15 @@ TEST_CASE("Multistate weight provider")

for (unsigned int i = 0; i < 50; i++)
{
if (i >= 1)
{
MemoryAllocationMonitor::startMonitor();
}
REQUIRE(provider.advance());
if (i >= 1)
{
REQUIRE(MemoryAllocationMonitor::endMonitorAndCheckNoMemoryAllocationInLastMonitor());
}
}

REQUIRE(provider.getOutput().isApprox(stanceWeight, tolerance));
Expand All @@ -84,7 +102,15 @@ TEST_CASE("Multistate weight provider")
REQUIRE(provider.setState("walking"));
for (unsigned int i = 0; i < 50; i++)
{
if (i >= 1)
{
MemoryAllocationMonitor::startMonitor();
}
REQUIRE(provider.advance());
if (i >= 1)
{
REQUIRE(MemoryAllocationMonitor::endMonitorAndCheckNoMemoryAllocationInLastMonitor());
}
}

REQUIRE(provider.getOutput().isApprox(walkingWeight, tolerance));
Expand Down
Loading

0 comments on commit 5ae4e0a

Please sign in to comment.