Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
166 lines (144 loc) · 5.2 KB
/
Copy pathmain.cpp
File metadata and controls
166 lines (144 loc) · 5.2 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
// Google Benchmark cross-check.
//
// Read the multi-threaded rows as *throughput per enqueue+dequeue pair*, not as
// one-way latency. Google Benchmark reports
// (sum of per-thread elapsed time) / (total iterations across threads),
// and with one producer and one consumer that is wall time per pair.
// bench_latency.cpp measures the actual cross-core handoff.
#include "spsc.hpp"
#include "bench_common.hpp"
#include "naive_queue.hpp"
#include <benchmark/benchmark.h>
#include <emmintrin.h>
#include <cstddef>
#include <string>
// One enqueue + one dequeue on a single thread. This is an L1-hot round trip
// through the ring, so the reported time is per *pair* of operations.
//
// No DoNotOptimize sink is needed: enqueue() writes the payload and releases
// the tail, and dequeue() releases the head, so neither call can be removed.
// Passing the payload through DoNotOptimize only adds a store/reload round trip
// to the measurement and makes clang look ~1.5 ns slower than gcc.
static void BM_SingleThreaded(benchmark::State& state) {
SPSCQueue<int, 1024> queue;
int value = 42;
for (auto _ : state) {
(void)queue.enqueue(value);
(void)queue.dequeue(value);
}
}
BENCHMARK(BM_SingleThreaded);
// Failure path: dequeue from an empty queue. The result must be consumed or
// the (side-effect-free) load can be hoisted out of the loop.
static void BM_EmptyDequeue(benchmark::State& state) {
SPSCQueue<int, 1024> queue;
int value = 0;
for (auto _ : state) {
bool popped = queue.dequeue(value);
benchmark::DoNotOptimize(popped);
}
}
BENCHMARK(BM_EmptyDequeue);
// Failure path: enqueue into a full queue.
static void BM_FullEnqueue(benchmark::State& state) {
SPSCQueue<int, 1024> queue;
for (int i = 0; i < 1023; ++i) {
if (!queue.enqueue(i)) {
state.SkipWithError("setup failed to fill the queue");
return;
}
}
for (auto _ : state) {
bool pushed = queue.enqueue(7);
benchmark::DoNotOptimize(pushed);
}
}
BENCHMARK(BM_FullEnqueue);
static SPSCQueue<int, 1024> g_padded;
static NaiveSPSCQueue<int, 1024> g_unpadded;
namespace {
// Captured before any benchmark pins a thread. A thread that calls
// pthread_setaffinity_np keeps that mask after the benchmark loop, so it must
// be restored or later benchmarks only see one core.
struct OriginalAffinity {
cpu_set_t mask;
OriginalAffinity() {
CPU_ZERO(&mask);
if (sched_getaffinity(0, sizeof(mask), &mask) != 0) {
CPU_ZERO(&mask);
}
}
};
const OriginalAffinity& original_affinity() {
static const OriginalAffinity saved;
return saved;
}
void restore_affinity() {
if (pthread_setaffinity_np(pthread_self(), sizeof(original_affinity().mask),
&original_affinity().mask) != 0) {
std::fprintf(stderr, "failed to restore thread affinity\n");
}
}
// Resolved once, before any pinning, so both the padded and unpadded
// benchmarks use the same pair of cores.
const bench::Pair& benchmark_pair() {
static const bench::Pair pair = [] {
const auto pairs = bench::topology_pairs();
for (const auto& candidate : pairs) {
if (candidate.label == "same L3") {
return candidate;
}
}
return pairs.empty() ? bench::Pair{0, 0, "unpinned"} : pairs.front();
}();
return pair;
}
} // namespace
template<typename Queue>
static void run_cross_core(benchmark::State& state, Queue& queue) {
// Google Benchmark has no affinity API, so pin explicitly. Pinning removes
// the run-to-run variance from the scheduler moving the two threads between
// SMT siblings, the same L3, and different L3s.
const bench::Pair& pair = benchmark_pair();
// Initialise the saved mask before pinning: the first call must see the
// process's original affinity, not the single core we are about to pin to.
(void)original_affinity();
const int cpu = state.thread_index() == 0 ? pair.producer : pair.consumer;
const bool pinned = pair.label != "unpinned";
if (pinned && !bench::pin_this_thread(cpu)) {
state.SkipWithError("failed to pin benchmark thread");
return;
}
state.SetLabel(pair.label);
if (state.thread_index() == 0) {
int value = 42;
for (auto _ : state) {
while (!queue.enqueue(value)) {
_mm_pause();
}
}
} else {
int value = 0;
for (auto _ : state) {
while (!queue.dequeue(value)) {
_mm_pause();
}
}
}
if (pinned) {
restore_affinity();
}
}
// Producer and consumer on two threads. The printed time is wall time per
// enqueue+dequeue pair at steady state; it is not one-way latency.
static void BM_MultiThreaded(benchmark::State& state) {
run_cross_core(state, g_padded);
}
BENCHMARK(BM_MultiThreaded)->Threads(2)->UseRealTime();
// Same logic with head/tail sharing a cache line. The gap against the row above
// is the false-sharing cost.
static void BM_MultiThreadedUnpadded(benchmark::State& state) {
run_cross_core(state, g_unpadded);
}
BENCHMARK(BM_MultiThreadedUnpadded)->Threads(2)->UseRealTime();
BENCHMARK_MAIN();