-
Notifications
You must be signed in to change notification settings - Fork 40
/
workload_string.cpp
executable file
·485 lines (401 loc) · 12.8 KB
/
workload_string.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
#include "microbench.h"
#include "index.h"
// Used for skiplist
thread_local long skiplist_steps = 0;
std::atomic<long> skiplist_total_steps;
typedef GenericKey<31> keytype;
typedef GenericComparator<31> keycomp;
extern bool hyperthreading;
using KeyEuqalityChecker = GenericEqualityChecker<31>;
using KeyHashFunc = GenericHasher<31>;
static const uint64_t key_type=0;
static const uint64_t value_type=1; // 0 = random pointers, 1 = pointers to keys
#include "util.h"
#define USE_27MB_FILE
// Whether to exit after insert operation
static bool insert_only = false;
/*
* MemUsage() - Reads memory usage from /proc file system
*/
size_t MemUsage() {
FILE *fp = fopen("/proc/self/statm", "r");
if(fp == nullptr) {
fprintf(stderr, "Could not open /proc/self/statm to read memory usage\n");
exit(1);
}
unsigned long unused;
unsigned long rss;
if (fscanf(fp, "%ld %ld %ld %ld %ld %ld %ld", &unused, &rss, &unused, &unused, &unused, &unused, &unused) != 7) {
perror("");
exit(1);
}
(void)unused;
fclose(fp);
return rss * (4096 / 1024); // in KiB (not kB)
}
//==============================================================
// LOAD
//==============================================================
inline void load(int wl,
int kt,
int index_type,
std::vector<keytype> &init_keys,
std::vector<keytype> &keys,
std::vector<uint64_t> &values,
std::vector<int> &ranges,
std::vector<int> &ops) {
std::string init_file;
std::string txn_file;
// If we do not use the 27MB file then use old set of files; Otherwise
// use 27 MB email workload
#ifndef USE_27MB_FILE
// 0 = a, 1 = c, 2 = e
if (kt == EMAIL_KEY && wl == WORKLOAD_A) {
init_file = "workloads/email_loada_zipf_int_100M.dat";
txn_file = "workloads/email_txnsa_zipf_int_100M.dat";
} else if (kt == EMAIL_KEY && wl == WORKLOAD_C) {
init_file = "workloads/email_loadc_zipf_int_100M.dat";
txn_file = "workloads/email_txnsc_zipf_int_100M.dat";
} else if (kt == EMAIL_KEY && wl == WORKLOAD_E) {
init_file = "workloads/email_loade_zipf_int_100M.dat";
txn_file = "workloads/email_txnse_zipf_int_100M.dat";
} else {
fprintf(stderr, "Unknown workload or key type: %d, %d\n", wl, kt);
exit(1);
}
#else
if (kt == EMAIL_KEY && wl == WORKLOAD_A) {
init_file = "workloads/email_load.dat";
txn_file = "workloads/email_a.dat";
} else if (kt == EMAIL_KEY && wl == WORKLOAD_C) {
init_file = "workloads/email_load.dat";
txn_file = "workloads/email_c.dat";
} else if (kt == EMAIL_KEY && wl == WORKLOAD_E) {
init_file = "workloads/email_load.dat";
txn_file = "workloads/email_e.dat";
} else {
fprintf(stderr, "Unknown workload or key type: %d, %d\n", wl, kt);
exit(1);
}
#endif
std::ifstream infile_load(init_file);
std::string op;
std::string key_str;
keytype key;
int range;
std::string insert("INSERT");
std::string read("READ");
std::string update("UPDATE");
std::string scan("SCAN");
int count = 0;
while ((count < INIT_LIMIT) && infile_load.good()) {
infile_load >> op >> key_str;
if (op.compare(insert) != 0) {
std::cout << "READING LOAD FILE FAIL!\n";
return;
}
key.setFromString(key_str);
init_keys.push_back(key);
count++;
}
count = 0;
uint64_t value = 0;
void *base_ptr = malloc(8);
uint64_t base = (uint64_t)(base_ptr);
free(base_ptr);
keytype *init_keys_data = init_keys.data();
if (value_type == 0) {
while (count < INIT_LIMIT) {
value = base + rand();
values.push_back(value);
count++;
}
}
else {
while (count < INIT_LIMIT) {
values.push_back((uint64_t)init_keys_data[count].data);
count++;
}
}
fprintf(stderr, "Number of init entries: %lu\n", init_keys.size());
// For insert only mode we return here
if(insert_only == true) {
return;
}
std::ifstream infile_txn(txn_file);
count = 0;
while ((count < LIMIT) && infile_txn.good()) {
infile_txn >> op >> key_str;
key.setFromString(key_str);
if (op.compare(insert) == 0) {
ops.push_back(OP_INSERT);
keys.push_back(key);
ranges.push_back(1);
}
else if (op.compare(read) == 0) {
ops.push_back(OP_READ);
keys.push_back(key);
}
else if (op.compare(update) == 0) {
ops.push_back(OP_UPSERT);
keys.push_back(key);
}
else if (op.compare(scan) == 0) {
infile_txn >> range;
ops.push_back(OP_SCAN);
keys.push_back(key);
ranges.push_back(range);
}
else {
std::cout << "UNRECOGNIZED CMD!\n";
return;
}
count++;
}
std::cout << "Finished loading workload file\n";
}
//==============================================================
// EXEC
//==============================================================
inline void exec(int wl,
int index_type,
int num_thread,
std::vector<keytype> &init_keys,
std::vector<keytype> &keys,
std::vector<uint64_t> &values,
std::vector<int> &ranges,
std::vector<int> &ops) {
Index<keytype, keycomp> *idx = \
getInstance<keytype, keycomp, KeyEuqalityChecker, KeyHashFunc>(index_type, key_type);
// WRITE ONLY TEST--------------
int count = (int)init_keys.size();
double start_time = get_now();
auto func = [idx, &init_keys, num_thread, &values](uint64_t thread_id, bool) {
size_t total_num_key = init_keys.size();
size_t key_per_thread = total_num_key / num_thread;
size_t start_index = key_per_thread * thread_id;
size_t end_index = start_index + key_per_thread;
threadinfo *ti = threadinfo::make(threadinfo::TI_MAIN, -1);
int counter = 0;
for(size_t i = start_index;i < end_index;i++) {
idx->insert(init_keys[i], values[i], ti);
counter++;
if(counter % 4096 == 0) {
ti->rcu_quiesce();
}
}
ti->rcu_quiesce();
return;
};
StartThreads(idx, num_thread, func, false);
double end_time = get_now();
double tput = count / (end_time - start_time) / 1000000; //Mops/sec
if(index_type == TYPE_SKIPLIST) {
fprintf(stderr, "SkipList size = %lu\n", idx->GetIndexSize());
}
std::cout << "\033[1;32m";
std::cout << "insert " << tput;
std::cout << "\033[0m" << "\n";
if(insert_only == true) {
delete idx;
return;
}
//READ/UPDATE/SCAN TEST----------------
start_time = get_now();
int txn_num = GetTxnCount(ops, index_type);
uint64_t sum = 0;
#ifdef PAPI_IPC
//Variables for PAPI
float real_time, proc_time, ipc;
long long ins;
int retval;
if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) {
printf("PAPI error: retval: %d\n", retval);
exit(1);
}
#endif
#ifdef PAPI_CACHE
int events[3] = {PAPI_L1_TCM, PAPI_L2_TCM, PAPI_L3_TCM};
long long counters[3] = {0, 0, 0};
int retval;
if ((retval = PAPI_start_counters(events, 3)) != PAPI_OK) {
fprintf(stderr, "PAPI failed to start counters: %s\n", PAPI_strerror(retval));
exit(1);
}
#endif
if(values.size() < keys.size()) {
fprintf(stderr, "Values array too small\n");
exit(1);
}
fprintf(stderr, "# of Txn: %d\n", txn_num);
auto func2 = [num_thread,
idx,
&keys,
&values,
&ranges,
&ops](uint64_t thread_id, bool) {
size_t total_num_op = ops.size();
size_t op_per_thread = total_num_op / num_thread;
size_t start_index = op_per_thread * thread_id;
size_t end_index = start_index + op_per_thread;
std::vector<uint64_t> v;
v.reserve(10);
threadinfo *ti = threadinfo::make(threadinfo::TI_MAIN, -1);
int counter = 0;
for(size_t i = start_index;i < end_index;i++) {
int op = ops[i];
if (op == OP_INSERT) { //INSERT
idx->insert(keys[i], values[i], ti);
}
else if (op == OP_READ) { //READ
v.clear();
idx->find(keys[i], &v, ti);
}
else if (op == OP_UPSERT) { //UPDATE
idx->upsert(keys[i], (uint64_t)keys[i].data, ti);
}
else if (op == OP_SCAN) { //SCAN
idx->scan(keys[i], ranges[i], ti);
}
counter++;
if(counter % 4096 == 0) {
ti->rcu_quiesce();
}
}
ti->rcu_quiesce();
return;
};
StartThreads(idx, num_thread, func2, false);
end_time = get_now();
#ifdef PAPI_IPC
if((retval = PAPI_ipc(&real_time, &proc_time, &ins, &ipc)) < PAPI_OK) {
printf("PAPI error: retval: %d\n", retval);
exit(1);
}
std::cout << "Time = " << real_time << "\n";
std::cout << "Tput = " << LIMIT/real_time << "\n";
std::cout << "Inst = " << ins << "\n";
std::cout << "IPC = " << ipc << "\n";
#endif
#ifdef PAPI_CACHE
if ((retval = PAPI_read_counters(counters, 3)) != PAPI_OK) {
fprintf(stderr, "PAPI failed to read counters: %s\n", PAPI_strerror(retval));
exit(1);
}
std::cout << "L1 miss = " << counters[0] << "\n";
std::cout << "L2 miss = " << counters[1] << "\n";
std::cout << "L3 miss = " << counters[2] << "\n";
#endif
tput = txn_num / (end_time - start_time) / 1000000; //Mops/sec
std::cout << "\033[1;31m";
if (wl == WORKLOAD_A) {
std::cout << "read/update " << (tput + (sum - sum));
}
else if (wl == WORKLOAD_C) {
std::cout << "read " << (tput + (sum - sum));
}
else if (wl == WORKLOAD_E) {
std::cout << "insert/scan " << (tput + (sum - sum));
}
else {
std::cout << "read/update " << (tput + (sum - sum));
}
std::cout << "\033[0m" << "\n";
delete idx;
return;
}
int main(int argc, char *argv[]) {
if (argc < 5) {
std::cout << "Usage:\n";
std::cout << "1. workload type: a, c, e\n";
std::cout << "2. key distribution: email\n";
std::cout << "3. index type: bwtree skiplist masstree artolc btreeolc\n";
std::cout << "4. Number of threads: (1 - 40)\n";
std::cout << " --hyper: Whether to pin all threads on NUMA node 0\n";
std::cout << " --insert-only: Whether to only execute insert operations\n";
std::cout << " --repeat: Repeat 5 times\n";
return 1;
}
int wl;
if (strcmp(argv[1], "a") == 0) {
wl = WORKLOAD_A;
} else if (strcmp(argv[1], "c") == 0) {
wl = WORKLOAD_C;
} else if (strcmp(argv[1], "e") == 0) {
wl = WORKLOAD_E;
} else {
fprintf(stderr, "Unknown workload type: %s\n", argv[1]);
exit(1);
}
int kt = EMAIL_KEY;
// The second argument must be exactly "email"
if(strcmp(argv[2], "email") != 0) {
fprintf(stderr, "Unknown key type: %s\n", argv[2]);
exit(1);
}
int index_type;
if (strcmp(argv[3], "bwtree") == 0) {
index_type = TYPE_BWTREE;
} else if (strcmp(argv[3], "masstree") == 0) {
index_type = TYPE_MASSTREE;
} else if (strcmp(argv[3], "artolc") == 0) {
index_type = TYPE_ARTOLC;
} else if (strcmp(argv[3], "btreeolc") == 0) {
index_type = TYPE_BTREEOLC;
} else if (strcmp(argv[3], "skiplist") == 0) {
index_type = TYPE_SKIPLIST;
} else {
fprintf(stderr, "Unknown index type: %d\n", index_type);
exit(1);
}
// Then read number of threads using command line
int num_thread = atoi(argv[4]);
if(num_thread < 1 || num_thread > 40) {
fprintf(stderr, "Do not support %d threads\n", num_thread);
return 1;
} else {
fprintf(stderr, "Number of threads: %d\n", num_thread);
}
fprintf(stderr, "Leaf delta chain threshold: %d; Inner delta chain threshold: %d\n",
LEAF_DELTA_CHAIN_LENGTH_THRESHOLD,
INNER_DELTA_CHAIN_LENGTH_THRESHOLD);
// Then read all remianing arguments
int repeat_counter = 1;
char **argv_end = argv + argc;
for(char **v = argv + 5;v != argv_end;v++) {
if(strcmp(*v, "--hyper") == 0) {
// Enable hyoerthreading for scheduling threads
hyperthreading = true;
} else if(strcmp(*v, "--insert-only") == 0) {
insert_only = true;
} else if(strcmp(*v, "--repeat") == 0) {
repeat_counter = 5;
}
}
if(hyperthreading == true) {
fprintf(stderr, " Hyperthreading enabled\n");
}
if(insert_only == true) {
fprintf(stderr, " Insert-only mode\n");
}
#ifdef USE_27MB_FILE
fprintf(stderr, " Using 27MB workload file\n");
#endif
if(repeat_counter != 1) {
fprintf(stderr, " We run the workload part for %d times\n", repeat_counter);
}
fprintf(stderr, "index type = %d\n", index_type);
std::vector<keytype> init_keys;
std::vector<keytype> keys;
std::vector<uint64_t> values;
std::vector<int> ranges;
std::vector<int> ops; //INSERT = 0, READ = 1, UPDATE = 2
load(wl, kt, index_type, init_keys, keys, values, ranges, ops);
fprintf(stderr, "Finish loading (Mem = %lu)\n", MemUsage());
while(repeat_counter > 0) {
exec(wl, index_type, num_thread, init_keys, keys, values, ranges, ops);
fprintf(stderr, "Finished execution (Mem = %lu)\n", MemUsage());
repeat_counter--;
}
return 0;
}