-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfalse_sharing.cpp
More file actions
93 lines (81 loc) · 2.41 KB
/
false_sharing.cpp
File metadata and controls
93 lines (81 loc) · 2.41 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
// clang++ -O3 -std=c++17 -pthread false_sharing_bench.cpp -o bench
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
#include <iomanip>
#include <cstring>
#if defined(__APPLE__)
#include <mach/thread_policy.h>
#include <mach/mach.h>
#elif defined(__linux__)
#include <pthread.h>
#include <sched.h>
#endif
// ====================== Case 1: False sharing ======================
struct FS_Bad {
std::atomic<long long> a{0};
std::atomic<long long> b{0};
};
// ====================== Case 2: Padded (no false sharing) ======================
struct alignas(64) FS_Good {
std::atomic<long long> a{0};
char pad1[64 - sizeof(std::atomic<long long>)];
std::atomic<long long> b{0};
char pad2[64 - sizeof(std::atomic<long long>)];
};
#if defined(__APPLE__)
void pin_thread_macos(int core_id) {
thread_affinity_policy_data_t policy = {core_id};
thread_policy_set(
mach_thread_self(),
THREAD_AFFINITY_POLICY,
(thread_policy_t)&policy,
1);
}
#elif defined(__linux__)
void pin_thread_linux(int core_id) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
}
#endif
template <typename T>
void run_benchmark(const char* name) {
T data;
constexpr size_t ITER = 200'000'000; // 2e8 iterations
auto start = std::chrono::high_resolution_clock::now();
std::thread t1([&] {
#if defined(__APPLE__)
pin_thread_macos(0);
#elif defined(__linux__)
pin_thread_linux(0);
#endif
for (size_t i = 0; i < ITER; ++i)
data.a.fetch_add(1, std::memory_order_relaxed);
});
std::thread t2([&] {
#if defined(__APPLE__)
pin_thread_macos(1);
#elif defined(__linux__)
pin_thread_linux(1);
#endif
for (size_t i = 0; i < ITER; ++i)
data.b.fetch_add(1, std::memory_order_relaxed);
});
t1.join();
t2.join();
auto end = std::chrono::high_resolution_clock::now();
double ms = std::chrono::duration<double, std::milli>(end - start).count();
std::cout << std::left << std::setw(20) << name
<< " time: " << std::fixed << std::setprecision(2)
<< ms << " ms" << std::endl;
}
int main() {
std::cout << "Running atomic false-sharing test...\n";
run_benchmark<FS_Bad>("❌ False Sharing");
run_benchmark<FS_Good>("✅ No False Sharing");
return 0;
}