-
Notifications
You must be signed in to change notification settings - Fork 116
/
kvthread.cc
254 lines (227 loc) · 7.54 KB
/
kvthread.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
/* 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.
*/
#include "kvthread.hh"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <new>
#include <sys/mman.h>
#if HAVE_SUPERPAGE && !NOSUPERPAGE
#include <sys/types.h>
#include <dirent.h>
#endif
threadinfo *threadinfo::allthreads;
#if ENABLE_ASSERTIONS
int threadinfo::no_pool_value;
#endif
inline threadinfo::threadinfo(int purpose, int index) {
gc_epoch_ = perform_gc_epoch_ = 0;
logger_ = nullptr;
next_ = nullptr;
purpose_ = purpose;
index_ = index;
for (size_t i = 0; i != sizeof(pool_) / sizeof(pool_[0]); ++i) {
pool_[i] = nullptr;
}
void *limbo_space = allocate(sizeof(limbo_group), memtag_limbo);
mark(tc_limbo_slots, limbo_group::capacity);
limbo_head_ = limbo_tail_ = new(limbo_space) limbo_group;
ts_ = 2;
for (size_t i = 0; i != sizeof(counters_) / sizeof(counters_[0]); ++i) {
counters_[i] = 0;
}
}
threadinfo *threadinfo::make(int purpose, int index) {
static int threads_initialized;
threadinfo* ti = new(malloc(8192)) threadinfo(purpose, index);
ti->next_ = allthreads;
allthreads = ti;
if (!threads_initialized) {
#if ENABLE_ASSERTIONS
const char* s = getenv("_");
no_pool_value = s && strstr(s, "valgrind") != 0;
#endif
threads_initialized = 1;
}
return ti;
}
void threadinfo::refill_rcu() {
if (!limbo_tail_->next_) {
void *limbo_space = allocate(sizeof(limbo_group), memtag_limbo);
mark(tc_limbo_slots, limbo_group::capacity);
limbo_tail_->next_ = new(limbo_space) limbo_group;
}
limbo_tail_ = limbo_tail_->next_;
assert(limbo_tail_->head_ == 0 && limbo_tail_->tail_ == 0);
}
inline unsigned limbo_group::clean_until(threadinfo& ti, mrcu_epoch_type epoch_bound,
unsigned count) {
epoch_type epoch = 0;
while (head_ != tail_) {
if (e_[head_].ptr_) {
ti.free_rcu(e_[head_].ptr_, e_[head_].u_.tag);
ti.mark(tc_gc);
--count;
if (!count) {
e_[head_].ptr_ = nullptr;
e_[head_].u_.epoch = epoch;
break;
}
} else {
epoch = e_[head_].u_.epoch;
if (signed_epoch_type(epoch_bound - epoch) < 0)
break;
}
++head_;
}
if (head_ == tail_)
head_ = tail_ = 0;
return count;
}
void threadinfo::hard_rcu_quiesce() {
limbo_group* empty_head = nullptr;
limbo_group* empty_tail = nullptr;
unsigned count = rcu_free_count;
mrcu_epoch_type epoch_bound = active_epoch.load() - 1;
if (limbo_head_->head_ == limbo_head_->tail_
|| mrcu_signed_epoch_type(epoch_bound - limbo_head_->first_epoch()) < 0)
goto done;
// clean [limbo_head_, limbo_tail_]
while (count) {
count = limbo_head_->clean_until(*this, epoch_bound, count);
if (limbo_head_->head_ != limbo_head_->tail_)
break;
if (!empty_head)
empty_head = limbo_head_;
empty_tail = limbo_head_;
if (limbo_head_ == limbo_tail_) {
limbo_head_ = limbo_tail_ = empty_head;
goto done;
}
limbo_head_ = limbo_head_->next_;
}
// hook empties after limbo_tail_
if (empty_head) {
empty_tail->next_ = limbo_tail_->next_;
limbo_tail_->next_ = empty_head;
}
done:
if (!count)
perform_gc_epoch_ = epoch_bound; // do GC again immediately
else
perform_gc_epoch_ = epoch_bound + 1;
}
void threadinfo::report_rcu(void *ptr) const
{
for (limbo_group *lg = limbo_head_; lg; lg = lg->next_) {
int status = 0;
limbo_group::epoch_type e = 0;
for (unsigned i = 0; i < lg->capacity; ++i) {
if (i == lg->head_)
status = 1;
if (i == lg->tail_) {
status = 0;
e = 0;
}
if (lg->e_[i].ptr_ == ptr)
fprintf(stderr, "thread %d: rcu %p@%d: %s as %x @%" PRIu64 "\n",
index_, lg, i, status ? "waiting" : "freed",
lg->e_[i].u_.tag, e);
else if (!lg->e_[i].ptr_)
e = lg->e_[i].u_.epoch;
}
}
}
void threadinfo::report_rcu_all(void *ptr)
{
for (threadinfo *ti = allthreads; ti; ti = ti->next())
ti->report_rcu(ptr);
}
#if HAVE_SUPERPAGE && !NOSUPERPAGE
static size_t read_superpage_size() {
if (DIR* d = opendir("/sys/kernel/mm/hugepages")) {
size_t n = (size_t) -1;
while (struct dirent* de = readdir(d))
if (de->d_type == DT_DIR
&& strncmp(de->d_name, "hugepages-", 10) == 0
&& de->d_name[10] >= '0' && de->d_name[10] <= '9') {
size_t x = strtol(&de->d_name[10], 0, 10) << 10;
n = (x < n ? x : n);
}
closedir(d);
return n;
} else
return 2 << 20;
}
static size_t superpage_size = 0;
#endif
static void initialize_pool(void* pool, size_t sz, size_t unit) {
char* p = reinterpret_cast<char*>(pool);
void** nextptr = reinterpret_cast<void**>(p);
for (size_t off = unit; off + unit <= sz; off += unit) {
*nextptr = p + off;
nextptr = reinterpret_cast<void**>(p + off);
}
*nextptr = 0;
}
void threadinfo::refill_pool(int nl) {
assert(!pool_[nl - 1]);
if (!use_pool()) {
pool_[nl - 1] = malloc(nl * CACHE_LINE_SIZE);
if (pool_[nl - 1])
*reinterpret_cast<void**>(pool_[nl - 1]) = 0;
return;
}
void* pool = 0;
size_t pool_size = 0;
int r;
#if HAVE_SUPERPAGE && !NOSUPERPAGE
if (!superpage_size)
superpage_size = read_superpage_size();
if (superpage_size != (size_t) -1) {
pool_size = superpage_size;
# if MADV_HUGEPAGE
if ((r = posix_memalign(&pool, pool_size, pool_size)) != 0) {
fprintf(stderr, "posix_memalign superpage: %s\n", strerror(r));
pool = 0;
superpage_size = (size_t) -1;
} else if (madvise(pool, pool_size, MADV_HUGEPAGE) != 0) {
perror("madvise superpage");
superpage_size = (size_t) -1;
}
# elif MAP_HUGETLB
pool = mmap(0, pool_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0);
if (pool == MAP_FAILED) {
perror("mmap superpage");
pool = 0;
superpage_size = (size_t) -1;
}
# else
superpage_size = (size_t) -1;
# endif
}
#endif
if (!pool) {
pool_size = 2 << 20;
if ((r = posix_memalign(&pool, CACHE_LINE_SIZE, pool_size)) != 0) {
fprintf(stderr, "posix_memalign: %s\n", strerror(r));
abort();
}
}
initialize_pool(pool, pool_size, nl * CACHE_LINE_SIZE);
pool_[nl - 1] = pool;
}