Skip to content

Commit f5829a1

Browse files
authored
Merge pull request #829 from DamianDuy/extendOOMtest
[umf] extend out of memory test for disjoint pool
2 parents 2b7a24a + 3336a83 commit f5829a1

File tree

4 files changed

+69
-25
lines changed

4 files changed

+69
-25
lines changed

test/unified_malloc_framework/common/pool.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <stdlib.h>
2424

2525
#include "base.hpp"
26+
#include "provider.hpp"
2627
#include "umf_helpers.hpp"
2728

2829
namespace umf_test {
@@ -31,6 +32,17 @@ auto wrapPoolUnique(umf_memory_pool_handle_t hPool) {
3132
return umf::pool_unique_handle_t(hPool, &umfPoolDestroy);
3233
}
3334

35+
template <typename T, typename... Args>
36+
auto makePoolWithOOMProvider(int allocNum, Args &&...args) {
37+
auto [ret, provider] =
38+
umf::memoryProviderMakeUnique<provider_mock_out_of_mem>(allocNum);
39+
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
40+
auto [retp, pool] = umf::poolMakeUnique<T, 1, Args...>(
41+
{std::move(provider)}, std::forward<Args>(args)...);
42+
EXPECT_EQ(retp, UMF_RESULT_SUCCESS);
43+
return std::move(pool);
44+
}
45+
3446
bool isReallocSupported(umf_memory_pool_handle_t hPool) {
3547
static constexpr size_t allocSize = 8;
3648
bool supported;
@@ -128,10 +140,13 @@ struct proxy_pool : public pool_base {
128140
void *calloc(size_t num, size_t size) noexcept {
129141
void *ptr;
130142
auto ret = umfMemoryProviderAlloc(provider, num * size, 0, &ptr);
143+
umf::getPoolLastStatusRef<proxy_pool>() = ret;
131144

132-
memset(ptr, 0, num * size);
145+
if (!ptr) {
146+
return ptr;
147+
}
133148

134-
umf::getPoolLastStatusRef<proxy_pool>() = ret;
149+
memset(ptr, 0, num * size);
135150
return ptr;
136151
}
137152
void *realloc([[maybe_unused]] void *ptr,

test/unified_malloc_framework/memoryPool.hpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,27 @@ struct umfMultiPoolTest : umfPoolTest {
5555
std::vector<umf::pool_unique_handle_t> pools;
5656
};
5757

58-
struct umfMemTest : umfPoolTest {
59-
void SetUp() override { umfPoolTest::SetUp(); }
60-
void TearDown() override { umfPoolTest::TearDown(); }
58+
struct umfMemTest
59+
: umf_test::test,
60+
::testing::WithParamInterface<
61+
std::tuple<std::function<umf::pool_unique_handle_t(void)>, int>> {
62+
umfMemTest() : pool(nullptr, nullptr), expectedRecycledPoolAllocs(0) {}
63+
void SetUp() override {
64+
test::SetUp();
65+
initialize();
66+
}
67+
68+
void TearDown() override { test::TearDown(); }
69+
70+
void initialize() {
71+
auto [pool_fun, expectedRecycledPoolAllocs] = this->GetParam();
72+
EXPECT_NE(pool_fun(), nullptr);
73+
this->pool = pool_fun();
74+
this->expectedRecycledPoolAllocs = expectedRecycledPoolAllocs;
75+
}
76+
77+
umf::pool_unique_handle_t pool;
78+
int expectedRecycledPoolAllocs;
6179
};
6280

6381
TEST_P(umfPoolTest, allocFree) {
@@ -259,7 +277,7 @@ TEST_P(umfPoolTest, multiThreadedMallocFreeRandomSizes) {
259277
}
260278

261279
TEST_P(umfMemTest, outOfMem) {
262-
static constexpr size_t allocSize = 16;
280+
static constexpr size_t allocSize = 4096;
263281
auto hPool = pool.get();
264282

265283
std::vector<void *> allocations;
@@ -274,10 +292,25 @@ TEST_P(umfMemTest, outOfMem) {
274292
ASSERT_NE(allocations.back(), nullptr);
275293
}
276294

295+
// next part of the test- freeing some memory to allocate it again (as the memory
296+
// should be acquired from the pool itself now, not from the provider),
297+
// is done only for the disjoint pool for now
298+
277299
// remove last nullptr from the allocations vector
278300
ASSERT_EQ(allocations.back(), nullptr);
279301
allocations.pop_back();
280302

303+
ASSERT_NE(allocations.back(), nullptr);
304+
for (int i = 0; i < expectedRecycledPoolAllocs; i++) {
305+
umfPoolFree(hPool, allocations.back());
306+
allocations.pop_back();
307+
}
308+
309+
for (int i = 0; i < expectedRecycledPoolAllocs; i++) {
310+
allocations.emplace_back(umfPoolMalloc(hPool, allocSize));
311+
ASSERT_NE(allocations.back(), nullptr);
312+
}
313+
281314
for (auto allocation : allocations) {
282315
umfPoolFree(hPool, allocation);
283316
}

test/unified_malloc_framework/memoryPoolAPI.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,12 @@ INSTANTIATE_TEST_SUITE_P(
158158
}));
159159

160160
INSTANTIATE_TEST_SUITE_P(
161-
proxyPoolOOMTest, umfMemTest, ::testing::Values([] {
162-
return umf::poolMakeUnique<umf_test::proxy_pool, 1>(
163-
{umf::memoryProviderMakeUnique<
164-
umf_test::provider_mock_out_of_mem>(10)
165-
.second})
166-
.second;
167-
}));
161+
proxyPoolOOMTest, umfMemTest,
162+
::testing::Values(std::tuple(
163+
[] {
164+
return umf_test::makePoolWithOOMProvider<umf_test::proxy_pool>(10);
165+
},
166+
0)));
168167

169168
////////////////// Negative test cases /////////////////
170169

test/unified_malloc_framework/umf_pools/disjoint_pool.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "disjoint_pool.hpp"
1212

1313
#include "memoryPool.hpp"
14+
#include "pool.hpp"
1415
#include "provider.h"
1516
#include "provider.hpp"
1617

@@ -33,16 +34,6 @@ static auto makePool() {
3334
return std::move(pool);
3435
}
3536

36-
static auto makePoolOOMProvider() {
37-
auto [ret, provider] =
38-
umf::memoryProviderMakeUnique<umf_test::provider_mock_out_of_mem>(10);
39-
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
40-
auto [retp, pool] = umf::poolMakeUnique<usm::DisjointPool, 1>(
41-
{std::move(provider)}, poolConfig());
42-
EXPECT_EQ(retp, UMF_RESULT_SUCCESS);
43-
return std::move(pool);
44-
}
45-
4637
using umf_test::test;
4738

4839
TEST_F(test, freeErrorPropagation) {
@@ -83,8 +74,14 @@ TEST_F(test, freeErrorPropagation) {
8374
INSTANTIATE_TEST_SUITE_P(disjointPoolTests, umfPoolTest,
8475
::testing::Values(makePool));
8576

86-
INSTANTIATE_TEST_SUITE_P(disjointPoolTests, umfMemTest,
87-
::testing::Values(makePoolOOMProvider));
77+
INSTANTIATE_TEST_SUITE_P(
78+
disjointPoolTests, umfMemTest,
79+
::testing::Values(std::make_tuple(
80+
[] {
81+
return umf_test::makePoolWithOOMProvider<usm::DisjointPool>(
82+
static_cast<int>(poolConfig().Capacity), poolConfig());
83+
},
84+
static_cast<int>(poolConfig().Capacity) / 2)));
8885

8986
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(umfMultiPoolTest);
9087
INSTANTIATE_TEST_SUITE_P(disjointMultiPoolTests, umfMultiPoolTest,

0 commit comments

Comments
 (0)