-
Notifications
You must be signed in to change notification settings - Fork 0
/
red.cpp
269 lines (227 loc) · 7.21 KB
/
red.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
#include "jsw-common.h"
#include "plot.h"
#include "rbt-allocator.h"
#include "rbt-log.h"
#include "rbt/state-machine-insert.h"
#include "rbt/state-machine-remove.h"
#include "recover.h"
#include "state-kind.h"
#include "statistics.h"
#include "tree.h"
#include "util.h"
#include <sched.h>
#include <unistd.h>
#include <algorithm>
#include <cinttypes>
#include <chrono>
#include <fstream>
#include <memory>
#include <optional>
#include <random>
#include <string>
unsigned Count = 10000000; // 1024 * 1024; // + 1024;
unsigned Seed = 0;
namespace {
#ifdef __linux__
void pin() {
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
int res = sched_setaffinity(0, sizeof(cpu_set_t), &mask);
assert(res == 0);
res += 1;
}
#endif
} // namespace
static void runInsertRemove(AllocatorType &Allocator, SMStateType &State,
LogType &Log) {
std::random_device rd;
std::mt19937 g(rd());
g.seed(Seed);
std::vector<int> v;
v.reserve(Count);
for (unsigned int i = 0; i < Count; i++)
v.push_back(i);
std::shuffle(v.begin(), v.end(), g);
auto start = std::chrono::high_resolution_clock::now();
for (unsigned i = 0; i < Count; i++) {
Rbt::Insert::GoToC0(Allocator, Log, State, v[i], 512);
}
auto stop = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = stop - start;
printf("insert=%fs\n", diff.count());
// shuffle for remove
std::shuffle(v.begin(), v.end(), g);
start = std::chrono::high_resolution_clock::now();
for (unsigned i = 0; i < Count; i++) {
Rbt::Remove::GoToC0(Allocator, Log, State, v[i]);
}
stop = std::chrono::high_resolution_clock::now();
diff = stop - start;
printf("remove=%fs\n", diff.count());
}
static void recoverInsert(AllocatorType &Allocator, SMStateType &State,
LogType &Log) {
#ifdef __APPLE__
printf("recover in %s; key=%llu\n",
StateKind2String(State.getStateKind()).c_str(), Log.getLog()->Key);
#else
printf("recover in %s; key=%lu\n",
StateKind2String(State.getStateKind()).c_str(), Log.getLog()->Key);
#endif
Rbt::Insert::RecoverToC0(Allocator, Log, State);
printf("assert=%d\n", rb_assert(Allocator, Log, Log.getLog()->Root));
}
static void recoverRemove(AllocatorType &Allocator, SMStateType &State,
LogType &Log) {
#ifdef __APPLE__
printf("recover in %s; key=%llu\n",
StateKind2String(State.getStateKind()).c_str(), Log.getLog()->Key);
#else
printf("recover in %s; key=%lu\n",
StateKind2String(State.getStateKind()).c_str(), Log.getLog()->Key);
#endif
Rbt::Remove::RecoverToC0(Allocator, Log, State);
printf("assert=%d\n", rb_assert(Allocator, Log, Log.getLog()->Root));
}
static void runInsertWithStatistics(AllocatorType &Allocator,
SMStateType &State, LogType &Log) {
// std::fstream f("flushes.txt", f.out | f.trunc);
std::random_device rd;
std::mt19937 g(rd());
g.seed(Seed);
std::vector<int> v;
for (unsigned int i = 0; i < Count; i++)
v.push_back(i);
std::shuffle(v.begin(), v.end(), g);
uint64_t lookUpDepths = 0;
uint64_t treeDepths = 0;
for (unsigned i = 0; i < Count; i++) {
// statistics->resetState();
Rbt::Insert::GoToC0(Allocator, Log, State, v[i], 512);
// f << i << " " << statistics->getFlushCounter() << std::endl;
//printf("%u\n", i);
std::optional<unsigned> foundAt =
getLookupDepth(Allocator, Log.getLog()->Root, v[i]);
assert(foundAt.has_value());
lookUpDepths += foundAt.value();
//treeDepths += getTreeDepth(Allocator, Log.getLog()->Root);
}
statistics->dump();
statistics->resetState();
printf("assert=%d\n", rb_assert(Allocator, Log, Log.getLog()->Root));
printf("depth=%u\n", getTreeDepth(Allocator, Log.getLog()->Root));
printf("lookup=%" PRId64 ", depth=%" PRId64 "\n", lookUpDepths / Count, treeDepths / Count);
// for (unsigned i = 0; i < Count; i++) {
// Remove::GoToC0(Allocator, Log, State, i);
//}
// statistics->dump();
}
static void runInsert(AllocatorType &Allocator, SMStateType &State,
LogType &Log) {
std::random_device rd;
std::mt19937 g(rd());
g.seed(Seed);
std::vector<int> v;
for (unsigned int i = 0; i < Count; i++)
v.push_back(i);
std::shuffle(v.begin(), v.end(), g);
auto start = std::chrono::high_resolution_clock::now();
for (unsigned i = 0; i < Count; i++) {
// printf("insert %d\n", i);
Rbt::Insert::GoToC0(Allocator, Log, State, v[i], 512);
}
auto stop = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = stop - start;
unsigned TreeSize = getTreeSize(Allocator, Log.getLog()->Root);
printf("assert=%d nodes=%d\n", rb_assert(Allocator, Log, Log.getLog()->Root),
TreeSize);
printf("insert=%fs\n", diff.count());
plotNode(Allocator, Log.getLog()->Root);
}
// static void runRemove(AllocatorType &Allocator, SMStateType &State, LogType
// &Log) {
// unsigned TreeSize = getTreeSize(Allocator, Log.getLog()->Root);
// printf("assert=%d nodes=%d\n", rb_assert(Allocator, Log,
// Log.getLog()->Root),
// TreeSize);
// auto start = std::chrono::high_resolution_clock::now();
// for (unsigned i = 0; i < Count; i++) {
// Rbt::Remove::GoToC0(Allocator, Log, State, i);
// }
// auto stop = std::chrono::high_resolution_clock::now();
// std::chrono::duration<double> diff = stop - start;
//
// printf("assert=%d remove=%fs\n",
// rb_assert(Allocator, Log, Log.getLog()->Root), diff.count());
//}
int main(int argc, char **argv) {
#ifdef __linux__
pin();
#endif
printCPU();
printf("%s\n", VERSION);
statistics = std::make_unique<Statistics>();
#if defined(HAVE_PMDK)
PMDK::LogState log;
PMDK::Allocator Allocator = {log};
PMDK::SMState State;
#else
DRAM::LogState log;
DRAM::Allocator Allocator = {log};
DRAM::SMState State;
#endif
printf("sizeof(Tree) = %lu; sizeof(Log) = %lu; sizeof(NodeIndex) = %lu\n",
sizeof(Node), sizeof(RBTLogStructure), sizeof(NodeIndex));
int opt;
while ((opt = getopt(argc, argv, "srcethd:x:")) != -1) {
switch (opt) {
case 's':
Allocator.resetState();
State.resetState();
log.resetState();
runInsert(Allocator, State, log);
break;
case 'e':
isRecover = true;
recoverRemove(Allocator, State, log);
break;
case 'r':
isRecover = true;
recoverInsert(Allocator, State, log);
break;
case 't':
Allocator.resetState();
State.resetState();
log.resetState();
runInsertWithStatistics(Allocator, State, log);
break;
case 'c':
Allocator.resetState();
State.resetState();
log.resetState();
runInsertRemove(Allocator, State, log);
break;
case 'd':
Count = atoi(optarg);
break;
case 'x':
Seed = atoi(optarg);
break;
case 'h':
default: /* '?' */
printf("red: \n");
printf(" -d count \n");
printf(" -s insert \n");
printf(" -r recover from insert\n");
printf(" -e recover from remove\n");
printf(" -t insert with statistics\n");
printf(" -c remove\n");
printf(" -x seed \n");
exit(EXIT_FAILURE);
}
}
exit(EXIT_SUCCESS);
static_assert(sizeof(Iterators) == 64);
static_assert(sizeof(RedoLog) == 64);
}