Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder and align LRUCacheShard data members to reduce false sharing #2568

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cache/lru_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace rocksdb {

LRUHandleTable::LRUHandleTable() : length_(0), elems_(0), list_(nullptr) {
LRUHandleTable::LRUHandleTable() : list_(nullptr), length_(0), elems_(0) {
Resize();
}

Expand Down Expand Up @@ -102,7 +102,7 @@ void LRUHandleTable::Resize() {
}

LRUCacheShard::LRUCacheShard()
: usage_(0), lru_usage_(0), high_pri_pool_usage_(0) {
: high_pri_pool_usage_(0), usage_(0), lru_usage_(0) {
// Make empty circular linked list
lru_.next = &lru_;
lru_.prev = &lru_;
Expand Down
37 changes: 24 additions & 13 deletions cache/lru_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ class LRUHandleTable {

// The table consists of an array of buckets where each bucket is
// a linked list of cache entries that hash into the bucket.
LRUHandle** list_;
uint32_t length_;
uint32_t elems_;
LRUHandle** list_;
};

// A single shard of sharded cache.
class LRUCacheShard : public CacheShard {
class ALIGN_AS(CACHE_LINE_SIZE) LRUCacheShard : public CacheShard {
public:
LRUCacheShard();
virtual ~LRUCacheShard();
Expand Down Expand Up @@ -221,12 +221,6 @@ class LRUCacheShard : public CacheShard {
// Initialized before use.
size_t capacity_;

// Memory size for entries residing in the cache
size_t usage_;

// Memory size for entries residing only in the LRU list
size_t lru_usage_;

// Memory size for entries in high-pri pool.
size_t high_pri_pool_usage_;

Expand All @@ -240,11 +234,6 @@ class LRUCacheShard : public CacheShard {
// Remember the value to avoid recomputing each time.
double high_pri_pool_capacity_;

// mutex_ protects the following state.
// We don't count mutex_ as the cache's internal state so semantically we
// don't mind mutex_ invoking the non-const actions.
mutable port::Mutex mutex_;

// Dummy head of LRU list.
// lru.prev is newest entry, lru.next is oldest entry.
// LRU contains items which can be evicted, ie reference only by cache
Expand All @@ -253,7 +242,29 @@ class LRUCacheShard : public CacheShard {
// Pointer to head of low-pri pool in LRU list.
LRUHandle* lru_low_pri_;

// ------------^^^^^^^^^^^^^-----------
// Not frequently modified data members
// ------------------------------------
//
// We separate data members that are updated frequently from the ones that
// are not frequently updated so that they don't share the same cache line
// which will lead into false cache sharing
//
// ------------------------------------
// Frequently modified data members
// ------------vvvvvvvvvvvvv-----------
LRUHandleTable table_;

// Memory size for entries residing in the cache
size_t usage_;

// Memory size for entries residing only in the LRU list
size_t lru_usage_;

// mutex_ protects the following state.
// We don't count mutex_ as the cache's internal state so semantically we
// don't mind mutex_ invoking the non-const actions.
mutable port::Mutex mutex_;
};

class LRUCache : public ShardedCache {
Expand Down
2 changes: 2 additions & 0 deletions port/port_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ extern void InitOnce(OnceType* once, void (*initializer)());
#define CACHE_LINE_SIZE 64U
#endif

#define ALIGN_AS(n) alignas(n)

#define PREFETCH(addr, rw, locality) __builtin_prefetch(addr, rw, locality)

extern void Crash(const std::string& srcfile, int srcline);
Expand Down
2 changes: 2 additions & 0 deletions port/win/port_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ extern void InitOnce(OnceType* once, void (*initializer)());
#define CACHE_LINE_SIZE 64U
#endif

#define ALIGN_AS(n)

static inline void AsmVolatilePause() {
#if defined(_M_IX86) || defined(_M_X64)
YieldProcessor();
Expand Down