Bytes read/written from cache statistics

Summary: Add 2 new counters BLOCK_CACHE_BYTES_WRITE, BLOCK_CACHE_BYTES_READ to keep track of how many bytes were written to the cache and how many bytes that we read from cache

Test Plan: make check

Reviewers: sdong, yhchiang, igor

Reviewed By: igor

Subscribers: dhruba

Differential Revision: https://reviews.facebook.net/D48195
This commit is contained in:
Islam AbdelRahman 2015-10-07 15:17:20 -07:00
parent f925208ffc
commit 51fa7ecec5
5 changed files with 57 additions and 0 deletions

View File

@ -104,6 +104,9 @@ class Cache {
// returns the memory size for the entries residing in the cache. // returns the memory size for the entries residing in the cache.
virtual size_t GetUsage() const = 0; virtual size_t GetUsage() const = 0;
// returns the memory size for a specific entry in the cache.
virtual size_t GetUsage(Handle* handle) const = 0;
// returns the memory size for the entries in use by the system // returns the memory size for the entries in use by the system
virtual size_t GetPinnedUsage() const = 0; virtual size_t GetPinnedUsage() const = 0;

View File

@ -45,6 +45,10 @@ enum Tickers : uint32_t {
BLOCK_CACHE_DATA_MISS, BLOCK_CACHE_DATA_MISS,
// # of times cache hit when accessing data block from block cache. // # of times cache hit when accessing data block from block cache.
BLOCK_CACHE_DATA_HIT, BLOCK_CACHE_DATA_HIT,
// # of bytes read from cache.
BLOCK_CACHE_BYTES_READ,
// # of bytes written into cache.
BLOCK_CACHE_BYTES_WRITE,
// # of times bloom filter has avoided file reads. // # of times bloom filter has avoided file reads.
BLOOM_FILTER_USEFUL, BLOOM_FILTER_USEFUL,
@ -177,6 +181,8 @@ const std::vector<std::pair<Tickers, std::string>> TickersNameMap = {
{BLOCK_CACHE_FILTER_HIT, "rocksdb.block.cache.filter.hit"}, {BLOCK_CACHE_FILTER_HIT, "rocksdb.block.cache.filter.hit"},
{BLOCK_CACHE_DATA_MISS, "rocksdb.block.cache.data.miss"}, {BLOCK_CACHE_DATA_MISS, "rocksdb.block.cache.data.miss"},
{BLOCK_CACHE_DATA_HIT, "rocksdb.block.cache.data.hit"}, {BLOCK_CACHE_DATA_HIT, "rocksdb.block.cache.data.hit"},
{BLOCK_CACHE_BYTES_READ, "rocksdb.block.cache.bytes.read"},
{BLOCK_CACHE_BYTES_WRITE, "rocksdb.block.cache.bytes.write"},
{BLOOM_FILTER_USEFUL, "rocksdb.bloom.filter.useful"}, {BLOOM_FILTER_USEFUL, "rocksdb.bloom.filter.useful"},
{MEMTABLE_HIT, "rocksdb.memtable.hit"}, {MEMTABLE_HIT, "rocksdb.memtable.hit"},
{MEMTABLE_MISS, "rocksdb.memtable.miss"}, {MEMTABLE_MISS, "rocksdb.memtable.miss"},

View File

@ -117,6 +117,9 @@ Cache::Handle* GetEntryFromCache(Cache* block_cache, const Slice& key,
PERF_COUNTER_ADD(block_cache_hit_count, 1); PERF_COUNTER_ADD(block_cache_hit_count, 1);
// overall cache hit // overall cache hit
RecordTick(statistics, BLOCK_CACHE_HIT); RecordTick(statistics, BLOCK_CACHE_HIT);
// total bytes read from cache
RecordTick(statistics, BLOCK_CACHE_BYTES_READ,
block_cache->GetUsage(cache_handle));
// block-type specific cache hit // block-type specific cache hit
RecordTick(statistics, block_cache_hit_ticker); RecordTick(statistics, block_cache_hit_ticker);
} else { } else {
@ -795,6 +798,8 @@ Status BlockBasedTable::PutDataBlockToCache(
block->value->usable_size(), block->value->usable_size(),
&DeleteCachedEntry<Block>); &DeleteCachedEntry<Block>);
RecordTick(statistics, BLOCK_CACHE_ADD); RecordTick(statistics, BLOCK_CACHE_ADD);
RecordTick(statistics, BLOCK_CACHE_BYTES_WRITE,
block->value->usable_size());
assert(reinterpret_cast<Block*>(block_cache->Value(block->cache_handle)) == assert(reinterpret_cast<Block*>(block_cache->Value(block->cache_handle)) ==
block->value); block->value);
} }
@ -886,6 +891,7 @@ BlockBasedTable::CachableEntry<FilterBlockReader> BlockBasedTable::GetFilter(
cache_handle = block_cache->Insert(key, filter, filter_size, cache_handle = block_cache->Insert(key, filter, filter_size,
&DeleteCachedEntry<FilterBlockReader>); &DeleteCachedEntry<FilterBlockReader>);
RecordTick(statistics, BLOCK_CACHE_ADD); RecordTick(statistics, BLOCK_CACHE_ADD);
RecordTick(statistics, BLOCK_CACHE_BYTES_WRITE, filter_size);
} }
} }
@ -944,6 +950,8 @@ Iterator* BlockBasedTable::NewIndexIterator(const ReadOptions& read_options,
block_cache->Insert(key, index_reader, index_reader->usable_size(), block_cache->Insert(key, index_reader, index_reader->usable_size(),
&DeleteCachedEntry<IndexReader>); &DeleteCachedEntry<IndexReader>);
RecordTick(statistics, BLOCK_CACHE_ADD); RecordTick(statistics, BLOCK_CACHE_ADD);
RecordTick(statistics, BLOCK_CACHE_BYTES_WRITE,
index_reader->usable_size());
} }
assert(cache_handle); assert(cache_handle);

View File

@ -1423,6 +1423,9 @@ class BlockCachePropertiesSnapshot {
filter_block_cache_miss = filter_block_cache_miss =
statistics->getTickerCount(BLOCK_CACHE_FILTER_MISS); statistics->getTickerCount(BLOCK_CACHE_FILTER_MISS);
filter_block_cache_hit = statistics->getTickerCount(BLOCK_CACHE_FILTER_HIT); filter_block_cache_hit = statistics->getTickerCount(BLOCK_CACHE_FILTER_HIT);
block_cache_bytes_read = statistics->getTickerCount(BLOCK_CACHE_BYTES_READ);
block_cache_bytes_write =
statistics->getTickerCount(BLOCK_CACHE_BYTES_WRITE);
} }
void AssertIndexBlockStat(int64_t expected_index_block_cache_miss, void AssertIndexBlockStat(int64_t expected_index_block_cache_miss,
@ -1453,6 +1456,10 @@ class BlockCachePropertiesSnapshot {
block_cache_hit); block_cache_hit);
} }
int64_t GetCacheBytesRead() { return block_cache_bytes_read; }
int64_t GetCacheBytesWrite() { return block_cache_bytes_write; }
private: private:
int64_t block_cache_miss = 0; int64_t block_cache_miss = 0;
int64_t block_cache_hit = 0; int64_t block_cache_hit = 0;
@ -1462,6 +1469,8 @@ class BlockCachePropertiesSnapshot {
int64_t data_block_cache_hit = 0; int64_t data_block_cache_hit = 0;
int64_t filter_block_cache_miss = 0; int64_t filter_block_cache_miss = 0;
int64_t filter_block_cache_hit = 0; int64_t filter_block_cache_hit = 0;
int64_t block_cache_bytes_read = 0;
int64_t block_cache_bytes_write = 0;
}; };
// Make sure, by default, index/filter blocks were pre-loaded (meaning we won't // Make sure, by default, index/filter blocks were pre-loaded (meaning we won't
@ -1537,12 +1546,17 @@ TEST_F(BlockBasedTableTest, FilterBlockInBlockCache) {
// Since block_cache is disabled, no cache activities will be involved. // Since block_cache is disabled, no cache activities will be involved.
unique_ptr<Iterator> iter; unique_ptr<Iterator> iter;
int64_t last_cache_bytes_read = 0;
// At first, no block will be accessed. // At first, no block will be accessed.
{ {
BlockCachePropertiesSnapshot props(options.statistics.get()); BlockCachePropertiesSnapshot props(options.statistics.get());
// index will be added to block cache. // index will be added to block cache.
props.AssertEqual(1, // index block miss props.AssertEqual(1, // index block miss
0, 0, 0); 0, 0, 0);
ASSERT_EQ(props.GetCacheBytesRead(), 0);
ASSERT_EQ(props.GetCacheBytesWrite(),
table_options.block_cache->GetUsage());
last_cache_bytes_read = props.GetCacheBytesRead();
} }
// Only index block will be accessed // Only index block will be accessed
@ -1554,6 +1568,11 @@ TEST_F(BlockBasedTableTest, FilterBlockInBlockCache) {
// value; other numbers remain the same. // value; other numbers remain the same.
props.AssertEqual(1, 0 + 1, // index block hit props.AssertEqual(1, 0 + 1, // index block hit
0, 0); 0, 0);
// Cache hit, bytes read from cache should increase
ASSERT_GT(props.GetCacheBytesRead(), last_cache_bytes_read);
ASSERT_EQ(props.GetCacheBytesWrite(),
table_options.block_cache->GetUsage());
last_cache_bytes_read = props.GetCacheBytesRead();
} }
// Only data block will be accessed // Only data block will be accessed
@ -1562,6 +1581,11 @@ TEST_F(BlockBasedTableTest, FilterBlockInBlockCache) {
BlockCachePropertiesSnapshot props(options.statistics.get()); BlockCachePropertiesSnapshot props(options.statistics.get());
props.AssertEqual(1, 1, 0 + 1, // data block miss props.AssertEqual(1, 1, 0 + 1, // data block miss
0); 0);
// Cache miss, Bytes read from cache should not change
ASSERT_EQ(props.GetCacheBytesRead(), last_cache_bytes_read);
ASSERT_EQ(props.GetCacheBytesWrite(),
table_options.block_cache->GetUsage());
last_cache_bytes_read = props.GetCacheBytesRead();
} }
// Data block will be in cache // Data block will be in cache
@ -1571,6 +1595,11 @@ TEST_F(BlockBasedTableTest, FilterBlockInBlockCache) {
BlockCachePropertiesSnapshot props(options.statistics.get()); BlockCachePropertiesSnapshot props(options.statistics.get());
props.AssertEqual(1, 1 + 1, /* index block hit */ props.AssertEqual(1, 1 + 1, /* index block hit */
1, 0 + 1 /* data block hit */); 1, 0 + 1 /* data block hit */);
// Cache hit, bytes read from cache should increase
ASSERT_GT(props.GetCacheBytesRead(), last_cache_bytes_read);
ASSERT_EQ(props.GetCacheBytesWrite(),
table_options.block_cache->GetUsage());
last_cache_bytes_read = props.GetCacheBytesRead();
} }
// release the iterator so that the block cache can reset correctly. // release the iterator so that the block cache can reset correctly.
iter.reset(); iter.reset();
@ -1587,6 +1616,8 @@ TEST_F(BlockBasedTableTest, FilterBlockInBlockCache) {
BlockCachePropertiesSnapshot props(options.statistics.get()); BlockCachePropertiesSnapshot props(options.statistics.get());
props.AssertEqual(1, // index block miss props.AssertEqual(1, // index block miss
0, 0, 0); 0, 0, 0);
// Cache miss, Bytes read from cache should not change
ASSERT_EQ(props.GetCacheBytesRead(), 0);
} }
{ {
@ -1598,6 +1629,8 @@ TEST_F(BlockBasedTableTest, FilterBlockInBlockCache) {
props.AssertEqual(1 + 1, // index block miss props.AssertEqual(1 + 1, // index block miss
0, 0, // data block miss 0, 0, // data block miss
0); 0);
// Cache hit, bytes read from cache should increase
ASSERT_EQ(props.GetCacheBytesRead(), 0);
} }
{ {
@ -1607,6 +1640,8 @@ TEST_F(BlockBasedTableTest, FilterBlockInBlockCache) {
BlockCachePropertiesSnapshot props(options.statistics.get()); BlockCachePropertiesSnapshot props(options.statistics.get());
props.AssertEqual(2, 0, 0 + 1, // data block miss props.AssertEqual(2, 0, 0 + 1, // data block miss
0); 0);
// Cache miss, Bytes read from cache should not change
ASSERT_EQ(props.GetCacheBytesRead(), 0);
} }
iter.reset(); iter.reset();

View File

@ -540,6 +540,11 @@ class ShardedLRUCache : public Cache {
} }
return usage; return usage;
} }
virtual size_t GetUsage(Handle* handle) const override {
return reinterpret_cast<LRUHandle*>(handle)->charge;
}
virtual size_t GetPinnedUsage() const override { virtual size_t GetPinnedUsage() const override {
// We will not lock the cache when getting the usage from shards. // We will not lock the cache when getting the usage from shards.
int num_shards = 1 << num_shard_bits_; int num_shards = 1 << num_shard_bits_;