Skip to content

Commit

Permalink
Shrink VM Log on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
nibbles83 committed Jul 20, 2023
1 parent 670266c commit 8d7f69b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
46 changes: 46 additions & 0 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<char> 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)
Expand Down
2 changes: 2 additions & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ namespace BCLog {
void DisconnectTestLogger();

void ShrinkDebugFile();
void ShrinkVMLogFile();

uint32_t GetCategoryMask() const { return m_categories.load(); }

Expand All @@ -134,6 +135,7 @@ namespace BCLog {
bool WillLogCategory(LogFlags category) const;

bool DefaultShrinkDebugFile() const;
bool DefaultShrinkVMLogFile() const;
};

} // namespace BCLog
Expand Down

0 comments on commit 8d7f69b

Please sign in to comment.