2016-05-24 08:35:23 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
#include "rocksdb/utilities/sim_cache.h"
|
|
|
|
#include <atomic>
|
2016-07-21 00:28:04 +02:00
|
|
|
#include "port/port.h"
|
2016-09-01 22:50:39 +02:00
|
|
|
#include "util/statistics.h"
|
2016-05-24 08:35:23 +02:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// SimCacheImpl definition
|
|
|
|
class SimCacheImpl : public SimCache {
|
|
|
|
public:
|
|
|
|
// capacity for real cache (ShardedLRUCache)
|
|
|
|
// test_capacity for key only cache
|
|
|
|
SimCacheImpl(std::shared_ptr<Cache> cache, size_t sim_capacity,
|
2016-09-01 22:50:39 +02:00
|
|
|
int num_shard_bits)
|
2016-05-24 08:35:23 +02:00
|
|
|
: cache_(cache),
|
|
|
|
key_only_cache_(NewLRUCache(sim_capacity, num_shard_bits)),
|
2016-08-11 02:42:24 +02:00
|
|
|
miss_times_(0),
|
2016-09-01 22:50:39 +02:00
|
|
|
hit_times_(0) {}
|
2016-05-24 08:35:23 +02:00
|
|
|
|
|
|
|
virtual ~SimCacheImpl() {}
|
|
|
|
virtual void SetCapacity(size_t capacity) override {
|
|
|
|
cache_->SetCapacity(capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetStrictCapacityLimit(bool strict_capacity_limit) override {
|
|
|
|
cache_->SetStrictCapacityLimit(strict_capacity_limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Status Insert(const Slice& key, void* value, size_t charge,
|
|
|
|
void (*deleter)(const Slice& key, void* value),
|
2016-08-20 01:43:31 +02:00
|
|
|
Handle** handle, Priority priority) override {
|
2016-05-24 08:35:23 +02:00
|
|
|
// The handle and value passed in are for real cache, so we pass nullptr
|
|
|
|
// to key_only_cache_ for both instead. Also, the deleter function pointer
|
|
|
|
// will be called by user to perform some external operation which should
|
|
|
|
// be applied only once. Thus key_only_cache accepts an empty function.
|
|
|
|
// *Lambda function without capture can be assgined to a function pointer
|
|
|
|
Handle* h = key_only_cache_->Lookup(key);
|
|
|
|
if (h == nullptr) {
|
|
|
|
key_only_cache_->Insert(key, nullptr, charge,
|
2016-08-20 01:43:31 +02:00
|
|
|
[](const Slice& k, void* v) {}, nullptr,
|
|
|
|
priority);
|
2016-05-24 08:35:23 +02:00
|
|
|
} else {
|
|
|
|
key_only_cache_->Release(h);
|
|
|
|
}
|
2016-08-20 01:43:31 +02:00
|
|
|
return cache_->Insert(key, value, charge, deleter, handle, priority);
|
2016-05-24 08:35:23 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 22:50:39 +02:00
|
|
|
virtual Handle* Lookup(const Slice& key, Statistics* stats) override {
|
2016-05-24 08:35:23 +02:00
|
|
|
Handle* h = key_only_cache_->Lookup(key);
|
|
|
|
if (h != nullptr) {
|
|
|
|
key_only_cache_->Release(h);
|
|
|
|
inc_hit_counter();
|
2016-09-01 22:50:39 +02:00
|
|
|
RecordTick(stats, SIM_BLOCK_CACHE_HIT);
|
2016-08-11 02:42:24 +02:00
|
|
|
} else {
|
|
|
|
inc_miss_counter();
|
2016-09-01 22:50:39 +02:00
|
|
|
RecordTick(stats, SIM_BLOCK_CACHE_MISS);
|
2016-05-24 08:35:23 +02:00
|
|
|
}
|
2016-09-01 22:50:39 +02:00
|
|
|
return cache_->Lookup(key, stats);
|
2016-05-24 08:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Release(Handle* handle) override { cache_->Release(handle); }
|
|
|
|
|
|
|
|
virtual void Erase(const Slice& key) override {
|
|
|
|
cache_->Erase(key);
|
|
|
|
key_only_cache_->Erase(key);
|
|
|
|
}
|
|
|
|
|
2016-07-15 19:41:36 +02:00
|
|
|
virtual void* Value(Handle* handle) override { return cache_->Value(handle); }
|
2016-05-24 08:35:23 +02:00
|
|
|
|
|
|
|
virtual uint64_t NewId() override { return cache_->NewId(); }
|
|
|
|
|
|
|
|
virtual size_t GetCapacity() const override { return cache_->GetCapacity(); }
|
|
|
|
|
|
|
|
virtual bool HasStrictCapacityLimit() const override {
|
|
|
|
return cache_->HasStrictCapacityLimit();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t GetUsage() const override { return cache_->GetUsage(); }
|
|
|
|
|
|
|
|
virtual size_t GetUsage(Handle* handle) const override {
|
2016-07-15 19:41:36 +02:00
|
|
|
return cache_->GetUsage(handle);
|
2016-05-24 08:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t GetPinnedUsage() const override {
|
|
|
|
return cache_->GetPinnedUsage();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void DisownData() override {
|
|
|
|
cache_->DisownData();
|
|
|
|
key_only_cache_->DisownData();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ApplyToAllCacheEntries(void (*callback)(void*, size_t),
|
|
|
|
bool thread_safe) override {
|
|
|
|
// only apply to _cache since key_only_cache doesn't hold value
|
|
|
|
cache_->ApplyToAllCacheEntries(callback, thread_safe);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EraseUnRefEntries() override {
|
|
|
|
cache_->EraseUnRefEntries();
|
|
|
|
key_only_cache_->EraseUnRefEntries();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t GetSimCapacity() const override {
|
|
|
|
return key_only_cache_->GetCapacity();
|
|
|
|
}
|
|
|
|
virtual size_t GetSimUsage() const override {
|
|
|
|
return key_only_cache_->GetUsage();
|
|
|
|
}
|
|
|
|
virtual void SetSimCapacity(size_t capacity) override {
|
|
|
|
key_only_cache_->SetCapacity(capacity);
|
|
|
|
}
|
|
|
|
|
2016-08-11 02:42:24 +02:00
|
|
|
virtual uint64_t get_miss_counter() const override {
|
|
|
|
return miss_times_.load(std::memory_order_relaxed);
|
2016-07-15 19:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual uint64_t get_hit_counter() const override {
|
|
|
|
return hit_times_.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void reset_counter() override {
|
2016-08-11 02:42:24 +02:00
|
|
|
miss_times_.store(0, std::memory_order_relaxed);
|
2016-07-15 19:41:36 +02:00
|
|
|
hit_times_.store(0, std::memory_order_relaxed);
|
2016-08-11 02:42:24 +02:00
|
|
|
SetTickerCount(stats_, SIM_BLOCK_CACHE_HIT, 0);
|
|
|
|
SetTickerCount(stats_, SIM_BLOCK_CACHE_MISS, 0);
|
2016-05-24 08:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::string ToString() const override {
|
|
|
|
std::string res;
|
2016-08-11 02:42:24 +02:00
|
|
|
res.append("SimCache MISSes: " + std::to_string(get_miss_counter()) + "\n");
|
2016-05-24 08:35:23 +02:00
|
|
|
res.append("SimCache HITs: " + std::to_string(get_hit_counter()) + "\n");
|
|
|
|
char buff[100];
|
2016-08-11 02:42:24 +02:00
|
|
|
auto lookups = get_miss_counter() + get_hit_counter();
|
2016-05-24 08:35:23 +02:00
|
|
|
snprintf(buff, sizeof(buff), "SimCache HITRATE: %.2f%%\n",
|
2016-08-11 02:42:24 +02:00
|
|
|
(lookups == 0 ? 0 : get_hit_counter() * 100.0f / lookups));
|
2016-05-24 08:35:23 +02:00
|
|
|
res.append(buff);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<Cache> cache_;
|
|
|
|
std::shared_ptr<Cache> key_only_cache_;
|
2016-08-11 02:42:24 +02:00
|
|
|
std::atomic<uint64_t> miss_times_;
|
2016-05-24 08:35:23 +02:00
|
|
|
std::atomic<uint64_t> hit_times_;
|
2016-08-11 02:42:24 +02:00
|
|
|
Statistics* stats_;
|
|
|
|
void inc_miss_counter() {
|
|
|
|
miss_times_.fetch_add(1, std::memory_order_relaxed);
|
2016-07-15 19:41:36 +02:00
|
|
|
}
|
|
|
|
void inc_hit_counter() { hit_times_.fetch_add(1, std::memory_order_relaxed); }
|
2016-05-24 08:35:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
// For instrumentation purpose, use NewSimCache instead
|
|
|
|
std::shared_ptr<SimCache> NewSimCache(std::shared_ptr<Cache> cache,
|
2016-09-01 22:50:39 +02:00
|
|
|
size_t sim_capacity, int num_shard_bits) {
|
2016-05-24 08:35:23 +02:00
|
|
|
if (num_shard_bits >= 20) {
|
|
|
|
return nullptr; // the cache cannot be sharded into too many fine pieces
|
|
|
|
}
|
2016-09-01 22:50:39 +02:00
|
|
|
return std::make_shared<SimCacheImpl>(cache, sim_capacity, num_shard_bits);
|
2016-05-24 08:35:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace rocksdb
|