Skip to content

Commit 9096f1e

Browse files
committed
item movement test
1 parent 107eb11 commit 9096f1e

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

cachelib/allocator/tests/AllocatorMemoryTiersTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValid) { this->testMultiTiersValid
2828
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersValidMixed) { this->testMultiTiersValidMixed(); }
2929
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersForceTierAllocation) { this->testMultiTiersForceTierAllocation(); }
3030
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersWatermarkTierAllocation) { this->testMultiTiersWatermarkAllocation(); }
31+
TEST_F(LruAllocatorMemoryTiersTest, MultiTiersWatermarkTierAllocation) { this->testSyncEviction(); }
3132

3233
} // end of namespace tests
3334
} // end of namespace cachelib
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) Intel Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "cachelib/allocator/tests/AllocatorMemoryTiersTest.h"
18+
19+
namespace facebook {
20+
namespace cachelib {
21+
namespace tests {
22+
23+
using LruAllocatorMemoryTiersTest = AllocatorMemoryTiersTest<LruAllocator>;
24+
25+
// 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+
30+
} // end of namespace tests
31+
} // end of namespace cachelib
32+
} // end of namespace facebook

cachelib/allocator/tests/AllocatorMemoryTiersTest.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,52 @@ class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
9797
ASSERT(handle != nullptr);
9898
ASSERT_NO_THROW(alloc->insertOrReplace(handle));
9999
}
100+
101+
void testSyncEviction() {
102+
auto config = makeDefaultConfig();
103+
config.setCacheSize(10 * Slab::kSize);
104+
config.acTopTierEvictionWatermark = 99.0;
105+
106+
{
107+
AllocatorT alloc(AllocatorT::SharedMemNew, config);
108+
auto pool = alloc.addPool("default", alloc.getCacheMemoryStats().cacheSize);
109+
110+
{
111+
// should be allocated in upper tier.
112+
auto handle = alloc.allocate(pool, "key", Slab::kSize / 2);
113+
ASSERT_NE(handle, nullptr);
114+
std::string data = "some data";
115+
std::memcpy(handle->getMemory(), data.data(), data.size());
116+
117+
alloc.insertOrReplace(handle);
118+
119+
auto found = alloc.find("key");
120+
ASSERT_NE(found, nullptr);
121+
ASSERT_EQ(found->getSize(), Slab::kSize / 2);
122+
ASSERT_EQ(std::string(reinterpret_cast<const char*>(found->getMemory()), data.size()), data);
123+
}
124+
125+
auto toptier_free = alloc.getCacheMemoryStats().slabsApproxFreePercentages[0];
126+
ASSERT_NE(toptier_free, 100.0);
127+
ASSERT_EQ(alloc.getCacheMemoryStats().slabsApproxFreePercentages[1], 100.0);
128+
129+
auto handle2 = alloc.allocate(pool, "key2", Slab::kSize / 2);
130+
ASSERT_NE(handle2, nullptr);
131+
std::string data2 = "other data";
132+
std::memcpy(reinterpret_cast<char*>(handle2->getMemory()), data2.data(), data2.size());
133+
134+
alloc.insertOrReplace(handle2);
135+
136+
auto found2 = alloc.find("key2");
137+
ASSERT_NE(found2, nullptr);
138+
ASSERT_EQ(found2->getSize(), Slab::kSize / 2);
139+
ASSERT_EQ(std::string(reinterpret_cast<const char*>(found2->getMemory()), data2.size()), data2);
140+
141+
// previous data should be evicted, and replaced by new one
142+
ASSERT_EQ(toptier_free, alloc.getCacheMemoryStats().slabsApproxFreePercentages[0]);
143+
ASSERT_NE(alloc.getCacheMemoryStats().slabsApproxFreePercentages[1], 100.0);
144+
}
145+
}
100146

101147
void testMultiTiersForceTierAllocation() {
102148
auto config = makeDefaultConfig();
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include "cachelib/allocator/CacheAllocatorConfig.h"
20+
#include "cachelib/allocator/MemoryTierCacheConfig.h"
21+
#include "cachelib/allocator/tests/TestBase.h"
22+
23+
namespace facebook {
24+
namespace cachelib {
25+
namespace tests {
26+
27+
template <typename AllocatorT>
28+
class AllocatorMemoryTiersTest : public AllocatorTest<AllocatorT> {
29+
public:
30+
void testMultiTiersInvalid() {
31+
typename AllocatorT::Config config;
32+
config.setCacheSize(100 * Slab::kSize);
33+
config.configureMemoryTiers({
34+
MemoryTierCacheConfig::fromFile("/tmp/a" + std::to_string(::getpid()))
35+
.setRatio(1),
36+
MemoryTierCacheConfig::fromFile("/tmp/b" + std::to_string(::getpid()))
37+
.setRatio(1)
38+
});
39+
40+
// More than one tier is not supported
41+
ASSERT_THROW(std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config),
42+
std::invalid_argument);
43+
}
44+
45+
void testMultiTiersValid() {
46+
typename AllocatorT::Config config;
47+
config.setCacheSize(100 * Slab::kSize);
48+
config.enableCachePersistence("/tmp");
49+
config.usePosixForShm();
50+
config.configureMemoryTiers({
51+
MemoryTierCacheConfig::fromFile("/tmp/a" + std::to_string(::getpid()))
52+
.setRatio(1),
53+
MemoryTierCacheConfig::fromFile("/tmp/b" + std::to_string(::getpid()))
54+
.setRatio(1)
55+
});
56+
57+
auto alloc = std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config);
58+
ASSERT(alloc != nullptr);
59+
60+
auto pool = alloc->addPool("default", alloc->getCacheMemoryStats().cacheSize);
61+
auto handle = alloc->allocate(pool, "key", std::string("value").size());
62+
ASSERT(handle != nullptr);
63+
ASSERT_NO_THROW(alloc->insertOrReplace(handle));
64+
}
65+
66+
void testMultiTiersValidMixed() {
67+
typename AllocatorT::Config config;
68+
config.setCacheSize(100 * Slab::kSize);
69+
config.enableCachePersistence("/tmp");
70+
config.usePosixForShm();
71+
config.configureMemoryTiers({
72+
MemoryTierCacheConfig::fromShm()
73+
.setRatio(1),
74+
MemoryTierCacheConfig::fromFile("/tmp/b" + std::to_string(::getpid()))
75+
.setRatio(1)
76+
});
77+
78+
auto alloc = std::make_unique<AllocatorT>(AllocatorT::SharedMemNew, config);
79+
ASSERT(alloc != nullptr);
80+
81+
auto pool = alloc->addPool("default", alloc->getCacheMemoryStats().cacheSize);
82+
auto handle = alloc->allocate(pool, "key", std::string("value").size());
83+
ASSERT(handle != nullptr);
84+
ASSERT_NO_THROW(alloc->insertOrReplace(handle));
85+
}
86+
};
87+
} // namespace tests
88+
} // namespace cachelib
89+
} // namespace facebook

0 commit comments

Comments
 (0)