]> git.proxmox.com Git - ceph.git/blame - ceph/src/rocksdb/cache/sharded_cache.cc
build: use dgit for download target
[ceph.git] / ceph / src / rocksdb / cache / sharded_cache.cc
CommitLineData
7c673cae 1// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
11fdf7f2
TL
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).
7c673cae
FG
5//
6// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7// Use of this source code is governed by a BSD-style license that can be
8// found in the LICENSE file. See the AUTHORS file for names of contributors.
9
10#ifndef __STDC_FORMAT_MACROS
11#define __STDC_FORMAT_MACROS
12#endif
13
14#include "cache/sharded_cache.h"
15
16#include <string>
17
18#include "util/mutexlock.h"
19
20namespace rocksdb {
21
22ShardedCache::ShardedCache(size_t capacity, int num_shard_bits,
23 bool strict_capacity_limit)
24 : num_shard_bits_(num_shard_bits),
25 capacity_(capacity),
26 strict_capacity_limit_(strict_capacity_limit),
27 last_id_(1) {}
28
29void ShardedCache::SetCapacity(size_t capacity) {
30 int num_shards = 1 << num_shard_bits_;
31 const size_t per_shard = (capacity + (num_shards - 1)) / num_shards;
32 MutexLock l(&capacity_mutex_);
33 for (int s = 0; s < num_shards; s++) {
34 GetShard(s)->SetCapacity(per_shard);
35 }
36 capacity_ = capacity;
37}
38
39void ShardedCache::SetStrictCapacityLimit(bool strict_capacity_limit) {
40 int num_shards = 1 << num_shard_bits_;
41 MutexLock l(&capacity_mutex_);
42 for (int s = 0; s < num_shards; s++) {
43 GetShard(s)->SetStrictCapacityLimit(strict_capacity_limit);
44 }
45 strict_capacity_limit_ = strict_capacity_limit;
46}
47
48Status ShardedCache::Insert(const Slice& key, void* value, size_t charge,
49 void (*deleter)(const Slice& key, void* value),
50 Handle** handle, Priority priority) {
51 uint32_t hash = HashSlice(key);
52 return GetShard(Shard(hash))
53 ->Insert(key, hash, value, charge, deleter, handle, priority);
54}
55
11fdf7f2 56Cache::Handle* ShardedCache::Lookup(const Slice& key, Statistics* /*stats*/) {
7c673cae
FG
57 uint32_t hash = HashSlice(key);
58 return GetShard(Shard(hash))->Lookup(key, hash);
59}
60
61bool ShardedCache::Ref(Handle* handle) {
62 uint32_t hash = GetHash(handle);
63 return GetShard(Shard(hash))->Ref(handle);
64}
65
66bool ShardedCache::Release(Handle* handle, bool force_erase) {
67 uint32_t hash = GetHash(handle);
68 return GetShard(Shard(hash))->Release(handle, force_erase);
69}
70
71void ShardedCache::Erase(const Slice& key) {
72 uint32_t hash = HashSlice(key);
73 GetShard(Shard(hash))->Erase(key, hash);
74}
75
76uint64_t ShardedCache::NewId() {
77 return last_id_.fetch_add(1, std::memory_order_relaxed);
78}
79
80size_t ShardedCache::GetCapacity() const {
81 MutexLock l(&capacity_mutex_);
82 return capacity_;
83}
84
85bool ShardedCache::HasStrictCapacityLimit() const {
86 MutexLock l(&capacity_mutex_);
87 return strict_capacity_limit_;
88}
89
90size_t ShardedCache::GetUsage() const {
91 // We will not lock the cache when getting the usage from shards.
92 int num_shards = 1 << num_shard_bits_;
93 size_t usage = 0;
94 for (int s = 0; s < num_shards; s++) {
95 usage += GetShard(s)->GetUsage();
96 }
97 return usage;
98}
99
100size_t ShardedCache::GetUsage(Handle* handle) const {
101 return GetCharge(handle);
102}
103
104size_t ShardedCache::GetPinnedUsage() const {
105 // We will not lock the cache when getting the usage from shards.
106 int num_shards = 1 << num_shard_bits_;
107 size_t usage = 0;
108 for (int s = 0; s < num_shards; s++) {
109 usage += GetShard(s)->GetPinnedUsage();
110 }
111 return usage;
112}
113
114void ShardedCache::ApplyToAllCacheEntries(void (*callback)(void*, size_t),
115 bool thread_safe) {
116 int num_shards = 1 << num_shard_bits_;
117 for (int s = 0; s < num_shards; s++) {
118 GetShard(s)->ApplyToAllCacheEntries(callback, thread_safe);
119 }
120}
121
122void ShardedCache::EraseUnRefEntries() {
123 int num_shards = 1 << num_shard_bits_;
124 for (int s = 0; s < num_shards; s++) {
125 GetShard(s)->EraseUnRefEntries();
126 }
127}
128
129std::string ShardedCache::GetPrintableOptions() const {
130 std::string ret;
131 ret.reserve(20000);
132 const int kBufferSize = 200;
133 char buffer[kBufferSize];
134 {
135 MutexLock l(&capacity_mutex_);
136 snprintf(buffer, kBufferSize, " capacity : %" ROCKSDB_PRIszt "\n",
137 capacity_);
138 ret.append(buffer);
139 snprintf(buffer, kBufferSize, " num_shard_bits : %d\n", num_shard_bits_);
140 ret.append(buffer);
141 snprintf(buffer, kBufferSize, " strict_capacity_limit : %d\n",
142 strict_capacity_limit_);
143 ret.append(buffer);
144 }
145 ret.append(GetShard(0)->GetPrintableOptions());
146 return ret;
147}
148int GetDefaultCacheShardBits(size_t capacity) {
149 int num_shard_bits = 0;
150 size_t min_shard_size = 512L * 1024L; // Every shard is at least 512KB.
151 size_t num_shards = capacity / min_shard_size;
152 while (num_shards >>= 1) {
153 if (++num_shard_bits >= 6) {
154 // No more than 6.
155 return num_shard_bits;
156 }
157 }
158 return num_shard_bits;
159}
160
161} // namespace rocksdb