Skip to content

Commit 3499c83

Browse files
committed
added ability for compressed pointer to use full 32 bits for addressing in single tier mode and use 31 bits for addressing in multi-tier mode
1 parent 519f664 commit 3499c83

File tree

7 files changed

+115
-40
lines changed

7 files changed

+115
-40
lines changed

cachelib/allocator/CCacheAllocator.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ CCacheAllocator::CCacheAllocator(MemoryAllocator& allocator,
3636
currentChunksIndex_(0) {
3737
auto& currentChunks = chunks_[currentChunksIndex_];
3838
for (auto chunk : *object.chunks()) {
39-
currentChunks.push_back(allocator_.unCompress(CompressedPtr(chunk)));
39+
// TODO : pass multi-tier flag when compact cache supports multi-tier config
40+
currentChunks.push_back(allocator_.unCompress(CompressedPtr(chunk), false));
4041
}
4142
}
4243

@@ -97,7 +98,8 @@ CCacheAllocator::SerializationType CCacheAllocator::saveState() {
9798

9899
std::lock_guard<std::mutex> guard(resizeLock_);
99100
for (auto chunk : getCurrentChunks()) {
100-
object.chunks()->push_back(allocator_.compress(chunk).saveState());
101+
// TODO : pass multi-tier flag when compact cache supports multi-tier config
102+
object.chunks()->push_back(allocator_.compress(chunk, false).saveState());
101103
}
102104
return object;
103105
}

cachelib/allocator/memory/CompressedPtr.h

Lines changed: 94 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,22 @@ namespace cachelib {
2727

2828
class SlabAllocator;
2929

30-
// the following are for pointer compression for the memory allocator. We
31-
// compress pointers by storing the slab index and the alloc index of the
32-
// allocation inside the slab. With slab worth kNumSlabBits of data, if we
33-
// have the min allocation size as 64 bytes, that requires kNumSlabBits - 6
34-
// bits for storing the alloc index. This leaves the remaining (32 -
35-
// (kNumSlabBits - 6)) bits for the slab index. Hence we can index 256 GiB
36-
// of memory in slabs and index anything more than 64 byte allocations inside
37-
// the slab using a 32 bit representation.
38-
//
30+
template <typename PtrType, typename AllocatorContainer>
31+
class PtrCompressor;
32+
3933
// This CompressedPtr makes decompression fast by staying away from division and
40-
// modulo arithmetic and doing those during the compression time. We most often
41-
// decompress a CompressedPtr than compress a pointer while creating one.
34+
// modulo arithmetic and doing those during the compression time. We most often
35+
// decompress a CompressedPtr than compress a pointer while creating one. This
36+
// is used for pointer compression by the memory allocator.
37+
38+
// We compress pointers by storing the tier index, slab index and alloc index of
39+
// the allocation inside the slab. With slab worth kNumSlabBits (22 bits) of data,
40+
// if we have the min allocation size as 64 bytes, that requires kNumSlabBits - 6
41+
// = 16 bits for storing the alloc index. The tier id occupies the 32nd bit only
42+
// since its value cannot exceed kMaxTiers (2). This leaves the remaining
43+
// (32 - (kNumSlabBits - 6) - 1 bit for tier id) = 15 bits for the slab index.
44+
// Hence we can index 128 GiB of memory in slabs per tier and index anything more
45+
// than 64 byte allocations inside the slab using a 32 bit representation.
4246
class CACHELIB_PACKED_ATTR CompressedPtr {
4347
public:
4448
using PtrType = uint32_t;
@@ -62,9 +66,9 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
6266
return static_cast<uint32_t>(1) << (Slab::kMinAllocPower);
6367
}
6468

65-
// maximum adressable memory for pointer compression to work.
69+
// maximum addressable memory for pointer compression to work.
6670
static constexpr size_t getMaxAddressableSize() noexcept {
67-
return static_cast<size_t>(1) << (kNumSlabIdxBits + Slab::kNumSlabBits);
71+
return static_cast<size_t>(1) << (kNumSlabIdxBits + Slab::kNumSlabBits + 1);
6872
}
6973

7074
// default construct to nullptr.
@@ -89,8 +93,8 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
8993
PtrType ptr_{kNull};
9094

9195
// create a compressed pointer for a valid memory allocation.
92-
CompressedPtr(uint32_t slabIdx, uint32_t allocIdx)
93-
: ptr_(compress(slabIdx, allocIdx)) {}
96+
CompressedPtr(uint32_t slabIdx, uint32_t allocIdx, bool isMultiTiered, TierId tid = 0)
97+
: ptr_(compress(slabIdx, allocIdx, isMultiTiered, tid)) {}
9498

9599
constexpr explicit CompressedPtr(PtrType ptr) noexcept : ptr_{ptr} {}
96100

@@ -100,31 +104,52 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
100104
static constexpr unsigned int kNumAllocIdxBits =
101105
Slab::kNumSlabBits - Slab::kMinAllocPower;
102106

107+
// Use 32nd bit position for TierId
108+
static constexpr unsigned int kNumTierIdxOffset = 31;
109+
103110
static constexpr PtrType kAllocIdxMask = ((PtrType)1 << kNumAllocIdxBits) - 1;
104111

112+
// kNumTierIdxBits most significant bits
113+
static constexpr PtrType kTierIdxMask = (PtrType)1 << kNumTierIdxOffset;
114+
105115
// Number of bits for the slab index. This will be the top 16 bits of the
106116
// compressed ptr.
107117
static constexpr unsigned int kNumSlabIdxBits =
108-
NumBits<PtrType>::value - kNumAllocIdxBits;
118+
kNumTierIdxOffset - kNumAllocIdxBits;
109119

110120
// Compress the given slabIdx and allocIdx into a 32-bit compressed
111121
// pointer.
112-
static PtrType compress(uint32_t slabIdx, uint32_t allocIdx) noexcept {
122+
static PtrType compress(uint32_t slabIdx, uint32_t allocIdx, bool isMultiTiered, TierId tid) noexcept {
113123
XDCHECK_LE(allocIdx, kAllocIdxMask);
124+
if (!isMultiTiered) {
125+
XDCHECK_LT(slabIdx, (1u << (kNumSlabIdxBits+1)) - 1);
126+
return (slabIdx << kNumAllocIdxBits) + allocIdx;
127+
}
114128
XDCHECK_LT(slabIdx, (1u << kNumSlabIdxBits) - 1);
115129
return (slabIdx << kNumAllocIdxBits) + allocIdx;
116130
}
117131

118132
// Get the slab index of the compressed ptr
119-
uint32_t getSlabIdx() const noexcept {
133+
uint32_t getSlabIdx(bool isMultiTiered) const noexcept {
120134
XDCHECK(!isNull());
121-
return static_cast<uint32_t>(ptr_ >> kNumAllocIdxBits);
135+
auto noTierIdPtr = isMultiTiered ? ptr_ & ~kTierIdxMask : ptr_;
136+
return static_cast<uint32_t>(noTierIdPtr >> kNumAllocIdxBits);
122137
}
123138

124139
// Get the allocation index of the compressed ptr
125-
uint32_t getAllocIdx() const noexcept {
140+
uint32_t getAllocIdx(bool isMultiTiered) const noexcept {
126141
XDCHECK(!isNull());
127-
return static_cast<uint32_t>(ptr_ & kAllocIdxMask);
142+
auto noTierIdPtr = isMultiTiered ? ptr_ & ~kTierIdxMask : ptr_;
143+
return static_cast<uint32_t>(noTierIdPtr & kAllocIdxMask);
144+
}
145+
146+
uint32_t getTierId(bool isMultiTiered) const noexcept {
147+
XDCHECK(!isNull());
148+
return isMultiTiered ? static_cast<uint32_t>(ptr_ >> kNumTierIdxOffset) : 0;
149+
}
150+
151+
void setTierId(TierId tid) noexcept {
152+
ptr_ += static_cast<uint64_t>(tid) << kNumTierIdxOffset;
128153
}
129154

130155
friend SlabAllocator;
@@ -137,11 +162,57 @@ class PtrCompressor {
137162
: allocator_(allocator) {}
138163

139164
const CompressedPtr compress(const PtrType* uncompressed) const {
140-
return allocator_.compress(uncompressed);
165+
return allocator_.compress(uncompressed, false);
166+
}
167+
168+
PtrType* unCompress(const CompressedPtr compressed) const {
169+
return static_cast<PtrType*>(allocator_.unCompress(compressed, false));
170+
}
171+
172+
bool operator==(const SingleTierPtrCompressor& rhs) const noexcept {
173+
return &allocator_ == &rhs.allocator_;
174+
}
175+
176+
bool operator!=(const SingleTierPtrCompressor& rhs) const noexcept {
177+
return !(*this == rhs);
178+
}
179+
180+
private:
181+
// memory allocator that does the pointer compression.
182+
const AllocatorT& allocator_;
183+
};
184+
185+
template <typename PtrType, typename AllocatorContainer>
186+
class PtrCompressor {
187+
public:
188+
explicit PtrCompressor(const AllocatorContainer& allocators) noexcept
189+
: allocators_(allocators) {}
190+
191+
const CompressedPtr compress(const PtrType* uncompressed) const {
192+
if (uncompressed == nullptr)
193+
return CompressedPtr{};
194+
195+
TierId tid;
196+
for (tid = 0; tid < allocators_.size(); tid++) {
197+
if (allocators_[tid]->isMemoryInAllocator(static_cast<const void*>(uncompressed)))
198+
break;
199+
}
200+
201+
bool isMultiTiered = allocators_.size() > 1;
202+
auto cptr = allocators_[tid]->compress(uncompressed, isMultiTiered);
203+
if (isMultiTiered) { // config has multiple tiers
204+
cptr.setTierId(tid);
205+
}
206+
return cptr;
141207
}
142208

143209
PtrType* unCompress(const CompressedPtr compressed) const {
144-
return static_cast<PtrType*>(allocator_.unCompress(compressed));
210+
if (compressed.isNull()) {
211+
return nullptr;
212+
}
213+
bool isMultiTiered = allocators_.size() > 1;
214+
auto &allocator = *allocators_[compressed.getTierId(isMultiTiered)];
215+
return static_cast<PtrType*>(allocator.unCompress(compressed, isMultiTiered));
145216
}
146217

147218
bool operator==(const PtrCompressor& rhs) const noexcept {

cachelib/allocator/memory/MemoryAllocator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ class MemoryAllocator {
534534
// as the original pointer is valid.
535535
//
536536
// @throw std::invalid_argument if the ptr is invalid.
537-
CompressedPtr CACHELIB_INLINE compress(const void* ptr) const {
538-
return slabAllocator_.compress(ptr);
537+
CompressedPtr CACHELIB_INLINE compress(const void* ptr, bool isMultiTiered) const {
538+
return slabAllocator_.compress(ptr, isMultiTiered);
539539
}
540540

541541
// retrieve the raw pointer corresponding to the compressed pointer. This is
@@ -546,8 +546,8 @@ class MemoryAllocator {
546546
// @return the raw pointer corresponding to this compressed pointer.
547547
//
548548
// @throw std::invalid_argument if the compressed pointer is invalid.
549-
void* CACHELIB_INLINE unCompress(const CompressedPtr cPtr) const {
550-
return slabAllocator_.unCompress(cPtr);
549+
void* CACHELIB_INLINE unCompress(const CompressedPtr cPtr, bool isMultiTiered) const {
550+
return slabAllocator_.unCompress(cPtr, isMultiTiered);
551551
}
552552

553553
// a special implementation of pointer compression for benchmarking purposes.

cachelib/allocator/memory/Slab.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ namespace cachelib {
5050
* independantly by the SlabAllocator.
5151
*/
5252

53+
// identifier for the memory tier
54+
using TierId = int8_t;
5355
// identifier for the memory pool
5456
using PoolId = int8_t;
5557
// identifier for the allocation class

cachelib/allocator/memory/SlabAllocator.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class SlabAllocator {
225225
// the corresponding memory allocator. trying to inline this just increases
226226
// the code size and does not move the needle on the benchmarks much.
227227
// Calling this with invalid input in optimized build is undefined behavior.
228-
CompressedPtr CACHELIB_INLINE compress(const void* ptr) const {
228+
CompressedPtr CACHELIB_INLINE compress(const void* ptr, bool isMultiTiered) const {
229229
if (ptr == nullptr) {
230230
return CompressedPtr{};
231231
}
@@ -246,19 +246,19 @@ class SlabAllocator {
246246
static_cast<uint32_t>(reinterpret_cast<const uint8_t*>(ptr) -
247247
reinterpret_cast<const uint8_t*>(slab)) /
248248
allocSize;
249-
return CompressedPtr{slabIndex, allocIdx};
249+
return CompressedPtr{slabIndex, allocIdx, isMultiTiered};
250250
}
251251

252252
// uncompress the point and return the raw ptr. This function never throws
253253
// in optimized build and assumes that the caller is responsible for calling
254254
// it with a valid compressed pointer.
255-
void* CACHELIB_INLINE unCompress(const CompressedPtr ptr) const {
255+
void* CACHELIB_INLINE unCompress(const CompressedPtr ptr, bool isMultiTiered) const {
256256
if (ptr.isNull()) {
257257
return nullptr;
258258
}
259259

260-
const SlabIdx slabIndex = ptr.getSlabIdx();
261-
const uint32_t allocIdx = ptr.getAllocIdx();
260+
const SlabIdx slabIndex = ptr.getSlabIdx(isMultiTiered);
261+
const uint32_t allocIdx = ptr.getAllocIdx(isMultiTiered);
262262
const Slab* slab = &slabMemoryStart_[slabIndex];
263263

264264
#ifndef NDEBUG

cachelib/allocator/memory/tests/MemoryAllocatorTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,13 @@ TEST_F(MemoryAllocatorTest, PointerCompression) {
401401
for (const auto& pool : poolAllocs) {
402402
const auto& allocs = pool.second;
403403
for (const auto* alloc : allocs) {
404-
CompressedPtr ptr = m.compress(alloc);
404+
CompressedPtr ptr = m.compress(alloc, false);
405405
ASSERT_FALSE(ptr.isNull());
406-
ASSERT_EQ(alloc, m.unCompress(ptr));
406+
ASSERT_EQ(alloc, m.unCompress(ptr, false));
407407
}
408408
}
409409

410-
ASSERT_EQ(nullptr, m.unCompress(m.compress(nullptr)));
410+
ASSERT_EQ(nullptr, m.unCompress(m.compress(nullptr, false), false));
411411
}
412412

413413
TEST_F(MemoryAllocatorTest, Restorable) {

cachelib/benchmarks/PtrCompressionBench.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void buildAllocs(size_t poolSize) {
6161
void* alloc = ma->allocate(pid, size);
6262
XDCHECK_GE(size, CompressedPtr::getMinAllocSize());
6363
if (alloc != nullptr) {
64-
validAllocs.push_back({alloc, ma->compress(alloc)});
64+
validAllocs.push_back({alloc, ma->compress(alloc, false)});
6565
validAllocsAlt.push_back({alloc, ma->compressAlt(alloc)});
6666
numAllocations++;
6767
}
@@ -83,7 +83,7 @@ BENCHMARK(CompressionAlt) {
8383

8484
BENCHMARK_RELATIVE(Compression) {
8585
for (const auto& alloc : validAllocs) {
86-
CompressedPtr c = m->compress(alloc.first);
86+
CompressedPtr c = m->compress(alloc.first, false);
8787
folly::doNotOptimizeAway(c);
8888
}
8989
}
@@ -97,7 +97,7 @@ BENCHMARK(DeCompressAlt) {
9797

9898
BENCHMARK_RELATIVE(DeCompress) {
9999
for (const auto& alloc : validAllocs) {
100-
void* ptr = m->unCompress(alloc.second);
100+
void* ptr = m->unCompress(alloc.second, false);
101101
folly::doNotOptimizeAway(ptr);
102102
}
103103
}

0 commit comments

Comments
 (0)