-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbench_cache_alignment.cpp
More file actions
417 lines (326 loc) · 11.8 KB
/
Copy pathbench_cache_alignment.cpp
File metadata and controls
417 lines (326 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* @file bench_cache_alignment.cpp
* @brief Cache alignment verification and optimization analysis
*
* Diagnostic tool to:
* - Verify cache line alignment of critical structures
* - Measure cache miss rates (via performance counters if available)
* - Compare aligned vs unaligned access patterns
* - Identify false sharing opportunities
* - Benchmark SIMD-ready data layouts
*/
#include <slick/orderbook/orderbook.hpp>
#include <slick/orderbook/detail/price_level_l3.hpp>
#include <slick/orderbook/detail/order.hpp>
#include <benchmark/benchmark.h>
#include <iostream>
#include <type_traits>
#include <cstddef>
#include <random>
#include <algorithm>
using namespace slick::orderbook;
using namespace slick::orderbook::detail;
// ============================================================================
// Cache Line Size Detection
// ============================================================================
constexpr size_t CACHE_LINE_SIZE = 64; // Standard on x86-64
// ============================================================================
// Alignment Verification
// ============================================================================
template<typename T>
void verifyAlignment(const char* type_name) {
std::cout << "\n=== " << type_name << " ===\n";
std::cout << "Size: " << sizeof(T) << " bytes\n";
std::cout << "Alignment: " << alignof(T) << " bytes\n";
std::cout << "Cache lines: " << (sizeof(T) + CACHE_LINE_SIZE - 1) / CACHE_LINE_SIZE << "\n";
if constexpr (alignof(T) >= CACHE_LINE_SIZE) {
std::cout << "Status: ✓ Cache-aligned\n";
} else if constexpr (alignof(T) >= 16) {
std::cout << "Status: ⚠ SIMD-aligned (16 bytes) but not cache-aligned\n";
} else {
std::cout << "Status: ✗ Not aligned for optimal performance\n";
}
}
static void BM_PrintAlignmentInfo(benchmark::State& state) {
// This benchmark just prints alignment info once
if (state.thread_index() == 0 && state.iterations() == 0) {
std::cout << "\n========================================\n";
std::cout << "Cache Line Size: " << CACHE_LINE_SIZE << " bytes\n";
std::cout << "========================================\n";
verifyAlignment<PriceLevelL2>("PriceLevelL2");
verifyAlignment<PriceLevelL3>("PriceLevelL3");
verifyAlignment<Order>("Order");
verifyAlignment<OrderBookL2>("OrderBookL2");
verifyAlignment<OrderBookL3>("OrderBookL3");
verifyAlignment<TopOfBook>("TopOfBook");
verifyAlignment<PriceLevelUpdate>("PriceLevelUpdate");
verifyAlignment<OrderUpdate>("OrderUpdate");
verifyAlignment<Trade>("Trade");
std::cout << "\n========================================\n";
}
for (auto _ : state) {
benchmark::DoNotOptimize(CACHE_LINE_SIZE);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_PrintAlignmentInfo)->Iterations(1);
// ============================================================================
// Benchmark: Aligned vs Unaligned Struct Access
// ============================================================================
struct alignas(64) AlignedStruct {
int64_t a;
int64_t b;
int64_t c;
int64_t d;
};
struct UnalignedStruct {
int64_t a;
int64_t b;
int64_t c;
int64_t d;
};
static void BM_AlignedStructAccess(benchmark::State& state) {
const size_t count = 1000;
std::vector<AlignedStruct> data(count);
for (auto _ : state) {
int64_t sum = 0;
for (size_t i = 0; i < count; ++i) {
sum += data[i].a + data[i].b + data[i].c + data[i].d;
}
benchmark::DoNotOptimize(sum);
}
state.SetItemsProcessed(state.iterations() * count);
}
BENCHMARK(BM_AlignedStructAccess);
static void BM_UnalignedStructAccess(benchmark::State& state) {
const size_t count = 1000;
std::vector<UnalignedStruct> data(count);
for (auto _ : state) {
int64_t sum = 0;
for (size_t i = 0; i < count; ++i) {
sum += data[i].a + data[i].b + data[i].c + data[i].d;
}
benchmark::DoNotOptimize(sum);
}
state.SetItemsProcessed(state.iterations() * count);
}
BENCHMARK(BM_UnalignedStructAccess);
// ============================================================================
// Benchmark: False Sharing Test
// ============================================================================
struct NoFalseSharing {
alignas(64) int64_t counter1;
alignas(64) int64_t counter2;
};
struct WithFalseSharing {
int64_t counter1;
int64_t counter2;
};
static void BM_NoFalseSharing_Thread1(benchmark::State& state) {
static NoFalseSharing data{};
for (auto _ : state) {
++data.counter1;
}
state.SetItemsProcessed(state.iterations());
}
static void BM_NoFalseSharing_Thread2(benchmark::State& state) {
static NoFalseSharing data{};
for (auto _ : state) {
++data.counter2;
}
state.SetItemsProcessed(state.iterations());
}
static void BM_WithFalseSharing_Thread1(benchmark::State& state) {
static WithFalseSharing data{};
for (auto _ : state) {
++data.counter1;
}
state.SetItemsProcessed(state.iterations());
}
static void BM_WithFalseSharing_Thread2(benchmark::State& state) {
static WithFalseSharing data{};
for (auto _ : state) {
++data.counter2;
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_NoFalseSharing_Thread1)->Threads(2);
BENCHMARK(BM_NoFalseSharing_Thread2)->Threads(2);
BENCHMARK(BM_WithFalseSharing_Thread1)->Threads(2);
BENCHMARK(BM_WithFalseSharing_Thread2)->Threads(2);
// ============================================================================
// Benchmark: Array of Structures (AoS) vs Structure of Arrays (SoA)
// ============================================================================
struct PriceQty {
Price price;
Quantity quantity;
};
static void BM_ArrayOfStructures(benchmark::State& state) {
const size_t count = 1000;
std::vector<PriceQty> data(count);
for (size_t i = 0; i < count; ++i) {
data[i] = {100000 + static_cast<Price>(i), 1000 + static_cast<Quantity>(i)};
}
for (auto _ : state) {
// Sum all quantities (common operation in orderbook)
Quantity total = 0;
for (size_t i = 0; i < count; ++i) {
total += data[i].quantity;
}
benchmark::DoNotOptimize(total);
}
state.SetItemsProcessed(state.iterations() * count);
}
BENCHMARK(BM_ArrayOfStructures);
static void BM_StructureOfArrays(benchmark::State& state) {
const size_t count = 1000;
std::vector<Price> prices(count);
std::vector<Quantity> quantities(count);
for (size_t i = 0; i < count; ++i) {
prices[i] = 100000 + static_cast<Price>(i);
quantities[i] = 1000 + static_cast<Quantity>(i);
}
for (auto _ : state) {
// Sum all quantities (common operation in orderbook)
Quantity total = 0;
for (size_t i = 0; i < count; ++i) {
total += quantities[i];
}
benchmark::DoNotOptimize(total);
}
state.SetItemsProcessed(state.iterations() * count);
}
BENCHMARK(BM_StructureOfArrays);
// ============================================================================
// Benchmark: Sequential vs Random Access (Cache Locality)
// ============================================================================
static void BM_SequentialAccess(benchmark::State& state) {
const size_t count = 10000;
std::vector<int64_t> data(count);
for (size_t i = 0; i < count; ++i) {
data[i] = static_cast<int64_t>(i);
}
for (auto _ : state) {
int64_t sum = 0;
for (size_t i = 0; i < count; ++i) {
sum += data[i];
}
benchmark::DoNotOptimize(sum);
}
state.SetItemsProcessed(state.iterations() * count);
}
BENCHMARK(BM_SequentialAccess);
static void BM_RandomAccess(benchmark::State& state) {
const size_t count = 10000;
std::vector<int64_t> data(count);
std::vector<size_t> indices(count);
for (size_t i = 0; i < count; ++i) {
data[i] = static_cast<int64_t>(i);
indices[i] = i;
}
// Shuffle indices
std::mt19937_64 rng(12345);
std::shuffle(indices.begin(), indices.end(), rng);
for (auto _ : state) {
int64_t sum = 0;
for (size_t i = 0; i < count; ++i) {
sum += data[indices[i]];
}
benchmark::DoNotOptimize(sum);
}
state.SetItemsProcessed(state.iterations() * count);
}
BENCHMARK(BM_RandomAccess);
// ============================================================================
// Benchmark: Prefetch Impact
// ============================================================================
static void BM_NoPrefetch(benchmark::State& state) {
const size_t count = 1000;
std::vector<int64_t> data(count);
std::vector<size_t> indices(count);
for (size_t i = 0; i < count; ++i) {
data[i] = static_cast<int64_t>(i);
indices[i] = i;
}
std::mt19937_64 rng(54321);
std::shuffle(indices.begin(), indices.end(), rng);
for (auto _ : state) {
int64_t sum = 0;
for (size_t i = 0; i < count; ++i) {
sum += data[indices[i]];
}
benchmark::DoNotOptimize(sum);
}
state.SetItemsProcessed(state.iterations() * count);
}
BENCHMARK(BM_NoPrefetch);
static void BM_WithPrefetch(benchmark::State& state) {
const size_t count = 1000;
std::vector<int64_t> data(count);
std::vector<size_t> indices(count);
for (size_t i = 0; i < count; ++i) {
data[i] = static_cast<int64_t>(i);
indices[i] = i;
}
std::mt19937_64 rng(54321);
std::shuffle(indices.begin(), indices.end(), rng);
for (auto _ : state) {
int64_t sum = 0;
for (size_t i = 0; i < count; ++i) {
// Prefetch next element
if (i + 1 < count) {
#ifdef __GNUC__
__builtin_prefetch(&data[indices[i + 1]], 0, 0);
#endif
}
sum += data[indices[i]];
}
benchmark::DoNotOptimize(sum);
}
state.SetItemsProcessed(state.iterations() * count);
}
BENCHMARK(BM_WithPrefetch);
// ============================================================================
// Benchmark: OrderBook Operations - Cache Effects
// ============================================================================
static void BM_OrderBook_ColdCache(benchmark::State& state) {
const size_t num_books = 100;
std::vector<OrderBookL2> books;
books.reserve(num_books);
for (size_t i = 0; i < num_books; ++i) {
books.emplace_back(static_cast<SymbolId>(i));
// Add some levels
for (int j = 0; j < 10; ++j) {
books[i].updateLevel(Side::Buy, 100000 - j * 10, 1000, 0, 0);
books[i].updateLevel(Side::Sell, 100100 + j * 10, 1000, 0, 0);
}
}
size_t book_idx = 0;
for (auto _ : state) {
// Access different orderbooks each time (cold cache)
auto tob = books[book_idx % num_books].getTopOfBook();
benchmark::DoNotOptimize(tob);
++book_idx;
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_OrderBook_ColdCache);
static void BM_OrderBook_HotCache(benchmark::State& state) {
OrderBookL2 book(1);
// Add some levels
for (int i = 0; i < 10; ++i) {
book.updateLevel(Side::Buy, 100000 - i * 10, 1000, 0, 0);
book.updateLevel(Side::Sell, 100100 + i * 10, 1000, 0, 0);
}
for (auto _ : state) {
// Access same orderbook (hot cache)
auto tob = book.getTopOfBook();
benchmark::DoNotOptimize(tob);
}
state.SetItemsProcessed(state.iterations());
}
BENCHMARK(BM_OrderBook_HotCache);
// ============================================================================
// Main
// ============================================================================
BENCHMARK_MAIN();