-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstaterecorder.go
90 lines (79 loc) · 2.14 KB
/
staterecorder.go
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
package gmars
type CoreState uint8
const (
CoreEmpty CoreState = iota
CoreExecuted
CoreWritten
CoreIncremented
CoreDecremented
CoreRead
CoreTerminated
)
// StateRecorder implements a Reporter which records the most recent operation
// performed each core address and the warrior index associated. The initial
// state of each address is CoreEmpty with a warrior index of -1.
type StateRecorder struct {
sim ReportingSimulator
coresize Address
color []int
state []CoreState
recordReads bool
}
func NewStateRecorder(sim ReportingSimulator) *StateRecorder {
coresize := sim.CoreSize()
color := make([]int, coresize)
for i := Address(0); i < coresize; i++ {
color[i] = -1
}
state := make([]CoreState, coresize)
return &StateRecorder{
sim: sim,
coresize: coresize,
color: color,
state: state,
}
}
func (r *StateRecorder) GetMemState(a Address) (CoreState, int) {
return r.state[a], r.color[a]
}
func (r *StateRecorder) SetRecordRead(val bool) {
r.recordReads = val
}
func (r *StateRecorder) reset() {
for i := Address(0); i < r.coresize; i++ {
r.state[i] = CoreEmpty
r.color[i] = -1
}
}
func (r *StateRecorder) Report(report Report) {
switch report.Type {
case SimReset:
r.reset()
case WarriorSpawn:
w := r.sim.GetWarrior(report.WarriorIndex)
for i := report.Address; i < report.Address+Address(w.Length()); i++ {
r.color[i%r.coresize] = report.WarriorIndex
r.state[i%r.coresize] = CoreWritten
}
case WarriorTaskTerminate:
r.color[report.Address] = report.WarriorIndex
r.state[report.Address] = CoreTerminated
case WarriorTaskPop:
r.color[report.Address] = report.WarriorIndex
r.state[report.Address] = CoreExecuted
case WarriorWrite:
r.color[report.Address] = report.WarriorIndex
r.state[report.Address] = CoreWritten
case WarriorRead:
if r.recordReads {
r.color[report.Address] = report.WarriorIndex
r.state[report.Address] = CoreRead
}
case WarriorIncrement:
r.color[report.Address] = report.WarriorIndex
r.state[report.Address] = CoreIncremented
case WarriorDecrement:
r.color[report.Address] = report.WarriorIndex
r.state[report.Address] = CoreDecremented
}
}