-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
293 lines (237 loc) · 4.59 KB
/
main.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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"sync"
"github.com/spf13/cobra"
)
func probeMessagePassing() {
R1 := make(chan int, 1)
R2 := make(chan int, 1)
var x, y int
f1 := func() {
x = 1
y = 1
}
f2 := func() {
r1 := y
r2 := x
R1 <- r1
R2 <- r2
}
run(f1, f2)
if <-R1 == 1 && <-R2 == 0 {
log.Printf("message passing detected!")
}
}
func probeBufferedWrites() {
R1 := make(chan int, 1)
R2 := make(chan int, 1)
var x, y int
f1 := func() {
x = 1
r1 := y
R1 <- r1
}
f2 := func() {
y = 1
r2 := x
R2 <- r2
}
run(f1, f2)
if <-R1 == 0 && <-R2 == 0 {
log.Printf("write buffering detected!")
}
}
func probeIRIW() {
R1 := make(chan int, 1)
R2 := make(chan int, 1)
R3 := make(chan int, 1)
R4 := make(chan int, 1)
var x, y int
f1 := func() {
x = 1
}
f2 := func() {
y = 1
}
f3 := func() {
r1 := x
r2 := y
R1 <- r1
R2 <- r2
}
f4 := func() {
r3 := y
r4 := x
R3 <- r3
R4 <- r4
}
run(f1, f2, f3, f4)
if <-R1 == 1 && <-R2 == 0 && <-R3 == 1 && <-R4 == 0 {
log.Printf("iriw: detected!")
}
}
func probeN6() {
R1 := make(chan int, 1)
R2 := make(chan int, 1)
var x, y int
f1 := func() {
x = 1
r1 := x
r2 := y
R1 <- r1
R2 <- r2
}
f2 := func() {
y = 1
x = 2
}
run(f1, f2)
if <-R1 == 1 && <-R2 == 0 && x == 1 {
log.Printf("n6: detected!")
}
}
func probeReadBuffering() {
R1 := make(chan int, 1)
R2 := make(chan int, 1)
var x, y int
f1 := func() {
r1 := x
y = 1
R1 <- r1
}
f2 := func() {
r2 := y
x = 1
R2 <- r2
}
run(f1, f2)
if <-R1 == 1 && <-R2 == 1 {
log.Printf("read buffering detected!")
}
}
// run ensures that the provided functions are run in a synchronized way.
func run(fs ...func()) {
// shuffle the order we run the goroutines
perm := rand.Perm(len(fs))
// setup wait groups to ensure all go routines are started before
// running funcs and all funcs finish before checking results.
var wgStart, wgDone sync.WaitGroup
wgStart.Add(len(fs))
wgDone.Add(len(fs))
// create and wait for all goroutines to start
for _, i := range perm {
go func(f func()) {
wgStart.Done()
wgStart.Wait()
f()
wgDone.Done()
}(fs[i])
}
wgDone.Wait()
}
func main() {
rootCmd := &cobra.Command{
Use: "probe-memory-model",
Short: "A tool for running various memory model probes in Go.",
Long: `This is a tool which implements a few of the memory model probes
discussed in Russ Cox's "Hardware Memory Models" blog post.
Note that variables are initialized to zero in all probes.
`,
}
rootCmd.AddCommand(&cobra.Command{
Use: "mp",
Short: "Probe for message passing.",
Long: `This probe runs the following test to determine whether message passing is happening:
Proc 1 Proc 2
x = 1 r1 = y
y = 1 r2 = x
Can we see?
r1 = 1
r2 = 0
`,
Run: func(cmd *cobra.Command, args []string) {
for {
probeMessagePassing()
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "bw",
Short: "Probe for buffered writes.",
Long: `This probe runs the following test to determine whether writes are buffered in a queue:
Proc 1 Proc 2
x = 1 y = 1
r1 = y r2 = x
Can we see?
r1 = 0
r2 = 0
`,
Run: func(cmd *cobra.Command, args []string) {
for {
probeBufferedWrites()
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "iriw",
Short: "Probe for independent reads of independent writes.",
Long: `This probe runs the following test to determine whether independent reads occur for independent writes:
Proc 1 Proc 2 Proc 3 Proc 3
x = 1 y = 1 r1 = x r3 = y
r2 = y r4 = x
Can we see?
r1 = 1
r2 = 0
r3 = 1
r4 = 0
`,
Run: func(cmd *cobra.Command, args []string) {
for {
probeIRIW()
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "n6",
Short: "Probe by Paul Loewenstein to show x86 violates TLO+CC memory model.",
Long: `This probe runs the following test to determine whether memory write queues:
Proc 1 Proc 2
x = 1 y = 1
r1 = x x = 2
r2 = y
Can we see?
r1 = 1
r2 = 0
x = 1
`,
Run: func(cmd *cobra.Command, args []string) {
for {
probeN6()
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "rb",
Short: "Probe for read buffering.",
Long: `This probe runs the following test to determine whether read buffering can happen:
Proc 1 Proc 2
r1 = x r2 = y
y = 1 x = 1
Can we see?
r1 = 1
r2 = 1
`,
Run: func(cmd *cobra.Command, args []string) {
for {
probeReadBuffering()
}
},
})
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}