From 8d7f69b59c9b2593e221a9351948d7350df5021c Mon Sep 17 00:00:00 2001 From: nibbles83 Date: Thu, 20 Jul 2023 13:48:35 +0100 Subject: [PATCH] Shrink VM Log on startup --- src/init.cpp | 1 + src/logging.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/logging.h | 2 ++ 3 files changed, 49 insertions(+) diff --git a/src/init.cpp b/src/init.cpp index 10944001..596f8477 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1346,6 +1346,7 @@ bool AppInitMain(NodeContext& node) // Do this first since it both loads a bunch of debug.log into memory, // and because this needs to happen before any other debug.log printing LogInstance().ShrinkDebugFile(); + LogInstance().ShrinkVMLogFile(); } } if (!LogInstance().StartLogging()) { diff --git a/src/logging.cpp b/src/logging.cpp index e9f05bae..09dca5b2 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -134,6 +134,11 @@ bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const return (m_categories.load(std::memory_order_relaxed) & category) != 0; } +bool BCLog::Logger::DefaultShrinkVMLogFile() const +{ + return m_categories == BCLog::NONE; +} + bool BCLog::Logger::DefaultShrinkDebugFile() const { return m_categories == BCLog::NONE; @@ -323,6 +328,47 @@ void BCLog::Logger::LogPrintStr(const std::string& str, bool useVMLog) } } +void BCLog::Logger::ShrinkVMLogFile() +{ + // Amount of vm.log to save at end when shrinking (must fit in memory) + constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; + + assert(!m_file_pathVM.empty()); + + // Scroll vm.log if it's getting too big + FILE* file = fsbridge::fopen(m_file_pathVM, "r"); + + // Special files (e.g. device nodes) may not have a size. + size_t log_size = 0; + try { + log_size = fs::file_size(m_file_pathVM); + } catch (const fs::filesystem_error&) {} + + // If vm.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE + // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes + if (file && log_size > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10)) + { + // Restart the file with some of the end + std::vector vch(RECENT_DEBUG_HISTORY_SIZE, 0); + if (fseek(file, -((long)vch.size()), SEEK_END)) { + LogPrintf("Failed to shrink VM log file: fseek(...) failed\n"); + fclose(file); + return; + } + int nBytes = fread(vch.data(), 1, vch.size(), file); + fclose(file); + + file = fsbridge::fopen(m_file_pathVM, "w"); + if (file) + { + fwrite(vch.data(), 1, nBytes, file); + fclose(file); + } + } + else if (file != nullptr) + fclose(file); +} + void BCLog::Logger::ShrinkDebugFile() { // Amount of debug.log to save at end when shrinking (must fit in memory) diff --git a/src/logging.h b/src/logging.h index f2ff8b5d..83db0d1e 100644 --- a/src/logging.h +++ b/src/logging.h @@ -123,6 +123,7 @@ namespace BCLog { void DisconnectTestLogger(); void ShrinkDebugFile(); + void ShrinkVMLogFile(); uint32_t GetCategoryMask() const { return m_categories.load(); } @@ -134,6 +135,7 @@ namespace BCLog { bool WillLogCategory(LogFlags category) const; bool DefaultShrinkDebugFile() const; + bool DefaultShrinkVMLogFile() const; }; } // namespace BCLog