sys

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package sys provides OS-level file descriptor primitives for page-cache bypass.

KeepAlive mandate

Every exported function that calls a raw syscall via an fd extracted from *os.File calls runtime.KeepAlive(f) unconditionally immediately after the syscall — before any error checking. This prevents the Go GC from finalizing f (and closing its fd) while the kernel still has it open:

err := unix.Fdatasync(int(f.Fd()))
runtime.KeepAlive(f) // <— unconditional, before error check
if err != unix.EINTR { … }

Cross-platform

Build-tagged files (io_linux.go, io_darwin.go) supply platform implementations for Fdatasync, Fallocate, PunchHole, Fadvise, CreateDirect, OpenDirect, and CopyFileRange. This file supplies shared types and pure-computation helpers.

Index

Constants

View Source
const (
	SyncNone OpenFlag = 0
	SyncData          = FlDSync
	SyncFull          = FlSync
)

Common flag combinations.

View Source
const (
	// UseFadvise reports whether posix_fadvise is effective on this platform.
	UseFadvise = true

	// RequiresAlignment reports whether O_DIRECT requires 4 KiB-aligned
	// buffers, offsets, and transfer sizes. Always true on Linux.
	RequiresAlignment = true

	// RequiresExplicitSync reports whether explicit sync calls are needed
	// for durable writes. On Linux, O_DSYNC/O_SYNC at open time suffices.
	RequiresExplicitSync = false
)

Platform capability constants.

Variables

View Source
var ErrAlignment = errors.New("dio/sys: buffer not aligned for O_DIRECT")

ErrAlignment is returned when a buffer is not properly aligned for O_DIRECT.

Functions

func AlignForHolePunch

func AlignForHolePunch(offset, length int64) (int64, int64, bool)

AlignForHolePunch computes filesystem-block-aligned boundaries for FALLOC_FL_PUNCH_HOLE / F_PUNCHHOLE so that adjacent blobs are not punched.

Returns (alignedOffset, alignedLength, canPunch). canPunch is false when the adjusted range is smaller than one block.

func CopyFileRange

func CopyFileRange(srcFile, dstFile *os.File, srcOff, dstOff *int64, length int) (int, error)

CopyFileRange copies length bytes from srcFile at *srcOff to dstFile at *dstOff using the copy_file_range(2) syscall. On XFS with reflinks this is a metadata-only operation (zero physical I/O). Retries automatically on EINTR.

func CreateAndAllocate

func CreateAndAllocate(path string, flags OpenFlag, allocSize int64) (*os.File, error)

CreateAndAllocate creates path with flags and reserves allocSize bytes via Fallocate.

func CreateDirect

func CreateDirect(path string, flags OpenFlag) (*os.File, error)

CreateDirect creates path for writing. Always uses O_CREATE|O_WRONLY|O_TRUNC. Pass FlDirectIO to add O_DIRECT; FlDSync / FlSync to add O_DSYNC / O_SYNC. Falls back to buffered I/O if the filesystem rejects O_DIRECT.

func Fadvise

func Fadvise(f *os.File, offset Offset_t, length int64, hint FadviseHint) error

Fadvise advises the kernel of the expected access pattern for f. Maps the cross-platform FadviseHint to Linux posix_fadvise constants. Retries automatically on EINTR.

func Fallocate

func Fallocate(f *os.File, size int64) error

Fallocate pre-allocates disk space for a file to reduce fragmentation and improve sequential write performance. Retries automatically on EINTR.

func Fdatasync

func Fdatasync(f *os.File) error

Fdatasync syncs file data to storage without syncing metadata. Prefers fdatasync(2) over fsync(2) for better performance on Linux. Retries automatically on EINTR (Go runtime SIGURG preemption).

func Ftruncate

func Ftruncate(f *os.File, size int64) error

Ftruncate sets the logical size of f to size bytes, releasing any extents beyond that point back to the filesystem without moving the file position.

The canonical use case is snapping an O_DIRECT file back to its true size after the tail-padding trick: the last chunk is written block-aligned (padded with zeros to the next 4 KiB boundary), and Ftruncate then sets the logical EOF to the actual byte count, discarding the padding.

Prefer this over syscall.Ftruncate(int(f.Fd()), size) directly. When the caller has extracted a raw fd via f.Fd() for use with iosched.WriteAt, the GC may finalise f — closing the file descriptor — before the call site if nothing explicitly references f after f.Fd(). os.File.Truncate is a method call on f, so the compiler guarantees f is live until Truncate returns; no runtime.KeepAlive bookkeeping is required at the call site.

func IsAligned

func IsAligned(block []byte) bool

IsAligned reports whether the first byte of block is aligned to align.BlockSize. Delegates to align.IsAligned; provided here so callers need only import sys.

func IsTransientIOError

func IsTransientIOError(err error) bool

IsTransientIOError reports whether err represents a temporary condition that may succeed on retry.

func OpenDirect

func OpenDirect(path string, flags OpenFlag) (*os.File, error)

OpenDirect opens an existing file for reading with optional O_DIRECT. Falls back to buffered I/O if the filesystem rejects O_DIRECT.

func PreadAligned

func PreadAligned(f *os.File, buf []byte, offset int64, flags OpenFlag) (int, error)

PreadAligned reads len(buf) bytes from f at offset into buf. When FlDirectIO is set, validates that buf, offset, and len(buf) all satisfy the align.BlockSize alignment requirements of O_DIRECT.

func PunchHole

func PunchHole(f *os.File, offset, length int64) (int64, error)

PunchHole deallocates a byte range within a file, creating a sparse file. Uses FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE to reclaim disk space without changing the logical file size.

The range is aligned inward to filesystem block boundaries so that adjacent data is never punched. Returns the number of bytes reclaimed after alignment, or (0, nil) if the range is smaller than one block. Retries automatically on EINTR.

func SyncFile

func SyncFile(f *os.File, flags OpenFlag) error

SyncFile syncs f according to flags after a write completes.

On Linux, O_DSYNC/O_SYNC at open time makes this a no-op. On Darwin, where those flags are unavailable at open time, RequiresExplicitSync is true and this function issues the call.

func WriteFile

func WriteFile(path string, data []byte, flags OpenFlag) (retErr error)

WriteFile atomically writes data to a new file. data must be align.BlockSize-aligned when FlDirectIO is set.

Types

type FadviseHint

type FadviseHint int

FadviseHint describes the expected access pattern for posix_fadvise.

const (
	FadvSequential FadviseHint = iota
	FadvDontNeed
	FadvRandom
)

type Offset_t

type Offset_t = int64

Offset_t is a signed file offset, matching the C off_t type.

type OpenFlag

type OpenFlag int

OpenFlag controls file opening and sync behavior.

const (
	// FlDirectIO bypasses the page cache (O_DIRECT on Linux, F_NOCACHE on Darwin).
	FlDirectIO OpenFlag = 1 << iota

	// FlDSync syncs data before each write returns (O_DSYNC on Linux).
	// On Darwin, explicit Fdatasync is needed after writes.
	FlDSync

	// FlSync syncs data and metadata before each write returns (O_SYNC on Linux).
	// On Darwin, explicit f.Sync is needed after writes.
	FlSync
)

func (OpenFlag) OpenFlags

func (fl OpenFlag) OpenFlags() int

OpenFlags returns the Linux-specific os.OpenFile flags for the given OpenFlag.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL