@ -24,7 +24,7 @@ LRUHandleTable::LRUHandleTable() : list_(nullptr), length_(0), elems_(0) {
LRUHandleTable : : ~ LRUHandleTable ( ) {
LRUHandleTable : : ~ LRUHandleTable ( ) {
ApplyToAllCacheEntries ( [ ] ( LRUHandle * h ) {
ApplyToAllCacheEntries ( [ ] ( LRUHandle * h ) {
if ( h - > refs = = 1 ) {
if ( ! h - > HasRefs ( ) ) {
h - > Free ( ) ;
h - > Free ( ) ;
}
}
} ) ;
} ) ;
@ -113,29 +113,17 @@ LRUCacheShard::LRUCacheShard(size_t capacity, bool strict_capacity_limit,
SetCapacity ( capacity ) ;
SetCapacity ( capacity ) ;
}
}
LRUCacheShard : : ~ LRUCacheShard ( ) { }
bool LRUCacheShard : : Unref ( LRUHandle * e ) {
assert ( e - > refs > 0 ) ;
e - > refs - - ;
return e - > refs = = 0 ;
}
// Call deleter and free
void LRUCacheShard : : EraseUnRefEntries ( ) {
void LRUCacheShard : : EraseUnRefEntries ( ) {
autovector < LRUHandle * > last_reference_list ;
autovector < LRUHandle * > last_reference_list ;
{
{
MutexLock l ( & mutex_ ) ;
MutexLock l ( & mutex_ ) ;
while ( lru_ . next ! = & lru_ ) {
while ( lru_ . next ! = & lru_ ) {
LRUHandle * old = lru_ . next ;
LRUHandle * old = lru_ . next ;
assert ( old - > InCache ( ) ) ;
// LRU list contains only elements which can be evicted
assert ( old - > refs = =
assert ( old - > InCache ( ) & & ! old - > HasRefs ( ) ) ;
1 ) ; // LRU list contains elements which may be evicted
LRU_Remove ( old ) ;
LRU_Remove ( old ) ;
table_ . Remove ( old - > key ( ) , old - > hash ) ;
table_ . Remove ( old - > key ( ) , old - > hash ) ;
old - > SetInCache ( false ) ;
old - > SetInCache ( false ) ;
Unref ( old ) ;
usage_ - = old - > charge ;
usage_ - = old - > charge ;
last_reference_list . push_back ( old ) ;
last_reference_list . push_back ( old ) ;
}
}
@ -148,22 +136,27 @@ void LRUCacheShard::EraseUnRefEntries() {
void LRUCacheShard : : ApplyToAllCacheEntries ( void ( * callback ) ( void * , size_t ) ,
void LRUCacheShard : : ApplyToAllCacheEntries ( void ( * callback ) ( void * , size_t ) ,
bool thread_safe ) {
bool thread_safe ) {
if ( thread_safe ) {
const auto applyCallback = [ & ] ( ) {
mutex_ . Lock ( ) ;
}
table_ . ApplyToAllCacheEntries (
table_ . ApplyToAllCacheEntries (
[ callback ] ( LRUHandle * h ) { callback ( h - > value , h - > charge ) ; } ) ;
[ callback ] ( LRUHandle * h ) { callback ( h - > value , h - > charge ) ; } ) ;
} ;
if ( thread_safe ) {
if ( thread_safe ) {
mutex_ . Unlock ( ) ;
MutexLock l ( & mutex_ ) ;
applyCallback ( ) ;
} else {
applyCallback ( ) ;
}
}
}
}
void LRUCacheShard : : TEST_GetLRUList ( LRUHandle * * lru , LRUHandle * * lru_low_pri ) {
void LRUCacheShard : : TEST_GetLRUList ( LRUHandle * * lru , LRUHandle * * lru_low_pri ) {
MutexLock l ( & mutex_ ) ;
* lru = & lru_ ;
* lru = & lru_ ;
* lru_low_pri = lru_low_pri_ ;
* lru_low_pri = lru_low_pri_ ;
}
}
size_t LRUCacheShard : : TEST_GetLRUSize ( ) {
size_t LRUCacheShard : : TEST_GetLRUSize ( ) {
MutexLock l ( & mutex_ ) ;
LRUHandle * lru_handle = lru_ . next ;
LRUHandle * lru_handle = lru_ . next ;
size_t lru_size = 0 ;
size_t lru_size = 0 ;
while ( lru_handle ! = & lru_ ) {
while ( lru_handle ! = & lru_ ) {
@ -231,14 +224,13 @@ void LRUCacheShard::MaintainPoolSize() {
void LRUCacheShard : : EvictFromLRU ( size_t charge ,
void LRUCacheShard : : EvictFromLRU ( size_t charge ,
autovector < LRUHandle * > * deleted ) {
autovector < LRUHandle * > * deleted ) {
while ( usage_ + charge > capacity_ & & lru_ . next ! = & lru_ ) {
while ( ( usage_ + charge ) > capacity_ & & lru_ . next ! = & lru_ ) {
LRUHandle * old = lru_ . next ;
LRUHandle * old = lru_ . next ;
assert ( old - > InCache ( ) ) ;
// LRU list contains only elements which can be evicted
assert ( old - > refs = = 1 ) ; // LRU list contains elements which may be evicted
assert ( old - > InCache ( ) & & ! old - > HasRefs ( ) ) ;
LRU_Remove ( old ) ;
LRU_Remove ( old ) ;
table_ . Remove ( old - > key ( ) , old - > hash ) ;
table_ . Remove ( old - > key ( ) , old - > hash ) ;
old - > SetInCache ( false ) ;
old - > SetInCache ( false ) ;
Unref ( old ) ;
usage_ - = old - > charge ;
usage_ - = old - > charge ;
deleted - > push_back ( old ) ;
deleted - > push_back ( old ) ;
}
}
@ -252,8 +244,8 @@ void LRUCacheShard::SetCapacity(size_t capacity) {
high_pri_pool_capacity_ = capacity_ * high_pri_pool_ratio_ ;
high_pri_pool_capacity_ = capacity_ * high_pri_pool_ratio_ ;
EvictFromLRU ( 0 , & last_reference_list ) ;
EvictFromLRU ( 0 , & last_reference_list ) ;
}
}
// we free the entries here outside of mutex for
// performance reasons
// Free the entries outside of mutex for performance reasons
for ( auto entry : last_reference_list ) {
for ( auto entry : last_reference_list ) {
entry - > Free ( ) ;
entry - > Free ( ) ;
}
}
@ -269,22 +261,22 @@ Cache::Handle* LRUCacheShard::Lookup(const Slice& key, uint32_t hash) {
LRUHandle * e = table_ . Lookup ( key , hash ) ;
LRUHandle * e = table_ . Lookup ( key , hash ) ;
if ( e ! = nullptr ) {
if ( e ! = nullptr ) {
assert ( e - > InCache ( ) ) ;
assert ( e - > InCache ( ) ) ;
if ( e - > refs = = 1 ) {
if ( ! e - > HasRefs ( ) ) {
// The entry is in LRU since it's in hash and has no external references
LRU_Remove ( e ) ;
LRU_Remove ( e ) ;
}
}
e - > refs + + ;
e - > Ref ( ) ;
e - > SetHit ( ) ;
e - > SetHit ( ) ;
}
}
return reinterpret_cast < Cache : : Handle * > ( e ) ;
return reinterpret_cast < Cache : : Handle * > ( e ) ;
}
}
bool LRUCacheShard : : Ref ( Cache : : Handle * h ) {
bool LRUCacheShard : : Ref ( Cache : : Handle * h ) {
LRUHandle * handl e = reinterpret_cast < LRUHandle * > ( h ) ;
LRUHandle * e = reinterpret_cast < LRUHandle * > ( h ) ;
MutexLock l ( & mutex_ ) ;
MutexLock l ( & mutex_ ) ;
if ( handle - > InCache ( ) & & handle - > refs = = 1 ) {
// To create another reference - entry must be already externally referenced
LRU_Remove ( handle ) ;
assert ( e - > HasRefs ( ) ) ;
}
e - > Ref ( ) ;
handle - > refs + + ;
return true ;
return true ;
}
}
@ -303,30 +295,27 @@ bool LRUCacheShard::Release(Cache::Handle* handle, bool force_erase) {
bool last_reference = false ;
bool last_reference = false ;
{
{
MutexLock l ( & mutex_ ) ;
MutexLock l ( & mutex_ ) ;
last_reference = Unref ( e ) ;
last_reference = e - > Unref ( ) ;
if ( last_reference ) {
if ( last_reference & & e - > InCache ( ) ) {
usage_ - = e - > charge ;
}
if ( e - > refs = = 1 & & e - > InCache ( ) ) {
// The item is still in cache, and nobody else holds a reference to it
// The item is still in cache, and nobody else holds a reference to it
if ( usage_ > capacity_ | | force_erase ) {
if ( usage_ > capacity_ | | force_erase ) {
// the cache is full
// The LRU list must be empty since the cache is full
// The LRU list must be empty since the cache is full
assert ( ! ( usage_ > capacity_ ) | | lru_ . next = = & lru_ ) ;
assert ( lru_ . next = = & lru_ | | force_erase ) ;
// t ake this opportunity and remove the item
// T ake this opportunity and remove the item
table_ . Remove ( e - > key ( ) , e - > hash ) ;
table_ . Remove ( e - > key ( ) , e - > hash ) ;
e - > SetInCache ( false ) ;
e - > SetInCache ( false ) ;
Unref ( e ) ;
usage_ - = e - > charge ;
last_reference = true ;
} else {
} else {
// put the item on the list to be potentially freed
// Put the item back on the LRU list, and don't free it
LRU_Insert ( e ) ;
LRU_Insert ( e ) ;
last_reference = false ;
}
}
}
if ( last_reference ) {
usage_ - = e - > charge ;
}
}
}
}
// free outside of mutex
// Free the entry here outside of mutex for performance reasons
if ( last_reference ) {
if ( last_reference ) {
e - > Free ( ) ;
e - > Free ( ) ;
}
}
@ -342,7 +331,7 @@ Status LRUCacheShard::Insert(const Slice& key, uint32_t hash, void* value,
// It shouldn't happen very often though.
// It shouldn't happen very often though.
LRUHandle * e = reinterpret_cast < LRUHandle * > (
LRUHandle * e = reinterpret_cast < LRUHandle * > (
new char [ sizeof ( LRUHandle ) - 1 + key . size ( ) ] ) ;
new char [ sizeof ( LRUHandle ) - 1 + key . size ( ) ] ) ;
Status s ;
Status s = Status : : OK ( ) ;
autovector < LRUHandle * > last_reference_list ;
autovector < LRUHandle * > last_reference_list ;
e - > value = value ;
e - > value = value ;
@ -351,9 +340,7 @@ Status LRUCacheShard::Insert(const Slice& key, uint32_t hash, void* value,
e - > key_length = key . size ( ) ;
e - > key_length = key . size ( ) ;
e - > flags = 0 ;
e - > flags = 0 ;
e - > hash = hash ;
e - > hash = hash ;
e - > refs = ( handle = = nullptr
e - > refs = 0 ;
? 1
: 2 ) ; // One from LRUCache, one for the returned handle
e - > next = e - > prev = nullptr ;
e - > next = e - > prev = nullptr ;
e - > SetInCache ( true ) ;
e - > SetInCache ( true ) ;
e - > SetPriority ( priority ) ;
e - > SetPriority ( priority ) ;
@ -366,11 +353,12 @@ Status LRUCacheShard::Insert(const Slice& key, uint32_t hash, void* value,
// is freed or the lru list is empty
// is freed or the lru list is empty
EvictFromLRU ( charge , & last_reference_list ) ;
EvictFromLRU ( charge , & last_reference_list ) ;
if ( usage_ - lru_usage_ + charge > capacity_ & &
if ( ( usage_ + charge ) > capacity_ & &
( strict_capacity_limit_ | | handle = = nullptr ) ) {
( strict_capacity_limit_ | | handle = = nullptr ) ) {
if ( handle = = nullptr ) {
if ( handle = = nullptr ) {
// Don't insert the entry but still return ok, as if the entry inserted
// Don't insert the entry but still return ok, as if the entry inserted
// into cache and get evicted immediately.
// into cache and get evicted immediately.
e - > SetInCache ( false ) ;
last_reference_list . push_back ( e ) ;
last_reference_list . push_back ( e ) ;
} else {
} else {
delete [ ] reinterpret_cast < char * > ( e ) ;
delete [ ] reinterpret_cast < char * > ( e ) ;
@ -378,32 +366,30 @@ Status LRUCacheShard::Insert(const Slice& key, uint32_t hash, void* value,
s = Status : : Incomplete ( " Insert failed due to LRU cache being full. " ) ;
s = Status : : Incomplete ( " Insert failed due to LRU cache being full. " ) ;
}
}
} else {
} else {
// insert into the cache
// Insert into the cache. Note that the cache might get larger than its
// note that the cache might get larger than its capacity if not enough
// capacity if not enough space was freed up.
// space was freed
LRUHandle * old = table_ . Insert ( e ) ;
LRUHandle * old = table_ . Insert ( e ) ;
usage_ + = e - > charge ;
usage_ + = e - > charge ;
if ( old ! = nullptr ) {
if ( old ! = nullptr ) {
assert ( old - > InCache ( ) ) ;
old - > SetInCache ( false ) ;
old - > SetInCache ( false ) ;
if ( Unref ( old ) ) {
if ( ! old - > HasRefs ( ) ) {
usage_ - = old - > charge ;
// old is on LRU because it's in cache and its reference count is 0
// old is on LRU because it's in cache and its reference count
// was just 1 (Unref returned 0)
LRU_Remove ( old ) ;
LRU_Remove ( old ) ;
usage_ - = old - > charge ;
last_reference_list . push_back ( old ) ;
last_reference_list . push_back ( old ) ;
}
}
}
}
if ( handle = = nullptr ) {
if ( handle = = nullptr ) {
LRU_Insert ( e ) ;
LRU_Insert ( e ) ;
} else {
} else {
e - > Ref ( ) ;
* handle = reinterpret_cast < Cache : : Handle * > ( e ) ;
* handle = reinterpret_cast < Cache : : Handle * > ( e ) ;
}
}
s = Status : : OK ( ) ;
}
}
}
}
// we free the entries here outside of mutex for
// Free the entries here outside of mutex for performance reasons
// performance reasons
for ( auto entry : last_reference_list ) {
for ( auto entry : last_reference_list ) {
entry - > Free ( ) ;
entry - > Free ( ) ;
}
}
@ -418,18 +404,18 @@ void LRUCacheShard::Erase(const Slice& key, uint32_t hash) {
MutexLock l ( & mutex_ ) ;
MutexLock l ( & mutex_ ) ;
e = table_ . Remove ( key , hash ) ;
e = table_ . Remove ( key , hash ) ;
if ( e ! = nullptr ) {
if ( e ! = nullptr ) {
last_reference = Unref ( e ) ;
assert ( e - > InCache ( ) ) ;
if ( last_reference ) {
e - > SetInCache ( false ) ;
usage_ - = e - > charge ;
if ( ! e - > HasRefs ( ) ) {
}
// The entry is in LRU since it's in hash and has no external references
if ( last_reference & & e - > InCache ( ) ) {
LRU_Remove ( e ) ;
LRU_Remove ( e ) ;
usage_ - = e - > charge ;
last_reference = true ;
}
}
e - > SetInCache ( false ) ;
}
}
}
}
// mutex not held here
// Free the entry here outside of mutex for performance reasons
// last_reference will only be true if e != nullptr
// last_reference will only be true if e != nullptr
if ( last_reference ) {
if ( last_reference ) {
e - > Free ( ) ;
e - > Free ( ) ;