-
Notifications
You must be signed in to change notification settings - Fork 1
/
sec_idx_bench.cc
352 lines (314 loc) · 10.4 KB
/
sec_idx_bench.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
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
#define STRIP_FLAG_HELP 1 // this must go before the #include!
#include "gflags/gflags.h"
#include "pebblesdb/db.h"
#include "pebblesdb/slice.h"
#include "pebblesdb/filter_policy.h"
#include "pebblesdb/cache.h"
#include "db/sec_idx/sec_idx_config.h"
#include "db/sec_idx/debug.h"
#include "util/timer.h"
#include <fstream>
#include <iostream>
#include <unistd.h>
#include <sstream>
#include <cassert>
#include <algorithm>
#include <thread>
#include <atomic>
DEFINE_string(db, "/mnt/lsm_eval/pebblesdb", "path to database");
DEFINE_bool(use_existing_db, false, "use_existing_db");
DEFINE_string(topk, "10", "list of secondary query topk");
DEFINE_string(preload, "", "preload workloads");
DEFINE_string(trace, "", "running workload trace");
DEFINE_int64(log_point, 1000000, "interval of printing results");
DEFINE_int32(threads, 1, "number of threads");
DEFINE_bool(range_search, false, "range search");
char content[4096];
// uint64_t MAX_OP = 1000000;
struct SharedThreadState {
std::atomic_int_fast32_t cur_count_{0};
std::atomic_int_fast32_t gen_{0};
void Barrier(int total_count) {
int local_gen = gen_.load(std::memory_order_acquire);
if (++cur_count_ == total_count) {
// the last one
gen_.fetch_add(1, std::memory_order_release);
cur_count_.store(0, std::memory_order_relaxed);
} else {
// wait for barrier
while (gen_.load(std::memory_order_consume) == local_gen)
;
}
}
};
struct alignas(64) Stats {
uint64_t w = 0; // write
uint64_t rs = 0; // read secondary key
uint64_t rp = 0; // read primary key
uint64_t rscount = 0;
double tp = 0;
double durationW = 0;
double durationRS = 0;
double durationRP = 0;
double durationPDB_RS = 0;
double duration_validate = 0;
double duration_pri_file_io = 0;
uint64_t num_pri_probe = 0; // number of read from pdb's memtable and sstable
void clear() {
w = 0;
rs = 0;
rp = 0;
rscount = 0;
tp = 0;
durationW = 0;
durationRS = 0;
durationRP = 0;
durationPDB_RS = 0;
duration_validate = 0;
duration_pri_file_io = 0;
num_pri_probe = 0;
}
void merge(const Stats &other) {
w += other.w;
rs += other.rs;
rp += other.rp;
rscount += other.rscount;
tp += other.tp;
durationW += other.durationW;
durationRS += other.durationRS;
durationRP += other.durationRP;
durationPDB_RS += other.durationPDB_RS;
duration_validate += other.duration_validate;
duration_pri_file_io += other.duration_pri_file_io;
num_pri_probe += other.num_pri_probe;
}
void print_header() {
std::cout
<< "No of Op (Millions), lat_op, lat_put, lat_get_pri, lat_get_sec, "
"lat_pri_table, lat_validate, avg_sec_topK, num_pri_probe"
<< std::endl;
}
void print() {
double total_time = durationW + durationRP + durationRS;
uint64_t total_op = w + rp + rs;
std::cout << total_op / 1e6 << ",\t";
std::cout << std::fixed;
std::cout.precision(3);
std::cout << total_time / total_op / 1000 << ",\t" << durationW / w / 1000
<< ",\t" << durationRP / rp / 1000 << ",\t"
<< durationRS / rs / 1000 << ",\t" << durationPDB_RS / rs / 1000
<< ",\t"
// << duration_pri_file_io / rs / 1000 << ",\t"
<< duration_validate / rs / 1000 << ",\t" << (double)rscount / rs
<< ",\t" << (double)num_pri_probe / rs << std::endl;
std::cout << std::defaultfloat;
}
void print_final(std::string header) {
double total_time = durationW + durationRP + durationRS;
uint64_t total_op = w + rp + rs;
std::cout << std::fixed;
std::cout.precision(3);
std::cout << header
<< " Final Result: Avg Latency: " << total_time / total_op / 1000
<< " us, Throughput: " << tp / 1000 << " kops/s" << std::endl;
std::cout << std::defaultfloat;
}
};
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
void run_trace(leveldb::DB *db, std::string trace, int tid,
SharedThreadState *shared_state, Stats *stats, int topk = 10) {
std::ifstream ifile;
if (FLAGS_threads == 1) {
ifile.open(trace);
} else {
ifile.open(trace + "_" + std::to_string(tid));
}
std::vector<leveldb::KeyValuePair> svalues;
std::vector<std::string> pkey_values;
if (!ifile) {
std::cerr << "Can't open input file " << trace << std::endl;
return;
}
shared_state->Barrier(FLAGS_threads);
std::string line;
uint64_t i = 0;
leveldb::ReadOptions roptions;
roptions.num_records = topk;
while (getline(ifile, line)) {
i++;
std::vector<std::string> x = split(line, '\t');
leveldb::WriteOptions woptions;
// struct timeval start, end;
if (x[0] == "w" || x[0] == "up") {
StopWatch<double> sw(stats->durationW);
uint64_t pk, sk, val_sz;
pk = std::stoul(x[1]);
sk = __builtin_bswap64(std::stoul(x[2]));
val_sz = std::stoul(x[3]);
leveldb::Slice pkey((const char *)&pk, sizeof(uint64_t));
leveldb::Slice skey((const char *)&sk, sizeof(uint64_t));
memcpy(content, skey.data(), skey.size());
leveldb::Status s =
db->PutWithSec(woptions, pkey, skey, leveldb::Slice(content, val_sz));
if (!s.ok()) {
ERROR_EXIT("put failed");
}
stats->w++;
} else if (x[0] == "rs" || x[0] == "ro") {
roptions.sec_read = true;
roptions.num_pri_probe = 0;
roptions.time_pdb = 0; // time to search pdb in secondary query
roptions.time_pri_file_io = 0;
roptions.time_validate = 0;
bool index_only = false;
{
StopWatch<double> sw(stats->durationRS);
uint64_t sk = __builtin_bswap64(std::stoul(x[1]));
leveldb::Slice skey((const char *)&sk, sizeof(uint64_t));
if (leveldb::config::kIndexOnlyQuery || x[0] == "ro") {
if (FLAGS_range_search) {
roptions.num_scan_pkey_cnt = 5;
leveldb::Status s =
db->SecScanPKeyOnly(roptions, skey, &pkey_values);
} else {
leveldb::Status s =
db->SecGetPKeyOnly(roptions, skey, &pkey_values);
}
index_only = true;
} else {
leveldb::Status s = db->SecGet(roptions, skey, &svalues);
}
}
stats->durationPDB_RS += roptions.time_pdb;
stats->duration_pri_file_io += roptions.time_pri_file_io;
stats->duration_validate += roptions.time_validate;
stats->num_pri_probe += roptions.num_pri_probe;
if (index_only) {
int size = pkey_values.size();
stats->rscount += size;
if (size > 0) {
pkey_values.clear();
}
} else {
int size = svalues.size();
stats->rscount += size;
if (size > 0) {
svalues.clear();
}
}
stats->rs++;
} else if (x[0] == "rp") {
roptions.sec_read = false;
uint64_t pk = std::stoul(x[1]);
std::string pvalue;
StopWatch<double> sw(stats->durationRP);
leveldb::Status s = db->Get(
roptions, leveldb::Slice((const char *)&pk, sizeof(pk)), &pvalue);
stats->rp++;
}
if (i % FLAGS_log_point == 0) {
if (FLAGS_threads == 1) {
if (i / FLAGS_log_point == 1) {
stats->print_header();
}
stats->print();
} else if (tid == 0) {
std::cout << "... finished " << i << " ops\r";
}
}
// if (i >= MAX_OP) break;
}
double total_time = stats->durationW + stats->durationRP + stats->durationRS;
stats->tp = (double)i * 1e9 / total_time;
ifile.close();
}
int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
std::cout << "DB: " << FLAGS_db << std::endl;
memset(content, 'A', sizeof(content));
leveldb::Options options;
options.write_buffer_size = 64 << 20; // 64MB memtable
// 16 GB cache for ~100 GB DB
options.block_cache = leveldb::NewLRUCache(16ul << 30);
options.max_open_files = 50000;
options.filter_policy = leveldb::NewBloomFilterPolicy(10);
if (FLAGS_use_existing_db) {
options.create_if_missing = false;
options.allow_seek_compaction = false;
} else {
options.create_if_missing = true;
options.error_if_exists = true;
options.allow_seek_compaction = true;
}
options.using_s_index = true;
leveldb::DB *db;
leveldb::Status status = leveldb::DB::Open(options, FLAGS_db, &db);
if (!status.ok()) {
std::cout << "Open DB failed" << std::endl;
return 0;
}
Stats stats[FLAGS_threads];
std::thread ths[FLAGS_threads - 1];
SharedThreadState shared_state;
if (FLAGS_preload != "") {
std::cout << "\nrunning " << FLAGS_preload << std::endl;
stats[0].clear();
for (int t = 1; t < FLAGS_threads; ++t) {
stats[t].clear();
ths[t - 1] = std::thread(run_trace, db, FLAGS_preload, t, &shared_state,
&stats[t], 10);
}
run_trace(db, FLAGS_preload, 0, &shared_state, &stats[0], 10);
for (int t = 1; t < FLAGS_threads; ++t) {
ths[t - 1].join();
stats[0].merge(stats[t]);
}
std::cout << "\n";
stats[0].print_header();
stats[0].print();
stats[0].print_final("Preload(PUT)");
}
if (FLAGS_trace != "") {
std::vector<int> topk_set;
std::stringstream ss(FLAGS_topk);
std::string s_topk;
while (std::getline(ss, s_topk, ',')) {
topk_set.push_back(std::stoi(s_topk));
}
for (int i = 0; i < topk_set.size(); ++i) {
std::cout << "\nrunning " << FLAGS_trace << " for topk " << topk_set[i];
if (FLAGS_range_search) {
std::cout << " range";
}
std::cout << std::endl;
// run_trace(db, FLAGS_trace, topk_set[i]);
stats[0].clear();
for (int t = 1; t < FLAGS_threads; ++t) {
stats[t].clear();
ths[t - 1] = std::thread(run_trace, db, FLAGS_trace, t, &shared_state,
&stats[t], topk_set[i]);
}
run_trace(db, FLAGS_trace, 0, &shared_state, &stats[0], topk_set[i]);
for (int t = 1; t < FLAGS_threads; ++t) {
ths[t - 1].join();
stats[0].merge(stats[t]);
}
std::cout << "\n";
stats[0].print_header();
stats[0].print();
stats[0].print_final("Trace_top" + std::to_string(topk_set[i]));
}
}
delete db;
delete options.filter_policy;
delete options.block_cache;
return 0;
}