-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathk-testwait.cc
312 lines (280 loc) · 7.87 KB
/
k-testwait.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
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
#include "kernel.hh"
#include "k-wait.hh"
#define WQTEST_NPROC 5
#define WQTEST1_NOP 400
#define WQTEST2_NOP 20000
#define WQTEST3_NOP 300
#define WQTEST4_NOP 100
#define WQTEST5_NOP 1000
static std::atomic<int> phase;
static std::atomic<unsigned> n;
static wait_queue wq, wq2;
static spinlock wqdata_lock;
static unsigned wqdata_i;
static bool wqdata_flag;
static proc* wqt_proc[WQTEST_NPROC];
static std::atomic<unsigned>* wqt_idn[WQTEST_NPROC];
static unsigned long start_ticks;
namespace {
struct wq_reporter {
const char* phase_;
unsigned expected_;
unsigned step_;
unsigned last_n_ = 0;
unsigned last_ticks_;
unsigned report_trigger_;
wq_reporter(const char* name, unsigned expected, unsigned step)
: phase_(name), expected_(expected), step_(step),
last_ticks_(ticks), report_trigger_(step) {
}
void check(unsigned n) {
if (n != last_n_) {
last_n_ = n;
last_ticks_ = ticks;
} else if (long(ticks - last_ticks_) > HZ) {
panic("ktestwait appears to have failed (%s, %u/%u rounds)!\n",
phase_, last_n_, expected_);
}
if (last_n_ >= report_trigger_) {
console_printf("done round %u/%u\n", last_n_, expected_);
report_trigger_ = last_n_ + step_;
}
}
};
}
static void wq_tester() {
cli();
// determine own ID
proc* p = current();
int id = 0;
while (p != wqt_proc[id]) {
++id;
}
assert(id < WQTEST_NPROC);
// initialize counters
n = 0;
std::atomic<unsigned> idn;
wqt_idn[id] = &idn;
// Initialize random seed with some physical memory whose contents
// seems to vary run to run.
unsigned long* seedptr = pa2kptr<unsigned long*>(0xff8);
rand_engine re(*seedptr + id * 192381 + ticks * 10123);
++phase;
while (phase < 1 + WQTEST_NPROC) {
p->yield();
}
// First test phase: run many wakeups
if (id == 0) {
start_ticks = ticks;
console_printf("ktestwait phase 1 commencing\n");
unsigned want = WQTEST1_NOP * (WQTEST_NPROC - 1);
wq_reporter wqr("phase 1", want, 500);
while (n < want) {
unsigned r = re();
if (r < re.max() / 4) {
wq.wake_all();
} else if (r < re.max() / 2) {
p->yield();
} else {
pause();
}
if (r < re.max() / 512) {
wqr.check(n);
}
}
n = 0;
phase = 100;
} else {
for (int i = 0; i < WQTEST1_NOP; ++i) {
waiter w;
w.prepare(wq);
pause();
w.maybe_block();
w.clear();
++n;
++idn;
}
}
while (phase < 100) {
p->yield();
}
// Second test phase: run many non-blocking wakeups (test synchronization
// of wait queue)
if (id == 0) {
start_ticks = ticks;
console_printf("ktestwait phase 2 commencing\n");
}
for (int i = 0; i < WQTEST2_NOP; ++i) {
unsigned r = re();
if (r < re.max() / 512) {
p->yield();
} else if (r < re.max() / 16) {
wq.wake_all();
pause();
} else {
waiter w;
w.prepare(wq);
pause();
++idn;
w.clear();
pause();
}
}
++n;
if (id == 0) {
while (n < WQTEST_NPROC) {
p->yield();
}
n = 0;
phase = 200;
}
while (phase < 200) {
p->yield();
}
// Third test phase: wake up waiters directly (not via wake_all)
if (id == 0) {
start_ticks = ticks;
console_printf("ktestwait phase 3 commencing\n");
unsigned want = WQTEST3_NOP * (WQTEST_NPROC - 1);
wq_reporter wqr("phase 3", want, 250);
unsigned i = 1;
while (n < want) {
wqt_proc[i]->unblock();
if (++i == WQTEST_NPROC) {
i = 1;
p->yield();
} else {
pause();
}
if (re() < re.max() / 512) {
wqr.check(n);
}
}
n = 0;
phase = 300;
} else {
for (int i = 0; i < WQTEST3_NOP; ++i) {
int tried = 0;
waiter w;
// block exactly once
w.block_until(wq, [&] () {
pause();
return tried++ != 0;
});
++n;
}
}
while (phase < 300) {
p->yield();
}
// Fourth test phase: check for lost wakeups
if (id == 0) {
start_ticks = ticks;
console_printf("ktestwait phase 4 commencing\n");
wq_reporter wqr("phase 3", WQTEST4_NOP, 1);
for (unsigned i = 0; i != WQTEST4_NOP; ++i) {
assert(wqdata_flag == false);
spinlock_guard guard(wqdata_lock);
waiter w;
w.block_until(wq2, [] () {
return wqdata_i == WQTEST_NPROC - 1;
}, guard);
wqdata_flag = true;
wq.wake_all();
w.block_until(wq2, [] () {
return wqdata_i == 0;
}, guard);
wqdata_flag = false;
wq.wake_all();
if (i % 32 == 31) {
wqr.check(i);
}
}
n = 0;
phase = 400;
} else {
for (unsigned i = 0; i != WQTEST4_NOP; ++i) {
spinlock_guard guard(wqdata_lock);
if (++wqdata_i == WQTEST_NPROC - 1) {
wq2.wake_all();
}
waiter w;
w.block_until(wq, [] () {
return wqdata_flag == true;
}, guard);
--wqdata_i;
wq2.wake_all();
if (re() < re.max() / 128) {
guard.unlock();
p->yield();
guard.lock();
} else {
pause();
}
w.block_until(wq, [] () {
return wqdata_flag == false;
}, guard);
}
}
while (phase < 400) {
p->yield();
}
// Fifth test phase: attempt to trigger a sleep/wakeup race
if (id == 0) {
start_ticks = ticks;
console_printf("ktestwait phase 5 commencing\n");
wq_reporter wqr("phase 5", WQTEST5_NOP, 200);
unsigned nn = 0;
for (unsigned i = 0; i != WQTEST5_NOP; ++i) {
while (n % 2 == 0) {
pause();
++nn;
if (nn % 1024 == 0) {
wqr.check(i);
} else if (nn % 512 == 0) {
p->yield();
}
}
spinlock_guard guard(wqdata_lock);
wq.wake_all();
++n;
}
} else if (id == 1) {
for (unsigned i = 0; i != WQTEST5_NOP; ++i) {
waiter w;
++n;
spinlock_guard guard(wqdata_lock);
w.block_until(wq, [] () {
return n % 2 == 0;
}, guard);
}
}
// That completes the test
if (id == 0) {
phase = 1000;
console_printf(COLOR_SUCCESS, "ktestwait succeeded!\n");
}
// block forever as if faulted (but do not free memory)
p->pstate_ = proc::ps_faulted;
p->yield();
}
int ktest_wait_queues() {
int start_phase = 0;
if (phase.compare_exchange_strong(start_phase, 1)) {
start_ticks = ticks;
for (int i = 0; i < WQTEST_NPROC; ++i) {
wqt_proc[i] = knew<proc>();
wqt_proc[i]->init_kernel(-1, wq_tester);
if (i == 0 || i == ncpu - 1 || ncpu == 1) {
cpus[0].enqueue(wqt_proc[i]);
} else {
cpus[1 + (i - 1) % (ncpu - 1)].enqueue(wqt_proc[i]);
}
}
}
if (phase == 1000 || long(ticks - start_ticks) <= HZ * 45) {
return phase;
} else {
return -1;
}
}