summaryrefslogtreecommitdiff
path: root/integrationtests/cmd/ioworkload/scenario_dir.go
blob: 7a787165df194bb11865d5716ab3eb90d0f57546 (plain)
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
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"syscall"
	"time"
	"unsafe"
)

// dirBasic creates a directory via raw SYS_MKDIR, checks access, then removes it
// via raw SYS_RMDIR. We use raw syscalls because Go's syscall.Mkdir wraps mkdirat
// and syscall.Rmdir wraps unlinkat on amd64.
func dirBasic() error {
	dir, cleanup, err := makeTempDir("dir-basic")
	if err != nil {
		return err
	}
	defer cleanup()

	subDir := filepath.Join(dir, "subdir")
	pathBytes, err := syscall.BytePtrFromString(subDir)
	if err != nil {
		return fmt.Errorf("path bytes: %w", err)
	}
	_, _, errno := syscall.Syscall(syscall.SYS_MKDIR, uintptr(unsafe.Pointer(pathBytes)), 0o755, 0)
	runtime.KeepAlive(pathBytes)
	if errno != 0 {
		return fmt.Errorf("mkdir: %w", errno)
	}

	if err := syscall.Access(subDir, syscall.F_OK); err != nil {
		return fmt.Errorf("access: %w", err)
	}

	pathBytes2, err := syscall.BytePtrFromString(subDir)
	if err != nil {
		return fmt.Errorf("path bytes: %w", err)
	}
	_, _, errno = syscall.Syscall(syscall.SYS_RMDIR, uintptr(unsafe.Pointer(pathBytes2)), 0, 0)
	runtime.KeepAlive(pathBytes2)
	if errno != 0 {
		return fmt.Errorf("rmdir: %w", errno)
	}
	return nil
}

// dirMkdirat creates a directory via mkdirat(2) using Go's syscall.Mkdir
// which wraps mkdirat with AT_FDCWD on amd64.
func dirMkdirat() error {
	dir, cleanup, err := makeTempDir("dir-mkdirat")
	if err != nil {
		return err
	}
	defer cleanup()

	subDir := filepath.Join(dir, "mkdirat-subdir")
	if err := syscall.Mkdir(subDir, 0o755); err != nil {
		return fmt.Errorf("mkdirat: %w", err)
	}
	return nil
}

// dirChdir creates a temp directory, then changes to it via chdir(2).
// Restores the original working directory afterward.
func dirChdir() error {
	origDir, err := os.Getwd()
	if err != nil {
		return fmt.Errorf("getwd: %w", err)
	}

	dir, cleanup, err := makeTempDir("dir-chdir")
	if err != nil {
		return err
	}
	defer cleanup()
	defer syscall.Chdir(origDir)

	if err := syscall.Chdir(dir); err != nil {
		return fmt.Errorf("chdir: %w", err)
	}
	return nil
}

// dirGetcwd changes into a temp directory and calls getcwd(2) directly.
func dirGetcwd() error {
	origDir, err := os.Getwd()
	if err != nil {
		return fmt.Errorf("getwd: %w", err)
	}

	dir, cleanup, err := makeTempDir("dir-getcwd")
	if err != nil {
		return err
	}
	defer cleanup()
	defer syscall.Chdir(origDir)

	if err := syscall.Chdir(dir); err != nil {
		return fmt.Errorf("chdir: %w", err)
	}

	buf := make([]byte, 4096)
	_, _, errno := syscall.Syscall(syscall.SYS_GETCWD, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)), 0)
	runtime.KeepAlive(buf)
	if errno != 0 {
		return fmt.Errorf("getcwd: %w", errno)
	}
	// Keep cwd unchanged long enough for ior to process enter/exit pairing.
	time.Sleep(300 * time.Millisecond)
	return nil
}

// dirGetdents opens a directory and reads its entries via getdents64(2).
func dirGetdents() error {
	dir, cleanup, err := makeTempDir("dir-getdents")
	if err != nil {
		return err
	}
	defer cleanup()

	// Create a file so getdents has something to return.
	filePath := filepath.Join(dir, "getdents-file.txt")
	fd, err := syscall.Open(filePath, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open file: %w", err)
	}
	syscall.Close(fd)

	dirFD, err := syscall.Open(dir, syscall.O_RDONLY|syscall.O_DIRECTORY, 0)
	if err != nil {
		return fmt.Errorf("open dir: %w", err)
	}
	defer syscall.Close(dirFD)

	buf := make([]byte, 4096)
	_, _, errno := syscall.Syscall(syscall.SYS_GETDENTS64, uintptr(dirFD), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
	runtime.KeepAlive(buf)
	if errno != 0 {
		return fmt.Errorf("getdents64: %w", errno)
	}
	return nil
}

// dirMkdirEexist attempts to create a directory that already exists via raw
// SYS_MKDIR. The syscall fails with EEXIST, but ior captures the tracepoint
// on entry.
func dirMkdirEexist() error {
	dir, cleanup, err := makeTempDir("dir-mkdir-eexist")
	if err != nil {
		return err
	}
	defer cleanup()

	subDir := filepath.Join(dir, "mkdir-eexist-subdir")
	pathBytes, err := syscall.BytePtrFromString(subDir)
	if err != nil {
		return fmt.Errorf("path bytes: %w", err)
	}

	// Create the directory first so the second attempt fails.
	_, _, errno := syscall.Syscall(syscall.SYS_MKDIR, uintptr(unsafe.Pointer(pathBytes)), 0o755, 0)
	runtime.KeepAlive(pathBytes)
	if errno != 0 {
		return fmt.Errorf("first mkdir: %w", errno)
	}

	// Second mkdir on the same path should fail with EEXIST.
	pathBytes2, err := syscall.BytePtrFromString(subDir)
	if err != nil {
		return fmt.Errorf("path bytes: %w", err)
	}
	_, _, errno = syscall.Syscall(syscall.SYS_MKDIR, uintptr(unsafe.Pointer(pathBytes2)), 0o755, 0)
	runtime.KeepAlive(pathBytes2)
	if errno == 0 {
		return fmt.Errorf("expected EEXIST, but mkdir succeeded")
	}
	return nil
}

// dirChdirEnoent attempts to change to a nonexistent directory via raw
// SYS_CHDIR. The syscall fails with ENOENT, but ior captures the tracepoint
// on entry.
func dirChdirEnoent() error {
	dir, cleanup, err := makeTempDir("dir-chdir-enoent")
	if err != nil {
		return err
	}
	defer cleanup()

	badPath := filepath.Join(dir, "chdir-enoent-missing")
	pathBytes, err := syscall.BytePtrFromString(badPath)
	if err != nil {
		return fmt.Errorf("path bytes: %w", err)
	}
	// Retry a few times to reduce dropped-event flakiness under high load.
	for i := 0; i < 5; i++ {
		_, _, errno := syscall.Syscall(syscall.SYS_CHDIR, uintptr(unsafe.Pointer(pathBytes)), 0, 0)
		runtime.KeepAlive(pathBytes)
		if errno == 0 {
			return fmt.Errorf("expected ENOENT, but chdir succeeded")
		}
	}
	return nil
}

// dirGetdentsEbadf calls getdents64(2) with an invalid file descriptor.
// The syscall fails with EBADF, but ior captures the tracepoint on entry.
func dirGetdentsEbadf() error {
	buf := make([]byte, 4096)
	// Keep issuing the syscall for a short window so ior has enough time to
	// attach under high parallel integration load.
	for i := 0; i < 40; i++ {
		_, _, errno := syscall.Syscall(syscall.SYS_GETDENTS64, uintptr(9999), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
		runtime.KeepAlive(buf)
		if errno == 0 {
			return fmt.Errorf("expected EBADF, but getdents64 succeeded")
		}
		time.Sleep(25 * time.Millisecond)
	}
	return nil
}