summaryrefslogtreecommitdiff
path: root/integrationtests/cmd/ioworkload/scenario_readwrite.go
blob: c676b900676b9caf62b888f137c0e4938d993585 (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
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
package main

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

// readwriteBasic opens a file, writes data, seeks to start, reads it back.
func readwriteBasic() error {
	dir, cleanup, err := makeTempDir("readwrite-basic")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "rwfile.txt")
	fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open: %w", err)
	}
	defer syscall.Close(fd)

	data := []byte("hello from ioworkload")
	if _, err := syscall.Write(fd, data); err != nil {
		return fmt.Errorf("write: %w", err)
	}
	if _, err := syscall.Seek(fd, 0, 0); err != nil {
		return fmt.Errorf("seek: %w", err)
	}

	buf := make([]byte, len(data))
	if _, err := syscall.Read(fd, buf); err != nil {
		return fmt.Errorf("read: %w", err)
	}
	return nil
}

// readwritePread opens a file, writes data, then reads it back via pread64.
func readwritePread() error {
	dir, cleanup, err := makeTempDir("readwrite-pread")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "preadfile.txt")
	fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open: %w", err)
	}
	defer syscall.Close(fd)

	data := []byte("pread test data")
	if _, err := syscall.Write(fd, data); err != nil {
		return fmt.Errorf("write: %w", err)
	}

	buf := make([]byte, len(data))
	if _, err := syscall.Pread(fd, buf, 0); err != nil {
		return fmt.Errorf("pread: %w", err)
	}
	return nil
}

// readwritePwrite opens a file and writes data via pwrite64.
func readwritePwrite() error {
	dir, cleanup, err := makeTempDir("readwrite-pwrite")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "pwritefile.txt")
	fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open: %w", err)
	}
	defer syscall.Close(fd)

	if _, err := syscall.Pwrite(fd, []byte("pwrite test data"), 0); err != nil {
		return fmt.Errorf("pwrite: %w", err)
	}
	return nil
}

// readwriteReadv opens a file, writes data, then reads it back via readv.
func readwriteReadv() error {
	dir, cleanup, err := makeTempDir("readwrite-readv")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "readvfile.txt")
	fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open: %w", err)
	}
	defer syscall.Close(fd)

	data := []byte("readv test data here")
	if _, err := syscall.Write(fd, data); err != nil {
		return fmt.Errorf("write: %w", err)
	}
	if _, err := syscall.Seek(fd, 0, 0); err != nil {
		return fmt.Errorf("seek: %w", err)
	}

	buf1 := make([]byte, 5)
	buf2 := make([]byte, 15)
	iovs := []syscall.Iovec{
		{Base: &buf1[0], Len: uint64(len(buf1))},
		{Base: &buf2[0], Len: uint64(len(buf2))},
	}
	_, _, errno := syscall.Syscall(syscall.SYS_READV, uintptr(fd), uintptr(unsafe.Pointer(&iovs[0])), uintptr(len(iovs)))
	runtime.KeepAlive(buf1)
	runtime.KeepAlive(buf2)
	if errno != 0 {
		return fmt.Errorf("readv: %w", errno)
	}
	return nil
}

// readwriteWritev opens a file and writes data via writev.
func readwriteWritev() error {
	dir, cleanup, err := makeTempDir("readwrite-writev")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "writevfile.txt")
	fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open: %w", err)
	}
	defer syscall.Close(fd)

	buf1 := []byte("writev ")
	buf2 := []byte("test data")
	iovs := []syscall.Iovec{
		{Base: &buf1[0], Len: uint64(len(buf1))},
		{Base: &buf2[0], Len: uint64(len(buf2))},
	}
	_, _, errno := syscall.Syscall(syscall.SYS_WRITEV, uintptr(fd), uintptr(unsafe.Pointer(&iovs[0])), uintptr(len(iovs)))
	runtime.KeepAlive(buf1)
	runtime.KeepAlive(buf2)
	if errno != 0 {
		return fmt.Errorf("writev: %w", errno)
	}
	return nil
}

// readwriteWronlyRead opens a file O_WRONLY, then attempts to read from it.
// The read fails with EBADF, but ior should capture the enter_read tracepoint
// because arguments are read on syscall entry before the kernel returns an error.
func readwriteWronlyRead() error {
	dir, cleanup, err := makeTempDir("readwrite-wronly-read")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "wronlyfile.txt")
	fd, err := syscall.Open(path, syscall.O_WRONLY|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open: %w", err)
	}
	defer syscall.Close(fd)

	buf := make([]byte, 16)
	_, err = syscall.Read(fd, buf)
	if err == nil {
		return fmt.Errorf("expected read from wronly fd to fail")
	}
	return nil
}

// readwriteRdonlyWrite opens a file O_RDONLY, then attempts to write to it.
// The write fails with EBADF, but ior should capture the enter_write tracepoint
// because arguments are read on syscall entry before the kernel returns an error.
func readwriteRdonlyWrite() error {
	dir, cleanup, err := makeTempDir("readwrite-rdonly-write")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "rdonlywritefile.txt")
	fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("create file: %w", err)
	}
	syscall.Close(fd)

	fd, err = syscall.Open(path, syscall.O_RDONLY, 0)
	if err != nil {
		return fmt.Errorf("open rdonly: %w", err)
	}
	defer syscall.Close(fd)

	_, err = syscall.Write(fd, []byte("should fail"))
	if err == nil {
		return fmt.Errorf("expected write to rdonly fd to fail")
	}
	return nil
}

// readwritePreadInvalid calls pread64 with a negative offset (-1).
// The syscall fails with EINVAL, but ior should capture the enter_pread64
// tracepoint because arguments are read on syscall entry.
func readwritePreadInvalid() error {
	dir, cleanup, err := makeTempDir("readwrite-pread-invalid")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "preadinvalid.txt")
	fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open: %w", err)
	}
	defer syscall.Close(fd)

	if _, err := syscall.Write(fd, []byte("some data")); err != nil {
		return fmt.Errorf("write: %w", err)
	}

	buf := make([]byte, 16)
	_, err = syscall.Pread(fd, buf, -1)
	if err == nil {
		return fmt.Errorf("expected pread with negative offset to fail")
	}
	return nil
}

// readwritePwriteInvalid calls pwrite64 with a negative offset (-1).
// The syscall fails with EINVAL, but ior should capture the enter_pwrite64
// tracepoint because arguments are read on syscall entry.
func readwritePwriteInvalid() error {
	dir, cleanup, err := makeTempDir("readwrite-pwrite-invalid")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "pwriteinvalid.txt")
	fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
	if err != nil {
		return fmt.Errorf("open: %w", err)
	}
	defer syscall.Close(fd)

	_, err = syscall.Pwrite(fd, []byte("should fail"), -1)
	if err == nil {
		return fmt.Errorf("expected pwrite with negative offset to fail")
	}
	return nil
}