FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
LFStack/LFStack/main.cpp at master · NotACat1/LFStack · GitHub
NotACat1
/
LFStack
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
LFStack
/
LFStack
/
main.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
168 lines (135 loc) · 4.99 KB
Breadcrumbs
LFStack
/
LFStack
/
main.cpp
Copy path
File metadata and controls
168 lines (135 loc) · 4.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#includ
e
<benchmark/benchmark.
h
>
#
include
<
memory
>
#
include
<
thread
>
#
include
<
vector
>
/*
*
* Include custom lock-free / concurrent stack implementations.
* Each implementation uses a different memory reclamation strategy.
*/
#
include
"
CounterReclamationStack.hpp
"
#
include
"
HazardPointerStack.hpp
"
#
include
"
SplitRefCountStack.hpp
"
#
include
"
AtomicSharedStack.hpp
"
//
--- TEST DATA TYPES ---
/*
*
* Represents a relatively heavy payload (~1 KB).
* Used to evaluate the impact of object size on throughput and memory behavior.
*/
struct
HeavyPayload
{
int
data[
256
];
//
Initialize memory to avoid undefined behavior and sanitizer warnings
HeavyPayload
() {
std::fill
(
std::begin
(data),
std::end
(data),
0
);
}
};
/*
*
* Lightweight payload used to measure raw synchronization overhead.
*/
using
LightPayload =
int
;
//
--- GENERIC BENCHMARK TEMPLATE ---
/*
*
* @brief Producer-Consumer benchmark for concurrent stack implementations.
*
* This benchmark models a typical multi-threaded workload where:
* - Half of the threads act as producers (push operations)
* - The other half act as consumers (pop operations)
*
* @tparam StackType Concrete stack implementation
* @tparam Payload Type of stored data (lightweight or heavyweight)
*
* @param state Google Benchmark state object
*/
template
<
typename
StackType,
typename
Payload>
void
BM_Stack_ProducerConsumer
(benchmark::State& state) {
/*
*
* Function-local static ensures:
* - Thread-safe initialization (since C++11)
* - A single shared stack instance across all threads in the benchmark
*
* IMPORTANT:
* This assumes a balanced workload (push == pop),
* so the stack is expected to be empty after each full benchmark run.
*/
static
StackType shared_stack;
/*
*
* Thread role assignment:
* - Even thread indices → Producers
* - Odd thread indices → Consumers
*/
if
(state.
thread_index
() %
2
==
0
) {
//
--- PRODUCER PATH ---
/*
*
* Pre-constructed payload to avoid per-iteration allocation cost.
* Value-initialization ensures deterministic content.
*/
Payload payload{};
for
(
auto
_ : state) {
shared_stack.
push
(payload);
}
}
else
{
//
--- CONSUMER PATH ---
for
(
auto
_ : state) {
std::shared_ptr<Payload> result;
/*
*
* pop() returns nullptr if the stack is empty.
* Use a spin-wait loop with cooperative yielding.
*
* NOTE:
* - This introduces contention and scheduler interaction.
* - Suitable for stress-testing lock-free behavior under load.
*/
while
((result = shared_stack.
pop
()) ==
nullptr
) {
std::this_thread::yield
();
}
/*
*
* Prevent the compiler from optimizing away the result usage.
* Ensures the pop operation is fully accounted for.
*/
benchmark::DoNotOptimize
(result);
}
}
/*
*
* Throughput metric: total number of processed operations.
*
* Only one thread updates the counter to avoid redundant writes.
*
* NOTE:
* This currently counts all threads equally.
* If precise accounting is required, consider counting only
* producer or consumer operations separately.
*/
if
(state.
thread_index
() ==
0
) {
state.
SetItemsProcessed
(state.
iterations
() * state.
threads
());
}
}
//
--- BENCHMARK REGISTRATION ---
/*
*
* Fixed configuration:
* 8 threads total → 4 producers + 4 consumers
*/
#
define
STACK_THREADS
->
Threads
(
8
)
//
--- LIGHT PAYLOAD (int) ---
BENCHMARK_TEMPLATE(BM_Stack_ProducerConsumer, LFStack_ThreadCounter<
int
>,
int
)
->Name(
"
Pop/CounterReclamationStack/Light
"
) STACK_THREADS;
BENCHMARK_TEMPLATE
(BM_Stack_ProducerConsumer, LFStack_HazardPtr<
int
>,
int
)
->
Name
(
"
Pop/HazardPointerStack/Light
"
) STACK_THREADS;
BENCHMARK_TEMPLATE
(BM_Stack_ProducerConsumer, LFStack_SplitRefCount<
int
>,
int
)
->
Name
(
"
Pop/SplitRefCountStack/Light
"
) STACK_THREADS;
BENCHMARK_TEMPLATE
(BM_Stack_ProducerConsumer, LFStack_AtomicSharedPtr<
int
>,
int
)
->
Name
(
"
Pop/AtomicSharedStack/Light
"
) STACK_THREADS;
//
--- HEAVY PAYLOAD (~1 KB) ---
BENCHMARK_TEMPLATE
(BM_Stack_ProducerConsumer, LFStack_ThreadCounter<HeavyPayload>, HeavyPayload)
->
Name
(
"
Pop/CounterReclamationStack/Heavy
"
) STACK_THREADS;
BENCHMARK_TEMPLATE
(BM_Stack_ProducerConsumer, LFStack_HazardPtr<HeavyPayload>, HeavyPayload)
->
Name
(
"
Pop/HazardPointerStack/Heavy
"
) STACK_THREADS;
BENCHMARK_TEMPLATE
(BM_Stack_ProducerConsumer, LFStack_SplitRefCount<HeavyPayload>, HeavyPayload)
->
Name
(
"
Pop/SplitRefCountStack/Heavy
"
) STACK_THREADS;
BENCHMARK_TEMPLATE
(BM_Stack_ProducerConsumer, LFStack_AtomicSharedPtr<HeavyPayload>, HeavyPayload)
->
Name
(
"
Pop/AtomicSharedStack/Heavy
"
) STACK_THREADS;
//
--- ENTRY POINT ---
/*
*
* Main entry point for Google Benchmark.
* Automatically registers and executes all defined benchmarks.
*/
BENCHMARK_MAIN
();
Back
|
FazBrowse Home
|
New Git URL