|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2024 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 "multithread.hpp" |
| 11 | + |
| 12 | +#include <umf/memory_pool.h> |
| 13 | +#include <umf/pools/pool_disjoint.h> |
| 14 | +#include <umf/pools/pool_jemalloc.h> |
| 15 | +#include <umf/pools/pool_scalable.h> |
| 16 | +#include <umf/providers/provider_os_memory.h> |
| 17 | + |
| 18 | +#include <iostream> |
| 19 | +#include <memory> |
| 20 | +#include <numeric> |
| 21 | + |
| 22 | +struct bench_params { |
| 23 | + // bench_params() = default; |
| 24 | + size_t n_repeats = 5; |
| 25 | + size_t n_iterations = 50000; |
| 26 | + size_t n_threads = 20; |
| 27 | + size_t alloc_size = 64; |
| 28 | +}; |
| 29 | + |
| 30 | +using poolCreateExtParams = std::tuple<umf_memory_pool_ops_t *, void *, |
| 31 | + umf_memory_provider_ops_t *, void *>; |
| 32 | + |
| 33 | +static auto poolCreateExtUnique(poolCreateExtParams params) { |
| 34 | + umf_memory_pool_handle_t hPool; |
| 35 | + auto [pool_ops, pool_params, provider_ops, provider_params] = params; |
| 36 | + |
| 37 | + umf_memory_provider_handle_t provider = nullptr; |
| 38 | + auto ret = |
| 39 | + umfMemoryProviderCreate(provider_ops, provider_params, &provider); |
| 40 | + if (ret != UMF_RESULT_SUCCESS) { |
| 41 | + std::cerr << "provider create failed" << std::endl; |
| 42 | + abort(); |
| 43 | + } |
| 44 | + |
| 45 | + ret = umfPoolCreate(pool_ops, provider, pool_params, |
| 46 | + UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &hPool); |
| 47 | + if (ret != UMF_RESULT_SUCCESS) { |
| 48 | + std::cerr << "pool create failed" << std::endl; |
| 49 | + abort(); |
| 50 | + } |
| 51 | + |
| 52 | + return std::shared_ptr<umf_memory_pool_t>(hPool, &umfPoolDestroy); |
| 53 | +} |
| 54 | + |
| 55 | +static void mt_alloc_free(poolCreateExtParams params, |
| 56 | + const bench_params &bench = bench_params()) { |
| 57 | + auto pool = poolCreateExtUnique(params); |
| 58 | + |
| 59 | + std::vector<std::vector<void *>> allocs(bench.n_threads); |
| 60 | + std::vector<size_t> numFailures(bench.n_threads); |
| 61 | + for (auto &v : allocs) { |
| 62 | + v.reserve(bench.n_iterations); |
| 63 | + } |
| 64 | + |
| 65 | + auto values = umf_bench::measure<std::chrono::milliseconds>( |
| 66 | + bench.n_repeats, bench.n_threads, |
| 67 | + [&, pool = pool.get()](auto thread_id) { |
| 68 | + for (int i = 0; i < bench.n_iterations; i++) { |
| 69 | + allocs[thread_id].push_back( |
| 70 | + umfPoolMalloc(pool, bench.alloc_size)); |
| 71 | + if (!allocs[thread_id].back()) { |
| 72 | + numFailures[thread_id]++; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + for (int i = 0; i < bench.n_iterations; i++) { |
| 77 | + umfPoolFree(pool, allocs[thread_id][i]); |
| 78 | + } |
| 79 | + |
| 80 | + // clear the vector as this function might be called multiple times |
| 81 | + allocs[thread_id].clear(); |
| 82 | + }); |
| 83 | + |
| 84 | + std::cout << "mean: " << umf_bench::mean(values) |
| 85 | + << " [ms] std_dev: " << umf_bench::std_dev(values) << " [ms]" |
| 86 | + << " (total alloc failures: " |
| 87 | + << std::accumulate(numFailures.begin(), numFailures.end(), 0) |
| 88 | + << " out of " |
| 89 | + << bench.n_iterations * bench.n_repeats * bench.n_threads << ")" |
| 90 | + << std::endl; |
| 91 | +} |
| 92 | + |
| 93 | +int main() { |
| 94 | +#if defined(UMF_BUILD_OS_MEMORY_PROVIDER) |
| 95 | + auto osParams = umfOsMemoryProviderParamsDefault(); |
| 96 | +#endif |
| 97 | + |
| 98 | +#if defined(UMF_BUILD_OS_MEMORY_PROVIDER) && \ |
| 99 | + defined(UMF_BUILD_LIBUMF_POOL_SCALABLE) |
| 100 | + |
| 101 | + // Increase iterations for scalable pool since it runs much faster than the remaining |
| 102 | + // ones. |
| 103 | + bench_params params; |
| 104 | + params.n_iterations *= 20; |
| 105 | + |
| 106 | + std::cout << "scalable_pool mt_alloc_free: "; |
| 107 | + mt_alloc_free(poolCreateExtParams{umfScalablePoolOps(), nullptr, |
| 108 | + umfOsMemoryProviderOps(), &osParams}, |
| 109 | + params); |
| 110 | +#else |
| 111 | + std::cout << "skipping scalable_pool mt_alloc_free" << std::endl; |
| 112 | +#endif |
| 113 | + |
| 114 | +#if defined(UMF_BUILD_OS_MEMORY_PROVIDER) && \ |
| 115 | + defined(UMF_BUILD_LIBUMF_POOL_JEMALLOC) |
| 116 | + std::cout << "jemalloc_pool mt_alloc_free: "; |
| 117 | + mt_alloc_free(poolCreateExtParams{umfJemallocPoolOps(), nullptr, |
| 118 | + umfOsMemoryProviderOps(), &osParams}); |
| 119 | +#else |
| 120 | + std::cout << "skipping jemalloc_pool mt_alloc_free" << std::endl; |
| 121 | +#endif |
| 122 | + |
| 123 | +#if defined(UMF_BUILD_OS_MEMORY_PROVIDER) && \ |
| 124 | + defined(UMF_BUILD_LIBUMF_POOL_DISJOINT) |
| 125 | + auto disjointParams = umfDisjointPoolParamsDefault(); |
| 126 | + |
| 127 | + std::cout << "disjoint_pool mt_alloc_free: "; |
| 128 | + mt_alloc_free(poolCreateExtParams{umfDisjointPoolOps(), &disjointParams, |
| 129 | + umfOsMemoryProviderOps(), &osParams}); |
| 130 | +#else |
| 131 | + std::cout << "skipping disjoint_pool mt_alloc_free" << std::endl; |
| 132 | +#endif |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
0 commit comments