#include <stdint.h>
#include <memory>
#include <string>
+#include "rocksdb/memory_allocator.h"
#include "rocksdb/slice.h"
#include "rocksdb/statistics.h"
#include "rocksdb/status.h"
class Cache;
+extern const bool kDefaultToAdaptiveMutex;
+
struct LRUCacheOptions {
// Capacity of the cache.
size_t capacity = 0;
// BlockBasedTableOptions::cache_index_and_filter_blocks_with_high_priority.
double high_pri_pool_ratio = 0.0;
+ // If non-nullptr will use this allocator instead of system allocator when
+ // allocating memory for cache blocks. Call this method before you start using
+ // the cache!
+ //
+ // Caveat: when the cache is used as block cache, the memory allocator is
+ // ignored when dealing with compression libraries that allocate memory
+ // internally (currently only XPRESS).
+ std::shared_ptr<MemoryAllocator> memory_allocator;
+
+ // Whether to use adaptive mutexes for cache shards. Note that adaptive
+ // mutexes need to be supported by the platform in order for this to have any
+ // effect. The default value is true if RocksDB is compiled with
+ // -DROCKSDB_DEFAULT_TO_ADAPTIVE_MUTEX, false otherwise.
+ bool use_adaptive_mutex = kDefaultToAdaptiveMutex;
+
LRUCacheOptions() {}
LRUCacheOptions(size_t _capacity, int _num_shard_bits,
- bool _strict_capacity_limit, double _high_pri_pool_ratio)
+ bool _strict_capacity_limit, double _high_pri_pool_ratio,
+ std::shared_ptr<MemoryAllocator> _memory_allocator = nullptr,
+ bool _use_adaptive_mutex = kDefaultToAdaptiveMutex)
: capacity(_capacity),
num_shard_bits(_num_shard_bits),
strict_capacity_limit(_strict_capacity_limit),
- high_pri_pool_ratio(_high_pri_pool_ratio) {}
+ high_pri_pool_ratio(_high_pri_pool_ratio),
+ memory_allocator(std::move(_memory_allocator)),
+ use_adaptive_mutex(_use_adaptive_mutex) {}
};
// Create a new cache with a fixed size capacity. The cache is sharded
// high_pri_pool_pct.
// num_shard_bits = -1 means it is automatically determined: every shard
// will be at least 512KB and number of shard bits will not exceed 6.
-extern std::shared_ptr<Cache> NewLRUCache(size_t capacity,
- int num_shard_bits = -1,
- bool strict_capacity_limit = false,
- double high_pri_pool_ratio = 0.0);
+extern std::shared_ptr<Cache> NewLRUCache(
+ size_t capacity, int num_shard_bits = -1,
+ bool strict_capacity_limit = false, double high_pri_pool_ratio = 0.0,
+ std::shared_ptr<MemoryAllocator> memory_allocator = nullptr,
+ bool use_adaptive_mutex = kDefaultToAdaptiveMutex);
extern std::shared_ptr<Cache> NewLRUCache(const LRUCacheOptions& cache_opts);
// likely to get evicted than low priority entries.
enum class Priority { HIGH, LOW };
- Cache() {}
+ Cache(std::shared_ptr<MemoryAllocator> allocator = nullptr)
+ : memory_allocator_(std::move(allocator)) {}
// Destroys all existing entries by calling the "deleter"
// function that was passed via the Insert() function.
virtual void TEST_mark_as_data_block(const Slice& /*key*/,
size_t /*charge*/) {}
+ MemoryAllocator* memory_allocator() const { return memory_allocator_.get(); }
+
private:
// No copying allowed
Cache(const Cache&);
Cache& operator=(const Cache&);
+
+ std::shared_ptr<MemoryAllocator> memory_allocator_;
};
} // namespace rocksdb