??Simulation Cache(SimCache)可以幫助用戶在模擬的內(nèi)存容量而不是物理上實(shí)際占用內(nèi)存下預(yù)測(cè)block cache的性能數(shù)據(jù),比如:hit、miss。
Motivation
??幫助用戶調(diào)優(yōu)參數(shù):block cache size、更高效地使用內(nèi)存。另外,也可以幫助用戶了解 fast storage的cache性能。
Introduction
??SimCache的基本思想是根據(jù)要模擬的容量封裝正常的block cache,但是這個(gè)封裝后的block cache只有key,沒有value。當(dāng)插入數(shù)據(jù)時(shí),把key插入到兩個(gè)cache中,但是value只插入到normal cache。value的size會(huì)在兩種cache中都計(jì)算進(jìn)去,但是SimCache中因?yàn)橹挥衚ey,所以并沒有占用那么多的內(nèi)存,但是以此卻可以模擬block cache的一些行為。
How to use SimCache
??由于SimCache是normal cache的封裝,所以必須要先創(chuàng)建一個(gè)block cache。
std::shared_ptr<rocksdb::Cache> normal_block_cache =
NewLRUCache(1024 * 1024 * 1024 /* capacity 1GB */);
然后使用NewSimCache來封裝normal_block_cache,將rocksdb::BlockBasedTableOptions的block cache變量設(shè)置為SimCache實(shí)例,然后生成options.table_factory。
rocksdb::Options options;
rocksdb::BlockBasedTableOptions bbt_opts;
std::shared_ptr<rocksdb::Cache> sim_cache =
NewSimCache(normal_block_cache,
10 * 1024 * 1024 * 1024 /* sim_capacity 10GB */);
bbt_opts.block_cache = sim_cache;
options.table_factory.reset(new BlockBasedTableFactory(bbt_opts));
通過options配置來打開DB。通過調(diào)用sim_cache->get_hit_counter() and sim_cache->get_miss_counter()可以獲取到SimCache的HIT/MISS。
Memory Overhead
用戶可能會(huì)關(guān)注SimCache的實(shí)際內(nèi)存占用,大致評(píng)估如下:
sim_capacity * entry_size / (entry_size + block_size),
- 76 <= entry_size (key_size + other) <= 104
- BlockBasedTableOptions.block_size = 4096 by default but is configurable
實(shí)際上,SimCache的實(shí)際內(nèi)存開銷大概是 sim_capacity * 2%