Skip to content

Commit fa6d72f

Browse files
committed
Add disableEvictionToMemory unit test.
1 parent a3e29dd commit fa6d72f

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

cachelib/allocator/tests/AllocatorMemoryTiersTest.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ namespace tests {
2323
using LruAllocatorMemoryTiersTest = AllocatorMemoryTiersTest<LruAllocator>;
2424

2525
// TODO(MEMORY_TIER): add more tests with different eviction policies
26-
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersInvalid) { this->testMultiTiersInvalid(); }
27-
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValid) { this->testMultiTiersValid(); }
28-
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValidMixed) { this->testMultiTiersValidMixed(); }
29-
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersForceTierAllocation) { this->testMultiTiersForceTierAllocation(); }
30-
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersWatermarkTierAllocation) { this->testMultiTiersWatermarkAllocation(); }
31-
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersSyncEviction) { this->testSyncEviction(); }
26+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersInvalid) {
27+
this->testMultiTiersInvalid(); }
28+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValid) {
29+
this->testMultiTiersValid(); }
30+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValidMixed) {
31+
this->testMultiTiersValidMixed(); }
32+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersForceTierAllocation) {
33+
this->testMultiTiersForceTierAllocation(); }
34+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersWatermarkTierAllocation) {
35+
this->testMultiTiersWatermarkAllocation(); }
36+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersSyncEviction) {
37+
this->testSyncEviction(); }
38+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersDisableEvictionToMemory) {
39+
this->testMultiTiersDisableEvictionToMemory(); }
40+
3241

3342
} // end of namespace tests
3443
} // end of namespace cachelib

cachelib/allocator/tests/AllocatorMemoryTiersTest.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,61 @@ class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
221221
alloc.getCacheMemoryStats().slabsApproxFreePercentages[1]);
222222
}
223223
}
224+
225+
void testMultiTiersDisableEvictionToMemory () {
226+
auto config = makeDefaultConfig();
227+
config.setCacheSize(4 * Slab::kSize);
228+
//Always allocate to fist tier.
229+
config.forceAllocationTier = 0;
230+
//Don't evict to memory.
231+
config.disableEvictionToMemory = true;
232+
{
233+
AllocatorT alloc(AllocatorT::SharedMemNew, config);
234+
auto pool = alloc.addPool("default",
235+
alloc.getCacheMemoryStats().cacheSize);
236+
{
237+
//Allocate first key.
238+
auto handle = alloc.allocate(pool, "key1", Slab::kSize / 2);
239+
ASSERT(handle != nullptr);
240+
std::string data = "lorem ipsum the first";
241+
std::memcpy(reinterpret_cast<char*>(handle->getMemory()), data.data(),
242+
data.size());
243+
alloc.insertOrReplace(handle);
244+
//Item in the first tier.
245+
ASSERT_NE(alloc.getCacheMemoryStats().slabsApproxFreePercentages[0],
246+
100.0);
247+
//Second tier is empty.
248+
ASSERT_EQ(alloc.getCacheMemoryStats().slabsApproxFreePercentages[1],
249+
100.0);
250+
}
251+
{
252+
//Allocate second key.
253+
auto handle = alloc.allocate(pool, "key2", Slab::kSize / 2);
254+
ASSERT(handle != nullptr);
255+
std::string data2 = "lorem ipsum the second";
256+
std::memcpy(reinterpret_cast<char*>(handle->getMemory()), data2.data(),
257+
data2.size());
258+
alloc.insertOrReplace(handle);
259+
//Item in the first tier.
260+
ASSERT_NE(alloc.getCacheMemoryStats().slabsApproxFreePercentages[0],
261+
100.0);
262+
//Second tier is empty.
263+
ASSERT_EQ(alloc.getCacheMemoryStats().slabsApproxFreePercentages[1],
264+
100.0);
265+
}
266+
{
267+
//Check if key1 was evicted.
268+
auto found = alloc.find("key1");
269+
ASSERT_EQ(found, nullptr);
270+
}
271+
{
272+
//Check if key2 is present.
273+
auto found = alloc.find("key2");
274+
ASSERT_NE(found, nullptr);
275+
ASSERT_EQ(found->getSize(), Slab::kSize / 2);
276+
}
277+
}
278+
}
224279
};
225280
} // namespace tests
226281
} // namespace cachelib

0 commit comments

Comments
 (0)