-
Notifications
You must be signed in to change notification settings - Fork 2
/
vhandle_sync.cc
240 lines (203 loc) · 5.95 KB
/
vhandle_sync.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
#include <unistd.h>
#include <sys/time.h>
#include <syscall.h>
#include "vhandle.h"
#include "vhandle_sync.h"
namespace felis {
static void *AllocateBuffer()
{
auto nr_zone = (NodeConfiguration::g_nr_threads - 1) / mem::kNrCorePerNode + 1;
auto p = (uint8_t *) mmap(
nullptr,
4096 * nr_zone,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
if (p == MAP_FAILED) {
perror("mmap");
std::abort();
}
for (int i = 0; i < nr_zone; i++) {
long nodemask = 1 << i;
if (syscall(__NR_mbind, p + i * 4096, 4096,
2, &nodemask, sizeof(long) * 8, 1) < 0) {
perror("mbind");
std::abort();
}
}
if (mlock(p, 4096 * nr_zone) < 0) {
perror("mlock");
std::abort();
}
memset(p, 0, 4096 * nr_zone);
return p;
}
struct SpinnerSlotData {
std::atomic_bool done;
long wait_cnt;
uint8_t __padding__[48];
};
static_assert(sizeof(SpinnerSlotData) == 64);
SpinnerSlotData *SpinnerSlot::slot(int idx)
{
auto d = std::div(idx, mem::kNrCorePerNode);
return buffer + 64 * d.quot + d.rem;
}
SpinnerSlot::SpinnerSlot()
{
buffer = (SpinnerSlotData *) AllocateBuffer();
}
void SpinnerSlot::ClearWaitCountStats()
{
for (int i = 0; i < NodeConfiguration::g_nr_threads; i++) {
slot(i)->wait_cnt = 0;
}
}
long SpinnerSlot::GetWaitCountStat(int core)
{
return slot(core)->wait_cnt;
}
void SpinnerSlot::WaitForData(volatile uintptr_t *addr, uint64_t sid, uint64_t ver,
void *handle)
{
probes::VersionRead{false, handle}();
uintptr_t oldval = *addr;
if (!IsPendingVal(oldval)) return;
probes::VersionRead{true, handle}();
int core = go::Scheduler::CurrentThreadPoolId() - 1;
uint64_t mask = 1ULL << core;
ulong wait_cnt = 2;
while (true) {
uintptr_t val = oldval;
uintptr_t newval = val & ~mask;
bool notified = false;
if ((oldval = __sync_val_compare_and_swap(addr, val, newval)) == val) {
notified = Spin(sid, ver, wait_cnt, addr);
oldval = *addr;
}
if (!IsPendingVal(oldval)) {
slot(core)->wait_cnt += wait_cnt;
return;
}
}
}
void SpinnerSlot::OfferData(volatile uintptr_t *addr, uintptr_t obj)
{
auto oldval = *addr;
auto newval = obj;
// installing newval
while (true) {
if (!IsPendingVal(oldval)) {
logger->critical("strange oldval {0:x}", oldval);
}
uintptr_t val = __sync_val_compare_and_swap(addr, oldval, newval);
if (val == oldval) break;
oldval = val;
}
// need to notify according to the bitmaps, which is oldval
uint64_t mask = (1ULL << 32) - 1;
uint64_t bitmap = mask - (oldval & mask);
Notify(bitmap);
}
bool SpinnerSlot::Spin(uint64_t sid, uint64_t ver, ulong &wait_cnt, volatile uintptr_t *ptr)
{
int core_id = go::Scheduler::CurrentThreadPoolId() - 1;
auto sched = go::Scheduler::Current();
auto &transport = util::Impl<PromiseRoutineTransportService>();
auto &dispatch = util::Impl<PromiseRoutineDispatchService>();
auto routine = sched->current_routine();
// routine->set_busy_poll(true);
// abort_if(core_id < 0, "We should not run on thread pool 0!");
while (!slot(core_id)->done.load(std::memory_order_acquire)) {
wait_cnt++;
if (unlikely((wait_cnt & 0x7FFFFFF) == 0)) {
int dep = dispatch.TraceDependency(ver);
printf("Deadlock on core %d? %lu (using %p) waiting for %lu (%d) node (%lu), ptr %p\n",
core_id, sid, routine, ver, dep, ver & 0xFF, ptr);
sleep(600);
}
if ((wait_cnt & 0x0FFFF) == 0) {
// Because periodic flush will run on all cores, we just have to flush our
// own per-core buffer.
transport.PeriodicIO(core_id);
}
if ((wait_cnt & 0x00FF) == 0) {
if (((BasePieceCollection::ExecutionRoutine *) routine)->Preempt()) {
// logger->info("Preempt back");
// Broken???
return true;
}
}
if (slot(core_id)->done.load(std::memory_order_acquire))
break;
_mm_pause();
}
probes::WaitCounters{wait_cnt, sid, ver, *ptr}();
slot(core_id)->done.store(false, std::memory_order_release);
return true;
}
void SpinnerSlot::Notify(uint64_t bitmap)
{
while (bitmap) {
int idx = __builtin_ctzll(bitmap);
slot(idx)->done.store(true, std::memory_order_release);
bitmap &= ~(1 << idx);
}
}
struct SimpleSyncData {
long wait_cnt;
uint8_t __padding__[56];
};
static_assert(sizeof(SimpleSyncData) == 64);
SimpleSync::SimpleSync()
{
buffer = (SimpleSyncData *) AllocateBuffer();
}
void SimpleSync::WaitForData(volatile uintptr_t *addr, uint64_t sid, uint64_t ver, void *handle)
{
long wait_cnt = 2;
int core_id = go::Scheduler::CurrentThreadPoolId() - 1;
auto sched = go::Scheduler::Current();
auto &transport = util::Impl<PromiseRoutineTransportService>();
auto &dispatch = util::Impl<PromiseRoutineDispatchService>();
auto routine = sched->current_routine();
while (IsPendingVal(*addr)) {
wait_cnt++;
if (unlikely((wait_cnt & 0x7FFFFFF) == 0)) {
int dep = dispatch.TraceDependency(ver);
printf("Deadlock on core %d? %lu (using %p) waiting for %lu (%d) node (%lu)\n",
core_id, sid, routine, ver, dep, ver & 0xFF);
sleep(600);
}
if ((wait_cnt & 0x0FFFF) == 0) {
transport.PeriodicIO(core_id);
}
if ((wait_cnt & 0x00FF) == 0) {
if (((BasePieceCollection::ExecutionRoutine *) routine)->Preempt()) {
continue;
}
}
if (!IsPendingVal(*addr))
break;
_mm_pause();
}
auto d = std::div(core_id, mem::kNrCorePerNode);
buffer[64 * d.quot + d.rem].wait_cnt += wait_cnt;
}
void SimpleSync::ClearWaitCountStats()
{
for (int i = 0; i < NodeConfiguration::g_nr_threads; i++) {
auto d = std::div(i, mem::kNrCorePerNode);
buffer[64 * d.quot + d.rem].wait_cnt = 0;
}
}
long SimpleSync::GetWaitCountStat(int core)
{
auto d = std::div(core, mem::kNrCorePerNode);
return buffer[64 * d.quot + d.rem].wait_cnt;
}
void SimpleSync::OfferData(volatile uintptr_t *addr, uintptr_t obj)
{
*addr = obj;
}
} // namespace felis