Encode flatbuffers directly into the encoder when logging
We were running out of memory when running for many hours. Initial
debugging looked like it was a heap fragmentation issue. Tracking the
allocated memory using the malloc hooks wasn't showing any growth of
memory. The heap was growing though.
Instead of allocating a FlatBufferBuilder/DetachedBuffer for each
message to be logged, we can instead have the BufferEncoder provide
memory to write to, and have it only alloate that buffer space once, and
allocate it to the maximum size that a writer might see.
Change-Id: I046bd2422aea368867b0c63cee7d04c6033fe724
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/aos/events/logging/buffer_encoder_test.cc b/aos/events/logging/buffer_encoder_test.cc
index 5a9b85f..8e12e37 100644
--- a/aos/events/logging/buffer_encoder_test.cc
+++ b/aos/events/logging/buffer_encoder_test.cc
@@ -13,13 +13,26 @@
class DummyEncoderTest : public BufferEncoderBaseTest {};
-// Tests that buffers are enqueued without any changes.
+// Tests that buffers are concatenated without being modified.
TEST_F(DummyEncoderTest, QueuesBuffersAsIs) {
- DummyEncoder encoder;
+ DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize);
const auto expected = CreateAndEncode(100, &encoder);
+ std::vector<uint8_t> data = Flatten(expected);
auto queue = encoder.queue();
- EXPECT_THAT(queue, ::testing::ElementsAreArray(expected));
+ ASSERT_EQ(queue.size(), 1u);
+ EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data));
+}
+
+// Tests that buffers are concatenated without being modified.
+TEST_F(DummyEncoderTest, CoppiesBuffersAsIs) {
+ DummyEncoder encoder(BufferEncoderBaseTest::kMaxMessageSize);
+ const auto expected = CreateAndEncode(100, &encoder);
+ std::vector<uint8_t> data = Flatten(expected);
+
+ auto queue = encoder.queue();
+ ASSERT_EQ(queue.size(), 1u);
+ EXPECT_EQ(queue[0], absl::Span<const uint8_t>(data));
}
// Checks that DummyDecoder can read into a buffer.
@@ -94,8 +107,8 @@
INSTANTIATE_TEST_SUITE_P(
Dummy, BufferEncoderTest,
- ::testing::Combine(::testing::Values([]() {
- return std::make_unique<DummyEncoder>();
+ ::testing::Combine(::testing::Values([](size_t max_buffer_size) {
+ return std::make_unique<DummyEncoder>(max_buffer_size);
}),
::testing::Values([](std::string_view filename) {
return std::make_unique<DummyDecoder>(filename);