Skip to content

Commit 941b6b6

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 941b6b6

File tree

3 files changed

+140
-28
lines changed

3 files changed

+140
-28
lines changed

.github/workflows/reusable_proxy_lib.yml

Lines changed: 10 additions & 2 deletions
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
71+
${{ matrix.proxy_lib_pool == 'JEMALLOC' && '-E "provider_file_memory_ipc|proxy_lib_basic"' || '-E provider_file_memory_ipc' }}
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
88-
ctest --output-on-failure -E provider_file_memory_ipc
95+
ctest --output-on-failure
96+
${{ matrix.proxy_lib_pool == 'JEMALLOC' && '-E "provider_file_memory_ipc|proxy_lib_basic"' || '-E provider_file_memory_ipc' }}
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: 128 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,150 @@ 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+
ASSERT_NE(ptr_63, nullptr);
62+
ASSERT_NE(ptr_64, nullptr);
6863

69-
::free(ptr);
70-
}
64+
// verify alignment
65+
ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr_63, ALIGN_1024)), 1);
66+
ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr_64, ALIGN_1024)), 1);
67+
68+
// umfPoolByPtr(ptr_size_63) == nullptr
69+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
70+
// umfPoolByPtr(ptr_size_64) != nullptr
71+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
7172

72-
TEST_F(test, proxyLib_aligned_alloc) {
7373
#ifdef _WIN32
74-
void *ptr = _aligned_malloc(SIZE_64, ALIGN_1024);
74+
_aligned_free(ptr_63);
75+
_aligned_free(ptr_64);
7576
#else
76-
void *ptr = ::aligned_alloc(ALIGN_1024, SIZE_64);
77+
::free(ptr_63);
78+
::free(ptr_64);
7779
#endif
80+
}
81+
82+
TEST_F(test, proxyLib_size_threshold_malloc) {
83+
void *ptr_63 = malloc(SIZE_63);
84+
void *ptr_64 = malloc(SIZE_64);
85+
86+
ASSERT_NE(ptr_63, nullptr);
87+
ASSERT_NE(ptr_64, nullptr);
88+
89+
// umfPoolByPtr(ptr_size_63) == nullptr
90+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
91+
// umfPoolByPtr(ptr_size_64) != nullptr
92+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
93+
94+
::free(ptr_63);
95+
::free(ptr_64);
96+
}
97+
98+
TEST_F(test, proxyLib_size_threshold_calloc) {
99+
void *ptr_63 = calloc(SIZE_63, 1);
100+
void *ptr_64 = calloc(SIZE_64, 1);
101+
102+
ASSERT_NE(ptr_63, nullptr);
103+
ASSERT_NE(ptr_64, nullptr);
104+
105+
// umfPoolByPtr(ptr_size_63) == nullptr
106+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
107+
// umfPoolByPtr(ptr_size_64) != nullptr
108+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
109+
110+
::free(ptr_63);
111+
::free(ptr_64);
112+
}
78113

79-
ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr, ALIGN_1024)), 1);
114+
TEST_F(test, proxyLib_size_threshold_realloc_up) {
115+
void *ptr_63 = malloc(SIZE_63);
116+
void *ptr_64 = malloc(SIZE_64);
117+
118+
ASSERT_NE(ptr_63, nullptr);
119+
ASSERT_NE(ptr_64, nullptr);
120+
121+
ptr_63 = realloc(ptr_63, 2 * SIZE_63);
122+
ptr_64 = realloc(ptr_64, 2 * SIZE_64);
123+
124+
ASSERT_NE(ptr_63, nullptr);
125+
ASSERT_NE(ptr_64, nullptr);
126+
127+
// umfPoolByPtr(ptr_size_63) == nullptr
128+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
129+
// umfPoolByPtr(ptr_size_64) != nullptr
130+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
131+
132+
::free(ptr_63);
133+
::free(ptr_64);
134+
}
135+
136+
TEST_F(test, proxyLib_size_threshold_realloc_down) {
137+
void *ptr_63 = malloc(SIZE_63);
138+
void *ptr_64 = malloc(SIZE_64);
139+
140+
ASSERT_NE(ptr_63, nullptr);
141+
ASSERT_NE(ptr_64, nullptr);
142+
143+
ptr_63 = realloc(ptr_63, SIZE_63 / 2);
144+
ptr_64 = realloc(ptr_64, SIZE_64 / 2);
145+
146+
ASSERT_NE(ptr_63, nullptr);
147+
ASSERT_NE(ptr_64, nullptr);
148+
149+
// umfPoolByPtr(ptr_size_63) == nullptr
150+
ASSERT_EQ(umfPoolByPtr(ptr_63), nullptr);
151+
// umfPoolByPtr(ptr_size_64) != nullptr
152+
ASSERT_NE(umfPoolByPtr(ptr_64), nullptr);
153+
154+
::free(ptr_63);
155+
::free(ptr_64);
156+
}
157+
158+
TEST_F(test, proxyLib_size_threshold_malloc_usable_size) {
159+
160+
void *ptr_63 = ::malloc(SIZE_63);
161+
void *ptr_64 = ::malloc(SIZE_64);
162+
163+
ASSERT_NE(ptr_63, nullptr);
164+
ASSERT_NE(ptr_64, nullptr);
165+
166+
if (ptr_63 == nullptr || ptr_64 == nullptr) {
167+
// Fix for the following CodeQL's warning on Windows:
168+
// 'ptr' could be '0': this does not adhere to the specification for the function '_msize'.
169+
return;
170+
}
80171

81172
#ifdef _WIN32
82-
_aligned_free(ptr);
173+
size_t size_63 = _msize(ptr_63);
174+
size_t size_64 = _msize(ptr_64);
175+
#elif __APPLE__
176+
size_t size_63 = ::malloc_size(ptr_63);
177+
size_t size_64 = ::malloc_size(ptr_64);
83178
#else
84-
::free(ptr);
179+
size_t size_63 = ::malloc_usable_size(ptr_63);
180+
size_t size_64 = ::malloc_usable_size(ptr_64);
85181
#endif
182+
183+
ASSERT_EQ((int)(size_63 == 0 || size_63 >= SIZE_63), 1);
184+
ASSERT_EQ((int)(size_64 == 0 || size_64 >= SIZE_64), 1);
185+
186+
::free(ptr_63);
187+
::free(ptr_64);
86188
}

0 commit comments

Comments
 (0)