Bläddra i källkod

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
master
ssid 9 år sedan
committed by Chris Mumford
förälder
incheckning
528c2bc6ad
5 ändrade filer med 42 tillägg och 1 borttagningar
  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 Visa fil

@ -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;

+ 11
- 0
db/db_test.cc Visa fil

@ -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

+ 4
- 0
include/leveldb/cache.h Visa fil

@ -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);

+ 2
- 0
include/leveldb/db.h Visa fil

@ -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

+ 12
- 1
util/cache.cc Visa fil

@ -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);
@ -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.
@ -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

Laddar…
Avbryt
Spara