forked from stephentu/silo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rcu.h
333 lines (274 loc) · 7.37 KB
/
rcu.h
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
#ifndef _RCU_H_
#define _RCU_H_
#include <stdint.h>
#include <pthread.h>
#include <map>
#include <vector>
#include <list>
#include <utility>
#include "allocator.h"
#include "counter.h"
#include "spinlock.h"
#include "util.h"
#include "ticker.h"
#include "pxqueue.h"
class rcu {
template <bool> friend class scoped_rcu_base;
public:
class sync;
typedef uint64_t epoch_t;
typedef void (*deleter_t)(void *);
struct delete_entry {
void* ptr;
intptr_t action;
inline delete_entry(void* ptr, size_t sz)
: ptr(ptr), action(-sz) {
INVARIANT(action < 0);
}
inline delete_entry(void* ptr, deleter_t fn)
: ptr(ptr), action(reinterpret_cast<uintptr_t>(fn)) {
INVARIANT(action > 0);
}
void run(rcu::sync& s) {
if (action < 0)
s.dealloc(ptr, -action);
else
(*reinterpret_cast<deleter_t>(action))(ptr);
}
bool operator==(const delete_entry& x) const {
return ptr == x.ptr && action == x.action;
}
bool operator!=(const delete_entry& x) const {
return !(*this == x);
}
bool operator<(const delete_entry& x) const {
return ptr < x.ptr || (ptr == x.ptr && action < x.action);
}
};
typedef basic_px_queue<delete_entry, 4096> px_queue;
template <typename T>
static inline void
deleter(void *p)
{
delete (T *) p;
}
template <typename T>
static inline void
deleter_array(void *p)
{
delete [] (T *) p;
}
#ifdef CHECK_INVARIANTS
static const uint64_t EpochTimeMultiplier = 10; /* 10 * 1 ms */
#else
static const uint64_t EpochTimeMultiplier = 25; /* 25 * 40 ms */
#endif
static_assert(EpochTimeMultiplier >= 1, "XX");
// legacy helpers
static const uint64_t EpochTimeUsec = ticker::tick_us * EpochTimeMultiplier;
static const uint64_t EpochTimeNsec = EpochTimeUsec * 1000;
static const size_t NQueueGroups = 32;
// all RCU threads interact w/ the RCU subsystem via
// a sync struct
//
// this is also serving as a memory allocator for the time being
class sync {
friend class rcu;
template <bool> friend class scoped_rcu_base;
public:
px_queue queue_;
px_queue scratch_;
unsigned depth_; // 0 indicates no rcu region
unsigned last_reaped_epoch_;
#ifdef ENABLE_EVENT_COUNTERS
uint64_t last_reaped_timestamp_us_;
uint64_t last_release_timestamp_us_;
#endif
private:
rcu *impl_;
// local memory allocator
ssize_t pin_cpu_;
void *arenas_[allocator::MAX_ARENAS];
size_t deallocs_[allocator::MAX_ARENAS]; // keeps track of the number of
// un-released deallocations
public:
sync(rcu *impl)
: depth_(0)
, last_reaped_epoch_(0)
#ifdef ENABLE_EVENT_COUNTERS
, last_reaped_timestamp_us_(0)
, last_release_timestamp_us_(0)
#endif
, impl_(impl)
, pin_cpu_(-1)
{
ALWAYS_ASSERT(((uintptr_t)this % CACHELINE_SIZE) == 0);
queue_.alloc_freelist(NQueueGroups);
scratch_.alloc_freelist(NQueueGroups);
NDB_MEMSET(&arenas_[0], 0, sizeof(arenas_));
NDB_MEMSET(&deallocs_[0], 0, sizeof(deallocs_));
}
inline void
set_pin_cpu(size_t cpu)
{
pin_cpu_ = cpu;
}
inline ssize_t
get_pin_cpu() const
{
return pin_cpu_;
}
// allocate a block of memory of size sz. caller needs to remember
// the size of the allocation when calling free
void *alloc(size_t sz);
// allocates a block of memory of size sz, with the intention of never
// free-ing it. is meant for reasonably large allocations (order of pages)
void *alloc_static(size_t sz);
void dealloc(void *p, size_t sz);
void dealloc_rcu(void *p, size_t sz);
// try to release local arenas back to the allocator based on some simple
// thresholding heuristics-- is relative expensive operation. returns true
// if a release was actually performed, false otherwise
bool try_release();
void do_cleanup();
inline unsigned depth() const { return depth_; }
private:
void do_release();
inline void
ensure_arena(size_t arena)
{
if (likely(arenas_[arena]))
return;
INVARIANT(pin_cpu_ >= 0);
arenas_[arena] = allocator::AllocateArenas(pin_cpu_, arena);
}
};
// thin forwarders
inline void *
alloc(size_t sz)
{
return mysync().alloc(sz);
}
inline void *
alloc_static(size_t sz)
{
return mysync().alloc_static(sz);
}
// this releases memory back to the allocator subsystem
// this should NOT be used to free objects!
inline void
dealloc(void *p, size_t sz)
{
return mysync().dealloc(p, sz);
}
void dealloc_rcu(void *p, size_t sz);
inline bool
try_release()
{
return mysync().try_release();
}
inline void
do_cleanup()
{
mysync().do_cleanup();
}
void free_with_fn(void *p, deleter_t fn);
template <typename T>
inline void
free(T *p)
{
free_with_fn(p, deleter<T>);
}
template <typename T>
inline void
free_array(T *p)
{
free_with_fn(p, deleter_array<T>);
}
// the tick is in units of rcu ticks
inline bool
in_rcu_region(uint64_t &rcu_tick) const
{
const sync *s = syncs_.myview();
if (unlikely(!s))
return false;
const bool is_guarded = ticker::s_instance.is_locally_guarded(rcu_tick);
const bool has_depth = s->depth();
if (has_depth && !is_guarded)
INVARIANT(false);
rcu_tick = to_rcu_ticks(rcu_tick);
return has_depth;
}
inline bool
in_rcu_region() const
{
uint64_t rcu_tick;
return in_rcu_region(rcu_tick);
}
// all threads have moved at least to the cleaning tick, so any pointers <
// the cleaning tick can be safely purged
inline uint64_t
cleaning_rcu_tick_exclusive() const
{
return to_rcu_ticks(ticker::s_instance.global_last_tick_exclusive());
}
// pin the current thread to CPU.
//
// this CPU number corresponds to the ones exposed by
// sched.h. note that we currently pin to the numa node
// associated with the cpu. memory allocation, however, is
// CPU-specific
void pin_current_thread(size_t cpu);
void fault_region();
static rcu s_instance CACHE_ALIGNED; // system wide instance
static void Test();
private:
rcu(); // private ctor to enforce singleton
static inline uint64_t constexpr
to_rcu_ticks(uint64_t ticks)
{
return ticks / EpochTimeMultiplier;
}
inline sync &mysync() { return syncs_.my(this); }
percore_lazy<sync> syncs_;
};
template <bool DoCleanup>
class scoped_rcu_base {
public:
// movable, but not copy-constructable
scoped_rcu_base(scoped_rcu_base &&) = default;
scoped_rcu_base(const scoped_rcu_base &) = delete;
scoped_rcu_base &operator=(const scoped_rcu_base &) = delete;
scoped_rcu_base()
: sync_(&rcu::s_instance.mysync()),
guard_(ticker::s_instance)
{
sync_->depth_++;
}
~scoped_rcu_base()
{
INVARIANT(sync_->depth_);
const unsigned new_depth = --sync_->depth_;
guard_.destroy();
if (new_depth || !DoCleanup)
return;
// out of RCU region now, check if we need to run cleaner
sync_->do_cleanup();
}
inline ticker::guard *
guard()
{
return guard_.obj();
}
inline rcu::sync *
sync()
{
return sync_;
}
private:
rcu::sync *sync_;
unmanaged<ticker::guard> guard_;
};
typedef scoped_rcu_base<true> scoped_rcu_region;
class disabled_rcu_region {};
#endif /* _RCU_H_ */