/
tests.go
294 lines (241 loc) · 4.1 KB
/
tests.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
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
package main
import "fmt"
import "math/rand"
import "sync"
import "runtime"
type packet struct {
ipaddr int
payload int
}
func wtf(tf *uint64) {
fmt.Printf("WTF!")
for {
}
}
func main() {
runtime.Install_traphandler(wtf)
for {
fmt.Printf("hi ")
for i := 0; i < 1000000; i++ {
}
}
}
func mainip() {
fmt.Printf("'network' test ")
ch := make(chan packet)
go genpackets(ch)
process_packets(ch)
}
func genpackets(ch chan packet) {
for {
n := packet{rand.Intn(1000), 0}
ch <- n
}
}
func spawnsend(p packet, outbound map[int]chan packet, f func(chan packet)) {
pri := p_priority(p)
if ch, ok := outbound[pri]; ok {
ch <- p
} else {
pch := make(chan packet)
outbound[pri] = pch
go f(pch)
pch <- p
}
}
func p_priority(p packet) int {
return p.ipaddr / 100
}
func process_packets(in chan packet) {
outbound := make(map[int]chan packet)
for {
p := <-in
spawnsend(p, outbound, ip_process)
}
}
func ip_process(ipchan chan packet) {
for {
p := <-ipchan
if rand.Intn(1000) == 1 {
fmt.Printf("%v ", p_priority(p))
}
}
}
func parcopy(to []byte, from []byte, done chan int) {
for i, c := range from {
to[i] = c
}
done <- 1
}
func sys_write(to []byte, p []byte) int {
PGSIZE := 4096
done := make(chan int)
cnt := len(p)
fmt.Printf("len is %v\n", cnt)
for i := 0; i < cnt/PGSIZE; i++ {
s := i * PGSIZE
e := (i + 1) * PGSIZE
if cnt < e {
e = cnt
}
go parcopy(to[s:e], p[s:e], done)
}
left := cnt % PGSIZE
if left != 0 {
t := cnt - left
for i := t; i < cnt; i++ {
to[i] = p[i]
}
}
for i := 0; i < cnt/PGSIZE; i++ {
<-done
}
return cnt
}
func ver(a []byte, b []byte) {
for i, c := range b {
if a[i] != c {
panic("bad")
}
}
}
func main_write() {
to := make([]byte, 4096*1)
from := make([]byte, 4096*1)
for i, _ := range from {
from[i] = byte(rand.Int())
}
sys_write(to, from)
ver(to, from)
fmt.Printf("done\n")
}
type bnode struct {
fd int
data int
l *bnode
r *bnode
}
func binsert(root *bnode, nfd int, ndata int) {
if root == nil {
panic("nil root")
}
if nfd < root.fd {
if root.l == nil {
root.l = &bnode{nfd, ndata, nil, nil}
return
}
binsert(root.l, nfd, ndata)
} else if nfd > root.fd {
if root.r == nil {
root.r = &bnode{nfd, ndata, nil, nil}
return
}
binsert(root.r, nfd, ndata)
}
}
func blookup(root *bnode, fd int) *bnode {
if root == nil {
return nil
} else if fd < root.fd {
return blookup(root.l, fd)
} else if fd > root.fd {
return blookup(root.r, fd)
}
return root
}
func bprint(root *bnode) {
if root == nil {
return
}
bprint(root.l)
fmt.Printf("%v ", root.fd)
bprint(root.r)
}
func bcount(root *bnode) int {
if root == nil {
return 0
}
return 1 + bcount(root.l) + bcount(root.r)
}
func bcopy(root *bnode, par *bnode) *bnode {
if root == nil {
return nil
}
ret := bnode{root.fd, root.data, nil, nil}
if par != nil {
if par.fd > ret.fd {
par.l = &ret
} else {
par.r = &ret
}
}
bcopy(root.l, &ret)
bcopy(root.r, &ret)
return &ret
}
func reader(cnt int, ch chan int) {
t := 0
oldc := cnt
for {
nc := bcount(&root)
if nc < oldc {
panic("bad count")
}
if nc != oldc {
oldc = nc
fmt.Printf("%v ", oldc)
}
t++
if t%100000 == 0 {
ch <- 1
}
}
}
func rup() {
wlock.Lock()
defer wlock.Unlock()
i := rand.Intn(100)
for ; blookup(&root, i) != nil; i = rand.Intn(100) {
}
newroot := bcopy(&root, nil)
binsert(newroot, i, 0)
fmt.Printf("(inserted %v) ", i)
root = *newroot
}
type addr int
type route int
var route_table *map[addr]*route
func route_get(dst addr) *route {
if r, ok := (*route_table)[dst]; ok {
return r
}
return nil
}
func route_insert(r *route, dst addr) {
rtlock.Lock()
defer rtlock.Unlock()
newrt := copy_table()
(*newrt)[dst] = r
route_table = newrt
}
func copy_table() *map[addr]*route {
ret := *route_table
return &ret
}
var rtlock sync.Mutex
var wlock sync.Mutex
var root bnode
func main_rcu() {
root.fd = 50
for i := 0; i < 9; i++ {
binsert(&root, rand.Intn(100), 0)
}
cnt := bcount(&root)
ch := make(chan int)
go reader(cnt, ch)
for {
<-ch
rup()
}
fmt.Printf("done")
}