forked from runshenzhu/palmtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
528 lines (416 loc) · 13.7 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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#define NDEBUG
#include <iostream>
#include <string>
#include <assert.h>
#include "palmtree.h"
#include <thread>
#include <cstdlib>
#include <glog/logging.h>
#include <map>
#include <time.h>
#include <unistd.h>
#include <jemalloc/jemalloc.h>
#include <stx/btree_map.h>
#include <pthread.h>
#include "CycleTimer.h"
#define TEST_SIZE 10240000
using namespace std;
int worker_num;
class fast_random {
public:
fast_random(unsigned long seed) : seed(0) { set_seed0(seed); }
inline unsigned long next() {
return ((unsigned long)next(32) << 32) + next(32);
}
inline uint32_t next_u32() { return next(32); }
inline uint16_t next_u16() { return (uint16_t)next(16); }
/** [0.0, 1.0) */
inline double next_uniform() {
return (((unsigned long)next(26) << 27) + next(27)) / (double)(1L << 53);
}
inline char next_char() { return next(8) % 256; }
inline char next_readable_char() {
static const char readables[] =
"0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
return readables[next(6)];
}
inline std::string next_string(size_t len) {
std::string s(len, 0);
for (size_t i = 0; i < len; i++) s[i] = next_char();
return s;
}
inline std::string next_readable_string(size_t len) {
std::string s(len, 0);
for (size_t i = 0; i < len; i++) s[i] = next_readable_char();
return s;
}
inline unsigned long get_seed() { return seed; }
inline void set_seed(unsigned long seed) { this->seed = seed; }
private:
inline void set_seed0(unsigned long seed) {
this->seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
}
inline unsigned long next(unsigned int bits) {
seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
return (unsigned long)(seed >> (48 - bits));
}
unsigned long seed;
};
void test() {
palmtree::PalmTree<int, int> palmtree(std::numeric_limits<int>::min(), worker_num);
palmtree::PalmTree<int, int> *palmtreep = &palmtree;
for (int i = 0; i < 32; i++) {
palmtreep->insert(i, i);
}
for (int i = 16; i <= 30; i++) {
palmtreep->remove(i);
}
for (int i = 0; i <= 15; i++) {
palmtreep->remove(i);
}
palmtreep->remove(31);
for (int i = 0; i < 32; i++) {
DLOG(INFO) << "Remove " << i;
palmtreep->remove(i);
int res;
DLOG(INFO) << "Find " << i;
bool success = palmtreep->find(i, res);
if (success) {
assert(false);
} else {
DLOG(INFO) << "Thread " << i << " get nothing";
}
}
srand(15618);
std::map<int, int> reference;
for (int i = 10; i < 256; i++) {
int key1 = i;
int value1 = rand() % 10;
int key2 = i - 10;
palmtreep->insert(key1, value1);
palmtreep->remove(key2);
reference.emplace(key1, value1);
reference.erase(key2);
}
for (auto itr = reference.begin(); itr != reference.end(); itr++) {
DLOG(INFO) << itr->first << " " << itr->second;
}
for (int i = 246; i < 256; i++) {
int res;
bool suc = palmtreep->find(i, res);
CHECK(suc == true && res == reference[i]) << "Should find " << i << " " << reference[i];
}
while(palmtree.task_nums > 0)
;
}
void bench() {
int *buff = new int[TEST_SIZE];
for(int i = 0; i < TEST_SIZE; i++) {
buff[i] = i;
}
std::random_shuffle(buff, buff + TEST_SIZE);
palmtree::PalmTree<int, int> palmtree(std::numeric_limits<int>::min(), worker_num);
palmtree::PalmTree<int, int> *palmtreep = &palmtree;
std::vector<std::thread> threads;
double start = CycleTimer::currentSeconds();
for (int i = 0; i < 1; i++) {
threads.push_back(std::thread([palmtreep, i, buff]() {
for(int j = 0; j < TEST_SIZE; j++) {
auto kv = buff[j];
int res;
palmtreep->insert(kv, kv);
palmtreep->find(kv, res);
}
}));
}
for (auto &thread : threads)
thread.join();
delete buff;
LOG(INFO) << "task_nums: " << palmtree.task_nums;
while(palmtree.task_nums > 0)
;
double end = CycleTimer::currentSeconds();
cout << "run for " << end-start << "s";
}
// Populate a palm tree with @entry_count entries
void populate_palm_tree(palmtree::PalmTree<int, int> *palmtreep, size_t entry_count) {
int *buff = new int[entry_count];
for(size_t i = 0; i < entry_count; i++) {
buff[i] = i;
}
std::random_shuffle(buff, buff + entry_count);
for(size_t j = 0; j < entry_count; j++) {
// auto kv = buff[j];
palmtreep->insert(2 * j, 2 * j);
}
delete buff;
// Wait for task finished
palmtreep->wait_finish();
}
void readonly_skew(size_t entry_count, size_t op_count, float contention_ratio, bool run_std_map = false) {
LOG(INFO) << "Begin palmtree readonly skew benchmark, contention ratio: " << contention_ratio;
palmtree::PalmTree<int, int> palmtree(std::numeric_limits<int>::min(), worker_num);
palmtree::PalmTree<int, int> *palmtreep = &palmtree;
populate_palm_tree(palmtreep, entry_count);
// Reset the metrics
palmtreep->reset_metric();
// Wait for insertion finished
LOG(INFO) << entry_count << " entries inserted";
fast_random rng(time(0));
double start = CycleTimer::currentSeconds();
LOG(INFO) << "Benchmark started";
int one_step = entry_count / (palmtreep->batch_size()+1);
int last_key = 0;
int batch_task_count = 0;
for (size_t i = 0; i < op_count; i++) {
last_key += rng.next_u32() % one_step;
last_key %= entry_count;
batch_task_count++;
auto id = rng.next_uniform();
auto k = last_key;
if(id < contention_ratio) {
k = (int) (k * 0.2);
}
int res;
palmtreep->find(2 * k, res);
if (batch_task_count >= palmtreep->batch_size()) {
batch_task_count = 0;
last_key = 0;
}
}
LOG(INFO) << palmtreep->task_nums << " left";
palmtreep->wait_finish();
double end = CycleTimer::currentSeconds();
LOG(INFO) << "Palmtree run for " << end-start << "s, " << "thput: " << std::fixed << 2 * op_count/(end-start)/1000 << " K rps";
double runtime = (end-start) / 2;
if (run_std_map) {
LOG(INFO) << "Running std map";
std::map<int, int> map;
for (size_t i = 0; i < entry_count; i++)
map.insert(std::make_pair(i, i));
pthread_rwlock_t lock_rw = PTHREAD_RWLOCK_INITIALIZER;
pthread_rwlock_t *l = &lock_rw;
auto map_p = ↦
start = CycleTimer::currentSeconds();
std::vector<std::thread> threads;
auto w_n = worker_num;
for(int i = 0; i < w_n; i++) {
threads.push_back(std::thread([map_p, op_count, entry_count, l, w_n, contention_ratio]() {
fast_random rng(time(0));
for (size_t i = 0; i < op_count / w_n; i++) {
int rand_key = rng.next_u32() % entry_count;
auto id = rng.next_uniform();
if(id < contention_ratio) {
rand_key = (int) (rand_key * 0.2);
}
pthread_rwlock_rdlock(l);
map_p->find(rand_key);
pthread_rwlock_unlock(l);
}
}));
}
for(auto &t : threads) {
t.join();
}
end = CycleTimer::currentSeconds();
LOG(INFO) << "std::map run for " << end-start << "s, " << "thput:" << std::fixed << op_count/(end-start)/1000 << " K rps";
double runtime_ref = end-start;
LOG(INFO) << "SPEEDUP over std map: " << runtime_ref / runtime << " X";
threads.clear();
// stx
LOG(INFO) << "Running stx map";
stx::btree_map<int, int> stx_map;
for (size_t i = 0; i < entry_count; i++)
stx_map.insert(std::make_pair(i, i));
start = CycleTimer::currentSeconds();
auto stx_p = &stx_map;
for(int i = 0; i < w_n; i++) {
threads.push_back(std::thread([stx_p, op_count, entry_count, l, w_n, contention_ratio]() {
fast_random rng(time(0));
for (size_t i = 0; i < op_count / w_n; i++) {
int rand_key = rng.next_u32() % entry_count;
auto id = rng.next_uniform();
if(id < contention_ratio) {
rand_key = (int) (rand_key * 0.2);
}
pthread_rwlock_rdlock(l);
stx_p->find(rand_key);
pthread_rwlock_unlock(l);
}
}));
}
for(auto &t : threads) {
t.join();
}
end = CycleTimer::currentSeconds();
LOG(INFO) << "stx map run for " << end-start << "s, " << "thput:" << std::fixed << op_count/(end-start)/1000 << " K rps";
runtime_ref = end-start;
LOG(INFO) << "SPEEDUP over PalmTree: " << runtime_ref / runtime << " X";
}
}
void update_skew(size_t entry_count, size_t op_count, float contention_ratio, bool run_std_map = false) {
LOG(INFO) << "Begin palmtree update skew benchmark, contention ratio: " << contention_ratio;
// palmtree::PalmTree<int, int> palmtree(std::numeric_limits<int>::min(), worker_num);
palmtree::PalmTree<int, int> *palmtreep = new palmtree::PalmTree<int, int> (std::numeric_limits<int>::min(), worker_num);;
populate_palm_tree(palmtreep, entry_count);
// Reset the metrics
palmtreep->reset_metric();
// Wait for insertion finished
LOG(INFO) << entry_count << " entries inserted";
fast_random rng(time(0));
double start = CycleTimer::currentSeconds();
LOG(INFO) << "Benchmark started";
int one_step = 2 * entry_count / (palmtreep->batch_size()+1);
int last_key = 0;
int batch_task_count = 0;
for (size_t i = 0; i < op_count; i++) {
last_key += rng.next_u32() % one_step;
last_key %= entry_count;
batch_task_count++;
auto id = rng.next_uniform();
int k = last_key;
if(id < contention_ratio) {
k = (int) (k * 0.2);
}
id = rng.next_uniform();
if(id < 0.1) {
palmtreep->insert(last_key, last_key);
} else if(id < 0.2) {
palmtreep->remove(last_key);
}else {
int res;
palmtreep->find(last_key, res);
}
if (batch_task_count >= palmtreep->batch_size()) {
batch_task_count = 0;
last_key = 0;
}
}
LOG(INFO) << palmtreep->task_nums << " left";
palmtreep->wait_finish();
double end = CycleTimer::currentSeconds();
LOG(INFO) << "Palmtree run for " << end-start << "s, " << "thput: " << std::fixed << 2 * op_count/(end-start)/1000 << " K rps";
double runtime = (end-start) / 2;
delete palmtreep;
if (run_std_map) {
LOG(INFO) << "Running std map";
std::map<int, int> map;
for (size_t i = 0; i < entry_count; i++)
map.insert(std::make_pair(i, i));
pthread_rwlock_t lock_rw = PTHREAD_RWLOCK_INITIALIZER;
pthread_rwlock_t *l = &lock_rw;
start = CycleTimer::currentSeconds();
auto map_p = ↦
start = CycleTimer::currentSeconds();
std::vector<std::thread> threads;
auto w_n = worker_num;
for(int i = 0; i < w_n; i++) {
threads.push_back(std::thread([map_p, op_count, entry_count, l, w_n, contention_ratio]() {
fast_random rng(time(0));
auto map = *map_p;
for (size_t i = 0; i < op_count / w_n; i++) {
int k = rng.next_u32() % entry_count;
auto id = rng.next_uniform();
auto rand_key = k;
if(id < contention_ratio) {
rand_key = (int) rand_key * 0.2;
}
id = rng.next_uniform();
if(id < 0.1) {
pthread_rwlock_wrlock(l);
map[rand_key] = rand_key;
}else if (id < 0.2) {
pthread_rwlock_wrlock(l);
map.erase(rand_key);
}else {
pthread_rwlock_rdlock(l);
map.find(rand_key);
}
pthread_rwlock_unlock(l);
}
}));
}
for(auto &t : threads) {
t.join();
}
threads.clear();
end = CycleTimer::currentSeconds();
LOG(INFO) << "std::map run for " << end-start << "s, " << "thput:" << std::fixed << op_count/(end-start)/1000 << " K rps";
double runtime_ref = end-start;
LOG(INFO) << "SPEEDUP over PalmTree: " << runtime_ref / runtime << " X";
// stx
LOG(INFO) << "Running stx map";
stx::btree_map<int, int> stx_map;
for (size_t i = 0; i < entry_count; i++)
stx_map.insert(std::make_pair(i, i));
start = CycleTimer::currentSeconds();
auto stx_p = &stx_map;
for(int i = 0; i < w_n; i++) {
threads.push_back(std::thread([stx_p, op_count, entry_count, l, w_n, contention_ratio]() {
fast_random rng(time(0));
auto stx = *stx_p;
for (size_t i = 0; i < op_count / w_n; i++) {
int k = rng.next_u32() % entry_count;
auto id = rng.next_uniform();
auto rand_key = k;
if(id < contention_ratio) {
rand_key = (int) rand_key * 0.2;
}
id = rng.next_uniform();
if(id < 0.1) {
pthread_rwlock_wrlock(l);
stx.insert(rand_key, rand_key);
}else if (id < 0.2) {
pthread_rwlock_wrlock(l);
stx.erase(rand_key);
}else {
pthread_rwlock_rdlock(l);
stx.find(rand_key);
}
pthread_rwlock_unlock(l);
}
}));
}
for(auto &t : threads) {
t.join();
}
end = CycleTimer::currentSeconds();
LOG(INFO) << "stx map run for " << end-start << "s, " << "thput:" << std::fixed << op_count/(end-start)/1000 << " K rps";
runtime_ref = end-start;
LOG(INFO) << "SPEEDUP over PalmTree: " << runtime_ref / runtime << " X";
}
}
int main(int argc, char *argv[]) {
// Google logging
FLAGS_logtostderr = 1;
google::InitGoogleLogging(argv[0]);
if(argc < 5) {
// print usage
cout << "usage example: 8 true r 0.8" << endl;
cout << "\trunning 8 workers, running map to compare performance, readonly, contention ratio 0.8" << endl;
exit(0);
}
worker_num = atoi(argv[1]);
bool c;
if(strcmp(argv[2], "true") == 0) {
c = true;
}else{
c = false;
}
bool r;
if(strcmp(argv[3], "r") == 0) {
r = true;
}else{
r = false;
}
float contention_ratio;
contention_ratio = atof(argv[4]);
auto insert = 1024 * 512 * 10;
auto op_num = 1024 * 1024 * 10;
if(r) {
readonly_skew(insert, op_num, contention_ratio, c);
}else {
update_skew(insert, op_num, contention_ratio, c);
}
return 0;
}