Skip to content

Commit

Permalink
Merge pull request #202 from TheLindaProjectInc/shrinkVMLog
Browse files Browse the repository at this point in the history
Shrink VM Log on startup
  • Loading branch information
nibbles83 authored Jul 20, 2023
2 parents 670266c + 555ffa4 commit 65945c1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ These are some resources that might be helpful in understanding Metrix.

Basic usage resources:

* [User guide Wiki](https://wiki.metrixcoin.com)
* [User guide Wiki](https://docs.metrixcoin.com)
* [Block explorer](https://explorer.metrixcoin.com)
* [Testnet Block explorer](https://testnet-explorer.metrixcoin.com)

Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ AC_PREREQ([2.60])
define(_CLIENT_VERSION_MAJOR, 4)
define(_CLIENT_VERSION_MINOR, 2)
define(_CLIENT_VERSION_REVISION, 0)
define(_CLIENT_VERSION_BUILD, 0)
define(_CLIENT_VERSION_BUILD, 1)
define(_CLIENT_VERSION_RC, 0)
define(_CLIENT_VERSION_IS_RELEASE, true)
define(_COPYRIGHT_YEAR, 2023)
Expand Down
16 changes: 16 additions & 0 deletions doc/release-notes/release-notes-4.2.0.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Metrix Core v4.2.0.1

## Bugfix Update

This update addresses an issue where the vm.log file can grow to huge sizes if left unattended. The daemon will now trim this file on startup to prevent this excessive bloat.

## Bug Reporting

Please report bugs using the issue tracker at github: https://github.com/thelindaproject/metrix/issues

## How to Upgrade
Shut down Metrix, wait until it has completely shut down (which might take a few minutes
for older versions), then just copy over the appropriate metrixd file.

## What's Changed
- Added shrink for the vm.log on daemon startup.
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 65945c1

Please sign in to comment.