From 4590b53a4b4b350a07e0f47c1311907c155de1b8 Mon Sep 17 00:00:00 2001 From: Aaron Gao Date: Thu, 1 Sep 2016 13:50:39 -0700 Subject: [PATCH] add stats to Cache::LookUp() Summary: basically for SimCache stats. I find most times it is hard to pass Statistics* to SimCache constructor. Test Plan: make all check Reviewers: andrewkr, sdong, yiwu Reviewed By: yiwu Subscribers: andrewkr, dhruba, leveldb Differential Revision: https://reviews.facebook.net/D62193 --- include/rocksdb/cache.h | 5 ++++- include/rocksdb/utilities/sim_cache.h | 8 ++++---- table/block_based_table_reader.cc | 2 +- util/sharded_cache.cc | 2 +- util/sharded_cache.h | 2 +- utilities/simulator_cache/sim_cache.cc | 20 +++++++++----------- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/include/rocksdb/cache.h b/include/rocksdb/cache.h index 93821b2c7..ab5b9469f 100644 --- a/include/rocksdb/cache.h +++ b/include/rocksdb/cache.h @@ -25,6 +25,7 @@ #include #include #include "rocksdb/slice.h" +#include "rocksdb/statistics.h" #include "rocksdb/status.h" namespace rocksdb { @@ -96,7 +97,9 @@ class Cache { // Else return a handle that corresponds to the mapping. The caller // must call this->Release(handle) when the returned mapping is no // longer needed. - virtual Handle* Lookup(const Slice& key) = 0; + // If stats is not nullptr, relative tickers could be used inside the + // function. + virtual Handle* Lookup(const Slice& key, Statistics* stats = nullptr) = 0; // Release a mapping returned by a previous Lookup(). // REQUIRES: handle must not have been released yet. diff --git a/include/rocksdb/utilities/sim_cache.h b/include/rocksdb/utilities/sim_cache.h index 039c66efd..64cb643ce 100644 --- a/include/rocksdb/utilities/sim_cache.h +++ b/include/rocksdb/utilities/sim_cache.h @@ -10,8 +10,8 @@ #include #include "rocksdb/cache.h" #include "rocksdb/slice.h" +#include "rocksdb/statistics.h" #include "rocksdb/status.h" -#include "util/statistics.h" namespace rocksdb { @@ -31,9 +31,9 @@ class SimCache; // BlockBasedTableOptions.block_size = 4096 by default but is configurable, // Therefore, generally the actual memory overhead of SimCache is Less than // sim_capacity * 2% -extern std::shared_ptr NewSimCache( - std::shared_ptr cache, size_t sim_capacity, int num_shard_bits, - std::shared_ptr stats = nullptr); +extern std::shared_ptr NewSimCache(std::shared_ptr cache, + size_t sim_capacity, + int num_shard_bits); class SimCache : public Cache { public: diff --git a/table/block_based_table_reader.cc b/table/block_based_table_reader.cc index 784110bb3..1ee2ecc18 100644 --- a/table/block_based_table_reader.cc +++ b/table/block_based_table_reader.cc @@ -119,7 +119,7 @@ Cache::Handle* GetEntryFromCache(Cache* block_cache, const Slice& key, Tickers block_cache_miss_ticker, Tickers block_cache_hit_ticker, Statistics* statistics) { - auto cache_handle = block_cache->Lookup(key); + auto cache_handle = block_cache->Lookup(key, statistics); if (cache_handle != nullptr) { PERF_COUNTER_ADD(block_cache_hit_count, 1); // overall cache hit diff --git a/util/sharded_cache.cc b/util/sharded_cache.cc index b02e5b5db..3a7fd0202 100644 --- a/util/sharded_cache.cc +++ b/util/sharded_cache.cc @@ -46,7 +46,7 @@ Status ShardedCache::Insert(const Slice& key, void* value, size_t charge, ->Insert(key, hash, value, charge, deleter, handle, priority); } -Cache::Handle* ShardedCache::Lookup(const Slice& key) { +Cache::Handle* ShardedCache::Lookup(const Slice& key, Statistics* stats) { uint32_t hash = HashSlice(key); return GetShard(Shard(hash))->Lookup(key, hash); } diff --git a/util/sharded_cache.h b/util/sharded_cache.h index 432e8eade..10e345090 100644 --- a/util/sharded_cache.h +++ b/util/sharded_cache.h @@ -60,7 +60,7 @@ class ShardedCache : public Cache { virtual Status Insert(const Slice& key, void* value, size_t charge, void (*deleter)(const Slice& key, void* value), Handle** handle, Priority priority) override; - virtual Handle* Lookup(const Slice& key) override; + virtual Handle* Lookup(const Slice& key, Statistics* stats) override; virtual void Release(Handle* handle) override; virtual void Erase(const Slice& key) override; virtual uint64_t NewId() override; diff --git a/utilities/simulator_cache/sim_cache.cc b/utilities/simulator_cache/sim_cache.cc index 9f69ff897..45e1fc54a 100644 --- a/utilities/simulator_cache/sim_cache.cc +++ b/utilities/simulator_cache/sim_cache.cc @@ -6,6 +6,7 @@ #include "rocksdb/utilities/sim_cache.h" #include #include "port/port.h" +#include "util/statistics.h" namespace rocksdb { @@ -16,12 +17,11 @@ class SimCacheImpl : public SimCache { // capacity for real cache (ShardedLRUCache) // test_capacity for key only cache SimCacheImpl(std::shared_ptr cache, size_t sim_capacity, - int num_shard_bits, Statistics* stats) + int num_shard_bits) : cache_(cache), key_only_cache_(NewLRUCache(sim_capacity, num_shard_bits)), miss_times_(0), - hit_times_(0), - stats_(stats) {} + hit_times_(0) {} virtual ~SimCacheImpl() {} virtual void SetCapacity(size_t capacity) override { @@ -51,17 +51,17 @@ class SimCacheImpl : public SimCache { return cache_->Insert(key, value, charge, deleter, handle, priority); } - virtual Handle* Lookup(const Slice& key) override { + virtual Handle* Lookup(const Slice& key, Statistics* stats) override { Handle* h = key_only_cache_->Lookup(key); if (h != nullptr) { key_only_cache_->Release(h); inc_hit_counter(); - RecordTick(stats_, SIM_BLOCK_CACHE_HIT); + RecordTick(stats, SIM_BLOCK_CACHE_HIT); } else { inc_miss_counter(); - RecordTick(stats_, SIM_BLOCK_CACHE_MISS); + RecordTick(stats, SIM_BLOCK_CACHE_MISS); } - return cache_->Lookup(key); + return cache_->Lookup(key, stats); } virtual void Release(Handle* handle) override { cache_->Release(handle); } @@ -160,13 +160,11 @@ class SimCacheImpl : public SimCache { // For instrumentation purpose, use NewSimCache instead std::shared_ptr NewSimCache(std::shared_ptr cache, - size_t sim_capacity, int num_shard_bits, - std::shared_ptr stats) { + size_t sim_capacity, int num_shard_bits) { if (num_shard_bits >= 20) { return nullptr; // the cache cannot be sharded into too many fine pieces } - return std::make_shared(cache, sim_capacity, num_shard_bits, - stats.get()); + return std::make_shared(cache, sim_capacity, num_shard_bits); } } // end namespace rocksdb