forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit-mt.cc
190 lines (158 loc) · 5.48 KB
/
unit-mt.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
#include <iostream>
#include <random>
#include <vector>
#include <thread>
#include <pthread.h>
#include "config.h"
#include "compiler.hh"
#include "masstree.hh"
#include "kvthread.hh"
#include "masstree_tcursor.hh"
#include "masstree_insert.hh"
#include "masstree_print.hh"
#include "masstree_remove.hh"
#include "masstree_scan.hh"
#include "masstree_stats.hh"
#include "string.hh"
#define NUM_THREADS 64
class key_unparse_unsigned {
public:
static int unparse_key(Masstree::key<uint64_t> key, char* buf, int buflen) {
return snprintf(buf, buflen, "%" PRIu64, key.ikey());
}
};
class MasstreeWrapper {
public:
static constexpr uint64_t insert_bound = 0xfffff; //0xffffff;
struct table_params : public Masstree::nodeparams<15,15> {
typedef uint64_t value_type;
typedef Masstree::value_print<value_type> value_print_type;
typedef threadinfo threadinfo_type;
typedef key_unparse_unsigned key_unparse_type;
static constexpr ssize_t print_max_indent_depth = 12;
};
typedef Masstree::Str Str;
typedef Masstree::basic_table<table_params> table_type;
typedef Masstree::unlocked_tcursor<table_params> unlocked_cursor_type;
typedef Masstree::tcursor<table_params> cursor_type;
typedef Masstree::leaf<table_params> leaf_type;
typedef Masstree::internode<table_params> internode_type;
typedef typename table_type::node_type node_type;
typedef typename unlocked_cursor_type::nodeversion_value_type nodeversion_value_type;
static __thread typename table_params::threadinfo_type *ti;
MasstreeWrapper() {
this->table_init();
}
void table_init() {
if (ti == nullptr)
ti = threadinfo::make(threadinfo::TI_MAIN, -1);
table_.initialize(*ti);
key_gen_ = 0;
}
void keygen_reset() {
key_gen_ = 0;
}
static void thread_init(int thread_id) {
if (ti == nullptr)
ti = threadinfo::make(threadinfo::TI_PROCESS, thread_id);
}
void insert_test() {
while (1) {
auto int_key = fetch_and_add(&key_gen_, 1);
uint64_t key_buf;
if (int_key > insert_bound)
break;
Str key = make_key(int_key, key_buf);
cursor_type lp(table_, key);
bool found = lp.find_insert(*ti);
always_assert(!found, "keys should all be unique");
lp.value() = int_key;
fence();
lp.finish(1, *ti);
}
}
void remove_test() {
while (1) {
auto int_key = fetch_and_add(&key_gen_, 1);
uint64_t key_buf;
if (int_key > insert_bound)
break;
Str key = make_key(int_key, key_buf);
cursor_type lp(table_, key);
bool found = lp.find_locked(*ti);
always_assert(found, "keys must all exist");
lp.finish(-1, *ti);
}
}
void insert_remove_test(int thread_id) {
std::mt19937 gen(thread_id);
std::uniform_int_distribution<int> dist(1, 6);
uint64_t int_key = 0;
bool need_print = true;
while (!stopping) {
int_key = fetch_and_add(&key_gen_, 1);
uint64_t key_buf;
if (int_key > insert_bound)
break;
Str key = make_key(int_key, key_buf);
cursor_type lp(table_, key);
bool found = lp.find_insert(*ti);
always_assert(!found, "keys should all be unique 1");
lp.value() = int_key;
fence();
lp.finish(1, *ti);
if (dist(gen) <= 2) {
cursor_type lp1(table_, key);
bool found1 = lp1.find_locked(*ti);
if (!found1) {
stopping = true;
lp1.finish(0, *ti);
printf("failed at key %" PRIu64 ", lp1 got %p\n", int_key, lp1.node());
need_print = true;
break;
always_assert(found1, "this is my key!");
} else {
lp1.finish(-1, *ti);
}
}
}
printf("stopped at key %" PRIu64 "\n", int_key);
if (need_print && fetch_and_add(&printing, 1) == 0) {
table_.print(stdout);
fflush(stdout);
fprintf(stdout, "Stats: %s\n",
Masstree::json_stats(table_, ti).unparse(lcdf::Json::indent_depth(1000)).c_str());
}
}
private:
table_type table_;
uint64_t key_gen_;
static bool stopping;
static uint32_t printing;
static inline Str make_key(uint64_t int_key, uint64_t& key_buf) {
key_buf = __builtin_bswap64(int_key);
return Str((const char *)&key_buf, sizeof(key_buf));
}
};
__thread typename MasstreeWrapper::table_params::threadinfo_type* MasstreeWrapper::ti = nullptr;
bool MasstreeWrapper::stopping = false;
uint32_t MasstreeWrapper::printing = 0;
volatile mrcu_epoch_type active_epoch = 1;
volatile uint64_t globalepoch = 1;
volatile bool recovering = false;
void test_thread(MasstreeWrapper* mt, int thread_id) {
mt->thread_init(thread_id);
mt->insert_remove_test(thread_id);
}
int main() {
auto mt = new MasstreeWrapper();
mt->keygen_reset();
std::cout << "insert_remove_test..." << std::endl;
std::vector<std::thread> ths;
for (int i = 0; i < NUM_THREADS; ++i)
ths.emplace_back(test_thread, mt, i);
for (auto& t : ths)
t.join();
std::cout << "test pass." << std::endl;
return 0;
}