Skip to content

Commit 6d521d6

Browse files
guptaskvinser52
authored andcommitted
This is the additional multi-tier support needed
for the compressed ptr changes that were introduced upstream. - Includes later cosmetic changes added by sounak 9cb5c29
1 parent 67b0d0b commit 6d521d6

File tree

7 files changed

+77
-20
lines changed

7 files changed

+77
-20
lines changed

cachelib/allocator/CacheAllocator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,7 @@ class CacheAllocator : public CacheBase {
13341334
sizeof(typename RefcountWithFlags::Value) + sizeof(uint32_t) +
13351335
sizeof(uint32_t) + sizeof(KAllocation)) == sizeof(Item),
13361336
"vtable overhead");
1337+
// Check for CompressedPtr single/multi tier support
13371338
static_assert(32 == sizeof(Item), "item overhead is 32 bytes");
13381339

13391340
// make sure there is no overhead in ChainedItem on top of a regular Item
@@ -1989,7 +1990,7 @@ class CacheAllocator : public CacheBase {
19891990
}
19901991

19911992
typename Item::PtrCompressor createPtrCompressor() const {
1992-
return allocator_[0 /* TODO */]->createPtrCompressor<Item>();
1993+
return typename Item::PtrCompressor(allocator_);
19931994
}
19941995

19951996
// helper utility to throttle and optionally log.

cachelib/allocator/memory/AllocationClass.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ AllocationClass::AllocationClass(ClassId classId,
5050
poolId_(poolId),
5151
allocationSize_(allocSize),
5252
slabAlloc_(s),
53-
freedAllocations_{slabAlloc_.createPtrCompressor<FreeAlloc>()} {
53+
freedAllocations_{slabAlloc_.createSingleTierPtrCompressor<FreeAlloc>()} {
5454
checkState();
5555
}
5656

@@ -102,7 +102,7 @@ AllocationClass::AllocationClass(
102102
currSlab_(s.getSlabForIdx(*object.currSlabIdx())),
103103
slabAlloc_(s),
104104
freedAllocations_(*object.freedAllocationsObject(),
105-
slabAlloc_.createPtrCompressor<FreeAlloc>()),
105+
slabAlloc_.createSingleTierPtrCompressor<FreeAlloc>()),
106106
canAllocate_(*object.canAllocate()) {
107107
if (!slabAlloc_.isRestorable()) {
108108
throw std::logic_error("The allocation class cannot be restored.");
@@ -356,9 +356,10 @@ std::pair<bool, std::vector<void*>> AllocationClass::pruneFreeAllocs(
356356
// allocated slab, release any freed allocations belonging to this slab.
357357
// Set the bit to true if the corresponding allocation is freed, false
358358
// otherwise.
359-
FreeList freeAllocs{slabAlloc_.createPtrCompressor<FreeAlloc>()};
360-
FreeList notInSlab{slabAlloc_.createPtrCompressor<FreeAlloc>()};
361-
FreeList inSlab{slabAlloc_.createPtrCompressor<FreeAlloc>()};
359+
FreeList freeAllocs{slabAlloc_.createSingleTierPtrCompressor<FreeAlloc>()};
360+
FreeList notInSlab{slabAlloc_.createSingleTierPtrCompressor<FreeAlloc>()};
361+
FreeList inSlab{slabAlloc_.createSingleTierPtrCompressor<FreeAlloc>()};
362+
362363

363364
lock_->lock_combine([&]() {
364365
// Take the allocation class free list offline

cachelib/allocator/memory/AllocationClass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ class AllocationClass {
445445
struct CACHELIB_PACKED_ATTR FreeAlloc {
446446
using CompressedPtr = facebook::cachelib::CompressedPtr;
447447
using PtrCompressor =
448-
facebook::cachelib::PtrCompressor<FreeAlloc, SlabAllocator>;
448+
facebook::cachelib::SingleTierPtrCompressor<FreeAlloc, SlabAllocator>;
449449
SListHook<FreeAlloc> hook_{};
450450
};
451451

cachelib/allocator/memory/CompressedPtr.h

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ namespace cachelib {
2727

2828
class SlabAllocator;
2929

30+
template <typename PtrType, typename AllocatorContainer>
31+
class PtrCompressor;
32+
3033
// This CompressedPtr makes decompression fast by staying away from division and
31-
// modulo arithmetic and doing those during the compression time. We most often
32-
// decompress a CompressedPtr than compress a pointer while creating one. This
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
3336
// is used for pointer compression by the memory allocator.
3437

3538
// We compress pointers by storing the tier index, slab index and alloc index of
@@ -173,12 +176,14 @@ class CACHELIB_PACKED_ATTR CompressedPtr {
173176
}
174177

175178
friend SlabAllocator;
179+
template <typename CPtrType, typename AllocatorContainer>
180+
friend class PtrCompressor;
176181
};
177182

178183
template <typename PtrType, typename AllocatorT>
179-
class PtrCompressor {
184+
class SingleTierPtrCompressor {
180185
public:
181-
explicit PtrCompressor(const AllocatorT& allocator) noexcept
186+
explicit SingleTierPtrCompressor(const AllocatorT& allocator) noexcept
182187
: allocator_(allocator) {}
183188

184189
const CompressedPtr compress(const PtrType* uncompressed) const {
@@ -190,17 +195,65 @@ class PtrCompressor {
190195
allocator_.unCompress(compressed, false /* isMultiTiered */));
191196
}
192197

193-
bool operator==(const PtrCompressor& rhs) const noexcept {
198+
bool operator==(const SingleTierPtrCompressor& rhs) const noexcept {
194199
return &allocator_ == &rhs.allocator_;
195200
}
196201

197-
bool operator!=(const PtrCompressor& rhs) const noexcept {
202+
bool operator!=(const SingleTierPtrCompressor& rhs) const noexcept {
198203
return !(*this == rhs);
199204
}
200205

201206
private:
202207
// memory allocator that does the pointer compression.
203208
const AllocatorT& allocator_;
204209
};
210+
211+
template <typename PtrType, typename AllocatorContainer>
212+
class PtrCompressor {
213+
public:
214+
explicit PtrCompressor(const AllocatorContainer& allocators) noexcept
215+
: allocators_(allocators) {}
216+
217+
const CompressedPtr compress(const PtrType* uncompressed) const {
218+
if (uncompressed == nullptr)
219+
return CompressedPtr{};
220+
221+
TierId tid;
222+
for (tid = 0; tid < allocators_.size(); tid++) {
223+
if (allocators_[tid]->isMemoryInAllocator(
224+
static_cast<const void*>(uncompressed)))
225+
break;
226+
}
227+
228+
bool isMultiTiered = allocators_.size() > 1;
229+
auto cptr = allocators_[tid]->compress(uncompressed, isMultiTiered);
230+
if (isMultiTiered) { // config has multiple tiers
231+
cptr.setTierId(tid);
232+
}
233+
return cptr;
234+
}
235+
236+
PtrType* unCompress(const CompressedPtr compressed) const {
237+
if (compressed.isNull()) {
238+
return nullptr;
239+
}
240+
bool isMultiTiered = allocators_.size() > 1;
241+
auto& allocator = *allocators_[compressed.getTierId(isMultiTiered)];
242+
return static_cast<PtrType*>(
243+
allocator.unCompress(compressed, isMultiTiered));
244+
}
245+
246+
bool operator==(const PtrCompressor& rhs) const noexcept {
247+
return &allocators_ == &rhs.allocators_;
248+
}
249+
250+
bool operator!=(const PtrCompressor& rhs) const noexcept {
251+
return !(*this == rhs);
252+
}
253+
254+
private:
255+
// memory allocator that does the pointer compression.
256+
const AllocatorContainer& allocators_;
257+
};
205258
} // namespace cachelib
206259
} // namespace facebook

cachelib/allocator/memory/MemoryAllocator.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,13 @@ class MemoryAllocator {
516516
using CompressedPtr = facebook::cachelib::CompressedPtr;
517517
template <typename PtrType>
518518
using PtrCompressor =
519-
facebook::cachelib::PtrCompressor<PtrType, SlabAllocator>;
520-
519+
facebook::cachelib::PtrCompressor<PtrType,
520+
std::vector<std::unique_ptr<MemoryAllocator>>>;
521+
521522
template <typename PtrType>
522-
PtrCompressor<PtrType> createPtrCompressor() {
523-
return slabAllocator_.createPtrCompressor<PtrType>();
524-
}
523+
using SingleTierPtrCompressor =
524+
facebook::cachelib::PtrCompressor<PtrType,
525+
SlabAllocator>;
525526

526527
// compress a given pointer to a valid allocation made out of this allocator
527528
// through an allocate() or nullptr. Calling this otherwise with invalid

cachelib/allocator/memory/SlabAllocator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ class SlabAllocator {
318318
}
319319

320320
template <typename PtrType>
321-
PtrCompressor<PtrType, SlabAllocator> createPtrCompressor() const {
322-
return PtrCompressor<PtrType, SlabAllocator>(*this);
321+
SingleTierPtrCompressor<PtrType, SlabAllocator> createSingleTierPtrCompressor() const {
322+
return SingleTierPtrCompressor<PtrType, SlabAllocator>(*this);
323323
}
324324

325325
// returns starting address of memory we own.

run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Newline separated list of tests to ignore
44
BLACKLIST="allocator-test-NavySetupTest
5+
allocator-test-NvmCacheTests
56
shm-test-test_page_size"
67

78
if [ "$1" == "long" ]; then

0 commit comments

Comments
 (0)