summaryrefslogtreecommitdiff
path: root/cmd/ioworkload/scenario_truncate.go
blob: 04288d54d697fd577cec44df0bd82a25daca268d (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
package main

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

// truncateBasic opens a file, writes data, then truncates it via
// syscall.Truncate which uses SYS_TRUNCATE directly on amd64 (path-based).
func truncateBasic() error {
	dir, cleanup, err := makeTempDir("truncate-basic")
	if err != nil {
		return err
	}
	defer cleanup()

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

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

	return syscall.Truncate(path, 5)
}

// truncateFtruncate opens a file, writes data, then truncates it via
// syscall.Ftruncate which uses SYS_FTRUNCATE directly on amd64 (fd-based).
func truncateFtruncate() error {
	dir, cleanup, err := makeTempDir("truncate-ftruncate")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "ftruncfile.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("ftruncate this content")); err != nil {
		return fmt.Errorf("write: %w", err)
	}
	return syscall.Ftruncate(fd, 5)
}

// truncateEnoent attempts to truncate a nonexistent file via raw SYS_TRUNCATE.
// The syscall fails with ENOENT, but ior captures the enter_truncate
// tracepoint because the path is read on entry.
func truncateEnoent() error {
	dir, cleanup, err := makeTempDir("truncate-enoent")
	if err != nil {
		return err
	}
	defer cleanup()

	path := filepath.Join(dir, "truncate-enoent-missing.txt")
	pathBytes, err := syscall.BytePtrFromString(path)
	if err != nil {
		return fmt.Errorf("path bytes: %w", err)
	}
	// Retry a few times to make this test resilient under high integration
	// parallelism where a single failed syscall event can be dropped.
	for i := 0; i < 5; i++ {
		_, _, errno := syscall.Syscall(syscall.SYS_TRUNCATE, uintptr(unsafe.Pointer(pathBytes)), 0, 0)
		runtime.KeepAlive(pathBytes)
		if errno == 0 {
			return fmt.Errorf("expected ENOENT, but truncate succeeded")
		}
	}
	return nil
}

// truncateFtruncateEbadf calls raw SYS_FTRUNCATE on an invalid fd (99999).
// The syscall fails with EBADF, but ior captures the enter_ftruncate
// tracepoint because it is recorded on syscall entry.
func truncateFtruncateEbadf() error {
	_, _, errno := syscall.Syscall(syscall.SYS_FTRUNCATE, 99999, 0, 0)
	if errno == 0 {
		return fmt.Errorf("expected EBADF, but ftruncate succeeded")
	}
	return nil
}