-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbm_memory_order.cpp
More file actions
393 lines (336 loc) · 12.6 KB
/
Copy pathbm_memory_order.cpp
File metadata and controls
393 lines (336 loc) · 12.6 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
#include <benchmark/benchmark.h>
#include <atomic>
#include <barrier>
#include <cstddef>
#include <cstdint>
#include <thread>
#include <vector>
namespace {
constexpr std::size_t kOpsPerThread = 500'000;
constexpr std::size_t kPingPongRounds = 200'000;
constexpr std::size_t kPublishRounds = 200'000;
constexpr std::size_t kRingRounds = 500'000;
constexpr std::size_t kRingCapacity = 1u << 12;
constexpr std::size_t kLitmusRounds = 100'000;
void BM_FetchAddRelaxed(benchmark::State& state) {
static std::atomic<std::uint64_t> counter{0};
for (auto _ : state) {
for (std::size_t i = 0; i < kOpsPerThread; ++i) {
counter.fetch_add(1, std::memory_order_relaxed);
}
benchmark::ClobberMemory();
}
if (state.thread_index() == 0) {
state.SetItemsProcessed(state.iterations() *
static_cast<int64_t>(kOpsPerThread * state.threads()));
}
}
void BM_FetchAddAcqRel(benchmark::State& state) {
static std::atomic<std::uint64_t> counter{0};
for (auto _ : state) {
for (std::size_t i = 0; i < kOpsPerThread; ++i) {
counter.fetch_add(1, std::memory_order_acq_rel);
}
benchmark::ClobberMemory();
}
if (state.thread_index() == 0) {
state.SetItemsProcessed(state.iterations() *
static_cast<int64_t>(kOpsPerThread * state.threads()));
}
}
void BM_FetchAddSeqCst(benchmark::State& state) {
static std::atomic<std::uint64_t> counter{0};
for (auto _ : state) {
for (std::size_t i = 0; i < kOpsPerThread; ++i) {
counter.fetch_add(1, std::memory_order_seq_cst);
}
benchmark::ClobberMemory();
}
if (state.thread_index() == 0) {
state.SetItemsProcessed(state.iterations() *
static_cast<int64_t>(kOpsPerThread * state.threads()));
}
}
template <std::memory_order StoreOrder, std::memory_order LoadOrder>
void RunFlagHandoff(benchmark::State& state) {
double total_seconds = 0.0;
for (auto _ : state) {
state.PauseTiming();
std::atomic<int> turn{0};
std::uint64_t checksum = 0;
std::thread worker([&]() {
for (std::size_t round = 0; round < kPingPongRounds; ++round) {
while (turn.load(LoadOrder) != 1) {
}
checksum += round;
turn.store(0, StoreOrder);
}
});
state.ResumeTiming();
const auto t0 = std::chrono::steady_clock::now();
for (std::size_t round = 0; round < kPingPongRounds; ++round) {
while (turn.load(LoadOrder) != 0) {
}
checksum += round;
turn.store(1, StoreOrder);
}
while (turn.load(LoadOrder) != 0) {
}
const auto t1 = std::chrono::steady_clock::now();
state.PauseTiming();
worker.join();
benchmark::DoNotOptimize(checksum);
total_seconds += std::chrono::duration<double>(t1 - t0).count();
}
state.counters["handoffs_per_sec"] =
static_cast<double>(state.iterations()) * (2.0 * kPingPongRounds) / total_seconds;
}
void BM_FlagHandoffAcqRel(benchmark::State& state) {
RunFlagHandoff<std::memory_order_release, std::memory_order_acquire>(state);
}
void BM_FlagHandoffSeqCst(benchmark::State& state) {
RunFlagHandoff<std::memory_order_seq_cst, std::memory_order_seq_cst>(state);
}
template <std::memory_order FlagStoreOrder, std::memory_order FlagLoadOrder>
void RunPublishConsume(benchmark::State& state) {
double total_seconds = 0.0;
for (auto _ : state) {
state.PauseTiming();
std::atomic<std::uint64_t> payload{0};
std::atomic<std::uint32_t> ready{0};
std::uint64_t checksum = 0;
std::thread consumer([&]() {
for (std::size_t round = 0; round < kPublishRounds; ++round) {
while (ready.load(FlagLoadOrder) != 1u) {
}
checksum += payload.load(std::memory_order_relaxed);
ready.store(0u, std::memory_order_relaxed);
}
});
state.ResumeTiming();
const auto t0 = std::chrono::steady_clock::now();
for (std::size_t round = 0; round < kPublishRounds; ++round) {
while (ready.load(std::memory_order_relaxed) != 0u) {
}
payload.store(static_cast<std::uint64_t>(round + 1), std::memory_order_relaxed);
ready.store(1u, FlagStoreOrder);
}
consumer.join();
const auto t1 = std::chrono::steady_clock::now();
state.PauseTiming();
benchmark::DoNotOptimize(checksum);
total_seconds += std::chrono::duration<double>(t1 - t0).count();
}
state.counters["publishes_per_sec"] =
static_cast<double>(state.iterations()) * kPublishRounds / total_seconds;
}
void BM_PublishConsumeReleaseAcquire(benchmark::State& state) {
RunPublishConsume<std::memory_order_release, std::memory_order_acquire>(state);
}
void BM_PublishConsumeSeqCst(benchmark::State& state) {
RunPublishConsume<std::memory_order_seq_cst, std::memory_order_seq_cst>(state);
}
template <std::memory_order MetaLoadOrder, std::memory_order MetaStoreOrder>
class OrderedSpscRing {
public:
OrderedSpscRing() : buf_(kRingCapacity) {}
bool push(std::uint64_t value) {
const std::size_t head = head_.load(std::memory_order_relaxed);
const std::size_t next = (head + 1) & (kRingCapacity - 1);
if (next == tail_.load(MetaLoadOrder)) {
return false;
}
buf_[head] = value;
head_.store(next, MetaStoreOrder);
return true;
}
bool pop(std::uint64_t& value) {
const std::size_t tail = tail_.load(std::memory_order_relaxed);
if (tail == head_.load(MetaLoadOrder)) {
return false;
}
value = buf_[tail];
tail_.store((tail + 1) & (kRingCapacity - 1), MetaStoreOrder);
return true;
}
private:
std::vector<std::uint64_t> buf_;
alignas(64) std::atomic<std::size_t> head_{0};
alignas(64) std::atomic<std::size_t> tail_{0};
};
template <std::memory_order MetaLoadOrder, std::memory_order MetaStoreOrder>
void RunRingTransfer(benchmark::State& state) {
double total_seconds = 0.0;
for (auto _ : state) {
state.PauseTiming();
OrderedSpscRing<MetaLoadOrder, MetaStoreOrder> ring;
std::atomic<bool> start{false};
std::uint64_t checksum = 0;
std::thread producer([&]() {
while (!start.load(std::memory_order_acquire)) {
}
for (std::uint64_t value = 0; value < kRingRounds; ++value) {
while (!ring.push(value)) {
}
}
});
std::thread consumer([&]() {
while (!start.load(std::memory_order_acquire)) {
}
std::uint64_t value = 0;
for (std::size_t i = 0; i < kRingRounds; ++i) {
while (!ring.pop(value)) {
}
checksum += value;
}
});
state.ResumeTiming();
const auto t0 = std::chrono::steady_clock::now();
start.store(true, std::memory_order_release);
producer.join();
consumer.join();
const auto t1 = std::chrono::steady_clock::now();
state.PauseTiming();
benchmark::DoNotOptimize(checksum);
total_seconds += std::chrono::duration<double>(t1 - t0).count();
}
state.counters["ring_ops_per_sec"] =
static_cast<double>(state.iterations()) * kRingRounds / total_seconds;
}
void BM_RingTransferAcqRel(benchmark::State& state) {
RunRingTransfer<std::memory_order_acquire, std::memory_order_release>(state);
}
void BM_RingTransferSeqCst(benchmark::State& state) {
RunRingTransfer<std::memory_order_seq_cst, std::memory_order_seq_cst>(state);
}
template <std::memory_order FlagStoreOrder, std::memory_order FlagLoadOrder>
void RunMessagePassingLitmus(benchmark::State& state, const char* counter_prefix) {
for (auto _ : state) {
state.PauseTiming();
std::atomic<int> payload{0};
std::atomic<int> ready{0};
std::atomic<int> observed{-1};
std::barrier sync_point(3);
std::uint64_t bad_reads = 0;
std::thread producer([&]() {
for (std::size_t round = 0; round < kLitmusRounds; ++round) {
sync_point.arrive_and_wait();
payload.store(1, std::memory_order_relaxed);
ready.store(1, FlagStoreOrder);
sync_point.arrive_and_wait();
}
});
std::thread consumer([&]() {
for (std::size_t round = 0; round < kLitmusRounds; ++round) {
sync_point.arrive_and_wait();
while (ready.load(FlagLoadOrder) != 1) {
}
observed.store(payload.load(std::memory_order_relaxed), std::memory_order_relaxed);
sync_point.arrive_and_wait();
}
});
state.ResumeTiming();
for (std::size_t round = 0; round < kLitmusRounds; ++round) {
payload.store(0, std::memory_order_relaxed);
ready.store(0, std::memory_order_relaxed);
observed.store(-1, std::memory_order_relaxed);
sync_point.arrive_and_wait();
sync_point.arrive_and_wait();
if (observed.load(std::memory_order_relaxed) != 1) {
++bad_reads;
}
}
state.PauseTiming();
producer.join();
consumer.join();
state.counters[std::string(counter_prefix) + "_bad_reads"] =
static_cast<double>(bad_reads);
state.counters[std::string(counter_prefix) + "_bad_rate"] =
static_cast<double>(bad_reads) / static_cast<double>(kLitmusRounds);
}
state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(kLitmusRounds));
}
void BM_MessagePassingRelaxed(benchmark::State& state) {
RunMessagePassingLitmus<std::memory_order_relaxed, std::memory_order_relaxed>(
state, "relaxed");
}
void BM_MessagePassingReleaseAcquire(benchmark::State& state) {
RunMessagePassingLitmus<std::memory_order_release, std::memory_order_acquire>(
state, "release_acquire");
}
template <std::memory_order StoreOrder, std::memory_order LoadOrder>
void RunStoreBufferingLitmus(benchmark::State& state, const char* counter_prefix) {
for (auto _ : state) {
state.PauseTiming();
std::atomic<int> x{0};
std::atomic<int> y{0};
std::atomic<int> r1{-1};
std::atomic<int> r2{-1};
std::barrier sync_point(3);
std::uint64_t both_zero = 0;
std::thread t1([&]() {
for (std::size_t round = 0; round < kLitmusRounds; ++round) {
sync_point.arrive_and_wait();
x.store(1, StoreOrder);
r1.store(y.load(LoadOrder), std::memory_order_relaxed);
sync_point.arrive_and_wait();
}
});
std::thread t2([&]() {
for (std::size_t round = 0; round < kLitmusRounds; ++round) {
sync_point.arrive_and_wait();
y.store(1, StoreOrder);
r2.store(x.load(LoadOrder), std::memory_order_relaxed);
sync_point.arrive_and_wait();
}
});
state.ResumeTiming();
for (std::size_t round = 0; round < kLitmusRounds; ++round) {
x.store(0, std::memory_order_relaxed);
y.store(0, std::memory_order_relaxed);
r1.store(-1, std::memory_order_relaxed);
r2.store(-1, std::memory_order_relaxed);
sync_point.arrive_and_wait();
sync_point.arrive_and_wait();
if (r1.load(std::memory_order_relaxed) == 0 &&
r2.load(std::memory_order_relaxed) == 0) {
++both_zero;
}
}
state.PauseTiming();
t1.join();
t2.join();
state.counters[std::string(counter_prefix) + "_both_zero"] =
static_cast<double>(both_zero);
state.counters[std::string(counter_prefix) + "_both_zero_rate"] =
static_cast<double>(both_zero) / static_cast<double>(kLitmusRounds);
}
state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(kLitmusRounds));
}
void BM_StoreBufferingRelaxed(benchmark::State& state) {
RunStoreBufferingLitmus<std::memory_order_relaxed, std::memory_order_relaxed>(
state, "relaxed");
}
void BM_StoreBufferingReleaseAcquire(benchmark::State& state) {
RunStoreBufferingLitmus<std::memory_order_release, std::memory_order_acquire>(
state, "release_acquire");
}
void BM_StoreBufferingSeqCst(benchmark::State& state) {
RunStoreBufferingLitmus<std::memory_order_seq_cst, std::memory_order_seq_cst>(
state, "seq_cst");
}
} // namespace
BENCHMARK(BM_FetchAddRelaxed)->Threads(1)->Threads(2)->Threads(4)->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_FetchAddAcqRel)->Threads(1)->Threads(2)->Threads(4)->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_FetchAddSeqCst)->Threads(1)->Threads(2)->Threads(4)->Unit(benchmark::kMicrosecond);
BENCHMARK(BM_FlagHandoffAcqRel)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_FlagHandoffSeqCst)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_PublishConsumeReleaseAcquire)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_PublishConsumeSeqCst)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_RingTransferAcqRel)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_RingTransferSeqCst)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_MessagePassingRelaxed)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_MessagePassingReleaseAcquire)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_StoreBufferingRelaxed)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_StoreBufferingReleaseAcquire)->Unit(benchmark::kMillisecond);
BENCHMARK(BM_StoreBufferingSeqCst)->Unit(benchmark::kMillisecond);