From cd65208865c5a1c88fb453cf290cac1b7fd5bfdb Mon Sep 17 00:00:00 2001 From: Evan Klitzke Date: Mon, 5 Mar 2018 14:24:12 -0500 Subject: [PATCH] Add DynamicMemoryUsage() to LevelDB This adds a DynamicMemoryUsage() method similar to the existing methods of the same name, and adds logging of memory usage to CDBWrapper::WriteBatch. Cherry-picked from: 741f0177c53ae536801a67c8ec194d6be3505d2d --- src/dbwrapper.cpp | 20 ++++++++++++++++++++ src/dbwrapper.h | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index a373b9324b1..53276659584 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -31,6 +31,7 @@ static leveldb::Options GetOptions(size_t nCacheSize) } CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate) + : m_name(fs::basename(path)) { penv = NULL; readoptions.verify_checksums = true; @@ -89,11 +90,30 @@ CDBWrapper::~CDBWrapper() bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync) { + const bool log_memory = LogAcceptCategory("leveldb"); + double mem_before = 0; + if (log_memory) { + mem_before = DynamicMemoryUsage() / 1024 / 1024; + } leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch); dbwrapper_private::HandleError(status); + if (log_memory) { + double mem_after = DynamicMemoryUsage() / 1024 / 1024; + LogPrint("leveldb", "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n", + m_name, mem_before, mem_after); + } return true; } +size_t CDBWrapper::DynamicMemoryUsage() const { + std::string memory; + if (!pdb->GetProperty("leveldb.approximate-memory-usage", &memory)) { + LogPrint("leveldb", "Failed to get approximate-memory-usage property\n"); + return 0; + } + return stoul(memory); +} + // Prefixed with null character to avoid collisions with other keys // // We must use a string constructor which specifies length so that we copy diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 9044c82a0f4..e5dd3d37818 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -202,6 +202,9 @@ class CDBWrapper //! the database itself leveldb::DB* pdb; + //! the name of this database + std::string m_name; + //! a key used for optional XOR-obfuscation of the database std::vector obfuscate_key; @@ -288,6 +291,9 @@ class CDBWrapper bool WriteBatch(CDBBatch& batch, bool fSync = false); + // Get an estimate of LevelDB memory usage (in bytes). + size_t DynamicMemoryUsage() const; + // not available for LevelDB; provide for compatibility with BDB bool Flush() {