]>
Commit | Line | Data |
---|---|---|
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 | // | |
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 | ||
20 | namespace rocksdb { | |
21 | ||
22 | ShardedCache::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 | ||
29 | void 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 | ||
39 | void 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 | ||
48 | Status 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 | ||
56 | Cache::Handle* ShardedCache::Lookup(const Slice& key, Statistics* /*stats*/) { | |
57 | uint32_t hash = HashSlice(key); | |
58 | return GetShard(Shard(hash))->Lookup(key, hash); | |
59 | } | |
60 | ||
61 | bool ShardedCache::Ref(Handle* handle) { | |
62 | uint32_t hash = GetHash(handle); | |
63 | return GetShard(Shard(hash))->Ref(handle); | |
64 | } | |
65 | ||
66 | bool ShardedCache::Release(Handle* handle, bool force_erase) { | |
67 | uint32_t hash = GetHash(handle); | |
68 | return GetShard(Shard(hash))->Release(handle, force_erase); | |
69 | } | |
70 | ||
71 | void ShardedCache::Erase(const Slice& key) { | |
72 | uint32_t hash = HashSlice(key); | |
73 | GetShard(Shard(hash))->Erase(key, hash); | |
74 | } | |
75 | ||
76 | uint64_t ShardedCache::NewId() { | |
77 | return last_id_.fetch_add(1, std::memory_order_relaxed); | |
78 | } | |
79 | ||
80 | size_t ShardedCache::GetCapacity() const { | |
81 | MutexLock l(&capacity_mutex_); | |
82 | return capacity_; | |
83 | } | |
84 | ||
85 | bool ShardedCache::HasStrictCapacityLimit() const { | |
86 | MutexLock l(&capacity_mutex_); | |
87 | return strict_capacity_limit_; | |
88 | } | |
89 | ||
90 | size_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 | ||
100 | size_t ShardedCache::GetUsage(Handle* handle) const { | |
101 | return GetCharge(handle); | |
102 | } | |
103 | ||
104 | size_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 | ||
114 | void 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 | ||
122 | void 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 | ||
129 | std::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 | } | |
148 | int 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 |