-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.go
157 lines (138 loc) · 2.77 KB
/
runner.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
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
package gosim
import (
"fmt"
_ "log"
"math"
"math/rand"
)
type lpEntry struct {
createTick int64
runTick int64
lp Lp
msgs []interface{}
}
func (lpe *lpEntry) reset() {
lpe.lp = nil
lpe.msgs = nil
}
type simMsg struct {
dst Lpid
msg interface{}
}
// Runner runs LPs.
type Runner struct {
id int
nextTick int64
lpCnt int
lps []lpEntry
aggs []Agg
msgs [][]simMsg
ch chan int64
r *rand.Rand
}
func (r *Runner) init(id, nr int) {
r.id = id
r.msgs = make([][]simMsg, nr)
r.ch = make(chan int64)
r.r = rand.New(rand.NewSource(int64(id)))
}
func (r *Runner) numLps() int {
return r.lpCnt
}
func (r *Runner) Rand() *rand.Rand {
return r.r
}
func (r *Runner) AddLp(s *Sim, lp Lp) Lpid {
id := len(r.lps)
if r.lpCnt == id {
r.lps = append(r.lps, lpEntry{})
} else {
for i, _ := range r.lps {
if r.lps[i].lp == nil {
id = i
break
}
}
}
r.lpCnt += 1
lpe := &r.lps[id]
lpe.createTick = s.CurrentTick()
lpe.runTick = lpe.createTick + 1
lpe.lp = lp
lpe.msgs = nil
return Lpid{r.id, id, lpe.createTick}
}
func (r *Runner) removeLp(idx int) {
r.lps[idx].runTick = math.MaxInt64
r.lps[idx].lp = nil
r.lps[idx].msgs = nil
r.lpCnt--
}
func (r *Runner) RemoveLp(lpid Lpid) {
r.removeLp(lpid.Id)
}
func (r *Runner) getLp(lpid Lpid) (Lp, error) {
if r.id != lpid.RunnerId {
return nil, fmt.Errorf("Mismatching runner id.")
}
if lpid.Id < 0 || lpid.Id >= len(r.lps) {
return nil, fmt.Errorf("Lpid.Id out of range.")
}
lpe := &r.lps[lpid.Id]
if lpid.CreateTick != lpe.createTick {
return nil, fmt.Errorf("Mismatching create tick.")
}
return lpe.lp, nil
}
func (r *Runner) UpdateAgg(aggId int, ival int64, fval float64) {
r.aggs[aggId].Update(ival, fval)
}
func (r *Runner) runLoop(s *Sim) {
for {
tick := <-r.ch
if tick < 0 {
s.ch <- tick
return
}
hasMsg := false
// First deliever message to each lp
for i, _ := range r.msgs {
for _, msg := range r.msgs[i] {
idx := msg.dst.Id
lpe := &r.lps[idx]
if lpe.lp != nil && lpe.createTick <= msg.dst.CreateTick {
lpe.msgs = append(lpe.msgs, msg.msg)
hasMsg = true
}
}
r.msgs[i] = nil
}
if !hasMsg && r.nextTick > tick {
// Nothing to run -- just skip to next loop
s.ch <- r.nextTick
} else {
// Next run LPs
r.nextTick = math.MaxInt64
for i, _ := range r.lps {
lpe := &r.lps[i]
if lpe.lp != nil {
if len(lpe.msgs) > 0 || lpe.runTick <= tick {
inc := lpe.lp.Run(s, r, lpe.msgs)
if inc == -1 {
r.removeLp(i)
} else if inc > 0 {
lpe.runTick = tick + inc
} else {
lpe.runTick = math.MaxInt64
}
}
if r.nextTick > lpe.runTick {
r.nextTick = lpe.runTick
}
}
lpe.msgs = nil
}
s.ch <- r.nextTick
}
}
}