forked from facebookarchive/Rseq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RseqBenchmark.cpp
464 lines (409 loc) · 15.2 KB
/
RseqBenchmark.cpp
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/*
`./rseq_benchmark` for usage.
The output of lscpu on my machine:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 32
On-line CPU(s) list: 0-31
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 2
NUMA node(s): 2
Vendor ID: GenuineIntel
CPU family: 6
Model: 45
Model name: Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz
Stepping: 6
CPU MHz: 2201.000
CPU max MHz: 2201.0000
CPU min MHz: 1200.0000
BogoMIPS: 4405.46
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 20480K
NUMA node0 CPU(s): 0-7,16-23
NUMA node1 CPU(s): 8-15,24-31
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx lahf_lm ida arat epb pln pts dtherm tpr_shadow vnmi flexpriority ept vpid xsaveopt
According to some rough benchmarks:
When there are lots of threads
- Counter increments using rseq stores are about 36% slower than ones using
stack variables.
- Counter increments using rseq stores are about 4.2x faster than ones using
per-cpu atomics.
When there is only one thread:
- Counter increments using rseq stores are about 9.8% slower than ones using
stack variables.
- Counter increments using rseq stores are about 5.3x faster than ones using
per-cpu atomics.
The output of `./rseq_benchmark threadLocal,rseq,atomicsCachedCpu 256 100000000`:
===========================================================
Benchmarking Thread-local operations only (no sharing)
Increments: 25600000000
Seconds: 2.739452
TSC ticks: 6026707360
Single-CPU TSC ticks per increment: 0.235418
Global TSC ticks per increment: 7.533384
===========================================================
===========================================================
Benchmarking Per-cpu restartable sequences
Increments: 25600000000
Seconds: 3.732481
TSC ticks: 8211339968
Single-CPU TSC ticks per increment: 0.320755
Global TSC ticks per increment: 10.264175
===========================================================
===========================================================
Benchmarking Per-cpu atomics (with cached sched_getcpu calls)
Increments: 25600000000
Seconds: 15.678768
TSC ticks: 34492797698
Single-CPU TSC ticks per increment: 1.347375
Global TSC ticks per increment: 43.115997
===========================================================
The output of `./rseq_benchmark threadLocal,rseq,atomicsCachedCpu 1 100000000`:
===========================================================
Benchmarking Thread-local operations only (no sharing)
Increments: 100000000
Seconds: 0.255986
TSC ticks: 563156988
Single-CPU TSC ticks per increment: 5.631570
Global TSC ticks per increment: 180.210236
===========================================================
===========================================================
Benchmarking Per-cpu restartable sequences
Increments: 100000000
Seconds: 0.281085
TSC ticks: 618375013
Single-CPU TSC ticks per increment: 6.183750
Global TSC ticks per increment: 197.880004
===========================================================
===========================================================
Benchmarking Per-cpu atomics (with cached sched_getcpu calls)
Increments: 100000000
Seconds: 1.478343
TSC ticks: 3252272957
Single-CPU TSC ticks per increment: 32.522730
Global TSC ticks per increment: 1040.727346
===========================================================
*/
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <thread>
#include <vector>
#include "rseq/Rseq.h"
#include "rseq/internal/NumCpus.h"
constexpr int kCachelineSize = 128;
struct PercpuCounter {
std::atomic<std::uint64_t> atomicCounter;
rseq::Value<std::uint64_t> rseqCounter;
std::mutex mu;
char padding[
kCachelineSize
- sizeof(atomicCounter)
- sizeof(rseqCounter)
- sizeof(mu)];
};
std::vector<PercpuCounter> counterByCpu;
char padding1[kCachelineSize - sizeof(counterByCpu)];
std::mutex contendedMu;
char padding2[kCachelineSize - sizeof(contendedMu)];
std::atomic<std::uint64_t> contendedCounter;
enum TestType {
kLongCriticalSection,
kContendedAtomics,
kContendedLocks,
kRseq,
kAtomics,
kAtomicsCachedCpu,
kLocks,
kLocksCachedCpu,
kThreadLocal,
kTestTypeEnd,
};
const char* testTypeString(TestType testType) {
switch (testType) {
case kLongCriticalSection:
return "Long critical section";
case kContendedAtomics:
return "Contended atomics";
case kContendedLocks:
return "Contended locks";
case kRseq:
return "Per-cpu restartable sequences";
case kAtomics:
return "Per-cpu atomics";
case kAtomicsCachedCpu:
return "Per-cpu atomics (with cached sched_getcpu calls)";
case kLocks:
return "Per-cpu locks";
case kLocksCachedCpu:
return "Per-cpu locks (with cached sched_getcpu calls)";
case kThreadLocal:
return "Thread-local operations only (no sharing)";
case kTestTypeEnd:
/* should never happen */
return nullptr;
}
return nullptr;
}
void doIncrementsLongCriticalSection(std::uint64_t numIncrements) {
std::lock_guard<std::mutex> lg(contendedMu);
for (std::uint64_t i = 0; i < numIncrements; ++i) {
contendedCounter.store(contendedCounter.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
}
}
void doIncrementsContendedAtomics(std::uint64_t numIncrements) {
for (std::uint64_t i = 0; i < numIncrements; ++i) {
std::uint64_t old = contendedCounter.load();
while (!contendedCounter.compare_exchange_weak(old, old + 1)) {
}
}
}
void doIncrementsContendedLocks(std::uint64_t numIncrements) {
for (std::uint64_t i = 0; i < numIncrements; ++i) {
std::lock_guard<std::mutex> lg(contendedMu);
contendedCounter.store(contendedCounter.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
}
}
void doIncrementsRseq(std::uint64_t numIncrements) {
for (std::uint64_t i = 0; i < numIncrements; ++i) {
bool success = false;
do {
int cpu = rseq::begin();
std::uint64_t curVal = counterByCpu[cpu].rseqCounter.load();
success = rseq::store(&counterByCpu[cpu].rseqCounter, curVal + 1);
} while (!success);
}
}
void doIncrementsAtomics(std::uint64_t numIncrements) {
for (std::uint64_t i = 0; i < numIncrements; ++i) {
std::uint64_t old;
int cpu;
do {
cpu = sched_getcpu();
old = counterByCpu[cpu].atomicCounter.load();
} while (!counterByCpu[cpu].atomicCounter.compare_exchange_weak(
old, old + 1));
}
}
void doIncrementsAtomicsCachedCpu(std::uint64_t numIncrements) {
for (std::uint64_t i = 0; i < numIncrements;) {
int cpu = sched_getcpu();
for (int j = 0; j < 100 && i < numIncrements; ++i, ++j) {
std::uint64_t old = counterByCpu[cpu].atomicCounter.load();
if (!counterByCpu[cpu].atomicCounter.compare_exchange_weak(
old, old + 1)) {
break;
}
}
}
}
void doIncrementsLocks(std::uint64_t numIncrements) {
for (std::uint64_t i = 0; i < numIncrements; ++i) {
int cpu = sched_getcpu();
std::lock_guard<std::mutex> lg(counterByCpu[cpu].mu);
counterByCpu[cpu].atomicCounter.store(
counterByCpu[cpu].atomicCounter.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
}
}
void doIncrementsLocksCachedCpu(std::uint64_t numIncrements) {
for (std::uint64_t i = 0; i < numIncrements;) {
int cpu = sched_getcpu();
for (int j = 0; j < 100 && i < numIncrements; ++i, ++j) {
std::lock_guard<std::mutex> lg(counterByCpu[cpu].mu);
counterByCpu[cpu].atomicCounter.store(
counterByCpu[cpu].atomicCounter.load(std::memory_order_relaxed) + 1,
std::memory_order_relaxed);
}
}
}
void doIncrementsThreadLocal(std::uint64_t numIncrements) {
volatile std::uint64_t counter = 0;
for (std::uint64_t i = 0; i < numIncrements; ++i) {
std::uint64_t oldVal = counter;
counter = oldVal + 1;
}
counterByCpu[0].atomicCounter.fetch_add(counter);
}
void printErrorIfNotEqual(std::uint64_t expected, std::uint64_t actual) {
if (expected != actual) {
std::printf(
"Error: actual increment count %lu "
"does not match expected increment count %lu.\n",
actual,
expected);
}
}
std::uint64_t rdtscp() {
std::uint32_t ecx;
std::uint64_t rax,rdx;
asm volatile ( "rdtscp\n" : "=a" (rax), "=d" (rdx), "=c" (ecx) : : );
return (rdx << 32) + rax;
}
void runTest(
TestType testType,
std::uint64_t numThreads,
std::uint64_t numIncrements) {
contendedCounter.store(0);
for (unsigned i = 0; i < counterByCpu.size(); ++i) {
counterByCpu[i].atomicCounter.store(0);
counterByCpu[i].rseqCounter.store(0);
}
void (*benchmarkThreadFunc)(std::uint64_t) =
testType == kLongCriticalSection ? doIncrementsLongCriticalSection :
testType == kContendedAtomics ? doIncrementsContendedAtomics :
testType == kContendedLocks ? doIncrementsContendedLocks :
testType == kRseq ? doIncrementsRseq :
testType == kAtomics ? doIncrementsAtomics :
testType == kAtomicsCachedCpu ? doIncrementsAtomicsCachedCpu :
testType == kLocks ? doIncrementsLocks :
testType == kLocksCachedCpu ? doIncrementsLocksCachedCpu :
testType == kThreadLocal ? doIncrementsThreadLocal :
nullptr;
std::printf("===========================================================\n");
std::printf("Benchmarking %s\n", testTypeString(testType));
auto beginTime = std::chrono::high_resolution_clock::now();
std::uint64_t beginCycles = rdtscp();
std::vector<std::thread> threads(numThreads);
for (unsigned i = 0; i < numThreads; ++i) {
threads[i] = std::thread(benchmarkThreadFunc, numIncrements);
}
for (unsigned i = 0; i < numThreads; ++i) {
threads[i].join();
}
std::uint64_t endCycles = rdtscp();
auto endTime = std::chrono::high_resolution_clock::now();
std::uint64_t expectedIncrements = numThreads * numIncrements;
std::uint64_t actualIncrements = contendedCounter.load();
for (std::uint64_t i = 0; i < rseq::internal::numCpus(); ++i) {
actualIncrements += counterByCpu[i].atomicCounter.load();
actualIncrements += counterByCpu[i].rseqCounter.load();
}
printErrorIfNotEqual(expectedIncrements, actualIncrements);
std::chrono::nanoseconds duration = endTime - beginTime;
std::uint64_t ns = duration.count();
std::uint64_t cycles = endCycles - beginCycles;
double seconds = static_cast<double>(ns) / 1000000000.0;
std::printf("Increments: %lu \n", actualIncrements);
std::printf("Seconds: %f\n", seconds);
std::printf("TSC ticks: %lu \n", cycles);
double myCycles = static_cast<double>(cycles) / actualIncrements;
std::printf("Single-CPU TSC ticks per increment: %f\n", myCycles);
std::printf("Global TSC ticks per increment: %f\n",
rseq::internal::numCpus() * myCycles);
std::printf("===========================================================\n");
}
const char* usage = R"(Usage: %s benchmarks num_threads increments_per_thread
Where 'benchmarks' is either 'all', or a comma-separated list containing the
benchmarks to run:
longCriticalSection: Each thread acquires a single shared lock, does all
its increments, and releases the lock.
contendedAtomics: Each thread updates a global counter with a CAS.
contendedLocks: Each thread acquires and releases a global lock for
counter increment.
rseq: Threads increment cpu-local counters using restartable
sequences.
atomics: Threads increment cpu-local counters using CASs.
atomicsCachedCpu: Threads increment cpu-local counters using CASs, but
only call sched_getcpu once every 100 increments (or
until contention is detected).
locks: Threads increment cpu-local counters, protecting their
increments with locks.
locksCachedCPu: Threads increment cpu-local counters, protecting their
increments with locks, but only call sched_getcpu once
every 100 increments.
threadLocal: Threads increment thread-local counters, with no
synchronization.
)";
std::vector<TestType> parseBenchmarks(const char* benchmarks) {
if (!strcmp(benchmarks, "all")) {
return {
kLongCriticalSection,
kContendedAtomics,
kContendedLocks,
kRseq,
kAtomics,
kAtomicsCachedCpu,
kLocks,
kLocksCachedCpu,
kThreadLocal
};
}
std::vector<TestType> result;
const char* benchmarksEnd = benchmarks + strlen(benchmarks);
const char* tokBegin = benchmarks;
while (true) {
const char* tokEnd = std::strpbrk(tokBegin, ",");
if (tokEnd == nullptr) {
tokEnd = benchmarksEnd;
}
auto matches
= [&](const char* str) { return std::equal(tokBegin, tokEnd, str); };
TestType testType =
matches("longCriticalSection") ? kLongCriticalSection :
matches("contendedAtomics") ? kContendedAtomics :
matches("contendedLocks") ? kContendedLocks :
matches("rseq") ? kRseq :
matches("atomics") ? kAtomics :
matches("atomicsCachedCpu") ? kAtomicsCachedCpu :
matches("locks") ? kLocks :
matches("locksCachedCpu") ? kLocksCachedCpu :
matches("threadLocal") ? kThreadLocal :
kTestTypeEnd;
if (testType == kTestTypeEnd) {
std::printf(
"Error: unknown benchmark type at the beginning of \"%s\"\n",
tokBegin);
std::exit(1);
}
result.push_back(testType);
if (tokEnd == benchmarksEnd) {
break;
}
tokBegin = tokEnd + 1;
}
return result;
}
int main(int argc, char** argv) {
if (argc != 4) {
std::printf(usage, argv[0]);
std::exit(1);
}
std::uint64_t numThreads;
std::uint64_t numIncrements;
std::vector<TestType> benchmarks = parseBenchmarks(argv[1]);
numThreads = atol(argv[2]);
numIncrements = atol(argv[3]);
if (numThreads == 0 || numIncrements == 0) {
std::printf("Error: invalid value for threads or increments\n");
std::exit(1);
}
// PercpuCounter objects aren't moveable, so we construct a vector then swap
// it with the global one.
std::vector<PercpuCounter> p(rseq::internal::numCpus());
counterByCpu.swap(p);
for (TestType benchmark : benchmarks) {
runTest(benchmark, numThreads, numIncrements);
}
return 0;
}