Browse Source

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
main
ssid 9 years ago
committed by Chris Mumford
parent
commit
528c2bc6ad
5 changed files with 42 additions and 1 deletions
  1. +13
    -0
      db/db_impl.cc
  2. +11
    -0
      db/db_test.cc
  3. +4
    -0
      include/leveldb/cache.h
  4. +2
    -0
      include/leveldb/db.h
  5. +12
    -1
      util/cache.cc

+ 13
- 0
db/db_impl.cc View File

@ -1425,6 +1425,19 @@ bool DBImpl::GetProperty(const Slice& property, std::string* value) {
} else if (in == "sstables") { } else if (in == "sstables") {
*value = versions_->current()->DebugString(); *value = versions_->current()->DebugString();
return true; 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; return false;

+ 11
- 0
db/db_test.cc View File

@ -563,6 +563,17 @@ TEST(DBTest, GetFromVersions) {
} while (ChangeOptions()); } 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) { TEST(DBTest, GetSnapshot) {
do { do {
// Try with both a short key and a long key // Try with both a short key and a long key

+ 4
- 0
include/leveldb/cache.h View File

@ -88,6 +88,10 @@ class Cache {
// leveldb may change Prune() to a pure abstract method. // leveldb may change Prune() to a pure abstract method.
virtual void Prune() {} virtual void Prune() {}
// Return an estimate of the combined charges of all elements stored in the
// cache.
virtual size_t TotalCharge() const = 0;
private: private:
void LRU_Remove(Handle* e); void LRU_Remove(Handle* e);
void LRU_Append(Handle* e); void LRU_Append(Handle* e);

+ 2
- 0
include/leveldb/db.h View File

@ -115,6 +115,8 @@ class DB {
// about the internal operation of the DB. // about the internal operation of the DB.
// "leveldb.sstables" - returns a multi-line string that describes all // "leveldb.sstables" - returns a multi-line string that describes all
// of the sstables that make up the db contents. // 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; virtual bool GetProperty(const Slice& property, std::string* value) = 0;
// For each i in [0,n-1], store in "sizes[i]", the approximate // For each i in [0,n-1], store in "sizes[i]", the approximate

+ 12
- 1
util/cache.cc View File

@ -148,6 +148,10 @@ class LRUCache {
void Release(Cache::Handle* handle); void Release(Cache::Handle* handle);
void Erase(const Slice& key, uint32_t hash); void Erase(const Slice& key, uint32_t hash);
void Prune(); void Prune();
size_t TotalCharge() const {
MutexLock l(&mutex_);
return usage_;
}
private: private:
void LRU_Remove(LRUHandle* e); void LRU_Remove(LRUHandle* e);
@ -158,7 +162,7 @@ class LRUCache {
size_t capacity_; size_t capacity_;
// mutex_ protects the following state. // mutex_ protects the following state.
port::Mutex mutex_;
mutable port::Mutex mutex_;
size_t usage_; size_t usage_;
// Dummy head of LRU list. // Dummy head of LRU list.
@ -333,6 +337,13 @@ class ShardedLRUCache : public Cache {
shard_[s].Prune(); 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 } // end anonymous namespace

Loading…
Cancel
Save