|
| 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 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | + |
| 12 | +#include <umf/memory_provider.h> |
| 13 | +#include <umf/providers/provider_file_memory.h> |
| 14 | +#include <umf/providers/provider_os_memory.h> |
| 15 | + |
| 16 | +#include <umf/memory_pool.h> |
| 17 | +#include <umf/pools/pool_jemalloc.h> |
| 18 | + |
| 19 | +static umf_memory_pool_handle_t create_dram_pool() { |
| 20 | + umf_memory_provider_handle_t provider_dram = NULL; |
| 21 | + umf_memory_pool_handle_t pool_dram; |
| 22 | + umf_result_t umf_result; |
| 23 | + |
| 24 | + umf_os_memory_provider_params_t params_dram = |
| 25 | + umfOsMemoryProviderParamsDefault(); |
| 26 | + |
| 27 | + umf_result = umfMemoryProviderCreate(umfOsMemoryProviderOps(), ¶ms_dram, |
| 28 | + &provider_dram); |
| 29 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 30 | + fprintf(stderr, "Creation of the OS memory provider failed"); |
| 31 | + return NULL; |
| 32 | + } |
| 33 | + |
| 34 | + // Create a DRAM memory pool |
| 35 | + umf_result = umfPoolCreate(umfJemallocPoolOps(), provider_dram, NULL, |
| 36 | + UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &pool_dram); |
| 37 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 38 | + fprintf(stderr, "Failed to create a DRAM memory pool!\n"); |
| 39 | + umfMemoryProviderDestroy(provider_dram); |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + |
| 43 | + printf("DRAM memory pool created at %p\n", (void *)pool_dram); |
| 44 | + |
| 45 | + return pool_dram; |
| 46 | +} |
| 47 | + |
| 48 | +static umf_memory_pool_handle_t create_fsdax_pool(const char *path) { |
| 49 | + umf_memory_provider_handle_t provider_fsdax = NULL; |
| 50 | + umf_memory_pool_handle_t pool_fsdax; |
| 51 | + umf_result_t umf_result; |
| 52 | + |
| 53 | + umf_file_memory_provider_params_t params_fsdax = |
| 54 | + umfFileMemoryProviderParamsDefault(path); |
| 55 | + // FSDAX requires mapping the UMF_MEM_MAP_SYNC flag |
| 56 | + params_fsdax.visibility = UMF_MEM_MAP_SYNC; |
| 57 | + |
| 58 | + umf_result = umfMemoryProviderCreate(umfFileMemoryProviderOps(), |
| 59 | + ¶ms_fsdax, &provider_fsdax); |
| 60 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 61 | + fprintf(stderr, "failed to create the FSDAX file provider"); |
| 62 | + return NULL; |
| 63 | + } |
| 64 | + |
| 65 | + // Create a DRAM memory pool |
| 66 | + // |
| 67 | + // The file memory provider does not support the free operation |
| 68 | + // (`umfMemoryProviderFree()` always returns `UMF_RESULT_ERROR_NOT_SUPPORTED`), |
| 69 | + // so it should be used with a pool manager that will take over |
| 70 | + // the managing of the provided memory - for example the jemalloc pool |
| 71 | + // with the `disable_provider_free` parameter set to true. |
| 72 | + umf_jemalloc_pool_params_t pool_params; |
| 73 | + pool_params.disable_provider_free = true; |
| 74 | + |
| 75 | + // Create a FSDAX memory pool |
| 76 | + umf_result = |
| 77 | + umfPoolCreate(umfJemallocPoolOps(), provider_fsdax, &pool_params, |
| 78 | + UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &pool_fsdax); |
| 79 | + if (umf_result != UMF_RESULT_SUCCESS) { |
| 80 | + fprintf(stderr, "Failed to create a FSDAX memory pool!\n"); |
| 81 | + umfMemoryProviderDestroy(provider_fsdax); |
| 82 | + return NULL; |
| 83 | + } |
| 84 | + |
| 85 | + printf("FSDAX memory pool created at %p\n", (void *)pool_fsdax); |
| 86 | + |
| 87 | + return pool_fsdax; |
| 88 | +} |
| 89 | + |
| 90 | +int main(void) { |
| 91 | + int ret = -1; |
| 92 | + |
| 93 | + char *path = getenv("UMF_TESTS_FSDAX_PATH"); |
| 94 | + if (path == NULL || path[0] == 0) { |
| 95 | + fprintf(stderr, |
| 96 | + "Warning: UMF_TESTS_FSDAX_PATH is not set, skipping ...\n"); |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + umf_memory_pool_handle_t dram_pool = create_dram_pool(); |
| 101 | + if (dram_pool == NULL) { |
| 102 | + fprintf(stderr, "Failed to create a DRAM memory pool!\n"); |
| 103 | + return -1; |
| 104 | + } |
| 105 | + |
| 106 | + fprintf(stderr, "Created a DRAM memory pool\n"); |
| 107 | + |
| 108 | + umf_memory_pool_handle_t fsdax_pool = create_fsdax_pool(path); |
| 109 | + if (fsdax_pool == NULL) { |
| 110 | + fprintf(stderr, "Failed to create a FSDAX memory pool!\n"); |
| 111 | + return -1; |
| 112 | + } |
| 113 | + |
| 114 | + fprintf(stderr, "Created an FSDAX memory pool\n"); |
| 115 | + |
| 116 | + size_t size = 2 * 1024 * 1024; // == 2 MB |
| 117 | + |
| 118 | + // Allocate from the DRAM memory pool |
| 119 | + char *dram_buf = umfPoolCalloc(dram_pool, 1, size); |
| 120 | + if (dram_buf == NULL) { |
| 121 | + fprintf(stderr, |
| 122 | + "Failed to allocate memory from the DRAM memory pool!\n"); |
| 123 | + goto err_destroy_pools; |
| 124 | + } |
| 125 | + |
| 126 | + fprintf(stderr, "Allocated memory from the DRAM memory pool\n"); |
| 127 | + |
| 128 | + // Allocate from the FSDAX memory pool |
| 129 | + char *fsdax_buf = umfPoolCalloc(fsdax_pool, 1, size); |
| 130 | + if (fsdax_buf == NULL) { |
| 131 | + fprintf(stderr, |
| 132 | + "Failed to allocate memory from the FSDAX memory pool!\n"); |
| 133 | + goto err_free_dram; |
| 134 | + } |
| 135 | + |
| 136 | + fprintf(stderr, "Allocated memory from the FSDAX memory pool\n"); |
| 137 | + |
| 138 | + // Use the allocation from DRAM |
| 139 | + dram_buf[0] = '.'; |
| 140 | + |
| 141 | + // Use the allocation from FSDAX |
| 142 | + fsdax_buf[0] = '.'; |
| 143 | + |
| 144 | + // success |
| 145 | + ret = 0; |
| 146 | + |
| 147 | + // The file memory provider does not support the free() operation, |
| 148 | + // so we do need to call: umfPoolFree(fsdax_pool, fsdax_buf); |
| 149 | + |
| 150 | +err_free_dram: |
| 151 | + fprintf(stderr, "Freeing the allocation from the DRAM memory pool ...\n"); |
| 152 | + umfPoolFree(dram_pool, dram_buf); |
| 153 | + |
| 154 | +err_destroy_pools: |
| 155 | + fprintf(stderr, "Destroying the DRAM memory pool ...\n"); |
| 156 | + umfPoolDestroy(dram_pool); |
| 157 | + |
| 158 | + fprintf(stderr, "Destroying the FSDAX memory pool ...\n"); |
| 159 | + umfPoolDestroy(fsdax_pool); |
| 160 | + |
| 161 | + return ret; |
| 162 | +} |
0 commit comments