Skip to content

Commit

Permalink
Merge pull request #829 from DamianDuy/extendOOMtest
Browse files Browse the repository at this point in the history
[umf] extend out of memory test for disjoint pool
  • Loading branch information
pbalcer authored Oct 4, 2023
2 parents 2b7a24a + 3336a83 commit f5829a1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
19 changes: 17 additions & 2 deletions test/unified_malloc_framework/common/pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stdlib.h>

#include "base.hpp"
#include "provider.hpp"
#include "umf_helpers.hpp"

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

template <typename T, typename... Args>
auto makePoolWithOOMProvider(int allocNum, Args &&...args) {
auto [ret, provider] =
umf::memoryProviderMakeUnique<provider_mock_out_of_mem>(allocNum);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
auto [retp, pool] = umf::poolMakeUnique<T, 1, Args...>(
{std::move(provider)}, std::forward<Args>(args)...);
EXPECT_EQ(retp, UMF_RESULT_SUCCESS);
return std::move(pool);
}

bool isReallocSupported(umf_memory_pool_handle_t hPool) {
static constexpr size_t allocSize = 8;
bool supported;
Expand Down Expand Up @@ -128,10 +140,13 @@ struct proxy_pool : public pool_base {
void *calloc(size_t num, size_t size) noexcept {
void *ptr;
auto ret = umfMemoryProviderAlloc(provider, num * size, 0, &ptr);
umf::getPoolLastStatusRef<proxy_pool>() = ret;

memset(ptr, 0, num * size);
if (!ptr) {
return ptr;
}

umf::getPoolLastStatusRef<proxy_pool>() = ret;
memset(ptr, 0, num * size);
return ptr;
}
void *realloc([[maybe_unused]] void *ptr,
Expand Down
41 changes: 37 additions & 4 deletions test/unified_malloc_framework/memoryPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,27 @@ struct umfMultiPoolTest : umfPoolTest {
std::vector<umf::pool_unique_handle_t> pools;
};

struct umfMemTest : umfPoolTest {
void SetUp() override { umfPoolTest::SetUp(); }
void TearDown() override { umfPoolTest::TearDown(); }
struct umfMemTest
: umf_test::test,
::testing::WithParamInterface<
std::tuple<std::function<umf::pool_unique_handle_t(void)>, int>> {
umfMemTest() : pool(nullptr, nullptr), expectedRecycledPoolAllocs(0) {}
void SetUp() override {
test::SetUp();
initialize();
}

void TearDown() override { test::TearDown(); }

void initialize() {
auto [pool_fun, expectedRecycledPoolAllocs] = this->GetParam();
EXPECT_NE(pool_fun(), nullptr);
this->pool = pool_fun();
this->expectedRecycledPoolAllocs = expectedRecycledPoolAllocs;
}

umf::pool_unique_handle_t pool;
int expectedRecycledPoolAllocs;
};

TEST_P(umfPoolTest, allocFree) {
Expand Down Expand Up @@ -259,7 +277,7 @@ TEST_P(umfPoolTest, multiThreadedMallocFreeRandomSizes) {
}

TEST_P(umfMemTest, outOfMem) {
static constexpr size_t allocSize = 16;
static constexpr size_t allocSize = 4096;
auto hPool = pool.get();

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

// next part of the test- freeing some memory to allocate it again (as the memory
// should be acquired from the pool itself now, not from the provider),
// is done only for the disjoint pool for now

// remove last nullptr from the allocations vector
ASSERT_EQ(allocations.back(), nullptr);
allocations.pop_back();

ASSERT_NE(allocations.back(), nullptr);
for (int i = 0; i < expectedRecycledPoolAllocs; i++) {
umfPoolFree(hPool, allocations.back());
allocations.pop_back();
}

for (int i = 0; i < expectedRecycledPoolAllocs; i++) {
allocations.emplace_back(umfPoolMalloc(hPool, allocSize));
ASSERT_NE(allocations.back(), nullptr);
}

for (auto allocation : allocations) {
umfPoolFree(hPool, allocation);
}
Expand Down
13 changes: 6 additions & 7 deletions test/unified_malloc_framework/memoryPoolAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,12 @@ INSTANTIATE_TEST_SUITE_P(
}));

INSTANTIATE_TEST_SUITE_P(
proxyPoolOOMTest, umfMemTest, ::testing::Values([] {
return umf::poolMakeUnique<umf_test::proxy_pool, 1>(
{umf::memoryProviderMakeUnique<
umf_test::provider_mock_out_of_mem>(10)
.second})
.second;
}));
proxyPoolOOMTest, umfMemTest,
::testing::Values(std::tuple(
[] {
return umf_test::makePoolWithOOMProvider<umf_test::proxy_pool>(10);
},
0)));

////////////////// Negative test cases /////////////////

Expand Down
21 changes: 9 additions & 12 deletions test/unified_malloc_framework/umf_pools/disjoint_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "disjoint_pool.hpp"

#include "memoryPool.hpp"
#include "pool.hpp"
#include "provider.h"
#include "provider.hpp"

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

static auto makePoolOOMProvider() {
auto [ret, provider] =
umf::memoryProviderMakeUnique<umf_test::provider_mock_out_of_mem>(10);
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
auto [retp, pool] = umf::poolMakeUnique<usm::DisjointPool, 1>(
{std::move(provider)}, poolConfig());
EXPECT_EQ(retp, UMF_RESULT_SUCCESS);
return std::move(pool);
}

using umf_test::test;

TEST_F(test, freeErrorPropagation) {
Expand Down Expand Up @@ -83,8 +74,14 @@ TEST_F(test, freeErrorPropagation) {
INSTANTIATE_TEST_SUITE_P(disjointPoolTests, umfPoolTest,
::testing::Values(makePool));

INSTANTIATE_TEST_SUITE_P(disjointPoolTests, umfMemTest,
::testing::Values(makePoolOOMProvider));
INSTANTIATE_TEST_SUITE_P(
disjointPoolTests, umfMemTest,
::testing::Values(std::make_tuple(
[] {
return umf_test::makePoolWithOOMProvider<usm::DisjointPool>(
static_cast<int>(poolConfig().Capacity), poolConfig());
},
static_cast<int>(poolConfig().Capacity) / 2)));

GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(umfMultiPoolTest);
INSTANTIATE_TEST_SUITE_P(disjointMultiPoolTests, umfMultiPoolTest,
Expand Down

0 comments on commit f5829a1

Please sign in to comment.