|
2 | 2 | // Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
|
3 | 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
4 | 4 |
|
5 |
| -#include "base.hpp" |
| 5 | +#include "memory_target_numa.h" |
| 6 | +#include "memspace_helpers.hpp" |
| 7 | +#include "memspace_internal.h" |
6 | 8 | #include "test_helpers.h"
|
7 | 9 |
|
| 10 | +#include <numa.h> |
| 11 | +#include <numaif.h> |
8 | 12 | #include <umf/memspace.h>
|
| 13 | +#include <unordered_set> |
9 | 14 |
|
10 | 15 | using umf_test::test;
|
11 | 16 |
|
12 |
| -#define SIZE_4K (4096) |
13 |
| - |
14 |
| -struct predefinedMemspaceTest : test { |
15 |
| - |
16 |
| - void SetUp() override { |
17 |
| - ::test::SetUp(); |
18 |
| - |
19 |
| - hMemspace = umfMemspaceHostAllGet(); |
20 |
| - UT_ASSERTne(hMemspace, nullptr); |
21 |
| - } |
22 |
| - |
23 |
| - umf_memspace_handle_t hMemspace; |
24 |
| -}; |
25 |
| - |
26 |
| -struct predefinedMemspaceProviderTest : predefinedMemspaceTest { |
27 |
| - |
28 |
| - void SetUp() override { |
29 |
| - ::predefinedMemspaceTest::SetUp(); |
30 |
| - |
31 |
| - enum umf_result_t ret = |
32 |
| - umfMemoryProviderCreateFromMemspace(hMemspace, nullptr, &provider); |
33 |
| - UT_ASSERTeq(ret, UMF_RESULT_SUCCESS); |
34 |
| - UT_ASSERTne(provider, nullptr); |
35 |
| - } |
36 |
| - |
37 |
| - void TearDown() override { |
38 |
| - ::predefinedMemspaceTest::TearDown(); |
| 17 | +TEST_F(numaNodesTest, memspaceGet) { |
| 18 | + umf_memspace_handle_t hMemspace = umfMemspaceHostAllGet(); |
| 19 | + UT_ASSERTne(hMemspace, nullptr); |
39 | 20 |
|
40 |
| - umfMemoryProviderDestroy(provider); |
| 21 | + // Confirm that the HOST ALL memspace is composed of all available NUMA nodes. |
| 22 | + UT_ASSERTeq(hMemspace->size, nodeIds.size()); |
| 23 | + for (size_t i = 0; i < hMemspace->size; i++) { |
| 24 | + // NUMA memory target internally casts the config directly into priv. |
| 25 | + // TODO: Use the memory target API when it becomes available. |
| 26 | + struct umf_numa_memory_target_config_t *numaTargetCfg = |
| 27 | + (struct umf_numa_memory_target_config_t *)hMemspace->nodes[i]->priv; |
| 28 | + UT_ASSERT(std::find(nodeIds.begin(), nodeIds.end(), |
| 29 | + numaTargetCfg->id) != nodeIds.end()); |
41 | 30 | }
|
| 31 | +} |
42 | 32 |
|
43 |
| - umf_memory_provider_handle_t provider = nullptr; |
44 |
| -}; |
| 33 | +TEST_F(memspaceHostAllTest, providerFromHostAllMemspace) { |
| 34 | + umf_memory_provider_handle_t hProvider = nullptr; |
| 35 | + enum umf_result_t ret = |
| 36 | + umfMemoryProviderCreateFromMemspace(hMemspace, nullptr, &hProvider); |
| 37 | + UT_ASSERTeq(ret, UMF_RESULT_SUCCESS); |
| 38 | + UT_ASSERTne(hProvider, nullptr); |
45 | 39 |
|
46 |
| -TEST_F(test, memspaceGet) { |
47 |
| - umf_memspace_handle_t hMemspace = umfMemspaceHostAllGet(); |
48 |
| - UT_ASSERTne(hMemspace, nullptr); |
| 40 | + umfMemoryProviderDestroy(hProvider); |
49 | 41 | }
|
50 | 42 |
|
51 |
| -TEST_F(predefinedMemspaceProviderTest, allocFree) { |
| 43 | +TEST_F(memspaceHostAllProviderTest, allocFree) { |
52 | 44 | void *ptr = nullptr;
|
53 | 45 | size_t size = SIZE_4K;
|
54 | 46 | size_t alignment = 0;
|
55 | 47 |
|
56 | 48 | enum umf_result_t ret =
|
57 |
| - umfMemoryProviderAlloc(provider, size, alignment, &ptr); |
| 49 | + umfMemoryProviderAlloc(hProvider, size, alignment, &ptr); |
58 | 50 | UT_ASSERTeq(ret, UMF_RESULT_SUCCESS);
|
59 | 51 | UT_ASSERTne(ptr, nullptr);
|
60 | 52 |
|
61 | 53 | memset(ptr, 0xFF, size);
|
62 | 54 |
|
63 |
| - ret = umfMemoryProviderFree(provider, ptr, size); |
| 55 | + ret = umfMemoryProviderFree(hProvider, ptr, size); |
64 | 56 | UT_ASSERTeq(ret, UMF_RESULT_SUCCESS);
|
65 | 57 | }
|
| 58 | + |
| 59 | +/// |
| 60 | +/// @brief Retrieves the memory policy information for \p ptr. |
| 61 | +/// @param ptr allocation pointer. |
| 62 | +/// @param maxNodeId maximum node id. |
| 63 | +/// @param mode [out] memory policy. |
| 64 | +/// @param boundNodeIds [out] node ids associated with the policy. |
| 65 | +/// @param allocNodeId [out] id of the node that allocated the memory. |
| 66 | +/// |
| 67 | +static void getAllocationPolicy(void *ptr, unsigned long maxNodeId, int &mode, |
| 68 | + std::vector<size_t> &boundNodeIds, |
| 69 | + size_t &allocNodeId) { |
| 70 | + const static unsigned bitsPerUlong = sizeof(unsigned long) * 8; |
| 71 | + |
| 72 | + const unsigned nrUlongs = (maxNodeId + bitsPerUlong) / bitsPerUlong; |
| 73 | + std::vector<unsigned long> memNodeMasks(nrUlongs, 0); |
| 74 | + |
| 75 | + int memMode = -1; |
| 76 | + // Get policy and the nodes associated with this policy. |
| 77 | + int ret = get_mempolicy(&memMode, memNodeMasks.data(), |
| 78 | + nrUlongs * bitsPerUlong, ptr, MPOL_F_ADDR); |
| 79 | + UT_ASSERTeq(ret, 0); |
| 80 | + mode = memMode; |
| 81 | + |
| 82 | + UT_ASSERTeq(boundNodeIds.size(), 0); |
| 83 | + for (size_t i = 0; i <= maxNodeId; i++) { |
| 84 | + const size_t memNodeMaskIdx = ((i + bitsPerUlong) / bitsPerUlong) - 1; |
| 85 | + const auto &memNodeMask = memNodeMasks.at(memNodeMaskIdx); |
| 86 | + |
| 87 | + if (memNodeMask && (1UL << (i % bitsPerUlong))) { |
| 88 | + boundNodeIds.emplace_back(i); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Get the node that allocated the memory at 'ptr'. |
| 93 | + int nodeId = -1; |
| 94 | + ret = get_mempolicy(&nodeId, nullptr, 0, ptr, MPOL_F_ADDR | MPOL_F_NODE); |
| 95 | + UT_ASSERTeq(ret, 0); |
| 96 | + allocNodeId = static_cast<size_t>(nodeId); |
| 97 | +} |
| 98 | + |
| 99 | +TEST_F(memspaceHostAllProviderTest, memoryPolicyOOM) { |
| 100 | + // Arbitrary allocation size, should be big enough to avoid unnecessarily |
| 101 | + // prolonging the test execution. |
| 102 | + size_t size = SIZE_4M * 128; |
| 103 | + size_t alignment = 0; |
| 104 | + std::vector<void *> allocs; |
| 105 | + |
| 106 | + enum umf_result_t umf_ret = UMF_RESULT_SUCCESS; |
| 107 | + // Create allocations until OOM. |
| 108 | + while (true) { |
| 109 | + void *ptr = nullptr; |
| 110 | + umf_ret = umfMemoryProviderAlloc(hProvider, size, alignment, &ptr); |
| 111 | + if (umf_ret != UMF_RESULT_SUCCESS) { |
| 112 | + break; |
| 113 | + } |
| 114 | + |
| 115 | + UT_ASSERTne(ptr, nullptr); |
| 116 | + allocs.push_back(ptr); |
| 117 | + } |
| 118 | + |
| 119 | + UT_ASSERTeq(umf_ret, UMF_RESULT_ERROR_MEMORY_PROVIDER_SPECIFIC); |
| 120 | + const char *msg = nullptr; |
| 121 | + int32_t err = 0; |
| 122 | + umfMemoryProviderGetLastNativeError(hProvider, &msg, &err); |
| 123 | + // In this scenario, 'UMF_OS_RESULT_ERROR_ALLOC_FAILED' indicates OOM. |
| 124 | + UT_ASSERTeq(err, UMF_OS_RESULT_ERROR_ALLOC_FAILED); |
| 125 | + |
| 126 | + // When allocating until OOM, the allocations should be distributed across |
| 127 | + // all the NUMA nodes bound to 'HOST ALL' memspace, until each node runs |
| 128 | + // out of memory. |
| 129 | + UT_ASSERT(allocs.size() >= nodeIds.size()); |
| 130 | + std::unordered_set<size_t> allocNodeIds; |
| 131 | + for (auto &ptr : allocs) { |
| 132 | + int mode = -1; |
| 133 | + std::vector<size_t> boundNodeIds; |
| 134 | + size_t allocNodeId = SIZE_MAX; |
| 135 | + getAllocationPolicy(ptr, maxNodeId, mode, boundNodeIds, allocNodeId); |
| 136 | + |
| 137 | + // 'BIND' mode specifies that the memory is bound to a set of NUMA nodes. |
| 138 | + // In case of 'HOST ALL' memspace, those set of nodes should be all |
| 139 | + // available nodes. |
| 140 | + UT_ASSERTeq(mode, MPOL_BIND); |
| 141 | + |
| 142 | + // Confirm that the memory is bound to all the nodes from 'HOST ALL' |
| 143 | + // memspace. |
| 144 | + for (auto &id : nodeIds) { |
| 145 | + auto it = std::find(boundNodeIds.begin(), boundNodeIds.end(), id); |
| 146 | + UT_ASSERT(it != boundNodeIds.end()); |
| 147 | + } |
| 148 | + |
| 149 | + // Confirm that the memory is allocated on one of the nodes in |
| 150 | + // 'HOST ALL' memspace. |
| 151 | + auto it = std::find(nodeIds.begin(), nodeIds.end(), allocNodeId); |
| 152 | + UT_ASSERT(it != nodeIds.end()); |
| 153 | + |
| 154 | + allocNodeIds.insert(allocNodeId); |
| 155 | + |
| 156 | + umf_ret = umfMemoryProviderFree(hProvider, ptr, size); |
| 157 | + UT_ASSERTeq(umf_ret, UMF_RESULT_SUCCESS); |
| 158 | + } |
| 159 | + |
| 160 | + // Confirm that all the NUMA nodes bound to 'HOST ALL' memspace were exhausted. |
| 161 | + for (auto &id : nodeIds) { |
| 162 | + auto it = std::find(allocNodeIds.begin(), allocNodeIds.end(), id); |
| 163 | + UT_ASSERT(it != allocNodeIds.end()); |
| 164 | + } |
| 165 | +} |
0 commit comments