-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrcudeadlock.go
313 lines (282 loc) · 7.13 KB
/
rcudeadlock.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"sync"
"time"
"github.com/containerd/cgroups/v3"
"github.com/containerd/cgroups/v3/cgroup1"
"github.com/containerd/cgroups/v3/cgroup2"
"github.com/iceber/iouring-go"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"
"golang.org/x/sys/unix"
)
func main() {
if err := Entrypoint().Run(os.Args); err != nil {
panic(err)
}
}
func Entrypoint() *cli.App {
return &cli.App{
Commands: []cli.Command{
RCUDeadlockCommand(),
RunTask(),
RunZombie(),
RunDone(),
RunStart(),
},
Action: func(cliCtx *cli.Context) error {
for {
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
cgroupPath := fmt.Sprintf("/rcu-deadlock-issue-%v", i)
go func() {
defer wg.Done()
// start to re-exec
exe, err := os.Executable()
if err != nil {
panic(fmt.Errorf("failed to get executable path: %w", err))
}
// NOTE: disable cpu limit
cmd := exec.Command(exe, "rcu-deadlock", "--cpu_quota_us", "-1", "--cpu_period_us", "100000", cgroupPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
panic(fmt.Errorf("failed to run rcu-deadlock: %w", err))
}
}()
}
wg.Wait()
time.Sleep(1 * time.Second)
}
return nil
},
}
}
func RCUDeadlockCommand() cli.Command {
return cli.Command{
Name: "rcu-deadlock",
Flags: []cli.Flag{
cli.Int64Flag{
Name: "cpu_quota_us",
},
cli.Uint64Flag{
Name: "cpu_period_us",
},
},
Action: func(cliCtx *cli.Context) error {
cgroupPath := cliCtx.Args().Get(0)
if cgroupPath == "" {
return fmt.Errorf("required cgroupPath as first arg")
}
quota := cliCtx.Int64("cpu_quota_us")
period := cliCtx.Uint64("cpu_period_us")
if quota != -1 {
switch mode := cgroups.Mode(); mode {
case cgroups.Unified:
mgr, err := cgroup2.NewManager("/sys/fs/cgroup", cgroupPath, &cgroup2.Resources{
CPU: &cgroup2.CPU{
Max: cgroup2.NewCPUMax("a, &period),
},
})
if err != nil {
return fmt.Errorf("failed to create cgroupv2 manager: %w", err)
}
if err := mgr.AddProc(uint64(os.Getpid())); err != nil {
return fmt.Errorf("failed to move to cgroup %s: %w", cgroupPath, err)
}
case cgroups.Legacy:
mgr, err := cgroup1.Load(cgroup1.RootPath)
if err != nil {
return fmt.Errorf("failed to load cgroupv1: %w", err)
}
mgr, err = mgr.New(cgroupPath, &specs.LinuxResources{
CPU: &specs.LinuxCPU{
Quota: "a,
Period: &period,
},
})
if err != nil {
return fmt.Errorf("failed to update cpu quota/period: %w", err)
}
if err := mgr.AddProc(uint64(os.Getpid())); err != nil {
return fmt.Errorf("failed to move to cgroup %s: %w", cgroupPath, err)
}
default:
return fmt.Errorf("only support cgroupv1 and cgroupv2, excluding hybrid mode: %v", mode)
}
}
// NOTE: Set it self as subreaper so that it can be the
// parent of child double-forked by unshare command.
if err := setSubreaper(1); err != nil {
return fmt.Errorf("failed to set sub reaper: %w", err)
}
// start to re-exec
exe, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get executable path: %w", err)
}
cmd := exec.Command(exe, "unshareworkload")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to run workload in pid/mnt namespace: %w", err)
}
var (
ws unix.WaitStatus
rus unix.Rusage
)
pid, err := unix.Wait4(-1, &ws, 0, &rus)
if err != nil {
return fmt.Errorf("failed to wait workload: %w", err)
}
fmt.Println(pid, " Exit")
return nil
},
}
}
func RunTask() cli.Command {
return cli.Command{
Name: "task",
Action: func(cliCtx *cli.Context) error {
// start to re-exec
exe, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get executable path: %w", err)
}
cmd := exec.Command("bash", "-c", fmt.Sprintf("%s zombie & %s done", exe, exe))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to run task: %w", err)
}
return nil
},
}
}
func RunZombie() cli.Command {
return cli.Command{
Name: "zombie",
Action: func(cliCtx *cli.Context) error {
cmd := exec.Command("bash", "-c", "while true; do echo zombie; sleep 1; done")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to run zombie: %w", err)
}
return nil
},
}
}
func RunDone() cli.Command {
return cli.Command{
Name: "done",
Action: func(cliCtx *cli.Context) error {
cmd := exec.Command("echo", "done")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to run done: %w", err)
}
return nil
},
}
}
func RunStart() cli.Command {
return cli.Command{
Name: "start",
Action: func(cliCtx *cli.Context) error {
for i := 0; i < 6; i++ {
if err := createIOUringThread(); err != nil {
return err
}
}
for i := 0; i < 10; i++ {
fmt.Println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
}
return nil
},
}
}
func init() {
runtime.LockOSThread()
if len(os.Args) == 2 && os.Args[1] == "unshareworkload" {
err := unix.Unshare(unix.CLONE_NEWNS | unix.CLONE_NEWPID)
if err != nil {
panic(err)
}
// start to re-exec
exe, err := os.Executable()
if err != nil {
panic(fmt.Errorf("failed to get executable path: %w", err))
}
cmd := exec.Command("bash", "-c", fmt.Sprintf("%s task && %s start", exe, exe))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
panic(err)
}
os.Exit(0)
}
}
// setSubreaper runs as sub-reaper
func setSubreaper(i int) error {
return unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
}
// createIOUringThread creates one busy iou-wrk-thread.
func createIOUringThread() error {
iour, err := iouring.New(64)
if err != nil {
return fmt.Errorf("failed to create iouring: %w", err)
}
curDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working dir")
}
curDirFile, err := os.Open(curDir)
if err != nil {
return fmt.Errorf("failed to open current working dir")
}
defer curDirFile.Close()
waitCh := make(chan struct{}, 0)
go func() error {
i := 0
for {
if i <= 2 {
i++
} else {
select {
case <-waitCh:
default:
close(waitCh)
}
}
var stat unix.Statx_t
req, err := iouring.Statx(int(curDirFile.Fd()), "./", 0, 0, &stat)
if err != nil {
return fmt.Errorf("failed to prepare statx iouring request: %w", err)
}
result, err := iour.SubmitRequest(
req,
make(chan iouring.Result, 1),
)
if err != nil {
return fmt.Errorf("failed to submit statx request: %w", err)
}
<-result.Done()
if err := result.Err(); err != nil {
return fmt.Errorf("failed to run statx: %w", err)
}
if i <= 2 {
fmt.Printf("DevMajor: %v, DevMinor: %v\n", stat.Dev_major, stat.Dev_minor)
}
}
}()
<-waitCh
return nil
}