Skip to content

Commit 9903537

Browse files
committed
Merge #12604: Add DynamicMemoryUsage() to CDBWrapper to estimate LevelDB memory use
741f017 Add DynamicMemoryUsage() to LevelDB (Evan Klitzke) Pull request description: This adds a new method `CDBWrapper::DynamicMemoryUsage()` similar to Bitcoin's existing methods of the same name. It's implemented by asking LevelDB for the information, and then parsing the string response. I've also added logging to `CDBWrapper::WriteBatch()` to track this information: ``` $ tail -f ~/.bitcoin/testnet3/debug.log | grep WriteBatch 2018-03-05 19:34:55 WriteBatch memory usage: db=chainstate, before=0.0MiB, after=0.0MiB 2018-03-05 19:35:17 WriteBatch memory usage: db=index, before=0.0MiB, after=0.0MiB 2018-03-05 19:35:17 WriteBatch memory usage: db=chainstate, before=0.0MiB, after=8.0MiB 2018-03-05 19:35:22 WriteBatch memory usage: db=index, before=0.0MiB, after=0.0MiB 2018-03-05 19:35:22 WriteBatch memory usage: db=chainstate, before=8.0MiB, after=17.0MiB 2018-03-05 19:35:26 WriteBatch memory usage: db=index, before=0.0MiB, after=0.0MiB 2018-03-05 19:35:27 WriteBatch memory usage: db=chainstate, before=9.0MiB, after=18.0MiB 2018-03-05 19:35:40 WriteBatch memory usage: db=index, before=0.0MiB, after=0.0MiB 2018-03-05 19:35:41 WriteBatch memory usage: db=chainstate, before=9.0MiB, after=7.0MiB 2018-03-05 19:35:52 WriteBatch memory usage: db=index, before=0.0MiB, after=0.0MiB 2018-03-05 19:35:52 WriteBatch memory usage: db=chainstate, before=7.0MiB, after=9.0MiB ^C ``` As LevelDB doesn't seem to provide a way to get the database name, I've also added a new `m_name` field to the `CDBWrapper`. This is necessary because we have multiple LevelDB databases (two now, and possibly more later, e.g. #11857). I am using this information in other branches where I'm experimenting with changing LevelDB buffer sizes. Tree-SHA512: 7ea8ff5484bb07ef806af17d000c74ccca27d2e0f6c3229e12d93818f00874553335d87428482bd8acbcae81ea35aef2a243326f9fccbfac25989323d24391b4
2 parents 7deba93 + 741f017 commit 9903537

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/dbwrapper.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ static leveldb::Options GetOptions(size_t nCacheSize)
8989
}
9090

9191
CDBWrapper::CDBWrapper(const fs::path& path, size_t nCacheSize, bool fMemory, bool fWipe, bool obfuscate)
92+
: m_name(fs::basename(path))
9293
{
9394
penv = nullptr;
9495
readoptions.verify_checksums = true;
@@ -155,11 +156,30 @@ CDBWrapper::~CDBWrapper()
155156

156157
bool CDBWrapper::WriteBatch(CDBBatch& batch, bool fSync)
157158
{
159+
const bool log_memory = LogAcceptCategory(BCLog::LEVELDB);
160+
double mem_before = 0;
161+
if (log_memory) {
162+
mem_before = DynamicMemoryUsage() / 1024 / 1024;
163+
}
158164
leveldb::Status status = pdb->Write(fSync ? syncoptions : writeoptions, &batch.batch);
159165
dbwrapper_private::HandleError(status);
166+
if (log_memory) {
167+
double mem_after = DynamicMemoryUsage() / 1024 / 1024;
168+
LogPrint(BCLog::LEVELDB, "WriteBatch memory usage: db=%s, before=%.1fMiB, after=%.1fMiB\n",
169+
m_name, mem_before, mem_after);
170+
}
160171
return true;
161172
}
162173

174+
size_t CDBWrapper::DynamicMemoryUsage() const {
175+
std::string memory;
176+
if (!pdb->GetProperty("leveldb.approximate-memory-usage", &memory)) {
177+
LogPrint(BCLog::LEVELDB, "Failed to get approximate-memory-usage property\n");
178+
return 0;
179+
}
180+
return stoul(memory);
181+
}
182+
163183
// Prefixed with null character to avoid collisions with other keys
164184
//
165185
// We must use a string constructor which specifies length so that we copy

src/dbwrapper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ class CDBWrapper
198198
//! the database itself
199199
leveldb::DB* pdb;
200200

201+
//! the name of this database
202+
std::string m_name;
203+
201204
//! a key used for optional XOR-obfuscation of the database
202205
std::vector<unsigned char> obfuscate_key;
203206

@@ -284,6 +287,9 @@ class CDBWrapper
284287

285288
bool WriteBatch(CDBBatch& batch, bool fSync = false);
286289

290+
// Get an estimate of LevelDB memory usage (in bytes).
291+
size_t DynamicMemoryUsage() const;
292+
287293
// not available for LevelDB; provide for compatibility with BDB
288294
bool Flush()
289295
{

0 commit comments

Comments
 (0)