sim_cache_test.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "rocksdb/utilities/sim_cache.h"
  6. #include <cstdlib>
  7. #include "db/db_test_util.h"
  8. #include "port/stack_trace.h"
  9. namespace ROCKSDB_NAMESPACE {
  10. class SimCacheTest : public DBTestBase {
  11. private:
  12. size_t miss_count_ = 0;
  13. size_t hit_count_ = 0;
  14. size_t insert_count_ = 0;
  15. size_t failure_count_ = 0;
  16. public:
  17. const size_t kNumBlocks = 5;
  18. const size_t kValueSize = 1000;
  19. SimCacheTest() : DBTestBase("/sim_cache_test") {}
  20. BlockBasedTableOptions GetTableOptions() {
  21. BlockBasedTableOptions table_options;
  22. // Set a small enough block size so that each key-value get its own block.
  23. table_options.block_size = 1;
  24. return table_options;
  25. }
  26. Options GetOptions(const BlockBasedTableOptions& table_options) {
  27. Options options = CurrentOptions();
  28. options.create_if_missing = true;
  29. // options.compression = kNoCompression;
  30. options.statistics = ROCKSDB_NAMESPACE::CreateDBStatistics();
  31. options.table_factory.reset(new BlockBasedTableFactory(table_options));
  32. return options;
  33. }
  34. void InitTable(const Options& /*options*/) {
  35. std::string value(kValueSize, 'a');
  36. for (size_t i = 0; i < kNumBlocks * 2; i++) {
  37. ASSERT_OK(Put(ToString(i), value.c_str()));
  38. }
  39. }
  40. void RecordCacheCounters(const Options& options) {
  41. miss_count_ = TestGetTickerCount(options, BLOCK_CACHE_MISS);
  42. hit_count_ = TestGetTickerCount(options, BLOCK_CACHE_HIT);
  43. insert_count_ = TestGetTickerCount(options, BLOCK_CACHE_ADD);
  44. failure_count_ = TestGetTickerCount(options, BLOCK_CACHE_ADD_FAILURES);
  45. }
  46. void CheckCacheCounters(const Options& options, size_t expected_misses,
  47. size_t expected_hits, size_t expected_inserts,
  48. size_t expected_failures) {
  49. size_t new_miss_count = TestGetTickerCount(options, BLOCK_CACHE_MISS);
  50. size_t new_hit_count = TestGetTickerCount(options, BLOCK_CACHE_HIT);
  51. size_t new_insert_count = TestGetTickerCount(options, BLOCK_CACHE_ADD);
  52. size_t new_failure_count =
  53. TestGetTickerCount(options, BLOCK_CACHE_ADD_FAILURES);
  54. ASSERT_EQ(miss_count_ + expected_misses, new_miss_count);
  55. ASSERT_EQ(hit_count_ + expected_hits, new_hit_count);
  56. ASSERT_EQ(insert_count_ + expected_inserts, new_insert_count);
  57. ASSERT_EQ(failure_count_ + expected_failures, new_failure_count);
  58. miss_count_ = new_miss_count;
  59. hit_count_ = new_hit_count;
  60. insert_count_ = new_insert_count;
  61. failure_count_ = new_failure_count;
  62. }
  63. };
  64. TEST_F(SimCacheTest, SimCache) {
  65. ReadOptions read_options;
  66. auto table_options = GetTableOptions();
  67. auto options = GetOptions(table_options);
  68. InitTable(options);
  69. LRUCacheOptions co;
  70. co.capacity = 0;
  71. co.num_shard_bits = 0;
  72. co.strict_capacity_limit = false;
  73. co.metadata_charge_policy = kDontChargeCacheMetadata;
  74. std::shared_ptr<SimCache> simCache = NewSimCache(NewLRUCache(co), 20000, 0);
  75. table_options.block_cache = simCache;
  76. options.table_factory.reset(new BlockBasedTableFactory(table_options));
  77. Reopen(options);
  78. RecordCacheCounters(options);
  79. std::vector<std::unique_ptr<Iterator>> iterators(kNumBlocks);
  80. Iterator* iter = nullptr;
  81. // Load blocks into cache.
  82. for (size_t i = 0; i < kNumBlocks; i++) {
  83. iter = db_->NewIterator(read_options);
  84. iter->Seek(ToString(i));
  85. ASSERT_OK(iter->status());
  86. CheckCacheCounters(options, 1, 0, 1, 0);
  87. iterators[i].reset(iter);
  88. }
  89. ASSERT_EQ(kNumBlocks,
  90. simCache->get_hit_counter() + simCache->get_miss_counter());
  91. ASSERT_EQ(0, simCache->get_hit_counter());
  92. size_t usage = simCache->GetUsage();
  93. ASSERT_LT(0, usage);
  94. ASSERT_EQ(usage, simCache->GetSimUsage());
  95. simCache->SetCapacity(usage);
  96. ASSERT_EQ(usage, simCache->GetPinnedUsage());
  97. // Test with strict capacity limit.
  98. simCache->SetStrictCapacityLimit(true);
  99. iter = db_->NewIterator(read_options);
  100. iter->Seek(ToString(kNumBlocks * 2 - 1));
  101. ASSERT_TRUE(iter->status().IsIncomplete());
  102. CheckCacheCounters(options, 1, 0, 0, 1);
  103. delete iter;
  104. iter = nullptr;
  105. // Release iterators and access cache again.
  106. for (size_t i = 0; i < kNumBlocks; i++) {
  107. iterators[i].reset();
  108. CheckCacheCounters(options, 0, 0, 0, 0);
  109. }
  110. // Add kNumBlocks again
  111. for (size_t i = 0; i < kNumBlocks; i++) {
  112. std::unique_ptr<Iterator> it(db_->NewIterator(read_options));
  113. it->Seek(ToString(i));
  114. ASSERT_OK(it->status());
  115. CheckCacheCounters(options, 0, 1, 0, 0);
  116. }
  117. ASSERT_EQ(5, simCache->get_hit_counter());
  118. for (size_t i = kNumBlocks; i < kNumBlocks * 2; i++) {
  119. std::unique_ptr<Iterator> it(db_->NewIterator(read_options));
  120. it->Seek(ToString(i));
  121. ASSERT_OK(it->status());
  122. CheckCacheCounters(options, 1, 0, 1, 0);
  123. }
  124. ASSERT_EQ(0, simCache->GetPinnedUsage());
  125. ASSERT_EQ(3 * kNumBlocks + 1,
  126. simCache->get_hit_counter() + simCache->get_miss_counter());
  127. ASSERT_EQ(6, simCache->get_hit_counter());
  128. }
  129. TEST_F(SimCacheTest, SimCacheLogging) {
  130. auto table_options = GetTableOptions();
  131. auto options = GetOptions(table_options);
  132. options.disable_auto_compactions = true;
  133. LRUCacheOptions co;
  134. co.capacity = 1024 * 1024;
  135. co.metadata_charge_policy = kDontChargeCacheMetadata;
  136. std::shared_ptr<SimCache> sim_cache = NewSimCache(NewLRUCache(co), 20000, 0);
  137. table_options.block_cache = sim_cache;
  138. options.table_factory.reset(new BlockBasedTableFactory(table_options));
  139. Reopen(options);
  140. int num_block_entries = 20;
  141. for (int i = 0; i < num_block_entries; i++) {
  142. Put(Key(i), "val");
  143. Flush();
  144. }
  145. std::string log_file = test::PerThreadDBPath(env_, "cache_log.txt");
  146. ASSERT_OK(sim_cache->StartActivityLogging(log_file, env_));
  147. for (int i = 0; i < num_block_entries; i++) {
  148. ASSERT_EQ(Get(Key(i)), "val");
  149. }
  150. for (int i = 0; i < num_block_entries; i++) {
  151. ASSERT_EQ(Get(Key(i)), "val");
  152. }
  153. sim_cache->StopActivityLogging();
  154. ASSERT_OK(sim_cache->GetActivityLoggingStatus());
  155. std::string file_contents = "";
  156. ReadFileToString(env_, log_file, &file_contents);
  157. int lookup_num = 0;
  158. int add_num = 0;
  159. std::string::size_type pos;
  160. // count number of lookups
  161. pos = 0;
  162. while ((pos = file_contents.find("LOOKUP -", pos)) != std::string::npos) {
  163. ++lookup_num;
  164. pos += 1;
  165. }
  166. // count number of additions
  167. pos = 0;
  168. while ((pos = file_contents.find("ADD -", pos)) != std::string::npos) {
  169. ++add_num;
  170. pos += 1;
  171. }
  172. // We asked for every block twice
  173. ASSERT_EQ(lookup_num, num_block_entries * 2);
  174. // We added every block only once, since the cache can hold all blocks
  175. ASSERT_EQ(add_num, num_block_entries);
  176. // Log things again but stop logging automatically after reaching 512 bytes
  177. // @lint-ignore TXT2 T25377293 Grandfathered in
  178. int max_size = 512;
  179. ASSERT_OK(sim_cache->StartActivityLogging(log_file, env_, max_size));
  180. for (int it = 0; it < 10; it++) {
  181. for (int i = 0; i < num_block_entries; i++) {
  182. ASSERT_EQ(Get(Key(i)), "val");
  183. }
  184. }
  185. ASSERT_OK(sim_cache->GetActivityLoggingStatus());
  186. uint64_t fsize = 0;
  187. ASSERT_OK(env_->GetFileSize(log_file, &fsize));
  188. // error margin of 100 bytes
  189. ASSERT_LT(fsize, max_size + 100);
  190. ASSERT_GT(fsize, max_size - 100);
  191. }
  192. } // namespace ROCKSDB_NAMESPACE
  193. int main(int argc, char** argv) {
  194. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  195. ::testing::InitGoogleTest(&argc, argv);
  196. return RUN_ALL_TESTS();
  197. }