-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
250 lines (207 loc) · 6.65 KB
/
main.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
#include <iostream>
#include <thread>
#include <new>
#if __x86_64__ || _M_X64
#include "fast_queue_x86_64.h"
#elif __aarch64__ || _M_ARM64
#include "fast_queue_arm64.h"
#else
#error Architecture not supported
#endif
#include "deaod_spsc/spsc_queue.hpp"
#include "pin_thread.h"
#define QUEUE_MASK 0b1111111111
#define L1_CACHE_LINE 64
#define TEST_TIME_DURATION_SEC 20
//Run the consumer on CPU
#define CONSUMER_CPU 1
//Run the producer on CPU
#define PRODUCER_CPU 3
std::atomic<uint64_t> gActiveConsumer = 0;
std::atomic<uint64_t> gCounter = 0;
bool gStartBench = false;
bool gActiveProducer = true;
class MyObject {
public:
uint64_t mIndex;
};
/// -----------------------------------------------------------
///
/// deaodSPSC section Start
///
/// -----------------------------------------------------------
void deaodSPSCProducer(deaod::spsc_queue<MyObject*, QUEUE_MASK, 6> *pQueue, int32_t aCPU) {
if (!pinThread(aCPU)) {
std::cout << "Pin CPU fail. " << std::endl;
return;
}
while (!gStartBench) {
#ifdef _MSC_VER
__nop();
#else
asm volatile ("NOP");
#endif
}
uint64_t lCounter = 0;
while (gActiveProducer) {
auto lTheObject = new MyObject();
lTheObject->mIndex = lCounter++;
bool lAbleToPush = false;
while (!lAbleToPush && gActiveProducer) {
lAbleToPush = pQueue->push(std::move(lTheObject));
}
}
}
void deaodSPSCConsumer(deaod::spsc_queue<MyObject*, QUEUE_MASK, 6> *pQueue, int32_t aCPU) {
if (!pinThread(aCPU)) {
std::cout << "Pin CPU fail. " << std::endl;
gActiveConsumer--;
return;
}
uint64_t lCounter = 0;
while (true) {
MyObject* lResult = nullptr;
bool lAbleToPop = false;
while (!lAbleToPop && gActiveProducer) {
lAbleToPop = pQueue->pop(lResult);
}
if (lResult == nullptr) {
break;
}
if (lResult->mIndex != lCounter) {
std::cout << "Queue item error" << std::endl;
}
lCounter++;
delete lResult;
}
gCounter += lCounter;
gActiveConsumer--;
}
/// -----------------------------------------------------------
///
/// deaodSPSC section End
///
/// -----------------------------------------------------------
/// -----------------------------------------------------------
///
/// FastQueue section Start
///
/// -----------------------------------------------------------
void fastQueueProducer(FastQueue<MyObject*, QUEUE_MASK, L1_CACHE_LINE> *pQueue, int32_t aCPU) {
if (!pinThread(aCPU)) {
std::cout << "Pin CPU fail. " << std::endl;
return;
}
while (!gStartBench) {
#ifdef _MSC_VER
__nop();
#else
asm volatile ("NOP");
#endif
}
uint64_t lCounter = 0;
while (gActiveProducer) {
auto lTheObject = new MyObject();
lTheObject->mIndex = lCounter++;
pQueue->push(lTheObject);
}
pQueue->stopQueue();
}
void fastQueueConsumer(FastQueue<MyObject*, QUEUE_MASK, L1_CACHE_LINE> *pQueue, int32_t aCPU) {
if (!pinThread(aCPU)) {
std::cout << "Pin CPU fail. " << std::endl;
gActiveConsumer--;
return;
}
uint64_t lCounter = 0;
while (true) {
MyObject* pResult = nullptr;
pQueue->pop(pResult);
if (pResult == nullptr) {
break;
}
if (pResult->mIndex != lCounter) {
std::cout << "Queue item error. got: " << pResult->mIndex << " expected: " << lCounter << std::endl;
}
lCounter++;
delete pResult;
}
gCounter += lCounter;
gActiveConsumer--;
}
/// -----------------------------------------------------------
///
/// FastQueue section End
///
/// -----------------------------------------------------------
int main() {
///
/// DeaodSPSC test ->
///
// Create the queue
auto deaodSPSC = new deaod::spsc_queue<MyObject*, QUEUE_MASK, 6>();
// Start the consumer(s) / Producer(s)
gActiveConsumer++;
std::thread([deaodSPSC] { deaodSPSCConsumer(deaodSPSC, CONSUMER_CPU); }).detach();
std::thread([deaodSPSC] { deaodSPSCProducer(deaodSPSC, PRODUCER_CPU); }).detach();
// Wait for the OS to actually get it done.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// Start the test
std::cout << "DeaodSPSC pointer test started." << std::endl;
gStartBench = true;
std::this_thread::sleep_for(std::chrono::seconds(TEST_TIME_DURATION_SEC));
// End the test
gActiveProducer = false;
std::cout << "DeaodSPSC pointer test ended." << std::endl;
// Wait for the consumers to 'join'
// Why not the classic join? I prepared for a multi thread case I need this function for.
while (gActiveConsumer) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
// Garbage collect the queue
delete deaodSPSC;
// Print the result.
std::cout << "DeaodSPSC Transactions -> " << gCounter / TEST_TIME_DURATION_SEC << "/s" << std::endl;
// Zero the test parameters.
gStartBench = false;
gActiveProducer = true;
gCounter = 0;
gActiveConsumer = 0;
///
/// FastQueue test ->
///
// Create the queue
auto lFastQueue = new FastQueue<MyObject*, QUEUE_MASK, L1_CACHE_LINE>();
// Start the consumer(s) / Producer(s)
gActiveConsumer++;
std::thread([lFastQueue] { return fastQueueConsumer(lFastQueue, CONSUMER_CPU); }).detach();
std::thread([lFastQueue] { return fastQueueProducer(lFastQueue, PRODUCER_CPU); }).detach();
// Wait for the OS to actually get it done.
std::this_thread::sleep_for(std::chrono::milliseconds(10));
// Start the test
std::cout << "FastQueue pointer test started." << std::endl;
gStartBench = true;
std::this_thread::sleep_for(std::chrono::seconds(TEST_TIME_DURATION_SEC));
// End the test
gActiveProducer = false;
std::cout << "FastQueue pointer test ended." << std::endl;
// Wait for the consumers to 'join'
// Why not the classic join? I prepared for a multi thread case I need this function for.
while (gActiveConsumer) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
// Garbage collect the queue
delete lFastQueue;
// Print the result.
std::cout << "FastQueue Transactions -> " << gCounter / TEST_TIME_DURATION_SEC << "/s" << std::endl;
// Zero the test parameters.
gStartBench = false;
gActiveProducer = true;
gCounter = 0;
gActiveConsumer = 0;
// Create the queue
auto lObject = std::make_unique<int>(8);
//auto lFastQueueTest = new FastQueue<std::unique_ptr<int>, QUEUE_MASK, L1_CACHE_LINE>();
//std::cout << std::cref(lFastQueueTest) << std::endl;
return 0;
}