util/cache.cc

filter: Statementstmt 76.3% 129 / 169 Branchbran 42.3% 44 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 63.3% 173 / 273 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« util/hash.cc ↑ Summary port/port_posix.cc »
1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4  
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8  
9 #include "leveldb/cache.h"
10 #include "port/port.h"
11 #include "util/hash.h"
12 #include "util/mutexlock.h"
13  
14 namespace leveldb {
15  
16 0 Cache::~Cache() {
17 0 }
18  
19 namespace {
20  
21 // LRU cache implementation
22  
23 // An entry is a variable length heap-allocated structure. Entries
24 // are kept in a circular doubly linked list ordered by access time.
25 struct LRUHandle {
26 void* value;
27 void (*deleter)(const Slice&, void* value);
28 LRUHandle* next_hash;
29 LRUHandle* next;
30 LRUHandle* prev;
31 size_t charge; // TODO(opt): Only allow uint32_t?
32 size_t key_length;
33 uint32_t refs;
34 uint32_t hash; // Hash of key(); used for fast sharding and comparisons
35 char key_data[1]; // Beginning of key
36  
37 5 Slice key() const {
38 // For cheaper lookups, we allow a temporary Handle object
39 // to store a pointer to a key in "value".
40 5 ▶ if (next == this) {
Branch: 1 branch
BranchTrueFalseTotal
if (next == this) { 0 5 5
41 0 return *(reinterpret_cast<Slice*>(value));
42 } else {
43 5 return Slice(key_data, key_length);
44 }
45 }
46 };
47  
48 // We provide our own simple hash table since it removes a whole bunch
49 // of porting hacks and is also faster than some of the built-in hash
50 // table implementations in some of the compiler/runtime combinations
51 // we have tested. E.g., readrandom speeds up by ~5% over the g++
52 // 4.4.3's builtin hashtable.
53 class HandleTable {
54 public:
55 128 HandleTable() : length_(0), elems_(0), list_(NULL) { Resize(); }
56 128 ▶ ~HandleTable() { delete[] list_; }
Branch: 1 branch
BranchTrueFalseTotal
~HandleTable() { delete[] list_; } 128 0 128
57  
58 3 LRUHandle* Lookup(const Slice& key, uint32_t hash) {
59 3 return *FindPointer(key, hash);
60 }
61  
62 2 LRUHandle* Insert(LRUHandle* h) {
63 2 LRUHandle** ptr = FindPointer(h->key(), h->hash);
64 2 LRUHandle* old = *ptr;
65 2 ▶ h->next_hash = (old == NULL ? NULL : old->next_hash);
Branch: 1 branch
BranchTrueFalseTotal
h->next_hash = (old == NULL ? NULL : old->next_hash); 0 2 2
66 2 *ptr = h;
67 2 ▶ if (old == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (old == NULL) { 2 0 2
68 2 ++elems_;
69 2 ▶ if (elems_ > length_) {
Branch: 1 branch
BranchTrueFalseTotal
if (elems_ > length_) { 0 2 2
70 // Since each cache entry is fairly large, we aim for a small
71 // average linked list length (<= 1).
72 0 Resize();
73 }
74 }
75 2 return old;
76 }
77  
78 0 LRUHandle* Remove(const Slice& key, uint32_t hash) {
79 0 LRUHandle** ptr = FindPointer(key, hash);
80 0 LRUHandle* result = *ptr;
81 0 ▶ if (result != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (result != NULL) { 0 0 0
82 0 *ptr = result->next_hash;
83 0 --elems_;
84 }
85 0 return result;
86 }
87  
88 private:
89 // The table consists of an array of buckets where each bucket is
90 // a linked list of cache entries that hash into the bucket.
91 uint32_t length_;
92 uint32_t elems_;
93 LRUHandle** list_;
94  
95 // Return a pointer to slot that points to a cache entry that
96 // matches key/hash. If there is no such cache entry, return a
97 // pointer to the trailing slot in the corresponding linked list.
98 5 LRUHandle** FindPointer(const Slice& key, uint32_t hash) {
99 5 LRUHandle** ptr = &list_[hash & (length_ - 1)];
100 6 ▶ while (*ptr != NULL &&
Branch: 1 branch
BranchTrueFalseTotal
while (*ptr != NULL && 1 4 5
101 6 ▶ ((*ptr)->hash != hash || key != (*ptr)->key())) {
Branch: 3 branches
BranchTrueFalseTotal
((*ptr)->hash != hash || key != (*ptr)->key())) { 1 0 1
((*ptr)->hash != hash || key != (*ptr)->key())) { 0 1 1
((*ptr)->hash != hash || key != (*ptr)->key())) { 0 5 5
102 0 ptr = &(*ptr)->next_hash;
103 }
104 5 return ptr;
105 }
106  
107 128 void Resize() {
108 128 uint32_t new_length = 4;
109 128 ▶ while (new_length < elems_) {
Branch: 1 branch
BranchTrueFalseTotal
while (new_length < elems_) { 0 128 128
110 0 new_length *= 2;
111 }
112 128 ▶ LRUHandle** new_list = new LRUHandle*[new_length];
Branch: 1 branch
BranchTrueFalseTotal
LRUHandle** new_list = new LRUHandle*[new_length]; 128 0 128
113 128 memset(new_list, 0, sizeof(new_list[0]) * new_length);
114 128 uint32_t count = 0;
115 128 ▶ for (uint32_t i = 0; i < length_; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (uint32_t i = 0; i < length_; i++) { 0 128 128
116 0 LRUHandle* h = list_[i];
117 0 ▶ while (h != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while (h != NULL) { 0 0 0
118 0 LRUHandle* next = h->next_hash;
119 0 Slice key = h->key();
120 0 uint32_t hash = h->hash;
121 0 LRUHandle** ptr = &new_list[hash & (new_length - 1)];
122 0 h->next_hash = *ptr;
123 0 *ptr = h;
124 0 h = next;
125 0 count++;
126 }
127 }
128 128 ▶ assert(elems_ == count);
Branch: 1 branch
BranchTrueFalseTotal
assert(elems_ == count); 0 128 128
129 128 ▶ delete[] list_;
Branch: 1 branch
BranchTrueFalseTotal
delete[] list_; 0 128 128
130 128 list_ = new_list;
131 128 length_ = new_length;
132 128 }
133 };
134  
135 // A single shard of sharded cache.
136 class LRUCache {
137 public:
138 LRUCache();
139 ~LRUCache();
140  
141 // Separate from constructor so caller can easily make an array of LRUCache
142 128 void SetCapacity(size_t capacity) { capacity_ = capacity; }
143  
144 // Like Cache methods, but with an extra "hash" parameter.
145 Cache::Handle* Insert(const Slice& key, uint32_t hash,
146 void* value, size_t charge,
147 void (*deleter)(const Slice& key, void* value));
148 Cache::Handle* Lookup(const Slice& key, uint32_t hash);
149 void Release(Cache::Handle* handle);
150 void Erase(const Slice& key, uint32_t hash);
151  
152 private:
153 void LRU_Remove(LRUHandle* e);
154 void LRU_Append(LRUHandle* e);
155 void Unref(LRUHandle* e);
156  
157 // Initialized before use.
158 size_t capacity_;
159  
160 // mutex_ protects the following state.
161 port::Mutex mutex_;
162 size_t usage_;
163 uint64_t last_id_;
164  
165 // Dummy head of LRU list.
166 // lru.prev is newest entry, lru.next is oldest entry.
167 LRUHandle lru_;
168  
169 HandleTable table_;
170 };
171  
172 128 LRUCache::LRUCache()
173 128 : usage_(0),
174 128 ▶ last_id_(0) {
Branch: 1 branch
BranchTrueFalseTotal
last_id_(0) { 128 0 128
175 // Make empty circular linked list
176 128 lru_.next = &lru_;
177 128 lru_.prev = &lru_;
178 128 }
179  
180 128 LRUCache::~LRUCache() {
181 130 ▶ for (LRUHandle* e = lru_.next; e != &lru_; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (LRUHandle* e = lru_.next; e != &lru_; ) { 2 128 130
182 2 LRUHandle* next = e->next;
183 2 ▶ assert(e->refs == 1); // Error if caller has an unreleased handle
Branch: 1 branch
BranchTrueFalseTotal
assert(e->refs == 1); // Error if caller has an unreleased handle 0 2 2
184 2 Unref(e);
185 2 e = next;
186 }
187 128 }
188  
189 5 void LRUCache::Unref(LRUHandle* e) {
190 5 ▶ assert(e->refs > 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(e->refs > 0); 0 5 5
191 5 e->refs--;
192 5 ▶ if (e->refs <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->refs <= 0) { 2 3 5
193 2 usage_ -= e->charge;
194 2 ▶ (*e->deleter)(e->key(), e->value);
Branch: 1 branch
BranchTrueFalseTotal
(*e->deleter)(e->key(), e->value); 2 0 2
195 2 free(e);
196 }
197 5 }
198  
199 1 void LRUCache::LRU_Remove(LRUHandle* e) {
200 1 e->next->prev = e->prev;
201 1 e->prev->next = e->next;
202 1 }
203  
204 3 void LRUCache::LRU_Append(LRUHandle* e) {
205 // Make "e" newest entry by inserting just before lru_
206 3 e->next = &lru_;
207 3 e->prev = lru_.prev;
208 3 e->prev->next = e;
209 3 e->next->prev = e;
210 3 }
211  
212 3 Cache::Handle* LRUCache::Lookup(const Slice& key, uint32_t hash) {
213 3 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 3 0 3
214 3 ▶ LRUHandle* e = table_.Lookup(key, hash);
Branch: 1 branch
BranchTrueFalseTotal
LRUHandle* e = table_.Lookup(key, hash); 3 0 3
215 3 ▶ if (e != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (e != NULL) { 1 2 3
216 1 e->refs++;
217 1 LRU_Remove(e);
218 1 LRU_Append(e);
219 }
220 3 return reinterpret_cast<Cache::Handle*>(e);
221 3 }
222  
223 3 void LRUCache::Release(Cache::Handle* handle) {
224 3 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 3 0 3
225 3 ▶ Unref(reinterpret_cast<LRUHandle*>(handle));
Branch: 1 branch
BranchTrueFalseTotal
Unref(reinterpret_cast<LRUHandle*>(handle)); 3 0 3
226 3 }
227  
228 2 Cache::Handle* LRUCache::Insert(
229 const Slice& key, uint32_t hash, void* value, size_t charge,
230 void (*deleter)(const Slice& key, void* value)) {
231 2 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 2 0 2
232  
233 LRUHandle* e = reinterpret_cast<LRUHandle*>(
234 2 malloc(sizeof(LRUHandle)-1 + key.size()));
235 2 e->value = value;
236 2 e->deleter = deleter;
237 2 e->charge = charge;
238 2 e->key_length = key.size();
239 2 e->hash = hash;
240 2 e->refs = 2; // One from LRUCache, one for the returned handle
241 2 memcpy(e->key_data, key.data(), key.size());
242 2 LRU_Append(e);
243 2 usage_ += charge;
244  
245 2 ▶ LRUHandle* old = table_.Insert(e);
Branch: 1 branch
BranchTrueFalseTotal
LRUHandle* old = table_.Insert(e); 2 0 2
246 2 ▶ if (old != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (old != NULL) { 0 2 2
247 0 LRU_Remove(old);
248 0 ▶ Unref(old);
Branch: 1 branch
BranchTrueFalseTotal
Unref(old); 0 0 0
249 }
250  
251 2 ▶ while (usage_ > capacity_ && lru_.next != &lru_) {
Branch: 2 branches
BranchTrueFalseTotal
while (usage_ > capacity_ && lru_.next != &lru_) { 0 2 2
while (usage_ > capacity_ && lru_.next != &lru_) { 0 0 0
252 0 LRUHandle* old = lru_.next;
253 0 LRU_Remove(old);
254 0 ▶ table_.Remove(old->key(), old->hash);
Branch: 1 branch
BranchTrueFalseTotal
table_.Remove(old->key(), old->hash); 0 0 0
255 0 ▶ Unref(old);
Branch: 1 branch
BranchTrueFalseTotal
Unref(old); 0 0 0
256 }
257  
258 2 return reinterpret_cast<Cache::Handle*>(e);
259 2 }
260  
261 0 void LRUCache::Erase(const Slice& key, uint32_t hash) {
262 0 ▶ MutexLock l(&mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&mutex_); 0 0 0
263 0 ▶ LRUHandle* e = table_.Remove(key, hash);
Branch: 1 branch
BranchTrueFalseTotal
LRUHandle* e = table_.Remove(key, hash); 0 0 0
264 0 ▶ if (e != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (e != NULL) { 0 0 0
265 0 LRU_Remove(e);
266 0 ▶ Unref(e);
Branch: 1 branch
BranchTrueFalseTotal
Unref(e); 0 0 0
267 }
268 0 }
269  
270 static const int kNumShardBits = 4;
271 static const int kNumShards = 1 << kNumShardBits;
272  
273 class ShardedLRUCache : public Cache {
274 private:
275 LRUCache shard_[kNumShards];
276 port::Mutex id_mutex_;
277 uint64_t last_id_;
278  
279 5 static inline uint32_t HashSlice(const Slice& s) {
280 5 return Hash(s.data(), s.size(), 0);
281 }
282  
283 8 static uint32_t Shard(uint32_t hash) {
284 8 return hash >> (32 - kNumShardBits);
285 }
286  
287 public:
288 136 ▶ explicit ShardedLRUCache(size_t capacity) {
Branch: 6 branches
BranchTrueFalseTotal
explicit ShardedLRUCache(size_t capacity) { 128 0 128
explicit ShardedLRUCache(size_t capacity) { 128 8 136
explicit ShardedLRUCache(size_t capacity) { 8 0 8
explicit ShardedLRUCache(size_t capacity) { 0 0 0
explicit ShardedLRUCache(size_t capacity) { 0 0 0
explicit ShardedLRUCache(size_t capacity) { 0 0 0
289 8 const size_t per_shard = (capacity + (kNumShards - 1)) / kNumShards;
290 136 ▶ for (int s = 0; s < kNumShards; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int s = 0; s < kNumShards; s++) { 128 8 136
291 128 shard_[s].SetCapacity(per_shard);
292 }
293 8 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 0 0 0
294 144 ▶ virtual ~ShardedLRUCache() { }
Branch: 2 branches
BranchTrueFalseTotal
virtual ~ShardedLRUCache() { } 8 0 8
virtual ~ShardedLRUCache() { } 128 8 136
295 2 virtual Handle* Insert(const Slice& key, void* value, size_t charge,
296 void (*deleter)(const Slice& key, void* value)) {
297 2 const uint32_t hash = HashSlice(key);
298 2 return shard_[Shard(hash)].Insert(key, hash, value, charge, deleter);
299 }
300 3 virtual Handle* Lookup(const Slice& key) {
301 3 const uint32_t hash = HashSlice(key);
302 3 return shard_[Shard(hash)].Lookup(key, hash);
303 }
304 3 virtual void Release(Handle* handle) {
305 3 LRUHandle* h = reinterpret_cast<LRUHandle*>(handle);
306 3 shard_[Shard(h->hash)].Release(handle);
307 3 }
308 0 virtual void Erase(const Slice& key) {
309 0 const uint32_t hash = HashSlice(key);
310 0 shard_[Shard(hash)].Erase(key, hash);
311 0 }
312 2 virtual void* Value(Handle* handle) {
313 2 return reinterpret_cast<LRUHandle*>(handle)->value;
314 }
315 1 virtual uint64_t NewId() {
316 1 ▶ MutexLock l(&id_mutex_);
Branch: 1 branch
BranchTrueFalseTotal
MutexLock l(&id_mutex_); 1 0 1
317 1 return ++(last_id_);
318 1 }
319 };
320  
321 } // end anonymous namespace
322  
323 8 Cache* NewLRUCache(size_t capacity) {
324 8 ▶ return new ShardedLRUCache(capacity);
Branch: 2 branches
BranchTrueFalseTotal
return new ShardedLRUCache(capacity); 8 0 8
return new ShardedLRUCache(capacity); 0 0 0
325 }
326  
327 }
« util/hash.cc ↑ Summary port/port_posix.cc »