Skip to content

Commit efc409a

Browse files
committed
Add tests for the size threshold of the proxy library (Linux only)
The proxyLib_size_threshold_* tests test the size threshold of the proxy library (Linux only yet). 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: #894 Signed-off-by: Lukasz Dorau <lukasz.dorau@intel.com>
1 parent ea3599d commit efc409a

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed

.github/workflows/reusable_proxy_lib.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ jobs:
7777
working-directory: ${{env.BUILD_DIR}}
7878
run: UMF_PROXY="page.disposition=shared-shm" LD_PRELOAD=./lib/libumf_proxy.so /usr/bin/date
7979

80+
# TODO enable the provider_file_memory_ipc test when the IPC tests with the proxy library are fixed
81+
# see the issue: https://github.com/oneapi-src/unified-memory-framework/issues/864
82+
- name: Run "ctest --output-on-failure" with proxy library and size.threshold=128
83+
working-directory: ${{env.BUILD_DIR}}
84+
run: >
85+
UMF_PROXY="size.threshold=128"
86+
LD_PRELOAD=./lib/libumf_proxy.so
87+
ctest --output-on-failure -E provider_file_memory_ipc
88+
8089
- name: Check coverage
8190
if: ${{ matrix.build_type == 'Debug' }}
8291
working-directory: ${{env.BUILD_DIR}}

test/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,18 @@ if(UMF_PROXY_LIB_ENABLED AND UMF_BUILD_SHARED_LIBRARY)
414414
SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib.cpp
415415
LIBS ${UMF_UTILS_FOR_TEST} umf_proxy)
416416

417+
# TODO enable this test on Windows
418+
if(LINUX)
419+
add_umf_test(
420+
NAME test_proxy_lib_size_threshold
421+
SRCS ${BA_SOURCES_FOR_TEST} test_proxy_lib_size_threshold.cpp
422+
LIBS ${UMF_UTILS_FOR_TEST} umf_proxy)
423+
set_property(
424+
TEST umf-test_proxy_lib_size_threshold
425+
PROPERTY ENVIRONMENT
426+
UMF_PROXY="page.disposition=shared-shm;size.threshold=64")
427+
endif()
428+
417429
# the memoryPool test run with the proxy library
418430
add_umf_test(
419431
NAME proxy_lib_memoryPool
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#if defined(__APPLE__)
9+
#include <malloc/malloc.h>
10+
#else
11+
#include <malloc.h>
12+
#endif
13+
14+
#include <umf/proxy_lib_new_delete.h>
15+
16+
#include "base.hpp"
17+
#include "test_helpers.h"
18+
#include "utils_common.h"
19+
20+
using umf_test::test;
21+
22+
// size threshold defined by the env variable UMF_PROXY="size.threshold=64"
23+
#define SIZE_THRESHOLD 64
24+
#define SIZE_EQ (SIZE_THRESHOLD)
25+
#define SIZE_LT (SIZE_THRESHOLD - 1)
26+
27+
#define ALIGN_1024 1024
28+
29+
TEST_F(test, proxyLib_basic) {
30+
// a check to verify we are running the proxy library
31+
void *ptr = (void *)0x01;
32+
33+
#ifdef _WIN32
34+
size_t size = _msize(ptr);
35+
#elif __APPLE__
36+
size_t size = ::malloc_size(ptr);
37+
#else
38+
size_t size = ::malloc_usable_size(ptr);
39+
#endif
40+
41+
ASSERT_EQ(size, 0xDEADBEEF);
42+
}
43+
44+
TEST_F(test, proxyLib_realloc_size0) {
45+
// realloc(ptr, 0) == free(ptr)
46+
// realloc(ptr, 0) returns NULL
47+
ASSERT_EQ(::realloc(::malloc(SIZE_EQ), 0), nullptr);
48+
}
49+
50+
// The proxyLib_size_threshold_* tests test the size threshold of the proxy library.
51+
// The size threshold is set to SIZE_THRESHOLD bytes in this test, so all allocations of:
52+
// 1) size < SIZE_THRESHOLD go through the default system allocator
53+
// (umfPoolByPtr(ptr_size < SIZE_THRESHOLD) == nullptr)
54+
// 2) size >= SIZE_THRESHOLD go through the proxy library allocator
55+
// (umfPoolByPtr(ptr_size >= SIZE_THRESHOLD) != nullptr)
56+
57+
TEST_F(test, proxyLib_size_threshold_aligned_alloc) {
58+
#ifdef _WIN32
59+
void *ptr_LT = _aligned_malloc(SIZE_LT, ALIGN_1024);
60+
void *ptr_EQ = _aligned_malloc(SIZE_EQ, ALIGN_1024);
61+
#else
62+
void *ptr_LT = ::aligned_alloc(ALIGN_1024, SIZE_LT);
63+
void *ptr_EQ = ::aligned_alloc(ALIGN_1024, SIZE_EQ);
64+
#endif
65+
66+
ASSERT_NE(ptr_LT, nullptr);
67+
ASSERT_NE(ptr_EQ, nullptr);
68+
69+
// verify alignment
70+
ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr_LT, ALIGN_1024)), 1);
71+
ASSERT_EQ((int)(IS_ALIGNED((uintptr_t)ptr_EQ, ALIGN_1024)), 1);
72+
73+
ASSERT_EQ(umfPoolByPtr(ptr_LT), nullptr);
74+
ASSERT_NE(umfPoolByPtr(ptr_EQ), nullptr);
75+
76+
#ifdef _WIN32
77+
_aligned_free(ptr_LT);
78+
_aligned_free(ptr_EQ);
79+
#else
80+
::free(ptr_LT);
81+
::free(ptr_EQ);
82+
#endif
83+
}
84+
85+
TEST_F(test, proxyLib_size_threshold_malloc) {
86+
void *ptr_LT = malloc(SIZE_LT);
87+
void *ptr_EQ = malloc(SIZE_EQ);
88+
89+
ASSERT_NE(ptr_LT, nullptr);
90+
ASSERT_NE(ptr_EQ, nullptr);
91+
92+
ASSERT_EQ(umfPoolByPtr(ptr_LT), nullptr);
93+
ASSERT_NE(umfPoolByPtr(ptr_EQ), nullptr);
94+
95+
::free(ptr_LT);
96+
::free(ptr_EQ);
97+
}
98+
99+
TEST_F(test, proxyLib_size_threshold_calloc) {
100+
void *ptr_LT = calloc(SIZE_LT, 1);
101+
void *ptr_EQ = calloc(SIZE_EQ, 1);
102+
103+
ASSERT_NE(ptr_LT, nullptr);
104+
ASSERT_NE(ptr_EQ, nullptr);
105+
106+
ASSERT_EQ(umfPoolByPtr(ptr_LT), nullptr);
107+
ASSERT_NE(umfPoolByPtr(ptr_EQ), nullptr);
108+
109+
::free(ptr_LT);
110+
::free(ptr_EQ);
111+
}
112+
113+
TEST_F(test, proxyLib_size_threshold_realloc_up) {
114+
void *ptr_LT = malloc(SIZE_LT);
115+
void *ptr_EQ = malloc(SIZE_EQ);
116+
117+
ASSERT_NE(ptr_LT, nullptr);
118+
ASSERT_NE(ptr_EQ, nullptr);
119+
120+
void *ptr_LT_r = realloc(ptr_LT, 2 * SIZE_LT);
121+
void *ptr_EQ_r = realloc(ptr_EQ, 2 * SIZE_EQ);
122+
123+
ASSERT_NE(ptr_LT_r, nullptr);
124+
ASSERT_NE(ptr_EQ_r, nullptr);
125+
126+
ASSERT_EQ(umfPoolByPtr(ptr_LT_r), nullptr);
127+
ASSERT_NE(umfPoolByPtr(ptr_EQ_r), nullptr);
128+
129+
::free(ptr_LT_r);
130+
::free(ptr_EQ_r);
131+
}
132+
133+
TEST_F(test, proxyLib_size_threshold_realloc_down) {
134+
void *ptr_LT = malloc(SIZE_LT);
135+
void *ptr_EQ = malloc(SIZE_EQ);
136+
137+
ASSERT_NE(ptr_LT, nullptr);
138+
ASSERT_NE(ptr_EQ, nullptr);
139+
140+
void *ptr_LT_r = realloc(ptr_LT, SIZE_LT / 2);
141+
void *ptr_EQ_r = realloc(ptr_EQ, SIZE_EQ / 2);
142+
143+
ASSERT_NE(ptr_LT_r, nullptr);
144+
ASSERT_NE(ptr_EQ_r, nullptr);
145+
146+
ASSERT_EQ(umfPoolByPtr(ptr_LT_r), nullptr);
147+
ASSERT_NE(umfPoolByPtr(ptr_EQ_r), nullptr);
148+
149+
::free(ptr_LT_r);
150+
::free(ptr_EQ_r);
151+
}
152+
153+
TEST_F(test, proxyLib_size_threshold_malloc_usable_size) {
154+
155+
void *ptr_LT = ::malloc(SIZE_LT);
156+
void *ptr_EQ = ::malloc(SIZE_EQ);
157+
158+
ASSERT_NE(ptr_LT, nullptr);
159+
ASSERT_NE(ptr_EQ, nullptr);
160+
161+
if (ptr_LT == nullptr || ptr_EQ == nullptr) {
162+
// Fix for the following CodeQL's warning on Windows:
163+
// 'ptr' could be '0': this does not adhere to the specification for the function '_msize'.
164+
return;
165+
}
166+
167+
#ifdef _WIN32
168+
size_t size_LT = _msize(ptr_LT);
169+
size_t size_EQ = _msize(ptr_EQ);
170+
#elif __APPLE__
171+
size_t size_LT = ::malloc_size(ptr_LT);
172+
size_t size_EQ = ::malloc_size(ptr_EQ);
173+
#else
174+
size_t size_LT = ::malloc_usable_size(ptr_LT);
175+
size_t size_EQ = ::malloc_usable_size(ptr_EQ);
176+
#endif
177+
178+
ASSERT_EQ((int)(size_LT == 0 || size_LT >= SIZE_LT), 1);
179+
ASSERT_EQ((int)(size_EQ == 0 || size_EQ >= SIZE_EQ), 1);
180+
181+
::free(ptr_LT);
182+
::free(ptr_EQ);
183+
}

test/utils/utils_linux.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,20 @@ TEST_F(test, utils_shm_create_invalid_args) {
6060
TEST_F(test, utils_get_size_threshold) {
6161
// Expected input to utils_get_size_threshold():
6262
// char *str_threshold = utils_env_var_get_str("UMF_PROXY", "size.threshold=");
63+
6364
// positive tests
6465
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=111"), 111);
6566
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=222;abcd"), 222);
6667
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=333;var=value"),
6768
333);
69+
// LONG_MAX = 9223372036854775807
70+
EXPECT_EQ(utils_get_size_threshold(
71+
(char *)"size.threshold=9223372036854775807;var=value"),
72+
9223372036854775807);
73+
6874
// negative tests
6975
EXPECT_EQ(utils_get_size_threshold(NULL), 0);
76+
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold="), -1);
7077
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=abc"), -1);
7178
EXPECT_EQ(utils_get_size_threshold((char *)"size.threshold=-111"), -1);
7279
}

0 commit comments

Comments
 (0)