forked from ooibc88/gam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectory.cc
475 lines (438 loc) · 12.5 KB
/
directory.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright (c) 2018 The GAM Authors
#include "structure.h"
#include "directory.h"
DirState Directory::GetState(ptr_t ptr) {
DirState s = DIR_UNSHARED;
if (dir.count(TOBLOCK(ptr))) {
s = dir.at(TOBLOCK(ptr))->state;
}
return s;
}
bool Directory::InTransitionState(ptr_t ptr) {
bool ret = false;
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
if (dir.count(block)) {
DirEntry* entry = dir.at(block);
epicLog(LOG_DEBUG, "found the directory entry for %lx, state = %d", block,
entry->state);
ret = entry->state == DIR_TO_DIRTY || entry->state == DIR_TO_SHARED
|| entry->state == DIR_TO_UNSHARED;
}
return ret;
}
void Directory::ToShared(DirEntry* entry, GAddr addr) {
epicAssert(entry);
if (entry->state == DIR_TO_SHARED) {
epicAssert(entry->shared.size() == 1);
/*
* if addr == Gnullptr, then a writeback request
* else, addr == shared.front();
*/
if (addr != Gnullptr) {
epicAssert(addr == entry->shared.front());
entry->shared.pop_front();
entry->shared.push_back(addr);
}
entry->state = DIR_SHARED;
} else {
epicAssert((entry->state == DIR_SHARED && entry->shared.size() > 0)
|| (entry->state == DIR_UNSHARED && IsBlockLocked(entry)
&& !IsBlockWLocked(entry)));
entry->state = DIR_SHARED;
if (!(entry->shared.size() == 0
|| (entry->shared.front() != addr
&& WID(entry->shared.front()) != WID(addr)))) {
epicLog(LOG_WARNING, "size = %d, front = %lx, current = %lx",
entry->shared.size(), entry->shared.front(), addr);
epicAssert(false);
}
entry->shared.push_back(addr);
}
}
/*
* if curr_state = DIRTY and wid = the owner worker, remote can be null
* otherwise, remote cannot be null
*/
DirEntry* Directory::ToShared(void* ptr, GAddr addr) {
//TODO: add a check on the max shared entries to 128 (i.e., MAX_UNSIGNED_CHAR)
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(block);
if (!entry) {
entry = new DirEntry();
entry->state = DIR_SHARED;
entry->shared.push_back(addr);
entry->addr = block;
dir[block] = entry;
} else {
ToShared(entry, addr);
}
return entry;
}
void Directory::Remove(DirEntry*& entry, int wid) {
if (!entry) {
epicLog(LOG_WARNING, "remove an empty entry");
return;
}
epicAssert(entry->shared.size() >= 1);
bool found = false;
for (auto it = entry->shared.begin(); it != entry->shared.end(); it++) {
if (WID(*it) == wid) {
entry->shared.erase(it);
found = true;
break;
}
}
epicAssert(found);
if (entry->shared.size() == 0 && !IsBlockLocked(entry)) {
int ret = dir.erase(entry->addr);
epicAssert(ret);
delete entry;
entry = nullptr;
}
}
void Directory::Remove(void* ptr, int wid) {
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(block);
epicAssert(entry);
Remove(entry, wid);
}
void Directory::UndoShared(DirEntry* entry) {
epicAssert(entry);
epicAssert(InTransitionState(entry->state));
entry->state = DIR_SHARED;
}
void Directory::UndoShared(void* ptr) {
epicLog(LOG_DEBUG, "UndoShared");
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
try {
DirEntry* entry = dir.at(block);
UndoShared(entry);
} catch (const exception& e) {
epicLog(LOG_FATAL, "Unexpected: cannot find the directory entry");
epicAssert(false);
}
}
void Directory::ToUnShared(DirEntry*& entry) {
if (!entry)
return;
epicLog(LOG_DEBUG, "dir.at(TOBLOCK(ptr)).state = %d", entry->state);
#ifndef SELECTIVE_CACHING
epicAssert(entry->state == DIR_TO_UNSHARED);
#endif
if (IsBlockLocked(entry)) {
entry->state = DIR_UNSHARED;
entry->shared.clear();
epicLog(LOG_DEBUG, "dir is locked, just change it to dir_unshared");
} else {
if (!dir.erase(entry->addr)) {
epicLog(LOG_WARNING, "cannot unshared the directory entry");
}
delete entry;
entry = nullptr;
}
}
void Directory::ToUnShared(void* ptr) {
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
DirEntry* entry = GetEntry(ptr);
if (!entry)
return;
ToUnShared(entry);
}
void Directory::ToDirty(DirEntry* entry, GAddr addr) {
epicAssert(entry);
epicAssert(
entry->state == DIR_UNSHARED || entry->state == DIR_SHARED
|| entry->state == DIR_TO_DIRTY);
entry->state = DIR_DIRTY;
entry->shared.clear();
entry->shared.push_back(addr);
}
DirEntry* Directory::ToDirty(void* ptr, GAddr addr) {
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(ptr);
if (!entry) {
entry = new DirEntry();
entry->state = DIR_DIRTY;
entry->shared.push_back(addr);
entry->addr = block;
dir[block] = entry;
} else {
ToDirty(entry, addr);
}
return entry;
}
void Directory::UndoDirty(DirEntry* entry) {
epicAssert(entry);
epicLog(LOG_DEBUG, "UndoDirty");
epicAssert(InTransitionState(entry->state));
entry->state = DIR_DIRTY;
}
void Directory::UndoDirty(void* ptr) {
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
try {
DirEntry* entry = dir.at(block);
UndoDirty(entry);
} catch (const exception& e) {
epicLog(LOG_FATAL, "Unexpected: cannot find the directory entry");
epicAssert(false);
}
}
void Directory::ToToShared(DirEntry* entry, GAddr addr) {
epicAssert(entry);
epicAssert(entry->state == DIR_DIRTY); //only transmission from dirty to shared needs this intermediate state
epicAssert(entry->shared.size() == 1);
entry->state = DIR_TO_SHARED;
}
/*
* intermediate state
* only happen in transition from dirty to shared
* READ Case 1, Case 3
*/
void Directory::ToToShared(void* ptr, GAddr addr) {
//TODO: add a check on the max shared entries to 128 (i.e., MAX_UNSIGNED_CHAR)
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(block);
ToToShared(entry, addr);
}
void Directory::ToToUnShared(DirEntry* entry) {
epicAssert(entry);
entry->state = DIR_TO_UNSHARED;
epicLog(LOG_DEBUG, "change to dir_to_unshared");
}
/*
* intermediate state
* in transition from dirty/shared to unshared
* WRITE Case 1 (shared to unshared), WRITE Case 2 (dirty to unshared)
*/
void Directory::ToToUnShared(void* ptr) {
ptr_t block = TOBLOCK(ptr);
epicAssert((ptr_t )ptr == block);
DirEntry* entry = GetEntry(block);
ToToUnShared(entry);
}
/*
* intermediate state
* in transition from dirty(ownership transfer) to dirty
* WRITE Case 4 (dirty to dirty)
* NOTE: shared/unshared to dirty (WRITE Case 3) doesn't need this state
* as we cannot update to_dirty to dirty in this cases.
* we use cache check to avoid races
*/
DirEntry* Directory::ToToDirty(void* ptr, GAddr addr) {
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(block);
if (!entry) { //unshared to dirty
entry = new DirEntry();
entry->state = DIR_TO_DIRTY;
entry->addr = block;
dir[block] = entry;
} else {
ToToDirty(entry);
}
return entry;
}
int Directory::RLock(DirEntry* entry, ptr_t ptr) {
epicAssert(entry);
epicAssert(!InTransitionState(entry));
if (IsWLocked(entry, ptr)) {
return -1;
}
if (entry->locks.count(ptr)) {
entry->locks[ptr]++;
} else {
entry->locks[ptr] = 1;
}
//TODO: add max check and handle
epicAssert(entry->locks[ptr] <= MAX_SHARED_LOCK);
epicAssert(IsBlockLocked(entry));
return 0;
}
int Directory::RLock(ptr_t ptr) {
//epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(block);
if (!entry) {
entry = new DirEntry();
entry->state = DIR_UNSHARED; //actually this state only happens when locked
entry->locks[ptr] = 1;
entry->addr = block;
dir[block] = entry;
epicAssert(IsBlockLocked(entry));
return 0;
} else {
return RLock(entry, ptr);
}
}
int Directory::WLock(DirEntry* entry, ptr_t ptr) {
epicAssert(entry);
epicAssert(!InTransitionState(entry));
if (IsWLocked(entry, ptr) || IsRLocked(entry, ptr)) {
return -1;
}
epicAssert(entry->state == DIR_UNSHARED);
entry->locks[ptr] = EXCLUSIVE_LOCK_TAG;
epicAssert(IsBlockWLocked(entry) && IsBlockLocked(entry));
return 0;
}
/*
* we should call ToUnshared before call WLock
*/
int Directory::WLock(ptr_t ptr) {
//epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(block);
if (!entry) {
entry = new DirEntry();
entry->state = DIR_UNSHARED; //actually this state only happens when locked
entry->locks[ptr] = EXCLUSIVE_LOCK_TAG;
entry->addr = block;
dir[block] = entry;
epicAssert(IsBlockWLocked(entry) && IsBlockLocked(entry));
return 0;
} else {
return WLock(entry, ptr);
}
}
bool Directory::IsWLocked(DirEntry* entry, ptr_t ptr) {
if (!entry)
return false;
return entry->state == DIR_UNSHARED && entry->locks.count(ptr)
&& entry->locks.at(ptr) == EXCLUSIVE_LOCK_TAG;
}
bool Directory::IsWLocked(ptr_t ptr) {
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(block);
if (!entry) {
return false;
} else {
return IsWLocked(entry, ptr);
}
}
bool Directory::IsRLocked(DirEntry* entry, ptr_t ptr) {
if (!entry)
return false;
return entry->locks.count(ptr) && entry->locks.at(ptr) > 0
&& entry->locks.at(ptr) != EXCLUSIVE_LOCK_TAG;
}
bool Directory::IsRLocked(ptr_t ptr) {
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(block);
if (!dir.count(block)) {
return false;
} else {
return IsRLocked(entry, ptr);
}
}
bool Directory::IsBlockWLocked(DirEntry* entry) {
if (!entry)
return false;
bool wlocked = false;
for (auto& en : entry->locks) {
if (en.second == EXCLUSIVE_LOCK_TAG) {
wlocked = true;
break;
}
}
return wlocked;
}
bool Directory::IsBlockWLocked(ptr_t block) {
epicAssert(block == TOBLOCK(block));
DirEntry* entry = GetEntry(block);
if (!entry) {
return false;
} else {
return IsBlockWLocked(entry);
}
}
bool Directory::IsBlockLocked(DirEntry* entry) {
if (!entry)
return false;
return entry->locks.size() > 0 ? true : false;
}
bool Directory::IsBlockLocked(ptr_t block) {
epicAssert(block == TOBLOCK(block));
DirEntry* entry = GetEntry(block);
if (!entry) {
return false;
} else {
return IsBlockLocked(entry);
}
}
void Directory::UnLock(DirEntry*& entry, ptr_t ptr) {
epicLog(LOG_DEBUG, "entry->state = %d, entry->locks.size() = %d",
entry->state, entry->locks.size());
epicAssert(entry->state == DIR_UNSHARED
|| ((entry->state == DIR_SHARED || entry->state == DIR_TO_UNSHARED)
&& IsRLocked(ptr)));
epicAssert(entry->locks.count(ptr) && entry->locks.at(ptr) > 0);
entry->locks[ptr] = IsWLocked(entry, ptr) ? 0 : entry->locks[ptr] - 1;
if (entry->locks[ptr] == 0) {
entry->locks.erase(ptr);
}
if (entry->state == DIR_UNSHARED && entry->locks.size() == 0) {
dir.erase(entry->addr);
delete entry;
entry = nullptr;
}
}
void Directory::UnLock(ptr_t ptr) {
ptr_t block = TOBLOCK(ptr);
try {
DirEntry* entry = dir.at(block);
UnLock(entry, ptr);
} catch (const exception& e) {
epicLog(LOG_FATAL, "Unexpected: cannot find the directory entry");
epicAssert(false);
}
}
void Directory::Clear(DirEntry*& entry, GAddr addr) {
if (!entry) {
epicLog(LOG_WARNING,
"there must be a race before, and it has already been invalidated (%lx)", addr);
return;
}
if (entry->state == DIR_DIRTY) {
epicAssert(entry->shared.size() == 1);
if (addr == entry->shared.front()) {
entry->shared.pop_front();
} else {
epicLog(LOG_WARNING,
"there must be a race before, and new owner change from %d to %d",
WID(addr), WID(entry->shared.front()));
}
} else {
epicAssert((entry->state == DIR_SHARED && entry->shared.size() > 0)
|| (entry->state == DIR_UNSHARED && IsBlockLocked(entry)));
entry->shared.remove(addr);
}
if (entry->shared.size() == 0) {
if (!IsBlockLocked(entry)) {
dir.erase(entry->addr);
delete entry;
entry = nullptr;
} else {
entry->state = DIR_UNSHARED;
}
}
}
void Directory::Clear(ptr_t ptr, GAddr addr) {
epicAssert((ptr_t)ptr == TOBLOCK(ptr));
ptr_t block = TOBLOCK(ptr);
DirEntry* entry = GetEntry(block);
epicAssert(!InTransitionState(entry));
if (entry) {
Clear(entry, addr);
} else {
epicLog(LOG_WARNING,
"there must be a race before, and it has already been invalidated (%lx:%lx)",
ptr, addr);
}
}