1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
#include "rocksdb/utilities/sim_cache.h"
#include <atomic>
#include <iomanip>
#include "file/writable_file_writer.h"
#include "monitoring/statistics_impl.h"
#include "port/port.h"
#include "rocksdb/env.h"
#include "rocksdb/file_system.h"
#include "util/mutexlock.h"
namespace ROCKSDB_NAMESPACE {
namespace {
class CacheActivityLogger {
public:
CacheActivityLogger()
: activity_logging_enabled_(false), max_logging_size_(0) {}
~CacheActivityLogger() {
MutexLock l(&mutex_);
StopLoggingInternal();
bg_status_.PermitUncheckedError();
}
Status StartLogging(const std::string& activity_log_file, Env* env,
uint64_t max_logging_size = 0) {
assert(activity_log_file != "");
assert(env != nullptr);
Status status;
FileOptions file_opts;
MutexLock l(&mutex_);
// Stop existing logging if any
StopLoggingInternal();
// Open log file
status = WritableFileWriter::Create(env->GetFileSystem(), activity_log_file,
file_opts, &file_writer_, nullptr);
if (!status.ok()) {
return status;
}
max_logging_size_ = max_logging_size;
activity_logging_enabled_.store(true);
return status;
}
void StopLogging() {
MutexLock l(&mutex_);
StopLoggingInternal();
}
void ReportLookup(const Slice& key) {
if (activity_logging_enabled_.load() == false) {
return;
}
std::ostringstream oss;
// line format: "LOOKUP - <KEY>"
oss << "LOOKUP - " << key.ToString(true) << std::endl;
MutexLock l(&mutex_);
Status s = file_writer_->Append(IOOptions(), oss.str());
if (!s.ok() && bg_status_.ok()) {
bg_status_ = s;
}
if (MaxLoggingSizeReached() || !bg_status_.ok()) {
// Stop logging if we have reached the max file size or
// encountered an error
StopLoggingInternal();
}
}
void ReportAdd(const Slice& key, size_t size) {
if (activity_logging_enabled_.load() == false) {
return;
}
std::ostringstream oss;
// line format: "ADD - <KEY> - <KEY-SIZE>"
oss << "ADD - " << key.ToString(true) << " - " << size << std::endl;
MutexLock l(&mutex_);
Status s = file_writer_->Append(IOOptions(), oss.str());
if (!s.ok() && bg_status_.ok()) {
bg_status_ = s;
}
if (MaxLoggingSizeReached() || !bg_status_.ok()) {
// Stop logging if we have reached the max file size or
// encountered an error
StopLoggingInternal();
}
}
Status& bg_status() {
MutexLock l(&mutex_);
return bg_status_;
}
private:
bool MaxLoggingSizeReached() {
mutex_.AssertHeld();
return (max_logging_size_ > 0 &&
file_writer_->GetFileSize() >= max_logging_size_);
}
void StopLoggingInternal() {
mutex_.AssertHeld();
if (!activity_logging_enabled_) {
return;
}
activity_logging_enabled_.store(false);
Status s = file_writer_->Close(IOOptions());
if (!s.ok() && bg_status_.ok()) {
bg_status_ = s;
}
}
// Mutex to sync writes to file_writer, and all following
// class data members
port::Mutex mutex_;
// Indicates if logging is currently enabled
// atomic to allow reads without mutex
std::atomic<bool> activity_logging_enabled_;
// When reached, we will stop logging and close the file
// Value of 0 means unlimited
uint64_t max_logging_size_;
std::unique_ptr<WritableFileWriter> file_writer_;
Status bg_status_;
};
// SimCacheImpl definition
class SimCacheImpl : public SimCache {
public:
// capacity for real cache (ShardedLRUCache)
// test_capacity for key only cache
SimCacheImpl(std::shared_ptr<Cache> sim_cache, std::shared_ptr<Cache> cache)
: SimCache(cache),
key_only_cache_(sim_cache),
miss_times_(0),
hit_times_(0),
stats_(nullptr) {}
~SimCacheImpl() override = default;
const char* Name() const override { return "SimCache"; }
void SetCapacity(size_t capacity) override { target_->SetCapacity(capacity); }
void SetStrictCapacityLimit(bool strict_capacity_limit) override {
target_->SetStrictCapacityLimit(strict_capacity_limit);
}
Status Insert(const Slice& key, Cache::ObjectPtr value,
const CacheItemHelper* helper, size_t charge, Handle** handle,
Priority priority, const Slice& compressed = {},
CompressionType type = kNoCompression) override {
// The handle and value passed in are for real cache, so we pass nullptr
// to key_only_cache_ for both instead. Also, the deleter function pointer
// will be called by user to perform some external operation which should
// be applied only once. Thus key_only_cache accepts an empty function.
// *Lambda function without capture can be assgined to a function pointer
Handle* h = key_only_cache_->Lookup(key);
if (h == nullptr) {
// TODO: Check for error here?
auto s =
key_only_cache_->Insert(key, nullptr, &kNoopCacheItemHelper, charge,
nullptr, priority, compressed, type);
s.PermitUncheckedError();
} else {
key_only_cache_->Release(h);
}
cache_activity_logger_.ReportAdd(key, charge);
if (!target_) {
return Status::OK();
}
return target_->Insert(key, value, helper, charge, handle, priority,
compressed, type);
}
Handle* Lookup(const Slice& key, const CacheItemHelper* helper,
CreateContext* create_context,
Priority priority = Priority::LOW,
Statistics* stats = nullptr) override {
HandleLookup(key, stats);
if (!target_) {
return nullptr;
}
return target_->Lookup(key, helper, create_context, priority, stats);
}
void StartAsyncLookup(AsyncLookupHandle& async_handle) override {
HandleLookup(async_handle.key, async_handle.stats);
if (target_) {
target_->StartAsyncLookup(async_handle);
}
}
bool Ref(Handle* handle) override { return target_->Ref(handle); }
using Cache::Release;
bool Release(Handle* handle, bool erase_if_last_ref = false) override {
return target_->Release(handle, erase_if_last_ref);
}
void Erase(const Slice& key) override {
target_->Erase(key);
key_only_cache_->Erase(key);
}
Cache::ObjectPtr Value(Handle* handle) override {
return target_->Value(handle);
}
uint64_t NewId() override { return target_->NewId(); }
size_t GetCapacity() const override { return target_->GetCapacity(); }
bool HasStrictCapacityLimit() const override {
return target_->HasStrictCapacityLimit();
}
size_t GetUsage() const override { return target_->GetUsage(); }
size_t GetUsage(Handle* handle) const override {
return target_->GetUsage(handle);
}
size_t GetCharge(Handle* handle) const override {
return target_->GetCharge(handle);
}
const CacheItemHelper* GetCacheItemHelper(Handle* handle) const override {
return target_->GetCacheItemHelper(handle);
}
size_t GetPinnedUsage() const override { return target_->GetPinnedUsage(); }
void DisownData() override {
target_->DisownData();
key_only_cache_->DisownData();
}
void ApplyToAllEntries(
const std::function<void(const Slice& key, ObjectPtr value, size_t charge,
const CacheItemHelper* helper)>& callback,
const ApplyToAllEntriesOptions& opts) override {
target_->ApplyToAllEntries(callback, opts);
}
void EraseUnRefEntries() override {
target_->EraseUnRefEntries();
key_only_cache_->EraseUnRefEntries();
}
size_t GetSimCapacity() const override {
return key_only_cache_->GetCapacity();
}
size_t GetSimUsage() const override { return key_only_cache_->GetUsage(); }
void SetSimCapacity(size_t capacity) override {
key_only_cache_->SetCapacity(capacity);
}
uint64_t get_miss_counter() const override {
return miss_times_.load(std::memory_order_relaxed);
}
uint64_t get_hit_counter() const override {
return hit_times_.load(std::memory_order_relaxed);
}
void reset_counter() override {
miss_times_.store(0, std::memory_order_relaxed);
hit_times_.store(0, std::memory_order_relaxed);
SetTickerCount(stats_, SIM_BLOCK_CACHE_HIT, 0);
SetTickerCount(stats_, SIM_BLOCK_CACHE_MISS, 0);
}
std::string ToString() const override {
std::ostringstream oss;
oss << "SimCache MISSes: " << get_miss_counter() << std::endl;
oss << "SimCache HITs: " << get_hit_counter() << std::endl;
auto lookups = get_miss_counter() + get_hit_counter();
oss << "SimCache HITRATE: " << std::fixed << std::setprecision(2)
<< (lookups == 0 ? 0 : get_hit_counter() * 100.0f / lookups)
<< std::endl;
return oss.str();
}
std::string GetPrintableOptions() const override {
std::ostringstream oss;
oss << " cache_options:" << std::endl;
oss << target_->GetPrintableOptions();
oss << " sim_cache_options:" << std::endl;
oss << key_only_cache_->GetPrintableOptions();
return oss.str();
}
Status StartActivityLogging(const std::string& activity_log_file, Env* env,
uint64_t max_logging_size = 0) override {
return cache_activity_logger_.StartLogging(activity_log_file, env,
max_logging_size);
}
void StopActivityLogging() override { cache_activity_logger_.StopLogging(); }
Status GetActivityLoggingStatus() override {
return cache_activity_logger_.bg_status();
}
private:
std::shared_ptr<Cache> key_only_cache_;
std::atomic<uint64_t> miss_times_;
std::atomic<uint64_t> hit_times_;
Statistics* stats_;
CacheActivityLogger cache_activity_logger_;
void inc_miss_counter() {
miss_times_.fetch_add(1, std::memory_order_relaxed);
}
void inc_hit_counter() { hit_times_.fetch_add(1, std::memory_order_relaxed); }
void HandleLookup(const Slice& key, Statistics* stats) {
Handle* h = key_only_cache_->Lookup(key);
if (h != nullptr) {
key_only_cache_->Release(h);
inc_hit_counter();
RecordTick(stats, SIM_BLOCK_CACHE_HIT);
} else {
inc_miss_counter();
RecordTick(stats, SIM_BLOCK_CACHE_MISS);
}
cache_activity_logger_.ReportLookup(key);
}
};
} // end anonymous namespace
// For instrumentation purpose, use NewSimCache instead
std::shared_ptr<SimCache> NewSimCache(std::shared_ptr<Cache> cache,
size_t sim_capacity, int num_shard_bits) {
LRUCacheOptions co;
co.capacity = sim_capacity;
co.num_shard_bits = num_shard_bits;
co.metadata_charge_policy = kDontChargeCacheMetadata;
return NewSimCache(NewLRUCache(co), cache, num_shard_bits);
}
std::shared_ptr<SimCache> NewSimCache(std::shared_ptr<Cache> sim_cache,
std::shared_ptr<Cache> cache,
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<SimCacheImpl>(sim_cache, cache);
}
} // namespace ROCKSDB_NAMESPACE
|