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/logfile_utils_test.cc b/aos/events/logging/logfile_utils_test.cc
index c8d16e0..3d99757 100644
--- a/aos/events/logging/logfile_utils_test.cc
+++ b/aos/events/logging/logfile_utils_test.cc
@@ -31,8 +31,11 @@
// test only boilerplate to DetachedBufferWriter.
class TestDetachedBufferWriter : public DetachedBufferWriter {
public:
+ // Pick a max size that is rather conservative.
+ static constexpr size_t kMaxMessageSize = 128 * 1024;
TestDetachedBufferWriter(std::string_view filename)
- : DetachedBufferWriter(filename, std::make_unique<DummyEncoder>()) {}
+ : DetachedBufferWriter(filename,
+ std::make_unique<DummyEncoder>(kMaxMessageSize)) {}
void WriteSizedFlatbuffer(flatbuffers::DetachedBuffer &&buffer) {
QueueSpan(absl::Span<const uint8_t>(buffer.data(), buffer.size()));
}
@@ -2983,12 +2986,13 @@
fbb.GetBufferSpan().size()));
// Make sure that both the builder and inline method agree on sizes.
- ASSERT_EQ(fbb.GetSize(), PackMessageSize(type, context))
+ ASSERT_EQ(fbb.GetSize(), PackMessageSize(type, context.size))
<< "log type " << static_cast<int>(type);
// Initialize the buffer to something nonzero to make sure all the padding
// bytes are set to 0.
- std::vector<uint8_t> repacked_message(PackMessageSize(type, context), 67);
+ std::vector<uint8_t> repacked_message(PackMessageSize(type, context.size),
+ 67);
// And verify packing inline works as expected.
EXPECT_EQ(repacked_message.size(),