-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspinlock.cc
408 lines (355 loc) · 7.34 KB
/
spinlock.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Mutual exclusion spin locks.
#include "types.h"
#include "kernel.hh"
#include "amd64.h"
#include "cpu.hh"
#include "bits.hh"
#include "spinlock.hh"
#include "condvar.hh"
#include "fs.h"
#include "file.hh"
#include "major.h"
#if LOCKSTAT
// The klockstat structure pointed to by spinlocks that want lockstat,
// but have never been acquired.
struct klockstat klockstat_lazy("<lazy>");
static int lockstat_enable;
void lockstat_init(struct spinlock *lk, bool lazy);
static inline struct cpulockstat *
mylockstat(struct spinlock *lk)
{
return &lk->stat->s.cpu[mycpu()->id];
}
void*
klockstat::operator new(unsigned long nbytes)
{
assert(nbytes == sizeof(klockstat));
void* p = kmalloc(sizeof(klockstat), "klockstat");
if (!p)
throw_bad_alloc();
return p;
}
void
klockstat::operator delete(void *p)
{
kmfree(p, sizeof(klockstat));
}
#endif
static inline void
locking(struct spinlock *lk)
{
#if SPINLOCK_DEBUG
if(holding(lk)) {
cprintf("%p\n", __builtin_return_address(0));
panic("acquire");
}
#endif
#if LOCKSTAT
if (lockstat_enable && lk->stat != nullptr) {
if (lk->stat == &klockstat_lazy)
lockstat_init(lk, true);
mylockstat(lk)->locking_ts = rdtsc();
}
#endif
}
static inline void
locked(struct spinlock *lk, u64 retries)
{
#if SPINLOCK_DEBUG
// Record info about lock acquisition for debugging.
lk->cpu = mycpu();
getcallerpcs(&lk, lk->pcs, NELEM(lk->pcs));
#endif
#if LOCKSTAT
if (lockstat_enable && lk->stat != nullptr) {
struct cpulockstat *s = mylockstat(lk);
if (retries > 0)
s->contends++;
s->acquires++;
s->locked_ts = rdtsc();
}
#endif
}
static inline void
releasing(struct spinlock *lk)
{
#if SPINLOCK_DEBUG
if(!holding(lk)) {
cprintf("lock: %s\n", lk->name);
panic("release");
}
#endif
#if SPINLOCK_DEBUG
lk->pcs[0] = 0;
lk->cpu = 0;
#endif
#if LOCKSTAT
if (lockstat_enable && lk->stat != nullptr) {
struct cpulockstat *s = mylockstat(lk);
u64 ts = rdtsc();
s->locking += ts - s->locking_ts;
s->locked += ts - s->locked_ts;
}
#endif
}
// Check whether this cpu is holding the lock.
#if SPINLOCK_DEBUG
bool
spinlock::holding()
{
return locked && cpu == mycpu();
}
#endif
#if LOCKSTAT
ilist<klockstat,&klockstat::link> lockstat_list;
//LIST_HEAD(lockstat_list, klockstat);
//static struct lockstat_list lockstat_list = { (struct klockstat*) nullptr };
static struct spinlock lockstat_lock("lockstat");
klockstat::klockstat(const char *name) :
rcu_freed("klockstat", this, sizeof(*this))
{
magic = LOCKSTAT_MAGIC;
memset(&s, 0, sizeof(s));
safestrcpy(s.name, name, sizeof(s.name));
};
void
lockstat_init(struct spinlock *lk, bool lazy)
{
klockstat *ls = new klockstat(lk->name);
if (!ls)
return;
if (lazy) {
if (!__sync_bool_compare_and_swap(&lk->stat, &klockstat_lazy, ls)) {
delete ls;
return;
}
} else {
lk->stat = ls;
}
acquire(&lockstat_lock);
lockstat_list.push_front(lk->stat);
//LIST_INSERT_HEAD(&lockstat_list, lk->stat, link);
release(&lockstat_lock);
}
static void
lockstat_stop(struct spinlock *lk)
{
if (lk->stat != nullptr) {
lk->stat->magic = 0;
lk->stat = nullptr;
}
}
void
lockstat_clear(void)
{
// struct klockstat *stat, *tmp;
acquire(&lockstat_lock);
//LIST_FOREACH_SAFE(stat, &lockstat_list, link, tmp) {
for (auto it = lockstat_list.begin(); it != lockstat_list.end(); it++) {
klockstat *stat = &(*it);
if (stat->magic == 0) {
lockstat_list.erase(it);
// LIST_REMOVE(stat, link);
// So verifyfree doesn't follow le_next
// stat->link.le_next = 0;
gc_delayed(stat);
} else {
memset(&stat->s.cpu, 0, sizeof(stat->s.cpu));
}
}
release(&lockstat_lock);
}
static int
lockstat_read(char *dst, u32 off, u32 n)
{
static const u64 sz = sizeof(struct lockstat);
static struct {
struct klockstat *stat;
u32 off;
} cache;
u32 cur;
iiterator<klockstat,&klockstat::link> it;
struct klockstat *stat;
if (off % sz || n < sz)
return -1;
acquire(&lockstat_lock);
if (cache.off == off && cache.stat != nullptr) {
cur = cache.off;
stat = cache.stat;
it = lockstat_list.iterator_to(stat);
} else {
cur = 0;
it = lockstat_list.begin();
// stat = LIST_FIRST(&lockstat_list);
// stat = lockstat_list.front();
}
stat = &(*it);
// for (; stat != nullptr; stat = LIST_NEXT(stat, link)) {
for (; it != lockstat_list.end(); it++) {
stat = &(*it);
struct lockstat *ls = &stat->s;
if (n < sizeof(*ls))
break;
if (cur >= off) {
memmove(dst, ls, sz);
dst += sz;
n -= sz;
}
cur += sz;
}
release(&lockstat_lock);
if (cur < off) {
cache.off = 0;
cache.stat = (struct klockstat*) nullptr;
return 0;
}
cache.off = cur;
cache.stat = stat;
return cur - off;
}
static int
lockstat_write(const char *buf, u32 n)
{
int cmd = buf[0] - '0';
switch(cmd) {
case LOCKSTAT_START:
lockstat_enable = 1;
break;
case LOCKSTAT_STOP:
lockstat_enable = 0;
break;
case LOCKSTAT_CLEAR:
lockstat_clear();
break;
default:
return -1;
}
return n;
}
void
initlockstat(void)
{
devsw[MAJ_LOCKSTAT].write = lockstat_write;
devsw[MAJ_LOCKSTAT].pread = lockstat_read;
}
#else
void
initlockstat(void)
{
}
#endif
spinlock::spinlock(spinlock &&o)
#if USE_CODEX_IMPL
: locked(o.locked)
#else
: locked(o.locked.load())
#endif
#if SPINLOCK_DEBUG
, name(o.name)
, cpu(o.cpu)
#endif
#if LOCKSTAT
, stat(o.stat)
#endif
{
#if SPINLOCK_DEBUG
memcpy(&pcs, &o.pcs, sizeof(pcs));
#endif
#if LOCKSTAT
lockstat_stop(&o);
#endif
}
spinlock &
spinlock::operator=(spinlock &&o)
{
#if LOCKSTAT
lockstat_stop(this);
#endif
#if USE_CODEX_IMPL
locked = o.locked;
#else
locked = o.locked.load();
#endif
#if SPINLOCK_DEBUG
name = o.name;
cpu = o.cpu;
memcpy(&pcs, &o.pcs, sizeof(pcs));
#endif
#if LOCKSTAT
stat = o.stat;
o.stat = nullptr;
#endif
return *this;
}
#if LOCKSTAT
// Conflicts with constexpr
// spinlock::~spinlock()
// {
// lockstat_stop(this);
// }
#endif
#if USE_CODEX_IMPL
// note: the codex implemention doesn't actually enforce mutual exclusion, but
// that's by design
bool
spinlock::try_acquire()
{
// XXX(stephentu): we'll need a new codex primitive to support this
return false;
}
void
spinlock::acquire()
{
pushcli();
codex::atomic_section a(!locked);
locked++; // must come *before* reporting to codex
codex_magic_action_run_acquire((intptr_t) &locked, false);
codex_magic_action_run_acquire((intptr_t) &locked, true);
}
void
spinlock::release()
{
assert(locked);
locked--; // must come *before* reporting to codex
codex_magic_action_run_release((intptr_t) &locked);
popcli();
}
#else
bool
spinlock::try_acquire()
{
pushcli();
locking(this);
if (locked.exchange(1, std::memory_order_acquire) != 0) {
popcli();
return false;
}
::locked(this, 0);
return true;
}
// Acquire the lock.
// Loops (spins) until the lock is acquired.
// Holding a lock for a long time may cause
// other CPUs to waste time spinning to acquire it.
void
spinlock::acquire()
{
u64 retries;
pushcli();
locking(this);
retries = 0;
while (locked.exchange(1, std::memory_order_acquire) != 0) {
retries++;
nop_pause();
}
::locked(this, retries);
}
// Release the lock.
void
spinlock::release()
{
releasing(this);
locked.store(0, std::memory_order_release);
popcli();
}
#endif