forked from poorpool/lock_or_rte_ring_benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring_spsc_rte.cc
291 lines (256 loc) · 8.73 KB
/
ring_spsc_rte.cc
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
#include "3rdparty/ring.h"
#include "3rdparty/wyhash.h"
#include <ankerl/unordered_dense.h>
#include <cstdint>
#include <functional>
#include <iostream>
#include <mutex>
#include <pthread.h>
#include <random>
#include <shared_mutex>
#include <string>
#include <sys/time.h>
#include <thread>
#include <vector>
using std::string;
using std::thread;
using std::vector;
using ReadLock = std::shared_lock<std::shared_mutex>;
using WriteLock = std::unique_lock<std::shared_mutex>;
constexpr int kOpsPerThread = 25000000; // 每个线程执行多少次读/写操作
constexpr int kPullNumber = 32; // 连续 pull 几下
pthread_barrier_t barrier1, barrier2, barrier3;
enum OP_TYPE { kOpTypeRead = 1, kOpTypeWrite = 2 };
struct Request {
OP_TYPE type;
string key;
int64_t value;
};
struct __attribute__((aligned(64))) PaddingInt { // cacheline 对齐
int val;
};
struct GlobalContext {
int thread_num;
int start_core;
vector<thread> threads;
vector<PaddingInt> finished_cnt; // thread_num 个
vector<vector<rte_ring *>> rings; // thread_num^2 个
};
GlobalContext g_ctx;
int64_t GetUs() {
timeval tv;
gettimeofday(&tv, nullptr);
return tv.tv_usec + tv.tv_sec * 1000000L;
}
// 注意:生成的 Key 有重复
void GenerateWriteRequests(vector<Request> &kvs) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(1, kOpsPerThread);
char key_buffer[105];
for (int i = 0; i < kOpsPerThread; i++) {
sprintf(key_buffer, "file.mdtest.%d.%d", dis(gen), dis(gen));
string key = key_buffer;
int32_t value = dis(gen);
kvs.push_back({OP_TYPE::kOpTypeWrite, key, value});
}
}
bool should_thread_run;
void threadFunc(int idx) {
if (g_ctx.start_core != -1) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset); // 初始化CPU集合,将 cpuset 置为空
CPU_SET(idx + g_ctx.start_core, &cpuset); // 将本进程绑定到 CPU 上
// 设置线程的 CPU 亲和性
if (pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset) != 0) {
printf("Set CPU affinity failed\n");
exit(-1);
}
}
std::hash<string> hasher;
ankerl::unordered_dense::map<string, int64_t> hash_map;
hash_map.reserve(kOpsPerThread * 2);
vector<Request> req;
void *deque_requests[kPullNumber];
req.reserve(kOpsPerThread);
GenerateWriteRequests(req);
// test put
int request_cnt = 0;
int ret;
pthread_barrier_wait(&barrier1);
// 主线程计时中
pthread_barrier_wait(&barrier2);
while (should_thread_run) {
for (int i = 0; request_cnt < kOpsPerThread && i < kPullNumber; i++) {
uint64_t key_hash = wyhash(req[request_cnt].key.c_str(),
req[request_cnt].key.length(), 0, _wyp);
int to_thread = key_hash % g_ctx.thread_num;
if (to_thread == idx) { // 就是我,不转移了
hash_map[req[request_cnt].key] = req[request_cnt].value;
g_ctx.finished_cnt[idx].val++; // 所有线程 finished_cnt
// 加起来等于总操作数即可结束循环
} else {
while ((ret = rte_ring_enqueue(g_ctx.rings[to_thread][idx],
&req[request_cnt])) != 0)
;
}
request_cnt++;
}
for (int i = 0; i < g_ctx.thread_num; i++) {
unsigned int n = rte_ring_dequeue_burst(
g_ctx.rings[idx][i], deque_requests, kPullNumber, nullptr);
for (int j = 0; j < n; j++) {
auto *r = static_cast<Request *>(deque_requests[j]);
hash_map[r->key] = r->value;
g_ctx.finished_cnt[idx].val++;
}
}
}
pthread_barrier_wait(&barrier3);
int invalid_cnt = 0;
request_cnt = 0;
// test get
pthread_barrier_wait(&barrier1);
// 主线程计时中
pthread_barrier_wait(&barrier2);
while (should_thread_run) {
for (int i = 0; request_cnt < kOpsPerThread && i < kPullNumber; i++) {
uint64_t key_hash = wyhash(req[request_cnt].key.c_str(),
req[request_cnt].key.length(), 0, _wyp);
int to_thread = key_hash % g_ctx.thread_num;
if (to_thread == idx) { // 就是我,不转移了
int value = hash_map.at(req[request_cnt].key);
if (value == 0) {
invalid_cnt++;
}
g_ctx.finished_cnt[idx].val++; // 所有线程 finished_cnt
// 加起来等于总操作数即可结束循环
} else {
while ((ret = rte_ring_enqueue(g_ctx.rings[to_thread][idx],
&req[request_cnt])) != 0)
;
}
request_cnt++;
}
for (int i = 0; i < g_ctx.thread_num; i++) {
unsigned int n = rte_ring_dequeue_burst(
g_ctx.rings[idx][i], deque_requests, kPullNumber, nullptr);
for (int j = 0; j < n; j++) {
auto *r = static_cast<Request *>(deque_requests[j]);
int value = hash_map.at(r->key);
if (value == 0) {
invalid_cnt++;
}
g_ctx.finished_cnt[idx].val++;
}
}
}
pthread_barrier_wait(&barrier3);
if (invalid_cnt != 0) {
printf("ERR %d: invalid_cnt %d", idx, invalid_cnt);
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <threads_num> <start_core>\n", argv[0]);
return 0;
}
printf("SPSC rte_ring test, %d write/read op per thread\n", kOpsPerThread);
g_ctx.thread_num = atoi(argv[1]);
g_ctx.start_core = atoi(argv[2]);
g_ctx.finished_cnt.resize(g_ctx.thread_num);
g_ctx.rings = vector<vector<rte_ring *>>(
g_ctx.thread_num, vector<rte_ring *>(g_ctx.thread_num, nullptr));
for (int i = 0; i < g_ctx.thread_num; i++) {
for (int j = 0; j < g_ctx.thread_num; j++) {
g_ctx.rings[i][j] =
rte_ring_create(512, RING_F_SC_DEQ | RING_F_SP_ENQ);
}
}
for (int i = 0; i < g_ctx.thread_num; i++) {
g_ctx.threads.emplace_back(threadFunc, i);
}
if (g_ctx.start_core != -1) {
cpu_set_t cpuset;
CPU_ZERO(&cpuset); // 初始化CPU集合,将 cpuset 置为空
CPU_SET(g_ctx.thread_num + g_ctx.start_core,
&cpuset); // 将本进程绑定到 CPU 上
// 设置线程的 CPU 亲和性
if (pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset) != 0) {
printf("Set CPU affinity failed\n");
exit(-1);
}
}
// PUT
should_thread_run = true;
pthread_barrier_init(&barrier1, nullptr, g_ctx.thread_num + 1);
pthread_barrier_init(&barrier2, nullptr, g_ctx.thread_num + 1);
pthread_barrier_init(&barrier3, nullptr, g_ctx.thread_num + 1);
// 计时前同步
pthread_barrier_wait(&barrier1);
pthread_barrier_destroy(&barrier1);
pthread_barrier_init(&barrier1, nullptr,
g_ctx.thread_num + 1); // 为 GET 做准备
// PUT 前同步并开始计时
int64_t start_ts = GetUs();
pthread_barrier_wait(&barrier2);
pthread_barrier_destroy(&barrier2);
// PUT 中……
while (should_thread_run) {
int64_t sum = 0;
for (int i = 0; i < g_ctx.thread_num; i++) {
sum += g_ctx.finished_cnt[i].val;
}
if (sum == static_cast<int64_t>(kOpsPerThread) * g_ctx.thread_num) {
should_thread_run = false;
}
}
for (int i = 0; i < g_ctx.thread_num; i++) {
g_ctx.finished_cnt[i].val = 0;
}
// PUT 后计时结束
pthread_barrier_wait(&barrier3);
pthread_barrier_destroy(&barrier3);
int64_t used_time_in_us = GetUs() - start_ts;
should_thread_run = true;
printf("[PUT] total %.4f Mops, in %.4f s\n"
" per-thread %.4f Mops\n",
static_cast<double>(kOpsPerThread) * g_ctx.thread_num /
used_time_in_us,
static_cast<double>(used_time_in_us) / 1000000,
static_cast<double>(kOpsPerThread) / used_time_in_us);
// GET
pthread_barrier_init(&barrier2, nullptr, g_ctx.thread_num + 1);
pthread_barrier_init(&barrier3, nullptr, g_ctx.thread_num + 1);
// 计时前同步
pthread_barrier_wait(&barrier1);
pthread_barrier_destroy(&barrier1);
// GET 前同步并开始计时
start_ts = GetUs();
pthread_barrier_wait(&barrier2);
pthread_barrier_destroy(&barrier2);
// GET 中……
while (should_thread_run) {
int64_t sum = 0;
for (int i = 0; i < g_ctx.thread_num; i++) {
sum += g_ctx.finished_cnt[i].val;
}
if (sum == static_cast<int64_t>(kOpsPerThread) * g_ctx.thread_num) {
should_thread_run = false;
}
}
// GET 后计时结束
pthread_barrier_wait(&barrier3);
pthread_barrier_destroy(&barrier3);
used_time_in_us = GetUs() - start_ts;
printf("[GET] total %.4f Mops, in %.4f s\n"
" per-thread %.4f Mops\n",
static_cast<double>(kOpsPerThread) * g_ctx.thread_num /
used_time_in_us,
static_cast<double>(used_time_in_us) / 1000000,
static_cast<double>(kOpsPerThread) / used_time_in_us);
for (int i = 0; i < g_ctx.thread_num; i++) {
g_ctx.threads[i].join();
}
return 0;
}