|
| 1 | + |
1 | 2 | #include <benchmark/benchmark.h> |
2 | 3 | #include <tlsf.h> |
3 | 4 | #include <iostream> |
| 5 | +#include <random> |
4 | 6 |
|
5 | 7 |
|
6 | 8 |
|
7 | 9 |
|
8 | 10 |
|
9 | | -class AllocatorFixture : public benchmark::Fixture |
| 11 | +class MyTLSFAllocatorFixture : public benchmark::Fixture |
10 | 12 | { |
11 | 13 | public: |
12 | | - TlsfAllocator* tlsf; |
13 | | - const size_t POOL_SIZE = 1024 * 1024 * 1;// 1 MiB pool size |
14 | | - void SetUp(const ::benchmark::State& state) |
| 14 | + TlsfAllocator* m_tlsf; |
| 15 | + const size_t POOL_SIZE = 1024 * 1024 *50;// 50 MiB pool size |
| 16 | + void SetUp(const ::benchmark::State& state) |
15 | 17 | { |
16 | | - tlsf = new TlsfAllocator(POOL_SIZE); |
| 18 | + m_tlsf = new TlsfAllocator(POOL_SIZE); |
17 | 19 | } |
18 | | - void TearDown(const ::benchmark::State& state) |
| 20 | + void TearDown(const ::benchmark::State& state) |
19 | 21 | { |
20 | | - delete tlsf; |
| 22 | + delete m_tlsf; |
21 | 23 | } |
22 | 24 | }; |
23 | 25 |
|
24 | 26 |
|
25 | | -static void BM_System_Malloc(benchmark::State& state) |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +/******* Mixed Allocation *********/ |
| 33 | + |
| 34 | + |
| 35 | +BENCHMARK_DEFINE_F(MyTLSFAllocatorFixture, MyTlsf_AllocDeallocCycle_MixedSize) (benchmark::State& state) |
26 | 36 | { |
27 | | - const size_t ALLOCATION_SIZE = state.range(0); |
| 37 | + const size_t NUM_ALLOCATIONS = state.range(0); |
| 38 | + const size_t MAX_ALLOC_SIZE = 1024 * 128; // Max 128 KiB per block |
| 39 | + |
| 40 | + std::mt19937 gen(28); |
| 41 | + std::uniform_int_distribution<> distrib(20, MAX_ALLOC_SIZE);//fom 20 bytes to 128 KiB |
| 42 | + std::vector<void*> allocated_pointers; |
| 43 | + |
| 44 | + allocated_pointers.reserve(NUM_ALLOCATIONS); |
| 45 | + |
28 | 46 | for (auto _ : state) |
29 | 47 | { |
30 | | - void* p = std::malloc(ALLOCATION_SIZE); |
| 48 | + // 1. ALLOCATION PHASE (TIME MEASURED) |
| 49 | + for (size_t i = 0; i < NUM_ALLOCATIONS; ++i) |
| 50 | + { |
| 51 | + size_t size = distrib(gen); |
| 52 | + void* p; |
| 53 | + |
31 | 54 |
|
32 | | - benchmark::DoNotOptimize(p); |
33 | | - std::free(p); |
| 55 | + p = m_tlsf->allocate(size); |
| 56 | + |
| 57 | + if (p == nullptr) |
| 58 | + { |
| 59 | + // Skips this benchmark run iteration gracefully |
| 60 | + state.SkipWithError("TLSF Allocation failed (Pool Exhaustion)."); |
| 61 | + return; |
| 62 | + } |
| 63 | + benchmark::DoNotOptimize(p); |
| 64 | + |
| 65 | + |
| 66 | + allocated_pointers.push_back(p); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + for (void* p : allocated_pointers) |
| 73 | + { |
| 74 | + m_tlsf->deallocate(p); |
| 75 | + } |
| 76 | + allocated_pointers.clear(); |
| 77 | + |
| 78 | + |
34 | 79 | } |
35 | | -} |
36 | 80 |
|
37 | | -BENCHMARK(BM_System_Malloc)->Arg( 1024 * 40 );//40 KiB |
38 | 81 |
|
39 | 82 |
|
| 83 | +} |
40 | 84 |
|
41 | | -BENCHMARK_DEFINE_F(AllocatorFixture, BM_Tlsf_AllocateAndFree) (benchmark::State& state) |
| 85 | +BENCHMARK_REGISTER_F(MyTLSFAllocatorFixture, MyTlsf_AllocDeallocCycle_MixedSize) |
| 86 | +->Args({ 10 }) |
| 87 | +->Args({ 20 }) |
| 88 | +->Args({ 30 }) |
| 89 | +->Args({ 40 }) |
| 90 | +->Args({ 50 }) |
| 91 | +->Args({ 60 }) |
| 92 | +->Args({ 70 }) |
| 93 | +->Args({ 80 }) |
| 94 | +->Args({ 90 }) |
| 95 | +->Args({ 100 }) |
| 96 | +->Args({ 110 }) |
| 97 | +->Args({ 120 }) |
| 98 | +->Args({ 130 }) |
| 99 | +->Args({ 140 }) |
| 100 | +->Args({ 150 }) |
| 101 | +->Args({ 160 }) |
| 102 | +->Args({ 170 }) |
| 103 | +->Args({ 180 }) |
| 104 | +->Args({ 190 }) |
| 105 | +->Args({ 200 }); |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +static void SystemMalloc_AllocDeallocCycle_MixedSize(benchmark::State& state) |
42 | 111 | { |
43 | | - const size_t ALLOCATION_SIZE = state.range(0); |
| 112 | + const size_t NUM_ALLOCATIONS = state.range(0); |
| 113 | + const size_t MAX_ALLOC_SIZE = 1024 * 128; // Max 128 KiB per block |
| 114 | + |
| 115 | + |
| 116 | + std::vector<void*> pointers; |
| 117 | + pointers.reserve(NUM_ALLOCATIONS); |
| 118 | + |
| 119 | + // Random number generation setup (fixed seed for stable testing) |
| 120 | + std::mt19937 gen(28); |
| 121 | + std::uniform_int_distribution<> distrib(20, MAX_ALLOC_SIZE);// from 20 bytes to 128 KiB |
| 122 | + |
| 123 | + |
44 | 124 | for (auto _ : state) |
45 | 125 | { |
46 | | - void* p = tlsf->allocate(ALLOCATION_SIZE); |
47 | | - benchmark::DoNotOptimize(p); |
48 | | - tlsf->deallocate(p); |
| 126 | + // 1. ALLOCATION PHASE (TIME MEASURED) |
| 127 | + for (size_t i = 0; i < NUM_ALLOCATIONS; ++i) |
| 128 | + { |
| 129 | + size_t size = distrib(gen); |
| 130 | + void* p; |
| 131 | + |
| 132 | + |
| 133 | + p = std::malloc(size); // <<<--- MALLOC CALL |
| 134 | + |
| 135 | + if (p == nullptr) |
| 136 | + { |
| 137 | + state.SkipWithError("System Malloc failed (OOM)."); |
| 138 | + return; |
| 139 | + } |
| 140 | + benchmark::DoNotOptimize(p); |
| 141 | + pointers.push_back(p); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + for (void* p : pointers) |
| 148 | + { |
| 149 | + std::free(p); // <<<--- FREE CALL |
| 150 | + } |
| 151 | + pointers.clear(); |
| 152 | + |
| 153 | + |
| 154 | + |
49 | 155 | } |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
50 | 160 | } |
51 | | -BENCHMARK_REGISTER_F(AllocatorFixture, BM_Tlsf_AllocateAndFree)->Arg(1024 * 40);// 40 KiB |
52 | 161 |
|
| 162 | +BENCHMARK(SystemMalloc_AllocDeallocCycle_MixedSize) |
| 163 | +->Args({ 10 }) |
| 164 | +->Args({ 20 }) |
| 165 | +->Args({ 30 }) |
| 166 | +->Args({ 40 }) |
| 167 | +->Args({ 50 }) |
| 168 | +->Args({ 60 }) |
| 169 | +->Args({ 70 }) |
| 170 | +->Args({ 80 }) |
| 171 | +->Args({ 90 }) |
| 172 | +->Args({ 100 }) |
| 173 | +->Args({ 110 }) |
| 174 | +->Args({ 120 }) |
| 175 | +->Args({ 130 }) |
| 176 | +->Args({ 140 }) |
| 177 | +->Args({ 150 }) |
| 178 | +->Args({ 160 }) |
| 179 | +->Args({ 170 }) |
| 180 | +->Args({ 180 }) |
| 181 | +->Args({ 190 }) |
| 182 | +->Args({ 200 }); |
53 | 183 |
|
54 | 184 | //BENCHMARK_MAIN(); |
55 | 185 |
|
56 | 186 |
|
57 | 187 |
|
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
58 | 193 | int main(int argc, char** argv) |
59 | 194 | { |
60 | 195 | // 1. Initialize and run the benchmarks |
61 | 196 | benchmark::Initialize(&argc, argv); |
62 | 197 | if (benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; |
63 | | - |
| 198 | + |
64 | 199 | bool success = benchmark::RunSpecifiedBenchmarks(); |
65 | 200 |
|
66 | 201 | std::cout << "\n\n--- Benchmarks Finished ---\nPress ENTER to exit..."; |
67 | 202 |
|
68 | 203 |
|
69 | 204 |
|
70 | 205 | // Wait for the user to press ENTER |
71 | | - //std::cin.get(); |
| 206 | + std::cin.get(); |
72 | 207 |
|
73 | 208 | return success ? 0 : 1; |
74 | 209 | } |
|
0 commit comments