Skip to content

Commit 60abe5f

Browse files
committed
Add tests for the size threshold of the proxy library
The proxyLib_size_threshold_* tests test the size threshold of the proxy library. The size threshold is set to 64 bytes in this test, so all allocations of: 1) size < 64 go through the default system allocator and (umfPoolByPtr(ptr_size < 64) == nullptr) 2) size >= 64 go through the proxy lib allocator and (umfPoolByPtr(ptr_size >= 64) != nullptr). Ref: oneapi-src#894 Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent b78ac8f commit 60abe5f

File tree

3 files changed

+118
-27
lines changed

3 files changed

+118
-27
lines changed

.github/workflows/reusable_proxy_lib.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,14 @@ jobs:
6161

6262
# TODO enable the provider_file_memory_ipc test when the IPC tests with the proxy library are fixed
6363
# see the issue: https://github.com/oneapi-src/unified-memory-framework/issues/864
64+
# TODO enable the proxy_lib_basic test for the JEMALLOC proxy_lib_pool
65+
# see the issue: https://github.com/oneapi-src/unified-memory-framework/issues/894
6466
- name: Run "ctest --output-on-failure" with proxy library
6567
working-directory: ${{env.BUILD_DIR}}
66-
run: LD_PRELOAD=./lib/libumf_proxy.so ctest --output-on-failure -E provider_file_memory_ipc
68+
run: >
69+
LD_PRELOAD=./lib/libumf_proxy.so
70+
ctest --output-on-failure -E provider_file_memory_ipc
71+
${{ matrix.proxy_lib_pool == 'JEMALLOC' && '-E proxy_lib_basic' || '' }}
6772
6873
- name: Run "./test/umf_test-memoryPool" with proxy library
6974
working-directory: ${{env.BUILD_DIR}}
@@ -79,13 +84,16 @@ jobs:
7984

8085
# TODO enable the provider_file_memory_ipc test when the IPC tests with the proxy library are fixed
8186
# see the issue: https://github.com/oneapi-src/unified-memory-framework/issues/864
87+
# TODO enable the proxy_lib_basic test for the JEMALLOC proxy_lib_pool
88+
# see the issue: https://github.com/oneapi-src/unified-memory-framework/issues/894
8289
- name: Run "ctest --output-on-failure" with proxy library and size.threshold=128
8390
working-directory: ${{env.BUILD_DIR}}
8491
run: >
8592
UMF_PROXY="size.threshold=128"
8693
UMF_LOG="level:debug;flush:debug;output:stderr;pid:yes"
8794
LD_PRELOAD=./lib/libumf_proxy.so
8895
ctest --output-on-failure -E provider_file_memory_ipc
96+
${{ matrix.proxy_lib_pool == 'JEMALLOC' && '-E proxy_lib_basic' || '' }}
8997
9098
- name: Check coverage
9199
if: ${{ matrix.build_type == 'Debug' }}

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY)
413413
NAME proxy_lib_basic
414414
SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib.cpp
415415
LIBS ${UMF_UTILS_FOR_TEST} umf_proxy)
416+
set_property(TEST umf-proxy_lib_basic
417+
PROPERTY ENVIRONMENT "UMF_PROXY=size.threshold=64")
416418

417419
# the memoryPool test run with the proxy library
418420
add_umf_test(

test/test_proxy_lib.cpp

Lines changed: 107 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@
1919

2020
using umf_test::test;
2121

22+
#define SIZE_63 63
2223
#define SIZE_64 64
2324
#define ALIGN_1024 1024
2425

2526
TEST_F(test, proxyLib_basic) {
26-
27-
::free(::malloc(SIZE_64));
28-
2927
// a check to verify we are running the proxy library
3028
void *ptr = (void *)0x01;
3129

@@ -41,46 +39,129 @@ TEST_F(test, proxyLib_basic) {
4139
}
4240

4341
TEST_F(test, proxyLib_realloc_size0) {
44-
// realloc(ptr, 0) == free (ptr)
42+
// realloc(ptr, 0) == free(ptr)
4543
// realloc(ptr, 0) returns NULL
4644
ASSERT_EQ(::realloc(::malloc(SIZE_64), 0), nullptr);
4745
}
4846

49-
TEST_F(test, proxyLib_malloc_usable_size) {
50-
51-
void *ptr = ::malloc(SIZE_64);
52-
ASSERT_NE(ptr, nullptr);
53-
if (ptr == nullptr) {
54-
// Fix for the following CodeQL's warning on Windows:
55-
// 'ptr' could be '0': this does not adhere to the specification for the function '_msize'.
56-
return;
57-
}
47+
// The proxyLib_size_threshold_* tests test the size threshold of the proxy library.
48+
// The size threshold is set to 64 bytes in this test, so all allocations of:
49+
// 1) size < 64 go through the default system allocator (umfPoolByPtr(ptr_size < 64) == nullptr)
50+
// 2) size >= 64 go through the proxy lib allocator (umfPoolByPtr(ptr_size >= 64) != nullptr)
5851

52+
TEST_F(test, proxyLib_size_threshold_aligned_alloc) {
5953
#ifdef _WIN32
60-
size_t size = _msize(ptr);
61-
#elif __APPLE__
62-
size_t size = ::malloc_size(ptr);
54+
void *ptr_63 = _aligned_malloc(SIZE_63, ALIGN_1024);
55+
void *ptr_64 = _aligned_malloc(SIZE_64, ALIGN_1024);
6356
#else
64-
size_t size = ::malloc_usable_size(ptr);
57+
void *ptr_63 = ::aligned_alloc(ALIGN_1024, SIZE_63);
58+
void *ptr_64 = ::aligned_alloc(ALIGN_1024, SIZE_64);
6559
#endif
6660

67-
ASSERT_EQ((int)(size == 0 || size >= SIZE_64), 1);
61+
// verify alignment
62+
ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr_63, ALIGN_1024)), 1);
63+
ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr_64, ALIGN_1024)), 1);
6864

69-
::free(ptr);
70-
}
65+
// umfPoolByPtr(ptr_size_63) == nullptr
66+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
67+
// umfPoolByPtr(ptr_size_64) != nullptr
68+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
7169

72-
TEST_F(test, proxyLib_aligned_alloc) {
7370
#ifdef _WIN32
74-
void *ptr = _aligned_malloc(SIZE_64, ALIGN_1024);
71+
_aligned_free(ptr_63);
72+
_aligned_free(ptr_64);
7573
#else
76-
void *ptr = ::aligned_alloc(ALIGN_1024, SIZE_64);
74+
::free(ptr_63);
75+
::free(ptr_64);
7776
#endif
77+
}
78+
79+
TEST_F(test, proxyLib_size_threshold_malloc) {
80+
void *ptr_63 = malloc(SIZE_63);
81+
void *ptr_64 = malloc(SIZE_64);
82+
83+
// umfPoolByPtr(ptr_size_63) == nullptr
84+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
85+
// umfPoolByPtr(ptr_size_64) != nullptr
86+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
87+
88+
::free(ptr_63);
89+
::free(ptr_64);
90+
}
91+
92+
TEST_F(test, proxyLib_size_threshold_calloc) {
93+
void *ptr_63 = calloc(SIZE_63, 1);
94+
void *ptr_64 = calloc(SIZE_64, 1);
95+
96+
// umfPoolByPtr(ptr_size_63) == nullptr
97+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
98+
// umfPoolByPtr(ptr_size_64) != nullptr
99+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
100+
101+
::free(ptr_63);
102+
::free(ptr_64);
103+
}
78104

79-
ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr, ALIGN_1024)), 1);
105+
TEST_F(test, proxyLib_size_threshold_realloc_up) {
106+
void *ptr_63 = malloc(SIZE_63);
107+
void *ptr_64 = malloc(SIZE_64);
108+
109+
ptr_63 = realloc(ptr_63, 2 * SIZE_63);
110+
ptr_64 = realloc(ptr_64, 2 * SIZE_64);
111+
112+
// umfPoolByPtr(ptr_size_63) == nullptr
113+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
114+
// umfPoolByPtr(ptr_size_64) != nullptr
115+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
116+
117+
::free(ptr_63);
118+
::free(ptr_64);
119+
}
120+
121+
TEST_F(test, proxyLib_size_threshold_realloc_down) {
122+
void *ptr_63 = malloc(SIZE_63);
123+
void *ptr_64 = malloc(SIZE_64);
124+
125+
ptr_63 = realloc(ptr_63, SIZE_63 / 2);
126+
ptr_64 = realloc(ptr_64, SIZE_64 / 2);
127+
128+
// umfPoolByPtr(ptr_size_63) == nullptr
129+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
130+
// umfPoolByPtr(ptr_size_64) != nullptr
131+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
132+
133+
::free(ptr_63);
134+
::free(ptr_64);
135+
}
136+
137+
TEST_F(test, proxyLib_size_threshold_malloc_usable_size) {
138+
139+
void *ptr_63 = ::malloc(SIZE_63);
140+
void *ptr_64 = ::malloc(SIZE_64);
141+
142+
ASSERT_NE(ptr_63, nullptr);
143+
ASSERT_NE(ptr_64, nullptr);
144+
145+
if (ptr_63 == nullptr || ptr_64 == nullptr) {
146+
// Fix for the following CodeQL's warning on Windows:
147+
// 'ptr' could be '0': this does not adhere to the specification for the function '_msize'.
148+
return;
149+
}
80150

81151
#ifdef _WIN32
82-
_aligned_free(ptr);
152+
size_t size_63 = _msize(ptr_63);
153+
size_t size_64 = _msize(ptr_64);
154+
#elif __APPLE__
155+
size_t size_63 = ::malloc_size(ptr_63);
156+
size_t size_64 = ::malloc_size(ptr_64);
83157
#else
84-
::free(ptr);
158+
size_t size_63 = ::malloc_usable_size(ptr_63);
159+
size_t size_64 = ::malloc_usable_size(ptr_64);
85160
#endif
161+
162+
ASSERT_EQ((int)(size_63 == 0 || size_63 >= SIZE_63), 1);
163+
ASSERT_EQ((int)(size_64 == 0 || size_64 >= SIZE_64), 1);
164+
165+
::free(ptr_63);
166+
::free(ptr_64);
86167
}

0 commit comments

Comments
 (0)