-
Notifications
You must be signed in to change notification settings - Fork 1
/
twostate.m
444 lines (359 loc) · 10.9 KB
/
twostate.m
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
-- two-state 4-hop VI protocol
----------------------------------------------------------------------
-- Constants
----------------------------------------------------------------------
const
ProcCount: 3; -- number processors
ValueCount: 2; -- number of data values.
VC0: 0; -- low priority
VC1: 1;
QMax: 2;
NumVCs: VC1 - VC0 + 1;
NetMax: ProcCount+1;
----------------------------------------------------------------------
-- Types
----------------------------------------------------------------------
type
Proc: scalarset(ProcCount); -- unordered range of processors
Value: scalarset(ValueCount); -- arbitrary values for tracking coherence
Home: enum { HomeType }; -- need enumeration for IsMember calls
Node: union { Home , Proc };
VCType: VC0..NumVCs-1;
MessageType: enum { ReadReq, -- request for data / exclusivity
ReadAck, -- read ack (w/ data)
WBReq, -- writeback request (w/ data)
WBAck, -- writeback ack
RecallReq -- Request & invalidate a valid copy
};
Message:
Record
mtype: MessageType;
src: Node;
-- do not need a destination for verification; the destination is indicated by which array entry in the Net the message is placed
vc: VCType;
val: Value;
End;
HomeState:
Record
state: enum { H_Valid, H_Invalid, --stable states
HT_Pending }; --transient states during recall
owner: Node;
--sharers: multiset [ProcCount] of Node; --No need for sharers in this protocol, but this is a good way to represent them
val: Value;
End;
ProcState:
Record
state: enum { P_Valid, P_Invalid,
PT_Pending, PT_WritebackPending
};
val: Value;
End;
----------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------
var
HomeNode: HomeState;
Procs: array [Proc] of ProcState;
Net: array [Node] of multiset [NetMax] of Message; -- One multiset for each destination - messages are arbitrarily reordered by the multiset
InBox: array [Node] of array [VCType] of Message; -- If a message is not processed, it is placed in InBox, blocking that virtual channel
msg_processed: boolean;
LastWrite: Value; -- Used to confirm that writes are not lost; this variable would not exist in real hardware
----------------------------------------------------------------------
-- Procedures
----------------------------------------------------------------------
Procedure Send(mtype:MessageType;
dst:Node;
src:Node;
vc:VCType;
val:Value;
);
var msg:Message;
Begin
Assert (MultiSetCount(i:Net[dst], true) < NetMax) "Too many messages";
msg.mtype := mtype;
msg.src := src;
msg.vc := vc;
msg.val := val;
MultiSetAdd(msg, Net[dst]);
End;
Procedure ErrorUnhandledMsg(msg:Message; n:Node);
Begin
error "Unhandled message type!";
End;
Procedure ErrorUnhandledState();
Begin
error "Unhandled state!";
End;
/*
-- These aren't needed for Valid/Invalid protocol, but this is a good way of writing these functions
Procedure AddToSharersList(n:Node);
Begin
if MultiSetCount(i:HomeNode.sharers, HomeNode.sharers[i] = n) = 0
then
MultiSetAdd(n, HomeNode.sharers);
endif;
End;
Function IsSharer(n:Node) : Boolean;
Begin
return MultiSetCount(i:HomeNode.sharers, HomeNode.sharers[i] = n) > 0
End;
Procedure RemoveFromSharersList(n:Node);
Begin
MultiSetRemovePred(i:HomeNode.sharers, HomeNode.sharers[i] = n);
End;
-- Sends a message to all sharers except rqst
Procedure SendInvReqToSharers(rqst:Node);
Begin
for n:Node do
if (IsMember(n, Proc) &
MultiSetCount(i:HomeNode.sharers, HomeNode.sharers[i] = n) != 0)
then
if n != rqst
then
-- Send invalidation message here
endif;
endif;
endfor;
End;
*/
Procedure HomeReceive(msg:Message);
var cnt:0..ProcCount; -- for counting sharers
Begin
-- Debug output may be helpful:
-- put "Receiving "; put msg.mtype; put " on VC"; put msg.vc;
-- put " at home -- "; put HomeNode.state;
-- The line below is not needed in Valid/Invalid protocol. However, the
-- compiler barfs if we put this inside a switch, so it is useful to
-- pre-calculate the sharer count here
--cnt := MultiSetCount(i:HomeNode.sharers, true);
-- default to 'processing' message. set to false otherwise
msg_processed := true;
switch HomeNode.state
case H_Invalid:
switch msg.mtype
case ReadReq:
HomeNode.state := H_Valid;
HomeNode.owner := msg.src;
Send(ReadAck, msg.src, HomeType, VC1, HomeNode.val);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case H_Valid:
Assert (IsUndefined(HomeNode.owner) = false)
"HomeNode has no owner, but line is Valid";
switch msg.mtype
case ReadReq:
HomeNode.state := HT_Pending;
Send(RecallReq, HomeNode.owner, HomeType, VC0, UNDEFINED);
HomeNode.owner := msg.src; --remember who the new owner will be
case WBReq:
assert (msg.src = HomeNode.owner) "Writeback from non-owner";
HomeNode.state := H_Invalid;
HomeNode.val := msg.val;
Send(WBAck, msg.src, HomeType, VC1, UNDEFINED);
undefine HomeNode.owner
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case HT_Pending:
switch msg.mtype
case WBReq:
Assert (!IsUnDefined(HomeNode.owner)) "owner undefined";
HomeNode.state := H_Valid;
HomeNode.val := msg.val;
Send(ReadAck, HomeNode.owner, HomeType, VC1, HomeNode.val);
case ReadReq:
msg_processed := false; -- stall message in InBox
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
endswitch;
End;
Procedure ProcReceive(msg:Message; p:Proc);
Begin
-- put "Receiving "; put msg.mtype; put " on VC"; put msg.vc;
-- put " at proc "; put p; put "\n";
-- default to 'processing' message. set to false otherwise
msg_processed := true;
alias ps:Procs[p].state do
alias pv:Procs[p].val do
switch ps
case P_Valid:
switch msg.mtype
case RecallReq:
Send(WBReq, msg.src, p, VC1, pv);
Undefine pv;
ps := P_Invalid;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_Pending:
switch msg.mtype
case ReadAck:
pv := msg.val;
ps := P_Valid;
case RecallReq:
msg_processed := false; -- stall message in InBox
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_WritebackPending:
switch msg.mtype
case WBAck:
ps := P_Invalid;
undefine pv;
case RecallReq: -- treat a recall request as a Writeback acknowledgement
ps := P_Invalid;
undefine pv;
else
ErrorUnhandledMsg(msg, p);
endswitch;
----------------------------
-- Error catch
----------------------------
else
ErrorUnhandledState();
endswitch;
endalias;
endalias;
End;
----------------------------------------------------------------------
-- Rules
----------------------------------------------------------------------
-- Processor actions (affecting coherency)
ruleset n:Proc Do
alias p:Procs[n] Do
ruleset v:Value Do
rule "store new value"
(p.state = P_Valid)
==>
p.val := v;
LastWrite := v; --We use LastWrite to sanity check that reads receive the value of the last write
endrule;
endruleset;
rule "read request"
p.state = P_Invalid
==>
Send(ReadReq, HomeType, n, VC0, UNDEFINED);
p.state := PT_Pending;
endrule;
rule "writeback"
(p.state = P_Valid)
==>
Send(WBReq, HomeType, n, VC1, p.val);
p.state := PT_WritebackPending;
undefine p.val;
endrule;
endalias;
endruleset;
-- Message delivery rules
ruleset n:Node do
choose midx:Net[n] do
alias chan:Net[n] do
alias msg:chan[midx] do
alias box:InBox[n] do
-- Pick a random message in the network and delivier it
rule "receive-net"
(isundefined(box[msg.vc].mtype))
==>
if IsMember(n, Home)
then
HomeReceive(msg);
else
ProcReceive(msg, n);
endif;
if ! msg_processed
then
-- The node refused the message, stick it in the InBox to block the VC.
box[msg.vc] := msg;
endif;
MultiSetRemove(midx, chan);
endrule;
endalias
endalias;
endalias;
endchoose;
-- Try to deliver a message from a blocked VC; perhaps the node can handle it now
ruleset vc:VCType do
rule "receive-blocked-vc"
(! isundefined(InBox[n][vc].mtype))
==>
if IsMember(n, Home)
then
HomeReceive(InBox[n][vc]);
else
ProcReceive(InBox[n][vc], n);
endif;
if msg_processed
then
-- Message has been handled, forget it
undefine InBox[n][vc];
endif;
endrule;
endruleset;
endruleset;
----------------------------------------------------------------------
-- Startstate
----------------------------------------------------------------------
startstate
For v:Value do
-- home node initialization
HomeNode.state := H_Invalid;
undefine HomeNode.owner;
HomeNode.val := v;
endfor;
LastWrite := HomeNode.val;
-- processor initialization
for i:Proc do
Procs[i].state := P_Invalid;
undefine Procs[i].val;
endfor;
-- network initialization
undefine Net;
endstartstate;
----------------------------------------------------------------------
-- Invariants
----------------------------------------------------------------------
invariant "Invalid implies empty owner"
HomeNode.state = H_Invalid
->
IsUndefined(HomeNode.owner);
invariant "value in memory matches value of last write, when invalid"
HomeNode.state = H_Invalid
->
HomeNode.val = LastWrite;
invariant "values in valid state match last write"
Forall n : Proc Do
Procs[n].state = P_Valid
->
Procs[n].val = LastWrite --LastWrite is updated whenever a new value is created
end;
invariant "value is undefined while invalid"
Forall n : Proc Do
Procs[n].state = P_Invalid
->
IsUndefined(Procs[n].val)
end;
/*
-- Here are some invariants that are helpful for validating shared state.
invariant "modified implies empty sharers list"
HomeNode.state = H_Modified
->
MultiSetCount(i:HomeNode.sharers, true) = 0;
invariant "Invalid implies empty sharer list"
HomeNode.state = H_Invalid
->
MultiSetCount(i:HomeNode.sharers, true) = 0;
invariant "values in memory matches value of last write, when shared or invalid"
Forall n : Proc Do
HomeNode.state = H_Shared | HomeNode.state = H_Invalid
->
HomeNode.val = LastWrite
end;
invariant "values in shared state match memory"
Forall n : Proc Do
HomeNode.state = H_Shared & Procs[n].state = P_Shared
->
HomeNode.val = Procs[n].val
end;
*/