summaryrefslogtreecommitdiff
path: root/cmd/ioworkload/scenario_readwrite.go
blob: 21fba8e18158d4cb59332d89e16cf0d8a183c7d0 (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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package main

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

	"golang.org/x/sys/unix"
)

// 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
}

// readwritePreadv opens a file, writes data, then reads it back via preadv
// (positional vectored read). preadv returns the number of bytes read and is
// READ_CLASSIFIED, so the scenario reads a known payload to validate
// end-to-end byte attribution for the positional vectored read variant.
func readwritePreadv() error {
	dir, cleanup, err := makeTempDir("readwrite-preadv")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "preadvfile.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("preadv test data")
	if _, err := syscall.Write(fd, data); err != nil {
		return fmt.Errorf("write: %w", err)
	}

	buf1 := make([]byte, 6)
	buf2 := make([]byte, 10)
	iovs := []syscall.Iovec{
		{Base: &buf1[0], Len: uint64(len(buf1))},
		{Base: &buf2[0], Len: uint64(len(buf2))},
	}
	// preadv(fd, iov, iovcnt, offset) reads at the given offset (0) without
	// changing the file position.
	_, _, errno := syscall.Syscall6(syscall.SYS_PREADV, uintptr(fd), uintptr(unsafe.Pointer(&iovs[0])), uintptr(len(iovs)), 0, 0, 0)
	runtime.KeepAlive(buf1)
	runtime.KeepAlive(buf2)
	if errno != 0 {
		return fmt.Errorf("preadv: %w", errno)
	}
	return nil
}

// readwritePreadv2 opens a file, writes data, then reads it back via preadv2
// (positional vectored read with flags). Like preadv it returns the bytes read
// and is READ_CLASSIFIED; the scenario reads a known payload to validate
// end-to-end byte attribution.
func readwritePreadv2() error {
	dir, cleanup, err := makeTempDir("readwrite-preadv2")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "preadv2file.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("preadv2 test data")
	if _, err := syscall.Write(fd, data); err != nil {
		return fmt.Errorf("write: %w", err)
	}

	buf1 := make([]byte, 7)
	buf2 := make([]byte, 10)
	iovs := []syscall.Iovec{
		{Base: &buf1[0], Len: uint64(len(buf1))},
		{Base: &buf2[0], Len: uint64(len(buf2))},
	}
	nr, err := preadv2SyscallNr(runtime.GOARCH)
	if err != nil {
		return err
	}
	// preadv2(fd, iov, iovcnt, pos_l, pos_h, flags): offset 0, no flags.
	_, _, errno := syscall.Syscall6(nr, uintptr(fd), uintptr(unsafe.Pointer(&iovs[0])), uintptr(len(iovs)), 0, 0, 0)
	runtime.KeepAlive(buf1)
	runtime.KeepAlive(buf2)
	if errno != 0 {
		return fmt.Errorf("preadv2: %w", errno)
	}
	return nil
}

// preadv2SyscallNr returns the preadv2(2) syscall number for the given GOARCH.
// Go's syscall package lacks a SYS_PREADV2 constant, so the architecture
// numbers are provided explicitly (matching securitySyscallNumbers' pattern).
func preadv2SyscallNr(arch string) (uintptr, error) {
	switch arch {
	case "amd64":
		return 327, nil
	case "arm64":
		return 286, nil
	default:
		return 0, fmt.Errorf("preadv2 syscall number not defined for GOARCH=%s", arch)
	}
}

// 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
}

// readwritePwritev opens a file and writes data via pwritev (positional
// vectored write). pwritev returns the number of bytes written and is
// WRITE_CLASSIFIED, so the scenario writes a known iovec total to validate
// end-to-end byte attribution for the positional vectored write variant.
func readwritePwritev() error {
	dir, cleanup, err := makeTempDir("readwrite-pwritev")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "pwritevfile.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("pwritev ")
	buf2 := []byte("test data")
	iovs := []syscall.Iovec{
		{Base: &buf1[0], Len: uint64(len(buf1))},
		{Base: &buf2[0], Len: uint64(len(buf2))},
	}
	// pwritev(fd, iov, iovcnt, offset) writes at the given offset (0) without
	// changing the file position.
	_, _, errno := syscall.Syscall6(syscall.SYS_PWRITEV, uintptr(fd), uintptr(unsafe.Pointer(&iovs[0])), uintptr(len(iovs)), 0, 0, 0)
	runtime.KeepAlive(buf1)
	runtime.KeepAlive(buf2)
	if errno != 0 {
		return fmt.Errorf("pwritev: %w", errno)
	}
	return nil
}

// readwritePwritev2 opens a file and writes data via pwritev2 (positional
// vectored write with flags). Like pwritev it returns the bytes written and is
// WRITE_CLASSIFIED; the scenario writes a known iovec total to validate
// end-to-end byte attribution.
func readwritePwritev2() error {
	dir, cleanup, err := makeTempDir("readwrite-pwritev2")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "pwritev2file.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("pwritev2 ")
	buf2 := []byte("test data")
	iovs := []syscall.Iovec{
		{Base: &buf1[0], Len: uint64(len(buf1))},
		{Base: &buf2[0], Len: uint64(len(buf2))},
	}
	nr, err := pwritev2SyscallNr(runtime.GOARCH)
	if err != nil {
		return err
	}
	// pwritev2(fd, iov, iovcnt, pos_l, pos_h, flags): offset 0, no flags.
	_, _, errno := syscall.Syscall6(nr, uintptr(fd), uintptr(unsafe.Pointer(&iovs[0])), uintptr(len(iovs)), 0, 0, 0)
	runtime.KeepAlive(buf1)
	runtime.KeepAlive(buf2)
	if errno != 0 {
		return fmt.Errorf("pwritev2: %w", errno)
	}
	return nil
}

// pwritev2SyscallNr returns the pwritev2(2) syscall number for the given GOARCH.
// Go's syscall package lacks a SYS_PWRITEV2 constant, so the architecture
// numbers are provided explicitly (mirroring preadv2SyscallNr).
func pwritev2SyscallNr(arch string) (uintptr, error) {
	switch arch {
	case "amd64":
		return 328, nil
	case "arm64":
		return 287, nil
	default:
		return 0, fmt.Errorf("pwritev2 syscall number not defined for GOARCH=%s", arch)
	}
}

// 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
}

// readwriteReadahead opens a file, writes data, then calls readahead(2) on it.
// readahead(fd, offset, count) initiates non-blocking readahead so subsequent
// reads are served from the page cache. Despite its ssize_t prototype it returns
// 0 on success / -1 on error (it does NOT return a byte count and transfers no
// bytes to userspace), so ior classifies it KindFd / UNCLASSIFIED. The scenario
// exercises the enter fd_event (fd at args[0]) and the exit ret_event end-to-end.
func readwriteReadahead() error {
	dir, cleanup, err := makeTempDir("readwrite-readahead")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "readaheadfile.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("readahead test data")); err != nil {
		return fmt.Errorf("write: %w", err)
	}

	// readahead(fd, offset=0, count=4096): prime the page cache for the file.
	_, _, errno := syscall.Syscall(syscall.SYS_READAHEAD, uintptr(fd), 0, 4096)
	if errno != 0 {
		return fmt.Errorf("readahead: %w", errno)
	}
	return nil
}

// readwriteReadaheadEbadf calls readahead(2) on an invalid fd.
// The syscall fails with EBADF, but ior captures the enter_readahead tracepoint
// because arguments are read on syscall entry before the kernel returns an error.
func readwriteReadaheadEbadf() error {
	for i := 0; i < 5; i++ {
		_, _, errno := syscall.Syscall(syscall.SYS_READAHEAD, 99999, 0, 4096)
		if errno == 0 {
			return fmt.Errorf("expected EBADF, but readahead succeeded")
		}
	}
	return nil
}

// cachestatRange mirrors the kernel's struct cachestat_range, the second
// cachestat(2) argument: { __u64 off; __u64 len; }. off=0/len=0 means "the
// whole file".
type cachestatRange struct {
	off uint64
	len uint64
}

// cachestatResult mirrors the kernel's struct cachestat output, the third
// cachestat(2) argument. The kernel fills it in; the scenario only needs to
// hand the kernel a correctly-sized buffer, so the fields are not inspected.
type cachestatResult struct {
	nrCache           uint64
	nrDirty           uint64
	nrWriteback       uint64
	nrEvicted         uint64
	nrRecentlyEvicted uint64
}

// readwriteCachestat opens a file, writes data (so the file has pages in the
// page cache), then queries the cache residency of the whole file via
// cachestat(2). cachestat has no glibc/unix wrapper, so it is issued as a raw
// syscall: cachestat(fd, &cachestat_range{0,0}, &cachestat, flags=0). It returns
// 0 on success / -1 on error and transfers no I/O bytes to userspace, so ior
// classifies it KindFd / UNCLASSIFIED. The scenario exercises the enter fd_event
// (fd at args[0]) and the exit ret_event end-to-end. cachestat is Linux 6.5+;
// ENOSYS on older kernels is tolerated so the workload stays portable.
func readwriteCachestat() error {
	dir, cleanup, err := makeTempDir("readwrite-cachestat")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "cachestatfile.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("cachestat test data")); err != nil {
		return fmt.Errorf("write: %w", err)
	}

	// cachestat(fd, &range{off:0,len:0}=whole file, &cstat output, flags=0).
	cr := cachestatRange{off: 0, len: 0}
	var cs cachestatResult
	_, _, errno := syscall.Syscall6(
		unix.SYS_CACHESTAT,
		uintptr(fd),
		uintptr(unsafe.Pointer(&cr)),
		uintptr(unsafe.Pointer(&cs)),
		0, // flags must be 0
		0,
		0,
	)
	runtime.KeepAlive(cr)
	runtime.KeepAlive(cs)
	if errno == syscall.ENOSYS {
		// Kernel < 6.5: cachestat is unavailable. Tolerate gracefully so the
		// scenario does not fail on older portable targets.
		return nil
	}
	if errno != 0 {
		return fmt.Errorf("cachestat: %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
}