From 5e295dfbdae36f2293be93093e332fff27b3286f Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 30 Sep 2023 20:19:28 -0700 Subject: [PATCH] [wpiutil] DataLog: Limit total buffer allocation (#5700) Pause logging if more than 1 MB of outgoing buffers are allocated. Limit free buffer list to 256 KB. --- wpiutil/src/main/native/cpp/DataLog.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/wpiutil/src/main/native/cpp/DataLog.cpp b/wpiutil/src/main/native/cpp/DataLog.cpp index 50effcfea9..967fd183da 100644 --- a/wpiutil/src/main/native/cpp/DataLog.cpp +++ b/wpiutil/src/main/native/cpp/DataLog.cpp @@ -37,6 +37,8 @@ using namespace wpi::log; static constexpr size_t kBlockSize = 16 * 1024; +static constexpr size_t kMaxBufferCount = 1024 * 1024 / kBlockSize; +static constexpr size_t kMaxFreeCount = 256 * 1024 / kBlockSize; static constexpr size_t kRecordMaxHeaderSize = 17; template @@ -354,7 +356,9 @@ void DataLog::WriterThreadMain(std::string_view dir) { // release buffers back to free list for (auto&& buf : toWrite) { buf.Clear(); - m_free.emplace_back(std::move(buf)); + if (m_free.size() < kMaxFreeCount) { + [[likely]] m_free.emplace_back(std::move(buf)); + } } toWrite.resize(0); } @@ -413,7 +417,9 @@ void DataLog::WriterThreadMain( // release buffers back to free list for (auto&& buf : toWrite) { buf.Clear(); - m_free.emplace_back(std::move(buf)); + if (m_free.size() < kMaxFreeCount) { + [[likely]] m_free.emplace_back(std::move(buf)); + } } toWrite.resize(0); } @@ -492,6 +498,13 @@ uint8_t* DataLog::Reserve(size_t size) { assert(size <= kBlockSize); if (m_outgoing.empty() || size > m_outgoing.back().GetRemaining()) { if (m_free.empty()) { + if (m_outgoing.size() >= kMaxBufferCount) { + [[unlikely]] WPI_ERROR( + m_msglog, + "outgoing buffers exceeded threshold, pausing logging--" + "consider flushing to disk more frequently (smaller period)"); + m_paused = true; + } m_outgoing.emplace_back(); } else { m_outgoing.emplace_back(std::move(m_free.back()));