We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running the following in a default Go VS Code dev container on macOS with an M3 max
package main import ( "fmt" "os" "unsafe" "github.com/iceber/iouring-go" "golang.org/x/sys/unix" ) const ( AlignSize = 4096 BlockSize = 4096 ) // AlignedBuffer represents a memory-aligned buffer for direct I/O operations type AlignedBuffer struct { data []byte } // NewAlignedBuffer creates a new aligned buffer of the specified size func NewAlignedBuffer(size int) (*AlignedBuffer, error) { // Allocate aligned memory using unix.Mmap data, err := unix.Mmap(-1, 0, size, unix.PROT_READ|unix.PROT_WRITE, unix.MAP_PRIVATE|unix.MAP_ANONYMOUS) if err != nil { return nil, fmt.Errorf("mmap failed: %v", err) } // Verify alignment if alignment := Alignment(data, AlignSize); alignment != 0 { unix.Munmap(data) return nil, fmt.Errorf("buffer not aligned: offset %d", alignment) } return &AlignedBuffer{data: data}, nil } // Bytes returns the underlying byte slice func (b *AlignedBuffer) Bytes() []byte { return b.data } // Free releases the allocated memory func (b *AlignedBuffer) Free() error { return unix.Munmap(b.data) } // IOUringDevice represents a device that uses io_uring for I/O operations type IOUringDevice struct { file *os.File ring *iouring.IOURing } // NewIOUringDevice creates a new IOUringDevice instance func NewIOUringDevice(path string) (*IOUringDevice, error) { // Open file with O_DIRECT flag for direct I/O file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|unix.O_DIRECT, 0644) if err != nil { return nil, fmt.Errorf("failed to open file: %v", err) } // Create new io_uring instance ring, err := iouring.New(128) if err != nil { file.Close() return nil, fmt.Errorf("failed to create io_uring: %v", err) } return &IOUringDevice{ file: file, ring: ring, }, nil } // ReadBlock reads a block from the device into the given buffer func (d *IOUringDevice) ReadBlock(offset int64, buffer *AlignedBuffer) error { ch := make(chan iouring.Result, 1) prepRequest := iouring.Pread(int(d.file.Fd()), buffer.Bytes(), uint64(offset)) if _, err := d.ring.SubmitRequest(prepRequest, ch); err != nil { return fmt.Errorf("submit read request failed: %v", err) } result := <-ch if n, err := result.ReturnInt(); err != nil { return fmt.Errorf("read failed: %v", err) } else if n != len(buffer.Bytes()) { return fmt.Errorf("short read: %d != %d", n, len(buffer.Bytes())) } return nil } // WriteBlock writes a block to the device from the given buffer func (d *IOUringDevice) WriteBlock(offset int64, buffer *AlignedBuffer) error { ch := make(chan iouring.Result, 1) prepRequest := iouring.Pwrite(int(d.file.Fd()), buffer.Bytes(), uint64(offset)) if _, err := d.ring.SubmitRequest(prepRequest, ch); err != nil { return fmt.Errorf("submit write request failed: %v", err) } result := <-ch if n, err := result.ReturnInt(); err != nil { return fmt.Errorf("write failed: %v", err) } else if n != len(buffer.Bytes()) { return fmt.Errorf("short write: %d != %d", n, len(buffer.Bytes())) } return nil } // Close closes the device and releases resources func (d *IOUringDevice) Close() error { if err := d.ring.Close(); err != nil { return err } return d.file.Close() } // Alignment checks the alignment of a byte slice func Alignment(block []byte, AlignSize int) int { return int(uintptr(unsafe.Pointer(&block[0])) & uintptr(AlignSize-1)) } func main() { device, err := NewIOUringDevice("test.dat") if err != nil { panic(err) } defer func() { if err := device.Close(); err != nil { panic(err) } // delete the file if err := os.Remove("test.dat"); err != nil { panic(err) } }() // Create aligned buffer buffer, err := NewAlignedBuffer(BlockSize) if err != nil { panic(err) } defer buffer.Free() // Write some data copy(buffer.Bytes(), []byte("Hello, world!\n")) if err := device.WriteBlock(0, buffer); err != nil { panic(err) } // Read it back readBuffer, err := NewAlignedBuffer(BlockSize) if err != nil { panic(err) } defer readBuffer.Free() if err := device.ReadBlock(0, readBuffer); err != nil { panic(err) } fmt.Printf("Read: %s", readBuffer.Bytes()[:13]) }
The text was updated successfully, but these errors were encountered:
I am also seeing this error.
Sorry, something went wrong.
This solved the issue for me.
No branches or pull requests
Running the following in a default Go VS Code dev container on macOS with an M3 max
The text was updated successfully, but these errors were encountered: