Skip to content

Commit 528c2bc

Browse files
ssiddharthacmumford
authored andcommitted
Add "approximate-memory-usage" property to leveldb::DB::GetProperty
The approximate RAM usage of the database is calculated from the memory allocated for write buffers and the block cache. This is to give an estimate of memory usage to leveldb clients. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=104222307
1 parent 359b6bc commit 528c2bc

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

db/db_impl.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,19 @@ bool DBImpl::GetProperty(const Slice& property, std::string* value) {
14251425
} else if (in == "sstables") {
14261426
*value = versions_->current()->DebugString();
14271427
return true;
1428+
} else if (in == "approximate-memory-usage") {
1429+
size_t total_usage = options_.block_cache->TotalCharge();
1430+
if (mem_) {
1431+
total_usage += mem_->ApproximateMemoryUsage();
1432+
}
1433+
if (imm_) {
1434+
total_usage += imm_->ApproximateMemoryUsage();
1435+
}
1436+
char buf[50];
1437+
snprintf(buf, sizeof(buf), "%llu",
1438+
static_cast<unsigned long long>(total_usage));
1439+
value->append(buf);
1440+
return true;
14281441
}
14291442

14301443
return false;

db/db_test.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,17 @@ TEST(DBTest, GetFromVersions) {
563563
} while (ChangeOptions());
564564
}
565565

566+
TEST(DBTest, GetMemUsage) {
567+
do {
568+
ASSERT_OK(Put("foo", "v1"));
569+
std::string val;
570+
ASSERT_TRUE(db_->GetProperty("leveldb.approximate-memory-usage", &val));
571+
int mem_usage = atoi(val.c_str());
572+
ASSERT_GT(mem_usage, 0);
573+
ASSERT_LT(mem_usage, 5*1024*1024);
574+
} while (ChangeOptions());
575+
}
576+
566577
TEST(DBTest, GetSnapshot) {
567578
do {
568579
// Try with both a short key and a long key

include/leveldb/cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class Cache {
8888
// leveldb may change Prune() to a pure abstract method.
8989
virtual void Prune() {}
9090

91+
// Return an estimate of the combined charges of all elements stored in the
92+
// cache.
93+
virtual size_t TotalCharge() const = 0;
94+
9195
private:
9296
void LRU_Remove(Handle* e);
9397
void LRU_Append(Handle* e);

include/leveldb/db.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class DB {
115115
// about the internal operation of the DB.
116116
// "leveldb.sstables" - returns a multi-line string that describes all
117117
// of the sstables that make up the db contents.
118+
// "leveldb.approximate-memory-usage" - returns the approximate number of
119+
// bytes of memory in use by the DB.
118120
virtual bool GetProperty(const Slice& property, std::string* value) = 0;
119121

120122
// For each i in [0,n-1], store in "sizes[i]", the approximate

util/cache.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ class LRUCache {
148148
void Release(Cache::Handle* handle);
149149
void Erase(const Slice& key, uint32_t hash);
150150
void Prune();
151+
size_t TotalCharge() const {
152+
MutexLock l(&mutex_);
153+
return usage_;
154+
}
151155

152156
private:
153157
void LRU_Remove(LRUHandle* e);
@@ -158,7 +162,7 @@ class LRUCache {
158162
size_t capacity_;
159163

160164
// mutex_ protects the following state.
161-
port::Mutex mutex_;
165+
mutable port::Mutex mutex_;
162166
size_t usage_;
163167

164168
// Dummy head of LRU list.
@@ -333,6 +337,13 @@ class ShardedLRUCache : public Cache {
333337
shard_[s].Prune();
334338
}
335339
}
340+
virtual size_t TotalCharge() const {
341+
size_t total = 0;
342+
for (int s = 0; s < kNumShards; s++) {
343+
total += shard_[s].TotalCharge();
344+
}
345+
return total;
346+
}
336347
};
337348

338349
} // end anonymous namespace

0 commit comments

Comments
 (0)