Skip to content

Commit

Permalink
Add "approximate-memory-usage" property to leveldb::DB::GetProperty
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ssiddhartha authored and cmumford committed Oct 13, 2015
1 parent bb61e00 commit 36fc955
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
13 changes: 13 additions & 0 deletions db/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,19 @@ bool DBImpl::GetProperty(const Slice& property, std::string* value) {
} else if (in == "sstables") {
*value = versions_->current()->DebugString();
return true;
} else if (in == "approximate-memory-usage") {
size_t total_usage = options_.block_cache->TotalCharge();
if (mem_) {
total_usage += mem_->ApproximateMemoryUsage();
}
if (imm_) {
total_usage += imm_->ApproximateMemoryUsage();
}
char buf[50];
snprintf(buf, sizeof(buf), "%llu",
static_cast<unsigned long long>(total_usage));
value->append(buf);
return true;
}

return false;
Expand Down
11 changes: 11 additions & 0 deletions db/db_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,17 @@ TEST(DBTest, GetFromVersions) {
} while (ChangeOptions());
}

TEST(DBTest, GetMemUsage) {
do {
ASSERT_OK(Put("foo", "v1"));
std::string val;
ASSERT_TRUE(db_->GetProperty("leveldb.approximate-memory-usage", &val));
int mem_usage = atoi(val.c_str());
ASSERT_GT(mem_usage, 0);
ASSERT_LT(mem_usage, 5*1024*1024);
} while (ChangeOptions());
}

TEST(DBTest, GetSnapshot) {
do {
// Try with both a short key and a long key
Expand Down
4 changes: 4 additions & 0 deletions include/leveldb/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class Cache {
// leveldb may change Prune() to a pure abstract method.
virtual void Prune() {}

// Return an estimate of the combined charges of all elements stored in the
// cache.
virtual size_t TotalCharge() const = 0;

private:
void LRU_Remove(Handle* e);
void LRU_Append(Handle* e);
Expand Down
2 changes: 2 additions & 0 deletions include/leveldb/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class DB {
// about the internal operation of the DB.
// "leveldb.sstables" - returns a multi-line string that describes all
// of the sstables that make up the db contents.
// "leveldb.approximate-memory-usage" - returns the approximate number of
// bytes of memory in use by the DB.
virtual bool GetProperty(const Slice& property, std::string* value) = 0;

// For each i in [0,n-1], store in "sizes[i]", the approximate
Expand Down
13 changes: 12 additions & 1 deletion util/cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ class LRUCache {
void Release(Cache::Handle* handle);
void Erase(const Slice& key, uint32_t hash);
void Prune();
size_t TotalCharge() const {
MutexLock l(&mutex_);
return usage_;
}

private:
void LRU_Remove(LRUHandle* e);
Expand All @@ -158,7 +162,7 @@ class LRUCache {
size_t capacity_;

// mutex_ protects the following state.
port::Mutex mutex_;
mutable port::Mutex mutex_;
size_t usage_;

// Dummy head of LRU list.
Expand Down Expand Up @@ -333,6 +337,13 @@ class ShardedLRUCache : public Cache {
shard_[s].Prune();
}
}
virtual size_t TotalCharge() const {
size_t total = 0;
for (int s = 0; s < kNumShards; s++) {
total += shard_[s].TotalCharge();
}
return total;
}
};

} // end anonymous namespace
Expand Down

0 comments on commit 36fc955

Please sign in to comment.