Skip to content

Commit

Permalink
simulator: Add test to track heap hygiene (RobotLocomotion#14900)
Browse files Browse the repository at this point in the history
* simulator: Add test to track heap hygiene

Relevant to: RobotLocomotion#14543, RobotLocomotion#14802

This is the beginning of a PR train to make heapless simulation
possible, with careful system construction.  All the good ideas are
inspired by @edrumwri's PR RobotLocomotion#14707; all the sketchy ones are mine.
  • Loading branch information
rpoyner-tri authored Apr 14, 2021
1 parent 3b98e38 commit a005c40
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions systems/analysis/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ drake_cc_googletest(
":simulator",
"//common/test_utilities:expect_throws_message",
"//common/test_utilities:is_dynamic_castable",
"//common/test_utilities:limit_malloc",
"//systems/analysis/test_utilities:controlled_spring_mass_system",
"//systems/analysis/test_utilities:logistic_system",
"//systems/analysis/test_utilities:my_spring_mass_system",
Expand Down
42 changes: 42 additions & 0 deletions systems/analysis/test/simulator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "drake/common/drake_copyable.h"
#include "drake/common/test_utilities/expect_throws_message.h"
#include "drake/common/test_utilities/is_dynamic_castable.h"
#include "drake/common/test_utilities/limit_malloc.h"
#include "drake/common/text_logging.h"
#include "drake/systems/analysis/explicit_euler_integrator.h"
#include "drake/systems/analysis/implicit_euler_integrator.h"
Expand Down Expand Up @@ -1105,6 +1106,47 @@ GTEST_TEST(SimulatorTest, ExampleDiscreteSystem) {
"3: 30 (0.06)\n");
}

class ExamplePublishingSystem final : public LeafSystem<double> {
public:
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(ExamplePublishingSystem)

ExamplePublishingSystem() {
DeclarePerStepPublishEvent(
&ExamplePublishingSystem::Update);
}

private:
systems::EventStatus Update(const systems::Context<double>&) const {
return systems::EventStatus::Succeeded();
}
};


// Tests that heap allocations do not occur from Simulator and the systems
// framework for systems that do unrestricted updates and do not have continuous
// state.
GTEST_TEST(SimulatorTest,
NoHeapAllocsInSimulatorForSystemsWithoutContinuousState) {
// Build a Diagram containing the Example system so we can test both Diagrams
// and LeafSystems at once.
DiagramBuilder<double> builder;
builder.AddSystem<ExamplePublishingSystem>();
auto diagram = builder.Build();

// Create a Simulator and use it to advance time until t=3.
Simulator<double> simulator(*diagram);
// Trigger first (and only allowable) heap allocation.
simulator.Initialize();
{
// TODO(rpoyner-tri): whittle allocations down to 0.
test::LimitMalloc heap_alloc_checker({.max_num_allocations = 88});
simulator.AdvanceTo(1.0);
simulator.AdvanceTo(2.0);
simulator.AdvanceTo(3.0);
simulator.AdvancePendingEvents();
}
}

// A hybrid discrete-continuous system:
// x_{n+1} = sin(1.234*t)
// y_n = x_n
Expand Down

0 comments on commit a005c40

Please sign in to comment.