-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrcu_bench1.cpp
More file actions
420 lines (349 loc) · 11.2 KB
/
rcu_bench1.cpp
File metadata and controls
420 lines (349 loc) · 11.2 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <cppurcu/cppurcu.h>
#include <iostream>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <thread>
#include <mutex>
#include <chrono>
#include <random>
#include <atomic>
using namespace std;
using namespace chrono;
vector<pair<string, string>> generate_test_ips(size_t count)
{
vector<pair<string, string>> ips;
ips.reserve(count);
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist(0, 255);
for (size_t i = 0; i < count; ++i)
{
string ip = to_string(dist(gen)) + "." +
to_string(dist(gen)) + "." +
to_string(dist(gen)) + "." +
to_string(dist(gen));
ips.push_back({ip, "test-data"});
}
return ips;
}
class MutexContainer
{
public:
MutexContainer()
{
ips_ = make_shared<unordered_map<string, string>>();
}
bool contains(const string &ip)
{
lock_guard<mutex> lock(mutex_);
return ips_->count(ip) > 0;
}
void update(shared_ptr<unordered_map<string, string>> new_ips)
{
lock_guard<mutex> lock(mutex_);
ips_ = new_ips;
}
private:
shared_ptr<unordered_map<string, string>> ips_;
mutex mutex_;
};
void benchmark_mutex(
size_t num_readers,
size_t num_writers,
seconds test_duration,
const vector<shared_ptr<unordered_map<string, string>>> &test_data_array,
const vector<pair<string, string>> &test_ips)
{
cout << "\n========================================\n";
cout << "std::mutex\n";
cout << "========================================\n";
cout << "Reader thread : " << num_readers << "\n";
cout << "Writer thread : " << num_writers << "\n";
cout << "test duration : " << test_duration.count() << " sec\n";
MutexContainer container;
// Set initial data (use the first in the array)
container.update(test_data_array[0]);
atomic<bool> stop_flag{false};
atomic<size_t> total_reads{0};
atomic<size_t> total_writes{0};
atomic<size_t> index{0}; // Array index
auto start = high_resolution_clock::now();
// Reader threads
vector<thread> readers;
for (size_t i = 0; i < num_readers; ++i)
{
readers.emplace_back([&, i]() {
random_device rd;
mt19937 gen(rd() + i);
uniform_int_distribution<size_t> dist(0, test_ips.size() - 1);
while (!stop_flag.load(memory_order_relaxed))
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
total_reads.fetch_add(1, memory_order_relaxed);
}
});
}
// Writer threads
vector<thread> writers;
for (size_t i = 0; i < num_writers; ++i)
{
writers.emplace_back([&, i]() {
size_t index = 0;
while (!stop_flag.load(memory_order_relaxed)) {
// Fetch sequentially from the array
container.update(test_data_array[index++]);
total_writes.fetch_add(1, memory_order_relaxed);
// Simulate intermittent updates
this_thread::sleep_for(milliseconds(100));
}
});
}
// Wait for the specified duration
this_thread::sleep_for(test_duration);
// Signal termination to all threads
stop_flag.store(true, memory_order_relaxed);
// Wait for all threads to finish
for (auto &t : readers)
{
t.join();
}
for (auto &t : writers)
{
t.join();
}
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
cout << "execution duration : " << duration.count() << " ms\n";
cout << "total read count : " << total_reads << "\n";
cout << "total write count : " << total_writes << "\n";
cout << "read throughput : " << (total_reads * 1000.0 / duration.count()) << " ops/sec\n";
cout << "read per second : " << (total_reads / test_duration.count()) << " reads/sec\n";
}
class CPPURCUContainer
{
public:
CPPURCUContainer()
: ips_(std::make_shared<unordered_map<string, string>>())
{
}
bool contains(const string &ip)
{
auto ips = ips_.load();
return ips->count(ip) > 0;
}
void update(shared_ptr<unordered_map<string, string>> new_ips)
{
ips_.update(new_ips);
}
private:
cppurcu::storage<unordered_map<string, string>> ips_;
};
void benchmark_cppurcu(
size_t num_readers,
size_t num_writers,
seconds test_duration,
const vector<shared_ptr<unordered_map<string, string>>> &test_data_array,
const vector<pair<string, string>> &test_ips)
{
cout << "\n========================================\n";
cout << "cppurcu\n";
cout << "========================================\n";
cout << "Reader thread : " << num_readers << "\n";
cout << "Writer thread : " << num_writers << "\n";
cout << "test duration : " << test_duration.count() << " sec\n";
CPPURCUContainer container;
// Set initial data (use the first in the array)
container.update(test_data_array[0]);
atomic<bool> stop_flag{false};
atomic<size_t> total_reads{0};
atomic<size_t> total_writes{0};
auto start = high_resolution_clock::now();
// Reader threads
vector<thread> readers;
for (size_t i = 0; i < num_readers; ++i)
{
readers.emplace_back([&, i]() {
random_device rd;
mt19937 gen(rd() + i);
uniform_int_distribution<size_t> dist(0, test_ips.size() - 1);
while (!stop_flag.load(memory_order_relaxed))
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
total_reads.fetch_add(1, memory_order_relaxed);
}
});
}
// Writer threads
vector<thread> writers;
for (size_t i = 0; i < num_writers; ++i)
{
writers.emplace_back([&, i]() {
size_t index = 0;
while (!stop_flag.load(memory_order_relaxed))
{
// Fetch sequentially from the array
container.update(test_data_array[index++]);
total_writes.fetch_add(1, memory_order_relaxed);
// Simulate intermittent updates
this_thread::sleep_for(milliseconds(100));
}
});
}
// Wait for the specified duration
this_thread::sleep_for(test_duration);
// Signal termination to all threads
stop_flag.store(true, memory_order_relaxed);
// Wait for all threads to finish
for (auto &t : readers)
{
t.join();
}
for (auto &t : writers)
{
t.join();
}
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
cout << "execution duration : " << duration.count() << " ms\n";
cout << "total read count : " << total_reads << "\n";
cout << "total write count : " << total_writes << "\n";
cout << "read throughput : " << (total_reads * 1000.0 / duration.count()) << " ops/sec\n";
cout << "read per second : " << (total_reads / test_duration.count()) << " reads/sec\n";
}
class CPPURCURetirementContainer
{
public:
CPPURCURetirementContainer()
: ips_(std::make_shared<unordered_map<string, string>>(),
std::make_shared<cppurcu::reclaimer_thread>())
{
}
bool contains(const string &ip)
{
auto ips = ips_.load();
return ips->count(ip) > 0;
}
void update(shared_ptr<unordered_map<string, string>> new_ips)
{
ips_ = std::move(new_ips);
}
private:
cppurcu::storage<unordered_map<string, string>> ips_;
};
void benchmark_reclaimer(
size_t num_readers,
size_t num_writers,
seconds test_duration,
const vector<shared_ptr<unordered_map<string, string>>> &test_data_array,
const vector<pair<string, string>> &test_ips)
{
cout << "\n========================================\n";
cout << "cppurcu + reclaimer_thread\n";
cout << "========================================\n";
cout << "Reader thread : " << num_readers << "\n";
cout << "Writer thread : " << num_writers << "\n";
cout << "test duration : " << test_duration.count() << " sec\n";
CPPURCURetirementContainer container;
container.update(test_data_array[0]);
atomic<bool> stop_flag{false};
atomic<size_t> total_reads{0};
atomic<size_t> total_writes{0};
auto start = high_resolution_clock::now();
vector<thread> readers;
for (size_t i = 0; i < num_readers; ++i)
{
readers.emplace_back([&, i]()
{
random_device rd;
mt19937 gen(rd() + i);
uniform_int_distribution<size_t> dist(0, test_ips.size() - 1);
while (!stop_flag.load(memory_order_relaxed))
{
const auto &[ip, value] = test_ips[dist(gen)];
container.contains(ip);
total_reads.fetch_add(1, memory_order_relaxed);
}
});
}
vector<thread> writers;
for (size_t i = 0; i < num_writers; ++i)
{
writers.emplace_back([&, i]()
{
size_t index = 0;
while (!stop_flag.load(memory_order_relaxed))
{
container.update(test_data_array[index++]);
total_writes.fetch_add(1, memory_order_relaxed);
this_thread::sleep_for(milliseconds(100));
}
});
}
this_thread::sleep_for(test_duration);
stop_flag.store(true, memory_order_relaxed);
for (auto &t : readers) t.join();
for (auto &t : writers) t.join();
auto end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start);
cout << "execution duration : " << duration.count() << " ms\n";
cout << "total read count : " << total_reads << "\n";
cout << "total write count : " << total_writes << "\n";
cout << "read throughput : " << (total_reads * 1000.0 / duration.count()) << " ops/sec\n";
cout << "read per second : " << (total_reads / test_duration.count()) << " reads/sec\n";
}
void flush_cache()
{
const size_t cache_size = 32 * 1024 * 1024;
volatile char *dummy = new char[cache_size];
for (size_t i = 0; i < cache_size; i += 64)
dummy[i] = 1;
delete[] dummy;
}
// ============================================================================
// main
// ============================================================================
int main(int argc, char **argv)
{
cout << "==================================\n";
size_t gen_size = 1000;
if (argc >= 2)
gen_size = atoi(argv[1]);
// Test parameters
size_t num_readers = 10; // read thread
size_t num_writers = 2; // write thread
seconds test_duration(10); // 10 seconds
cout << "TEST SET : " << gen_size << "\n";
cout << "- Reader thread : " << num_readers << "\n";
cout << "- Writer thread : " << num_writers << "\n";
cout << "- test duration : " << test_duration.count() << " sec\n";
cout << "- Update period : 100 ms\n";
// Pre-generate test data
cout << "generating test data...\n";
auto test_ips = generate_test_ips(gen_size);
// Convert to unordered_map
auto test_data = make_shared<unordered_map<string, string>>();
for (const auto &[ip, value] : test_ips)
{
test_data->insert({ip, value});
}
// Pre-create 200 copies of test_data
vector<shared_ptr<unordered_map<string, string>>> test_data_array;
test_data_array.reserve(200);
for (int i = 0; i < 200; ++i)
test_data_array.push_back(make_shared<unordered_map<string, string>>(*test_data));
cout << "Test data generation completed (200 copies)\n";
// std::mutex test
flush_cache();
benchmark_mutex (num_readers, num_writers, test_duration, test_data_array, test_ips);
flush_cache();
benchmark_reclaimer(num_readers, num_writers, test_duration, test_data_array, test_ips);
flush_cache();
benchmark_cppurcu (num_readers, num_writers, test_duration, test_data_array, test_ips);
cout << "\n==================================\n";
cout << "Test completed\n";
return 0;
}