-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutex_sync_falsesharing.cpp
74 lines (61 loc) · 2.35 KB
/
mutex_sync_falsesharing.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
#include <benchmark/benchmark.h>
#include <mutex>
#include <atomic>
namespace mutex_false_sharing
{
volatile long mutex_add_before = 0;
std::mutex mutex_lock;
volatile long mutex_add_after = 0;
volatile long *mutex_add_far_p = new long(0);
volatile long& mutex_add_far = *mutex_add_far_p;
static void BM_FalseSharingMutexAddBefore(benchmark::State& state) {
if (state.thread_index == 0) {
mutex_add_before = 0;
}
while (state.KeepRunning()) {
std::lock_guard<std::mutex> guard(mutex_lock);
mutex_add_before += 1;
benchmark::DoNotOptimize(mutex_add_before);
}
benchmark::ClobberMemory();
}
static void BM_FalseSharingMutexAddAfter(benchmark::State& state) {
if (state.thread_index == 0) {
mutex_add_after = 0;
}
while (state.KeepRunning()) {
std::lock_guard<std::mutex> guard(mutex_lock);
mutex_add_after += 1;
benchmark::DoNotOptimize(mutex_add_after);
}
benchmark::ClobberMemory();
}
static void BM_FalseSharingMutexAddFar(benchmark::State& state) {
if (state.thread_index == 0) {
mutex_add_far = 0;
}
while (state.KeepRunning()) {
std::lock_guard<std::mutex> guard(mutex_lock);
mutex_add_far += 1;
benchmark::DoNotOptimize(mutex_add_far);
}
benchmark::ClobberMemory();
}
#define ARGS(N) ->Threads(N)->UseRealTime();
// Register the function as a benchmark
BENCHMARK(BM_FalseSharingMutexAddBefore) ARGS(1);
BENCHMARK(BM_FalseSharingMutexAddBefore) ARGS(2);
BENCHMARK(BM_FalseSharingMutexAddBefore) ARGS(4);
BENCHMARK(BM_FalseSharingMutexAddBefore) ARGS(8);
BENCHMARK(BM_FalseSharingMutexAddBefore) ARGS(12);
BENCHMARK(BM_FalseSharingMutexAddAfter) ARGS(1);
BENCHMARK(BM_FalseSharingMutexAddAfter) ARGS(2);
BENCHMARK(BM_FalseSharingMutexAddAfter) ARGS(4);
BENCHMARK(BM_FalseSharingMutexAddAfter) ARGS(8);
BENCHMARK(BM_FalseSharingMutexAddAfter) ARGS(12);
BENCHMARK(BM_FalseSharingMutexAddFar) ARGS(1);
BENCHMARK(BM_FalseSharingMutexAddFar) ARGS(2);
BENCHMARK(BM_FalseSharingMutexAddFar) ARGS(4);
BENCHMARK(BM_FalseSharingMutexAddFar) ARGS(8);
BENCHMARK(BM_FalseSharingMutexAddFar) ARGS(12);
} // namespace false_sharing