|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2023 Intel Corporation |
| 4 | + * |
| 5 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 6 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <assert.h> |
| 11 | +#include <dlfcn.h> |
| 12 | +#include <pthread.h> |
| 13 | +#include <stdbool.h> |
| 14 | +#include <stdint.h> |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | +#include <string.h> |
| 18 | + |
| 19 | +#include "umf/pools/pool_tbb.h" |
| 20 | +#include <umf/memory_pool.h> |
| 21 | +#include <umf/memory_pool_ops.h> |
| 22 | +#include <umf/memory_provider.h> |
| 23 | + |
| 24 | +#include "common.h" |
| 25 | + |
| 26 | +typedef void *(*raw_alloc_tbb_type)(intptr_t, size_t *); |
| 27 | +typedef void (*raw_free_tbb_type)(intptr_t, void *, size_t); |
| 28 | + |
| 29 | +static __TLS umf_result_t TLS_last_allocation_error; |
| 30 | + |
| 31 | +struct mem_pool_policy_s { |
| 32 | + raw_alloc_tbb_type pAlloc; |
| 33 | + raw_free_tbb_type pFree; |
| 34 | + size_t granularity; |
| 35 | + int version; |
| 36 | + unsigned fixed_pool : 1, keep_all_memory : 1, reserved : 30; |
| 37 | +}; |
| 38 | + |
| 39 | +struct tbb_callbacks { |
| 40 | + void *(*pool_malloc)(void *, size_t); |
| 41 | + void *(*pool_realloc)(void *, void *, size_t); |
| 42 | + void *(*pool_aligned_malloc)(void *, size_t, size_t); |
| 43 | + bool (*pool_free)(void *, void *); |
| 44 | + int (*pool_create_v1)(intptr_t, const struct mem_pool_policy_s *, void **); |
| 45 | + bool (*pool_destroy)(void *); |
| 46 | + void *(*pool_identify)(void *object); |
| 47 | + size_t (*pool_msize)(void *, void *); |
| 48 | +}; |
| 49 | + |
| 50 | +struct tbb_memory_pool { |
| 51 | + umf_memory_provider_handle_t mem_provider; |
| 52 | + void *tbb_pool; |
| 53 | +}; |
| 54 | + |
| 55 | +static struct tbb_callbacks g_tbb_ops; |
| 56 | +static pthread_once_t tbb_is_initialized = PTHREAD_ONCE_INIT; |
| 57 | + |
| 58 | +static void load_tbb_symbols(void) { |
| 59 | + fprintf(stdout, "Initializing TBB symbols\n"); |
| 60 | + const char so_name[] = "libtbbmalloc.so.2"; |
| 61 | + void *tbb_handle = dlopen(so_name, RTLD_LAZY); |
| 62 | + if (!tbb_handle) { |
| 63 | + fprintf(stderr, "%s not found.\n", so_name); |
| 64 | + abort(); |
| 65 | + } |
| 66 | + |
| 67 | + struct tbb_callbacks tbb_ops; |
| 68 | + |
| 69 | + *(void **)&tbb_ops.pool_malloc = |
| 70 | + dlsym(tbb_handle, "_ZN3rml11pool_mallocEPNS_10MemoryPoolEm"); |
| 71 | + *(void **)&tbb_ops.pool_realloc = |
| 72 | + dlsym(tbb_handle, "_ZN3rml12pool_reallocEPNS_10MemoryPoolEPvm"); |
| 73 | + *(void **)&tbb_ops.pool_aligned_malloc = |
| 74 | + dlsym(tbb_handle, "_ZN3rml19pool_aligned_mallocEPNS_10MemoryPoolEmm"); |
| 75 | + *(void **)&tbb_ops.pool_free = |
| 76 | + dlsym(tbb_handle, "_ZN3rml9pool_freeEPNS_10MemoryPoolEPv"); |
| 77 | + *(void **)&tbb_ops.pool_create_v1 = dlsym( |
| 78 | + tbb_handle, |
| 79 | + "_ZN3rml14pool_create_v1ElPKNS_13MemPoolPolicyEPPNS_10MemoryPoolE"); |
| 80 | + *(void **)&tbb_ops.pool_destroy = |
| 81 | + dlsym(tbb_handle, "_ZN3rml12pool_destroyEPNS_10MemoryPoolE"); |
| 82 | + *(void **)&tbb_ops.pool_identify = |
| 83 | + dlsym(tbb_handle, "_ZN3rml13pool_identifyEPv"); |
| 84 | + *(void **)&tbb_ops.pool_msize = |
| 85 | + dlsym(tbb_handle, "_ZN3rml10pool_msizeEPNS_10MemoryPoolEPv"); |
| 86 | + |
| 87 | + if (!tbb_ops.pool_malloc || !tbb_ops.pool_realloc || |
| 88 | + !tbb_ops.pool_aligned_malloc || !tbb_ops.pool_free || |
| 89 | + !tbb_ops.pool_create_v1 || !tbb_ops.pool_destroy || |
| 90 | + !tbb_ops.pool_identify) { |
| 91 | + fprintf(stderr, "Could not find symbols in %s.\n", so_name); |
| 92 | + dlclose(tbb_handle); |
| 93 | + abort(); |
| 94 | + } |
| 95 | + |
| 96 | + g_tbb_ops = tbb_ops; |
| 97 | +} |
| 98 | + |
| 99 | +static void *tbb_raw_alloc_wrapper(intptr_t pool_id, size_t *raw_bytes) { |
| 100 | + void *resPtr; |
| 101 | + struct tbb_memory_pool *pool = (struct tbb_memory_pool *)pool_id; |
| 102 | + umf_result_t ret = |
| 103 | + umfMemoryProviderAlloc(pool->mem_provider, *raw_bytes, 0, &resPtr); |
| 104 | + if (ret != UMF_RESULT_SUCCESS) { |
| 105 | + TLS_last_allocation_error = UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 106 | + return NULL; |
| 107 | + } |
| 108 | + |
| 109 | + return resPtr; |
| 110 | +} |
| 111 | + |
| 112 | +static void tbb_raw_free_wrapper(intptr_t pool_id, void *ptr, size_t bytes) { |
| 113 | + struct tbb_memory_pool *pool = (struct tbb_memory_pool *)pool_id; |
| 114 | + umf_result_t ret = umfMemoryProviderFree(pool->mem_provider, ptr, bytes); |
| 115 | + if (ret != UMF_RESULT_SUCCESS) { |
| 116 | + fprintf( |
| 117 | + stderr, |
| 118 | + "Memory provider failed to free memory, addr = %p, size = %lu\n", |
| 119 | + ptr, bytes); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +static umf_result_t tbb_pool_initialize(umf_memory_provider_handle_t provider, |
| 124 | + void *params, void **pool) { |
| 125 | + (void)params; // unused |
| 126 | + |
| 127 | + const size_t GRANULARITY = 2 * 1024 * 1024; |
| 128 | + struct mem_pool_policy_s policy = {.pAlloc = tbb_raw_alloc_wrapper, |
| 129 | + .pFree = tbb_raw_free_wrapper, |
| 130 | + .granularity = GRANULARITY, |
| 131 | + .version = 1, |
| 132 | + .fixed_pool = false, |
| 133 | + .keep_all_memory = false, |
| 134 | + .reserved = 0}; |
| 135 | + |
| 136 | + pthread_once(&tbb_is_initialized, load_tbb_symbols); |
| 137 | + |
| 138 | + struct tbb_memory_pool *pool_data = malloc(sizeof(struct tbb_memory_pool)); |
| 139 | + if (!pool_data) { |
| 140 | + fprintf(stderr, "cannot allocate memory for metadata\n"); |
| 141 | + return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 142 | + } |
| 143 | + |
| 144 | + pool_data->mem_provider = provider; |
| 145 | + g_tbb_ops.pool_create_v1((intptr_t)pool_data, &policy, |
| 146 | + &(pool_data->tbb_pool)); |
| 147 | + *pool = (void *)pool_data; |
| 148 | + |
| 149 | + return UMF_RESULT_SUCCESS; |
| 150 | +} |
| 151 | + |
| 152 | +static void tbb_pool_finalize(void *pool) { |
| 153 | + pthread_once(&tbb_is_initialized, load_tbb_symbols); |
| 154 | + struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool; |
| 155 | + g_tbb_ops.pool_destroy(pool_data->tbb_pool); |
| 156 | +} |
| 157 | + |
| 158 | +static void *tbb_malloc(void *pool, size_t size) { |
| 159 | + struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool; |
| 160 | + TLS_last_allocation_error = 0; |
| 161 | + void *ptr = g_tbb_ops.pool_malloc(pool_data->tbb_pool, size); |
| 162 | + if (ptr == NULL) { |
| 163 | + if (TLS_last_allocation_error == 0) { |
| 164 | + TLS_last_allocation_error = UMF_RESULT_ERROR_UNKNOWN; |
| 165 | + } |
| 166 | + return NULL; |
| 167 | + } |
| 168 | + return ptr; |
| 169 | +} |
| 170 | + |
| 171 | +static void *tbb_calloc(void *pool, size_t num, size_t size) { |
| 172 | + assert(pool); |
| 173 | + size_t csize = num * size; |
| 174 | + void *ptr = tbb_malloc(pool, csize); |
| 175 | + if (ptr == NULL) { |
| 176 | + // TLS_last_allocation_error is set by tbb_malloc() |
| 177 | + return NULL; |
| 178 | + } |
| 179 | + |
| 180 | + memset(ptr, 0, csize); // TODO: device memory is not accessible by host |
| 181 | + return ptr; |
| 182 | +} |
| 183 | + |
| 184 | +static void *tbb_realloc(void *pool, void *ptr, size_t size) { |
| 185 | + struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool; |
| 186 | + TLS_last_allocation_error = 0; |
| 187 | + void *new_ptr = g_tbb_ops.pool_realloc(pool_data->tbb_pool, ptr, size); |
| 188 | + if (new_ptr == NULL) { |
| 189 | + if (TLS_last_allocation_error == 0) { |
| 190 | + TLS_last_allocation_error = UMF_RESULT_ERROR_UNKNOWN; |
| 191 | + } |
| 192 | + return NULL; |
| 193 | + } |
| 194 | + return new_ptr; |
| 195 | +} |
| 196 | + |
| 197 | +static void *tbb_aligned_malloc(void *pool, size_t size, size_t alignment) { |
| 198 | + struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool; |
| 199 | + TLS_last_allocation_error = 0; |
| 200 | + void *ptr = |
| 201 | + g_tbb_ops.pool_aligned_malloc(pool_data->tbb_pool, size, alignment); |
| 202 | + if (ptr == NULL) { |
| 203 | + if (TLS_last_allocation_error == 0) { |
| 204 | + TLS_last_allocation_error = UMF_RESULT_ERROR_UNKNOWN; |
| 205 | + } |
| 206 | + return NULL; |
| 207 | + } |
| 208 | + return ptr; |
| 209 | +} |
| 210 | + |
| 211 | +static umf_result_t tbb_free(void *pool, void *ptr) { |
| 212 | + struct tbb_memory_pool *pool_data = (struct tbb_memory_pool *)pool; |
| 213 | + g_tbb_ops.pool_free(pool_data->tbb_pool, ptr); |
| 214 | + return UMF_RESULT_SUCCESS; |
| 215 | +} |
| 216 | + |
| 217 | +static size_t tbb_malloc_usable_size(void *pool, void *ptr) { |
| 218 | + (void)pool; // not used |
| 219 | + (void)ptr; // not used |
| 220 | + return 0; // unsupported |
| 221 | +} |
| 222 | + |
| 223 | +static umf_result_t tbb_get_last_allocation_error(void *pool) { |
| 224 | + (void)pool; // not used |
| 225 | + return TLS_last_allocation_error; |
| 226 | +} |
| 227 | + |
| 228 | +umf_memory_pool_ops_t UMF_TBB_POOL_OPS = { |
| 229 | + .version = UMF_VERSION_CURRENT, |
| 230 | + .initialize = tbb_pool_initialize, |
| 231 | + .finalize = tbb_pool_finalize, |
| 232 | + .malloc = tbb_malloc, |
| 233 | + .calloc = tbb_calloc, |
| 234 | + .realloc = tbb_realloc, |
| 235 | + .aligned_malloc = tbb_aligned_malloc, |
| 236 | + .malloc_usable_size = tbb_malloc_usable_size, |
| 237 | + .free = tbb_free, |
| 238 | + .get_last_allocation_error = tbb_get_last_allocation_error}; |
0 commit comments