Add support to specify the number of shards for the Block cache. By default, the block cache is sharded into 16 parts.

Summary:
Task ID: #

Blame Rev:

Test Plan: Revert Plan:

Differential Revision: https://reviews.facebook.net/D3273
This commit is contained in:
Dhruba Borthakur
2012-05-16 17:22:33 -07:00
parent 95af128225
commit a2a0e358cb
3 changed files with 33 additions and 7 deletions

View File

@@ -267,12 +267,12 @@ void LRUCache::Erase(const Slice& key, uint32_t hash) {
}
}
static const int kNumShardBits = 4;
static const int kNumShards = 1 << kNumShardBits;
static int kNumShardBits = 4; // default values, can be overridden
static int kNumShards = 1 << kNumShardBits;
class ShardedLRUCache : public Cache {
private:
LRUCache shard_[kNumShards];
LRUCache* shard_;
port::Mutex id_mutex_;
uint64_t last_id_;
@@ -284,14 +284,25 @@ class ShardedLRUCache : public Cache {
return hash >> (32 - kNumShardBits);
}
public:
explicit ShardedLRUCache(size_t capacity)
: last_id_(0) {
void init(size_t capacity, int numShardBits) {
kNumShardBits = numShardBits;
kNumShards = 1 << kNumShardBits;
shard_ = new LRUCache[kNumShards];
const size_t per_shard = (capacity + (kNumShards - 1)) / kNumShards;
for (int s = 0; s < kNumShards; s++) {
shard_[s].SetCapacity(per_shard);
}
}
public:
explicit ShardedLRUCache(size_t capacity)
: last_id_(0) {
init(capacity, kNumShardBits);
}
ShardedLRUCache(size_t capacity, int numShardBits)
: last_id_(0) {
init(capacity, numShardBits);
}
virtual ~ShardedLRUCache() { }
virtual Handle* Insert(const Slice& key, void* value, size_t charge,
void (*deleter)(const Slice& key, void* value)) {
@@ -325,4 +336,8 @@ Cache* NewLRUCache(size_t capacity) {
return new ShardedLRUCache(capacity);
}
Cache* NewLRUCache(size_t capacity, int numShardBits) {
return new ShardedLRUCache(capacity, numShardBits);
}
} // namespace leveldb