-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathprovider_os_memory.cpp
484 lines (391 loc) · 17.1 KB
/
provider_os_memory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// Copyright (C) 2023-2025 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "base.hpp"
#include "cpp_helpers.hpp"
#include "ipcFixtures.hpp"
#include "test_helpers.h"
#include <umf/memory_provider.h>
#include <umf/pools/pool_disjoint.h>
#include <umf/providers/provider_os_memory.h>
#ifdef UMF_POOL_JEMALLOC_ENABLED
#include <umf/pools/pool_jemalloc.h>
#endif
#ifdef UMF_POOL_SCALABLE_ENABLED
#include <umf/pools/pool_scalable.h>
#endif
using umf_test::test;
#define INVALID_PTR ((void *)0x01)
typedef enum purge_t {
PURGE_NONE = 0,
PURGE_LAZY = 1,
PURGE_FORCE = 2,
} purge_t;
static const char *Native_error_str[] = {
"success", // UMF_OS_RESULT_SUCCESS
"memory allocation failed", // UMF_OS_RESULT_ERROR_ALLOC_FAILED
"allocated address is not aligned", // UMF_OS_RESULT_ERROR_ADDRESS_NOT_ALIGNED
"binding memory to NUMA node failed", // UMF_OS_RESULT_ERROR_BIND_FAILED
"memory deallocation failed", // UMF_OS_RESULT_ERROR_FREE_FAILED
"lazy purging failed", // UMF_OS_RESULT_ERROR_PURGE_LAZY_FAILED
"force purging failed", // UMF_OS_RESULT_ERROR_PURGE_FORCE_FAILED
"HWLOC topology discovery failed", // UMF_OS_RESULT_ERROR_TOPO_DISCOVERY_FAILED
};
// test helpers
static int compare_native_error_str(const char *message, int error) {
const char *error_str = Native_error_str[error - UMF_OS_RESULT_SUCCESS];
size_t len = strlen(error_str);
return strncmp(message, error_str, len);
}
using providerCreateExtParams = std::tuple<umf_memory_provider_ops_t *, void *>;
static void providerCreateExt(providerCreateExtParams params,
umf::provider_unique_handle_t *handle) {
umf_memory_provider_handle_t hProvider = nullptr;
auto [provider_ops, provider_params] = params;
auto ret =
umfMemoryProviderCreate(provider_ops, provider_params, &hProvider);
ASSERT_EQ(ret, UMF_RESULT_SUCCESS);
ASSERT_NE(hProvider, nullptr);
*handle =
umf::provider_unique_handle_t(hProvider, &umfMemoryProviderDestroy);
}
struct umfProviderTest
: umf_test::test,
::testing::WithParamInterface<providerCreateExtParams> {
void SetUp() override {
test::SetUp();
providerCreateExt(this->GetParam(), &provider);
umf_result_t umf_result =
umfMemoryProviderGetMinPageSize(provider.get(), NULL, &page_size);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
page_plus_64 = page_size + 64;
}
void TearDown() override { test::TearDown(); }
umf::provider_unique_handle_t provider;
size_t page_size;
size_t page_plus_64;
};
static void test_alloc_free_success(umf_memory_provider_handle_t provider,
size_t size, size_t alignment,
purge_t purge) {
void *ptr = nullptr;
umf_result_t umf_result =
umfMemoryProviderAlloc(provider, size, alignment, &ptr);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
ASSERT_NE(ptr, nullptr);
memset(ptr, 0xFF, size);
if (purge == PURGE_LAZY) {
umf_result = umfMemoryProviderPurgeLazy(provider, ptr, size);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
} else if (purge == PURGE_FORCE) {
umf_result = umfMemoryProviderPurgeForce(provider, ptr, size);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
umf_result = umfMemoryProviderFree(provider, ptr, size);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
static void verify_last_native_error(umf_memory_provider_handle_t provider,
int32_t err) {
const char *message;
int32_t error;
umfMemoryProviderGetLastNativeError(provider, &message, &error);
ASSERT_EQ(error, err);
ASSERT_EQ(compare_native_error_str(message, error), 0);
}
static void test_alloc_failure(umf_memory_provider_handle_t provider,
size_t size, size_t alignment,
umf_result_t result, int32_t err) {
void *ptr = nullptr;
umf_result_t umf_result =
umfMemoryProviderAlloc(provider, size, alignment, &ptr);
ASSERT_EQ(umf_result, result);
ASSERT_EQ(ptr, nullptr);
if (umf_result == UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC) {
verify_last_native_error(provider, err);
}
}
// TESTS
// negative tests for umfMemoryProviderCreate()
static umf_result_t create_os_provider_with_mode(umf_numa_mode_t mode,
unsigned *node_list,
unsigned node_list_size) {
umf_result_t umf_result;
umf_memory_provider_handle_t os_memory_provider = nullptr;
umf_os_memory_provider_params_handle_t os_memory_provider_params = nullptr;
umf_result = umfOsMemoryProviderParamsCreate(&os_memory_provider_params);
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
umf_result =
umfOsMemoryProviderParamsSetNumaMode(os_memory_provider_params, mode);
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
umf_result = umfOsMemoryProviderParamsSetNumaList(
os_memory_provider_params, node_list, node_list_size);
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
umf_result =
umfMemoryProviderCreate(umfOsMemoryProviderOps(),
os_memory_provider_params, &os_memory_provider);
umfOsMemoryProviderParamsDestroy(os_memory_provider_params);
if (umf_result == UMF_RESULT_SUCCESS) {
EXPECT_NE(os_memory_provider, nullptr);
umfMemoryProviderDestroy(os_memory_provider);
} else {
EXPECT_EQ(os_memory_provider, nullptr);
}
return umf_result;
}
static unsigned valid_list = 0x1;
static unsigned long valid_list_len = 1;
TEST_F(test, create_WRONG_NUMA_MODE_DEFAULT) {
auto ret = create_os_provider_with_mode(UMF_NUMA_MODE_DEFAULT, &valid_list,
valid_list_len);
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
TEST_F(test, create_WRONG_NUMA_MODE_LOCAL) {
auto ret = create_os_provider_with_mode(UMF_NUMA_MODE_LOCAL, &valid_list,
valid_list_len);
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
TEST_F(test, create_WRONG_NUMA_MODE_BIND) {
auto ret = create_os_provider_with_mode(UMF_NUMA_MODE_BIND, nullptr, 0);
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
TEST_F(test, create_WRONG_NUMA_MODE_INTERLEAVE) {
auto ret =
create_os_provider_with_mode(UMF_NUMA_MODE_INTERLEAVE, nullptr, 0);
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
TEST_F(test, create_WRONG_NUMA_MODE_SPLIT) {
auto ret = create_os_provider_with_mode(UMF_NUMA_MODE_SPLIT, nullptr, 0);
ASSERT_EQ(ret, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
TEST_F(test, create_ZERO_WEIGHT_PARTITION) {
umf_numa_split_partition_t p = {0, 0};
umf_result_t umf_result;
umf_memory_provider_handle_t os_memory_provider = nullptr;
umf_os_memory_provider_params_handle_t os_memory_provider_params = NULL;
umf_result = umfOsMemoryProviderParamsCreate(&os_memory_provider_params);
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
umf_result = umfOsMemoryProviderParamsSetNumaMode(os_memory_provider_params,
UMF_NUMA_MODE_SPLIT);
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
umf_result = umfOsMemoryProviderParamsSetNumaList(
os_memory_provider_params, &valid_list, valid_list_len);
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
umf_result = umfOsMemoryProviderParamsSetPartitions(
os_memory_provider_params, &p, 1);
EXPECT_EQ(umf_result, UMF_RESULT_SUCCESS);
umf_result = umfMemoryProviderCreate(umfOsMemoryProviderOps(),
&os_memory_provider_params,
&os_memory_provider);
umfOsMemoryProviderParamsDestroy(os_memory_provider_params);
EXPECT_EQ(os_memory_provider, nullptr);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
// positive tests using test_alloc_free_success
using os_params_unique_handle_t =
std::unique_ptr<umf_os_memory_provider_params_t,
decltype(&umfOsMemoryProviderParamsDestroy)>;
os_params_unique_handle_t createOsMemoryProviderParams() {
umf_os_memory_provider_params_handle_t params = nullptr;
umf_result_t res = umfOsMemoryProviderParamsCreate(¶ms);
if (res != UMF_RESULT_SUCCESS) {
throw std::runtime_error("Failed to create os memory provider params");
}
return os_params_unique_handle_t(params, &umfOsMemoryProviderParamsDestroy);
}
auto defaultParams = createOsMemoryProviderParams();
INSTANTIATE_TEST_SUITE_P(osProviderTest, umfProviderTest,
::testing::Values(providerCreateExtParams{
umfOsMemoryProviderOps(), defaultParams.get()}));
TEST_P(umfProviderTest, create_destroy) {}
TEST_P(umfProviderTest, alloc_page64_align_0) {
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_NONE);
}
TEST_P(umfProviderTest, alloc_page64_align_page_div_2) {
test_alloc_free_success(provider.get(), page_plus_64, page_size / 2,
PURGE_NONE);
}
TEST_P(umfProviderTest, alloc_page64_align_3_pages) {
test_alloc_free_success(provider.get(), page_plus_64, 3 * page_size,
PURGE_NONE);
}
TEST_P(umfProviderTest, alloc_3pages_align_3pages) {
test_alloc_free_success(provider.get(), 3 * page_size, 3 * page_size,
PURGE_NONE);
}
TEST_P(umfProviderTest, purge_lazy) {
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_LAZY);
}
TEST_P(umfProviderTest, purge_force) {
test_alloc_free_success(provider.get(), page_plus_64, 0, PURGE_FORCE);
}
// negative tests using test_alloc_failure
TEST_P(umfProviderTest, alloc_page64_align_page_minus_1_WRONG_ALIGNMENT_1) {
test_alloc_failure(provider.get(), page_plus_64, page_size - 1,
UMF_RESULT_ERROR_INVALID_ARGUMENT, 0);
}
TEST_P(umfProviderTest, alloc_page64_align_one_half_pages_WRONG_ALIGNMENT_2) {
test_alloc_failure(provider.get(), page_plus_64,
page_size + (page_size / 2),
UMF_RESULT_ERROR_INVALID_ARGUMENT, 0);
}
TEST_P(umfProviderTest, alloc_WRONG_SIZE) {
test_alloc_failure(provider.get(), -1, 0,
UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC,
UMF_OS_RESULT_ERROR_ALLOC_FAILED);
}
TEST_P(umfProviderTest, alloc_MAX_SIZE) {
test_alloc_failure(provider.get(), SIZE_MAX, 0,
UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC,
UMF_OS_RESULT_ERROR_ALLOC_FAILED);
}
// other positive tests
TEST_P(umfProviderTest, get_min_page_size) {
size_t min_page_size;
umf_result_t umf_result = umfMemoryProviderGetMinPageSize(
provider.get(), nullptr, &min_page_size);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
ASSERT_LE(min_page_size, page_size);
}
TEST_P(umfProviderTest, get_recommended_page_size) {
size_t min_page_size;
umf_result_t umf_result = umfMemoryProviderGetMinPageSize(
provider.get(), nullptr, &min_page_size);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
ASSERT_LE(min_page_size, page_size);
size_t recommended_page_size;
umf_result = umfMemoryProviderGetRecommendedPageSize(
provider.get(), 0, &recommended_page_size);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
ASSERT_GE(recommended_page_size, min_page_size);
}
TEST_P(umfProviderTest, get_name) {
const char *name = umfMemoryProviderGetName(provider.get());
ASSERT_STREQ(name, "OS");
}
TEST_P(umfProviderTest, free_size_0_ptr_not_null) {
umf_result_t umf_result =
umfMemoryProviderFree(provider.get(), INVALID_PTR, 0);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC);
}
TEST_P(umfProviderTest, free_NULL) {
umf_result_t umf_result = umfMemoryProviderFree(provider.get(), nullptr, 0);
ASSERT_EQ(umf_result, UMF_RESULT_SUCCESS);
}
// other negative tests
TEST_P(umfProviderTest, free_INVALID_POINTER_SIZE_GT_0) {
umf_result_t umf_result =
umfMemoryProviderFree(provider.get(), INVALID_PTR, page_plus_64);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC);
verify_last_native_error(provider.get(), UMF_OS_RESULT_ERROR_FREE_FAILED);
}
TEST_P(umfProviderTest, purge_lazy_INVALID_POINTER) {
umf_result_t umf_result =
umfMemoryProviderPurgeLazy(provider.get(), INVALID_PTR, 1);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC);
verify_last_native_error(provider.get(),
UMF_OS_RESULT_ERROR_PURGE_LAZY_FAILED);
}
TEST_P(umfProviderTest, purge_force_INVALID_POINTER) {
umf_result_t umf_result =
umfMemoryProviderPurgeForce(provider.get(), INVALID_PTR, 1);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC);
verify_last_native_error(provider.get(),
UMF_OS_RESULT_ERROR_PURGE_FORCE_FAILED);
}
TEST_P(umfProviderTest, get_ipc_handle_size_wrong_visibility) {
size_t size;
umf_result_t umf_result =
umfMemoryProviderGetIPCHandleSize(provider.get(), &size);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
TEST_P(umfProviderTest, get_ipc_handle_wrong_visibility) {
char providerIpcData;
umf_result_t umf_result = umfMemoryProviderGetIPCHandle(
provider.get(), INVALID_PTR, 1, &providerIpcData);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
TEST_P(umfProviderTest, put_ipc_handle_wrong_visibility) {
char providerIpcData;
umf_result_t umf_result =
umfMemoryProviderPutIPCHandle(provider.get(), &providerIpcData);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
TEST_P(umfProviderTest, open_ipc_handle_wrong_visibility) {
char providerIpcData;
void *ptr;
umf_result_t umf_result =
umfMemoryProviderOpenIPCHandle(provider.get(), &providerIpcData, &ptr);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
TEST_P(umfProviderTest, close_ipc_handle_wrong_visibility) {
umf_result_t umf_result =
umfMemoryProviderCloseIPCHandle(provider.get(), INVALID_PTR, 1);
ASSERT_EQ(umf_result, UMF_RESULT_ERROR_INVALID_ARGUMENT);
}
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(umfIpcTest);
void *createOsMemoryProviderParamsShared() {
umf_os_memory_provider_params_handle_t params = nullptr;
umf_result_t res = umfOsMemoryProviderParamsCreate(¶ms);
if (res != UMF_RESULT_SUCCESS) {
throw std::runtime_error("Failed to create os memory provider params");
}
res = umfOsMemoryProviderParamsSetVisibility(params, UMF_MEM_MAP_SHARED);
if (res != UMF_RESULT_SUCCESS) {
throw std::runtime_error("Failed to set protection");
}
return params;
}
umf_result_t destroyOsMemoryProviderParamsShared(void *params) {
return umfOsMemoryProviderParamsDestroy(
static_cast<umf_os_memory_provider_params_handle_t>(params));
}
HostMemoryAccessor hostAccessor;
void *createDisjointPoolParams() {
umf_disjoint_pool_params_handle_t params = nullptr;
umf_result_t res = umfDisjointPoolParamsCreate(¶ms);
if (res != UMF_RESULT_SUCCESS) {
throw std::runtime_error("Failed to create pool params");
}
res = umfDisjointPoolParamsSetSlabMinSize(params, 4096);
if (res != UMF_RESULT_SUCCESS) {
umfDisjointPoolParamsDestroy(params);
throw std::runtime_error("Failed to set slab min size");
}
res = umfDisjointPoolParamsSetMaxPoolableSize(params, 4096);
if (res != UMF_RESULT_SUCCESS) {
umfDisjointPoolParamsDestroy(params);
throw std::runtime_error("Failed to set max poolable size");
}
res = umfDisjointPoolParamsSetCapacity(params, 4);
if (res != UMF_RESULT_SUCCESS) {
umfDisjointPoolParamsDestroy(params);
throw std::runtime_error("Failed to set capacity");
}
res = umfDisjointPoolParamsSetMinBucketSize(params, 64);
if (res != UMF_RESULT_SUCCESS) {
umfDisjointPoolParamsDestroy(params);
throw std::runtime_error("Failed to set min bucket size");
}
return params;
}
umf_result_t destroyDisjointPoolParams(void *params) {
return umfDisjointPoolParamsDestroy(
static_cast<umf_disjoint_pool_params_handle_t>(params));
}
static std::vector<ipcTestParams> ipcTestParamsList = {
{umfDisjointPoolOps(), createDisjointPoolParams, destroyDisjointPoolParams,
umfOsMemoryProviderOps(), createOsMemoryProviderParamsShared,
destroyOsMemoryProviderParamsShared, &hostAccessor},
#ifdef UMF_POOL_JEMALLOC_ENABLED
{umfJemallocPoolOps(), nullptr, nullptr, umfOsMemoryProviderOps(),
createOsMemoryProviderParamsShared, destroyOsMemoryProviderParamsShared,
&hostAccessor},
#endif
#ifdef UMF_POOL_SCALABLE_ENABLED
{umfScalablePoolOps(), nullptr, nullptr, umfOsMemoryProviderOps(),
createOsMemoryProviderParamsShared, destroyOsMemoryProviderParamsShared,
&hostAccessor},
#endif
};
INSTANTIATE_TEST_SUITE_P(osProviderTest, umfIpcTest,
::testing::ValuesIn(ipcTestParamsList));