Skip to content

Commit 40eea01

Browse files
Merge pull request #690 from vinser52/svinogra_ipc_tests
Refactor IPC tests and enable them for OS provider
2 parents c4d12b6 + 9d1b51b commit 40eea01

File tree

5 files changed

+97
-12
lines changed

5 files changed

+97
-12
lines changed

src/ipc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ umf_result_t umfPoolGetIPCHandleSize(umf_memory_pool_handle_t hPool,
5151

5252
umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle,
5353
size_t *size) {
54+
if (ptr == NULL || umfIPCHandle == NULL || size == NULL) {
55+
LOG_ERR("invalid argument.");
56+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
57+
}
58+
5459
size_t ipcHandleSize = 0;
5560
umf_alloc_info_t allocInfo;
5661
umf_result_t ret = umfMemoryTrackerGetAllocInfo(ptr, &allocInfo);

test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ if(LINUX AND (NOT UMF_DISABLE_HWLOC)) # OS-specific functions are implemented
186186
NAME provider_os_memory
187187
SRCS provider_os_memory.cpp
188188
LIBS ${UMF_UTILS_FOR_TEST})
189+
if(UMF_BUILD_LIBUMF_POOL_DISJOINT)
190+
target_compile_definitions(umf_test-provider_os_memory
191+
PRIVATE UMF_POOL_DISJOINT_ENABLED=1)
192+
target_link_libraries(umf_test-provider_os_memory PRIVATE disjoint_pool)
193+
endif()
194+
189195
add_umf_test(
190196
NAME provider_os_memory_multiple_numa_nodes
191197
SRCS provider_os_memory_multiple_numa_nodes.cpp

test/ipcFixtures.hpp

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ using ipcTestParams =
5252

5353
struct umfIpcTest : umf_test::test,
5454
::testing::WithParamInterface<ipcTestParams> {
55-
umfIpcTest() : pool(nullptr, nullptr) {}
56-
void SetUp() override {
57-
test::SetUp();
58-
this->pool = makePool();
59-
}
55+
umfIpcTest() {}
56+
void SetUp() override { test::SetUp(); }
6057

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

@@ -74,7 +71,9 @@ struct umfIpcTest : umf_test::test,
7471

7572
auto trace = [](void *trace_context, const char *name) {
7673
stats_type *stat = static_cast<stats_type *>(trace_context);
77-
if (std::strcmp(name, "get_ipc_handle") == 0) {
74+
if (std::strcmp(name, "alloc") == 0) {
75+
++stat->allocCount;
76+
} else if (std::strcmp(name, "get_ipc_handle") == 0) {
7877
++stat->getCount;
7978
} else if (std::strcmp(name, "put_ipc_handle") == 0) {
8079
++stat->putCount;
@@ -98,30 +97,60 @@ struct umfIpcTest : umf_test::test,
9897
}
9998

10099
struct stats_type {
100+
std::atomic<size_t> allocCount;
101101
std::atomic<size_t> getCount;
102102
std::atomic<size_t> putCount;
103103
std::atomic<size_t> openCount;
104104
std::atomic<size_t> closeCount;
105105

106-
stats_type() : getCount(0), putCount(0), openCount(0), closeCount(0) {}
106+
stats_type()
107+
: allocCount(0), getCount(0), putCount(0), openCount(0),
108+
closeCount(0) {}
107109
};
108110

109-
umf::pool_unique_handle_t pool;
110111
static constexpr int NTHREADS = 10;
111112
stats_type stat;
112113
MemoryAccessor *memAccessor = nullptr;
113114
};
114115

115116
TEST_P(umfIpcTest, GetIPCHandleSize) {
116117
size_t size = 0;
118+
umf::pool_unique_handle_t pool = makePool();
119+
117120
umf_result_t ret = umfPoolGetIPCHandleSize(pool.get(), &size);
118121
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
119122
EXPECT_GT(size, 0);
120123
}
121124

125+
TEST_P(umfIpcTest, GetIPCHandleInvalidArgs) {
126+
constexpr size_t SIZE = 100;
127+
umf_ipc_handle_t ipcHandle = nullptr;
128+
size_t handleSize = 0;
129+
umf_result_t ret = umfGetIPCHandle(nullptr, &ipcHandle, &handleSize);
130+
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
131+
132+
void *ptr = (void *)0xBAD;
133+
ret = umfGetIPCHandle(ptr, &ipcHandle, &handleSize);
134+
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
135+
136+
umf::pool_unique_handle_t pool = makePool();
137+
ptr = umfPoolMalloc(pool.get(), SIZE);
138+
EXPECT_NE(ptr, nullptr);
139+
140+
ret = umfGetIPCHandle(ptr, nullptr, &handleSize);
141+
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
142+
143+
ret = umfGetIPCHandle(ptr, &ipcHandle, nullptr);
144+
EXPECT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
145+
146+
ret = umfFree(ptr);
147+
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
148+
}
149+
122150
TEST_P(umfIpcTest, BasicFlow) {
123151
constexpr size_t SIZE = 100;
124152
std::vector<int> expected_data(SIZE);
153+
umf::pool_unique_handle_t pool = makePool();
125154
int *ptr = (int *)umfPoolMalloc(pool.get(), SIZE * sizeof(int));
126155
EXPECT_NE(ptr, nullptr);
127156

@@ -172,6 +201,7 @@ TEST_P(umfIpcTest, BasicFlow) {
172201
ret = umfPoolFree(pool.get(), ptr);
173202
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
174203

204+
pool.reset(nullptr);
175205
EXPECT_EQ(stat.getCount, 1);
176206
EXPECT_EQ(stat.putCount, stat.getCount);
177207
// TODO: enale check below once cache for open IPC handles is implemented
@@ -183,6 +213,8 @@ TEST_P(umfIpcTest, ConcurrentGetPutHandles) {
183213
std::vector<void *> ptrs;
184214
constexpr size_t ALLOC_SIZE = 100;
185215
constexpr size_t NUM_POINTERS = 100;
216+
umf::pool_unique_handle_t pool = makePool();
217+
186218
for (size_t i = 0; i < NUM_POINTERS; ++i) {
187219
void *ptr = umfPoolMalloc(pool.get(), ALLOC_SIZE);
188220
EXPECT_NE(ptr, nullptr);
@@ -221,15 +253,16 @@ TEST_P(umfIpcTest, ConcurrentGetPutHandles) {
221253
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
222254
}
223255

224-
EXPECT_GE(stat.getCount, NUM_POINTERS);
225-
EXPECT_LE(stat.getCount, NUM_POINTERS * NTHREADS);
256+
pool.reset(nullptr);
226257
EXPECT_EQ(stat.putCount, stat.getCount);
227258
}
228259

229260
TEST_P(umfIpcTest, ConcurrentOpenCloseHandles) {
230261
std::vector<void *> ptrs;
231262
constexpr size_t ALLOC_SIZE = 100;
232263
constexpr size_t NUM_POINTERS = 100;
264+
umf::pool_unique_handle_t pool = makePool();
265+
233266
for (size_t i = 0; i < NUM_POINTERS; ++i) {
234267
void *ptr = umfPoolMalloc(pool.get(), ALLOC_SIZE);
235268
EXPECT_NE(ptr, nullptr);
@@ -249,8 +282,8 @@ TEST_P(umfIpcTest, ConcurrentOpenCloseHandles) {
249282

250283
umf_test::syncthreads_barrier syncthreads(NTHREADS);
251284

252-
auto openHandlesFn = [this, &ipcHandles, &openedIpcHandles,
253-
&syncthreads](size_t tid) {
285+
auto openHandlesFn = [this, &ipcHandles, &openedIpcHandles, &syncthreads,
286+
&pool](size_t tid) {
254287
syncthreads();
255288
for (auto ipcHandle : ipcHandles) {
256289
void *ptr;
@@ -282,6 +315,11 @@ TEST_P(umfIpcTest, ConcurrentOpenCloseHandles) {
282315
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
283316
}
284317

318+
pool.reset(nullptr);
319+
EXPECT_EQ(stat.getCount, stat.allocCount);
320+
EXPECT_EQ(stat.putCount, stat.getCount);
321+
// TODO: enale check below once cache for open IPC handles is implemented
322+
// EXPECT_EQ(stat.openCount, stat.allocCount);
285323
EXPECT_EQ(stat.openCount, stat.closeCount);
286324
}
287325

test/provider_os_memory.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#include "base.hpp"
66

77
#include "cpp_helpers.hpp"
8+
#include "ipcFixtures.hpp"
89

910
#include <umf/memory_provider.h>
11+
#include <umf/pools/pool_disjoint.h>
1012
#include <umf/providers/provider_os_memory.h>
1113

1214
using umf_test::test;
@@ -327,3 +329,34 @@ TEST_P(umfProviderTest, purge_force_INVALID_POINTER) {
327329
verify_last_native_error(provider.get(),
328330
UMF_OS_RESULT_ERROR_PURGE_FORCE_FAILED);
329331
}
332+
333+
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(umfIpcTest);
334+
335+
umf_os_memory_provider_params_t osMemoryProviderParamsShared() {
336+
auto params = umfOsMemoryProviderParamsDefault();
337+
params.visibility = UMF_MEM_MAP_SHARED;
338+
return params;
339+
}
340+
auto os_params = osMemoryProviderParamsShared();
341+
342+
HostMemoryAccessor hostAccessor;
343+
344+
umf_disjoint_pool_params_t disjointPoolParams() {
345+
umf_disjoint_pool_params_t params = umfDisjointPoolParamsDefault();
346+
params.SlabMinSize = 4096;
347+
params.MaxPoolableSize = 4096;
348+
params.Capacity = 4;
349+
params.MinBucketSize = 64;
350+
return params;
351+
}
352+
umf_disjoint_pool_params_t disjointParams = disjointPoolParams();
353+
354+
static std::vector<ipcTestParams> ipcTestParamsList = {
355+
#if (defined UMF_POOL_DISJOINT_ENABLED)
356+
{umfDisjointPoolOps(), &disjointParams, umfOsMemoryProviderOps(),
357+
&os_params, &hostAccessor},
358+
#endif
359+
};
360+
361+
INSTANTIATE_TEST_SUITE_P(osProviderTest, umfIpcTest,
362+
::testing::ValuesIn(ipcTestParamsList));

test/test_valgrind.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ for test in $(ls -1 umf_test-*); do
9191
umf_test-memspace_host_all)
9292
FILTER='--gtest_filter="-*allocsSpreadAcrossAllNumaNodes"'
9393
;;
94+
umf_test-provider_os_memory)
95+
FILTER='--gtest_filter="-osProviderTest/umfIpcTest*"'
96+
;;
9497
umf_test-provider_os_memory_config)
9598
FILTER='--gtest_filter="-*protection_flag_none:*protection_flag_read:*providerConfigTestNumaMode*"'
9699
;;

0 commit comments

Comments
 (0)