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
main
Aaron Gao 8 years ago
parent 85bb308258
commit 4590b53a4b
  1. 5
      include/rocksdb/cache.h
  2. 8
      include/rocksdb/utilities/sim_cache.h
  3. 2
      table/block_based_table_reader.cc
  4. 2
      util/sharded_cache.cc
  5. 2
      util/sharded_cache.h
  6. 20
      utilities/simulator_cache/sim_cache.cc

@ -25,6 +25,7 @@
#include <stdint.h> #include <stdint.h>
#include <memory> #include <memory>
#include "rocksdb/slice.h" #include "rocksdb/slice.h"
#include "rocksdb/statistics.h"
#include "rocksdb/status.h" #include "rocksdb/status.h"
namespace rocksdb { namespace rocksdb {
@ -96,7 +97,9 @@ class Cache {
// Else return a handle that corresponds to the mapping. The caller // Else return a handle that corresponds to the mapping. The caller
// must call this->Release(handle) when the returned mapping is no // must call this->Release(handle) when the returned mapping is no
// longer needed. // 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(). // Release a mapping returned by a previous Lookup().
// REQUIRES: handle must not have been released yet. // REQUIRES: handle must not have been released yet.

@ -10,8 +10,8 @@
#include <string> #include <string>
#include "rocksdb/cache.h" #include "rocksdb/cache.h"
#include "rocksdb/slice.h" #include "rocksdb/slice.h"
#include "rocksdb/statistics.h"
#include "rocksdb/status.h" #include "rocksdb/status.h"
#include "util/statistics.h"
namespace rocksdb { namespace rocksdb {
@ -31,9 +31,9 @@ class SimCache;
// BlockBasedTableOptions.block_size = 4096 by default but is configurable, // BlockBasedTableOptions.block_size = 4096 by default but is configurable,
// Therefore, generally the actual memory overhead of SimCache is Less than // Therefore, generally the actual memory overhead of SimCache is Less than
// sim_capacity * 2% // sim_capacity * 2%
extern std::shared_ptr<SimCache> NewSimCache( extern std::shared_ptr<SimCache> NewSimCache(std::shared_ptr<Cache> cache,
std::shared_ptr<Cache> cache, size_t sim_capacity, int num_shard_bits, size_t sim_capacity,
std::shared_ptr<Statistics> stats = nullptr); int num_shard_bits);
class SimCache : public Cache { class SimCache : public Cache {
public: public:

@ -119,7 +119,7 @@ Cache::Handle* GetEntryFromCache(Cache* block_cache, const Slice& key,
Tickers block_cache_miss_ticker, Tickers block_cache_miss_ticker,
Tickers block_cache_hit_ticker, Tickers block_cache_hit_ticker,
Statistics* statistics) { Statistics* statistics) {
auto cache_handle = block_cache->Lookup(key); auto cache_handle = block_cache->Lookup(key, statistics);
if (cache_handle != nullptr) { if (cache_handle != nullptr) {
PERF_COUNTER_ADD(block_cache_hit_count, 1); PERF_COUNTER_ADD(block_cache_hit_count, 1);
// overall cache hit // overall cache hit

@ -46,7 +46,7 @@ Status ShardedCache::Insert(const Slice& key, void* value, size_t charge,
->Insert(key, hash, value, charge, deleter, handle, priority); ->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); uint32_t hash = HashSlice(key);
return GetShard(Shard(hash))->Lookup(key, hash); return GetShard(Shard(hash))->Lookup(key, hash);
} }

@ -60,7 +60,7 @@ class ShardedCache : public Cache {
virtual Status Insert(const Slice& key, void* value, size_t charge, virtual Status Insert(const Slice& key, void* value, size_t charge,
void (*deleter)(const Slice& key, void* value), void (*deleter)(const Slice& key, void* value),
Handle** handle, Priority priority) override; 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 Release(Handle* handle) override;
virtual void Erase(const Slice& key) override; virtual void Erase(const Slice& key) override;
virtual uint64_t NewId() override; virtual uint64_t NewId() override;

@ -6,6 +6,7 @@
#include "rocksdb/utilities/sim_cache.h" #include "rocksdb/utilities/sim_cache.h"
#include <atomic> #include <atomic>
#include "port/port.h" #include "port/port.h"
#include "util/statistics.h"
namespace rocksdb { namespace rocksdb {
@ -16,12 +17,11 @@ class SimCacheImpl : public SimCache {
// capacity for real cache (ShardedLRUCache) // capacity for real cache (ShardedLRUCache)
// test_capacity for key only cache // test_capacity for key only cache
SimCacheImpl(std::shared_ptr<Cache> cache, size_t sim_capacity, SimCacheImpl(std::shared_ptr<Cache> cache, size_t sim_capacity,
int num_shard_bits, Statistics* stats) int num_shard_bits)
: cache_(cache), : cache_(cache),
key_only_cache_(NewLRUCache(sim_capacity, num_shard_bits)), key_only_cache_(NewLRUCache(sim_capacity, num_shard_bits)),
miss_times_(0), miss_times_(0),
hit_times_(0), hit_times_(0) {}
stats_(stats) {}
virtual ~SimCacheImpl() {} virtual ~SimCacheImpl() {}
virtual void SetCapacity(size_t capacity) override { virtual void SetCapacity(size_t capacity) override {
@ -51,17 +51,17 @@ class SimCacheImpl : public SimCache {
return cache_->Insert(key, value, charge, deleter, handle, priority); 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); Handle* h = key_only_cache_->Lookup(key);
if (h != nullptr) { if (h != nullptr) {
key_only_cache_->Release(h); key_only_cache_->Release(h);
inc_hit_counter(); inc_hit_counter();
RecordTick(stats_, SIM_BLOCK_CACHE_HIT); RecordTick(stats, SIM_BLOCK_CACHE_HIT);
} else { } else {
inc_miss_counter(); 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); } virtual void Release(Handle* handle) override { cache_->Release(handle); }
@ -160,13 +160,11 @@ class SimCacheImpl : public SimCache {
// For instrumentation purpose, use NewSimCache instead // For instrumentation purpose, use NewSimCache instead
std::shared_ptr<SimCache> NewSimCache(std::shared_ptr<Cache> cache, std::shared_ptr<SimCache> NewSimCache(std::shared_ptr<Cache> cache,
size_t sim_capacity, int num_shard_bits, size_t sim_capacity, int num_shard_bits) {
std::shared_ptr<Statistics> stats) {
if (num_shard_bits >= 20) { if (num_shard_bits >= 20) {
return nullptr; // the cache cannot be sharded into too many fine pieces return nullptr; // the cache cannot be sharded into too many fine pieces
} }
return std::make_shared<SimCacheImpl>(cache, sim_capacity, num_shard_bits, return std::make_shared<SimCacheImpl>(cache, sim_capacity, num_shard_bits);
stats.get());
} }
} // end namespace rocksdb } // end namespace rocksdb

Loading…
Cancel
Save