forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvthread.hh
378 lines (340 loc) · 11.1 KB
/
kvthread.hh
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2016 President and Fellows of Harvard College
* Copyright (c) 2012-2016 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef KVTHREAD_HH
#define KVTHREAD_HH 1
#include "mtcounters.hh"
#include "compiler.hh"
#include "circular_int.hh"
#include "timestamp.hh"
#include "memdebug.hh"
#include <assert.h>
#include <pthread.h>
#include <sys/mman.h>
#include <stdlib.h>
class threadinfo;
class loginfo;
typedef uint64_t mrcu_epoch_type;
typedef int64_t mrcu_signed_epoch_type;
extern volatile mrcu_epoch_type globalepoch; // global epoch, updated regularly
extern volatile mrcu_epoch_type active_epoch;
struct limbo_group {
typedef mrcu_epoch_type epoch_type;
typedef mrcu_signed_epoch_type signed_epoch_type;
struct limbo_element {
void* ptr_;
union {
memtag tag;
epoch_type epoch;
} u_;
};
enum { capacity = (4076 - sizeof(epoch_type) - sizeof(limbo_group*)) / sizeof(limbo_element) };
unsigned head_;
unsigned tail_;
epoch_type epoch_;
limbo_group* next_;
limbo_element e_[capacity];
limbo_group()
: head_(0), tail_(0), next_() {
}
epoch_type first_epoch() const {
assert(head_ != tail_);
return e_[head_].u_.epoch;
}
void push_back(void* ptr, memtag tag, mrcu_epoch_type epoch) {
assert(tail_ + 2 <= capacity);
if (head_ == tail_ || epoch_ != epoch) {
e_[tail_].ptr_ = nullptr;
e_[tail_].u_.epoch = epoch;
epoch_ = epoch;
++tail_;
}
e_[tail_].ptr_ = ptr;
e_[tail_].u_.tag = tag;
++tail_;
}
inline unsigned clean_until(threadinfo& ti, mrcu_epoch_type epoch_bound, unsigned count);
};
template <int N> struct has_threadcounter {
static bool test(threadcounter ci) {
return unsigned(ci) < unsigned(N);
}
};
template <> struct has_threadcounter<0> {
static bool test(threadcounter) {
return false;
}
};
struct mrcu_callback {
virtual ~mrcu_callback() {
}
virtual void operator()(threadinfo& ti) = 0;
};
class threadinfo {
public:
enum {
TI_MAIN, TI_PROCESS, TI_LOG, TI_CHECKPOINT
};
static threadinfo* allthreads;
threadinfo* next() const {
return next_;
}
static threadinfo* make(int purpose, int index);
// XXX destructor
// thread information
int purpose() const {
return purpose_;
}
int index() const {
return index_;
}
loginfo* logger() const {
return logger_;
}
void set_logger(loginfo* logger) {
assert(!logger_ && logger);
logger_ = logger;
}
// timestamps
kvtimestamp_t operation_timestamp() const {
return timestamp();
}
kvtimestamp_t update_timestamp() const {
return ts_;
}
kvtimestamp_t update_timestamp(kvtimestamp_t x) const {
if (circular_int<kvtimestamp_t>::less_equal(ts_, x))
// x might be a marker timestamp; ensure result is not
ts_ = (x | 1) + 1;
return ts_;
}
template <typename N> void observe_phantoms(N* n) {
if (circular_int<kvtimestamp_t>::less(ts_, n->phantom_epoch_[0]))
ts_ = n->phantom_epoch_[0];
}
// event counters
void mark(threadcounter ci) {
if (has_threadcounter<int(ncounters)>::test(ci))
++counters_[ci];
}
void mark(threadcounter ci, int64_t delta) {
if (has_threadcounter<int(ncounters)>::test(ci))
counters_[ci] += delta;
}
void set_counter(threadcounter ci, uint64_t value) {
if (has_threadcounter<int(ncounters)>::test(ci))
counters_[ci] = value;
}
bool has_counter(threadcounter ci) const {
return has_threadcounter<int(ncounters)>::test(ci);
}
uint64_t counter(threadcounter ci) const {
return has_threadcounter<int(ncounters)>::test(ci) ? counters_[ci] : 0;
}
struct accounting_relax_fence_function {
threadinfo* ti_;
threadcounter ci_;
accounting_relax_fence_function(threadinfo* ti, threadcounter ci)
: ti_(ti), ci_(ci) {
}
void operator()() {
relax_fence();
ti_->mark(ci_);
}
};
/** @brief Return a function object that calls mark(ci); relax_fence().
*
* This function object can be used to count the number of relax_fence()s
* executed. */
accounting_relax_fence_function accounting_relax_fence(threadcounter ci) {
return accounting_relax_fence_function(this, ci);
}
struct stable_accounting_relax_fence_function {
threadinfo* ti_;
stable_accounting_relax_fence_function(threadinfo* ti)
: ti_(ti) {
}
template <typename V>
void operator()(V v) {
relax_fence();
ti_->mark(threadcounter(tc_stable + (v.isleaf() << 1) + v.splitting()));
}
};
/** @brief Return a function object that calls mark(ci); relax_fence().
*
* This function object can be used to count the number of relax_fence()s
* executed. */
stable_accounting_relax_fence_function stable_fence() {
return stable_accounting_relax_fence_function(this);
}
accounting_relax_fence_function lock_fence(threadcounter ci) {
return accounting_relax_fence_function(this, ci);
}
// memory allocation
void* allocate(size_t sz, memtag tag) {
void* p = malloc(sz + memdebug_size);
p = memdebug::make(p, sz, tag);
if (p)
mark(threadcounter(tc_alloc + (tag > memtag_value)), sz);
return p;
}
void deallocate(void* p, size_t sz, memtag tag) {
// in C++ allocators, 'p' must be nonnull
assert(p);
p = memdebug::check_free(p, sz, tag);
free(p);
mark(threadcounter(tc_alloc + (tag > memtag_value)), -sz);
}
void deallocate_rcu(void* p, size_t sz, memtag tag) {
assert(p);
memdebug::check_rcu(p, sz, tag);
record_rcu(p, tag);
mark(threadcounter(tc_alloc + (tag > memtag_value)), -sz);
}
void* pool_allocate(size_t sz, memtag tag) {
int nl = (sz + memdebug_size + CACHE_LINE_SIZE - 1) / CACHE_LINE_SIZE;
assert(nl <= pool_max_nlines);
if (unlikely(!pool_[nl - 1]))
refill_pool(nl);
void* p = pool_[nl - 1];
if (p) {
pool_[nl - 1] = *reinterpret_cast<void **>(p);
p = memdebug::make(p, sz, memtag(tag + nl));
mark(threadcounter(tc_alloc + (tag > memtag_value)),
nl * CACHE_LINE_SIZE);
}
return p;
}
void pool_deallocate(void* p, size_t sz, memtag tag) {
int nl = (sz + memdebug_size + CACHE_LINE_SIZE - 1) / CACHE_LINE_SIZE;
assert(p && nl <= pool_max_nlines);
p = memdebug::check_free(p, sz, memtag(tag + nl));
if (use_pool()) {
*reinterpret_cast<void **>(p) = pool_[nl - 1];
pool_[nl - 1] = p;
} else
free(p);
mark(threadcounter(tc_alloc + (tag > memtag_value)),
-nl * CACHE_LINE_SIZE);
}
void pool_deallocate_rcu(void* p, size_t sz, memtag tag) {
int nl = (sz + memdebug_size + CACHE_LINE_SIZE - 1) / CACHE_LINE_SIZE;
assert(p && nl <= pool_max_nlines);
memdebug::check_rcu(p, sz, memtag(tag + nl));
record_rcu(p, memtag(tag + nl));
mark(threadcounter(tc_alloc + (tag > memtag_value)),
-nl * CACHE_LINE_SIZE);
}
// RCU
enum { rcu_free_count = 128 }; // max # of entries to free per rcu_quiesce() call
void rcu_start() {
if (gc_epoch_ != globalepoch)
gc_epoch_ = globalepoch;
}
void rcu_stop() {
if (perform_gc_epoch_ != active_epoch)
hard_rcu_quiesce();
gc_epoch_ = 0;
}
void rcu_quiesce() {
rcu_start();
if (perform_gc_epoch_ != active_epoch)
hard_rcu_quiesce();
}
typedef ::mrcu_callback mrcu_callback;
void rcu_register(mrcu_callback* cb) {
record_rcu(cb, memtag(-1));
}
// thread management
pthread_t& pthread() {
return pthreadid_;
}
pthread_t pthread() const {
return pthreadid_;
}
void report_rcu(void* ptr) const;
static void report_rcu_all(void* ptr);
static inline mrcu_epoch_type min_active_epoch();
private:
union {
struct {
mrcu_epoch_type gc_epoch_;
mrcu_epoch_type perform_gc_epoch_;
loginfo *logger_;
threadinfo *next_;
int purpose_;
int index_; // the index of a udp, logging, tcp,
// checkpoint or recover thread
pthread_t pthreadid_;
};
char padding1[CACHE_LINE_SIZE];
};
enum { pool_max_nlines = 20 };
void* pool_[pool_max_nlines];
limbo_group* limbo_head_;
limbo_group* limbo_tail_;
mutable kvtimestamp_t ts_;
//enum { ncounters = (int) tc_max };
enum { ncounters = 0 };
uint64_t counters_[ncounters];
void refill_pool(int nl);
void refill_rcu();
void free_rcu(void *p, memtag tag) {
if ((tag & memtag_pool_mask) == 0) {
p = memdebug::check_free_after_rcu(p, tag);
::free(p);
} else if (tag == memtag(-1))
(*static_cast<mrcu_callback*>(p))(*this);
else {
p = memdebug::check_free_after_rcu(p, tag);
int nl = tag & memtag_pool_mask;
*reinterpret_cast<void**>(p) = pool_[nl - 1];
pool_[nl - 1] = p;
}
}
void record_rcu(void* ptr, memtag tag) {
if (limbo_tail_->tail_ + 2 > limbo_tail_->capacity)
refill_rcu();
uint64_t epoch = globalepoch;
limbo_tail_->push_back(ptr, tag, epoch);
}
#if ENABLE_ASSERTIONS
static int no_pool_value;
#endif
static bool use_pool() {
#if ENABLE_ASSERTIONS
return !no_pool_value;
#else
return true;
#endif
}
inline threadinfo(int purpose, int index);
threadinfo(const threadinfo&) = delete;
~threadinfo() {}
threadinfo& operator=(const threadinfo&) = delete;
void hard_rcu_quiesce();
friend struct limbo_group;
};
inline mrcu_epoch_type threadinfo::min_active_epoch() {
mrcu_epoch_type ae = globalepoch;
for (threadinfo* ti = allthreads; ti; ti = ti->next()) {
prefetch((const void*) ti->next());
mrcu_epoch_type te = ti->gc_epoch_;
if (te && mrcu_signed_epoch_type(te - ae) < 0)
ae = te;
}
return ae;
}
#endif