diff --git a/.gitignore b/.gitignore index 2315eb5..a177441 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .vscode/ *.cpu *.mem -*.test \ No newline at end of file +*.test + +.idea \ No newline at end of file diff --git a/README.md b/README.md index 8fe59ce..ac80826 100644 --- a/README.md +++ b/README.md @@ -7,31 +7,82 @@ ```bash go get github.com/ii64/gouring ``` -## Example +# Examples +## Write ```go // setup h, err := gouring.New(256, 0) -if err != nil { /*...*/ } -defer h.Close() +if err != nil { + log.Fatal("Error creating ring: ", err) +} +defer h.Close() -sqe := h.GetSQE() +fd, err := unix.Open("/tmp/gouring_test", unix.O_RDWR, 0677) +if err != nil { + log.Fatal("Error opening file: ", err) +} + +sqe := h.GetSqe() b := []byte("io_uring!\n") -PrepWrite(sqe, 1, &b[0], len(b), 0) +gouring.PrepWrite(sqe, fd, &b[0], len(b), 0) submitted, err := h.SubmitAndWait(1) -if err != nil { /*...*/ } +if err != nil { + log.Fatal("Error waiting ring: ", err) +} println(submitted) // 1 var cqe *gouring.IoUringCqe err = h.WaitCqe(&cqe) -if err != nil { /*...*/ } // check also EINTR +if err != nil { + log.Fatal("Error waiting cqe: ", err) +} // check also EINTR _ = cqe.UserData _ = cqe.Res _ = cqe.Flags ``` +## Read +```go +// setup +h, err := gouring.New(256, 0) +if err != nil { + log.Fatal("Error creating ring: ", err) +} +defer h.Close() + +fd, err := unix.Open("/tmp/gouring_test", unix.O_RDWR, 0677) +if err != nil { + log.Fatal("Error opening file: ", err) +} + +sqe := h.GetSqe() +b := make([]byte, 20) +gouring.PrepRead(sqe, fd, &b[0], len(b), 0) + +submitted, err := h.SubmitAndWait(1) +if err != nil { + log.Fatal("Error waiting ring: ", err) +} +println(submitted) // 1 + +var cqe *gouring.IoUringCqe +err = h.WaitCqe(&cqe) +if err != nil { + log.Fatal("Error waiting cqe: ", err) +} // check also EINTR + +_ = cqe.UserData +_ = cqe.Res +_ = cqe.Flags + +log.Println("CQE: ", cqe) +log.Println("Buffer: ", b) +log.Println("Buffer: ", string(b)) +``` + ## Graph | SQPOLL | non-SQPOLL | diff --git a/examples/read.go b/examples/read.go new file mode 100644 index 0000000..4af58b8 --- /dev/null +++ b/examples/read.go @@ -0,0 +1,43 @@ +package main + +import ( + "github.com/ii64/gouring" + "golang.org/x/sys/unix" + "log" +) + +func main() { + + h, err := gouring.New(256, 0) + if err != nil { + log.Fatal(err) + } + defer h.Close() + + fd, err := unix.Open("/tmp/test", unix.O_RDWR, 0677) + + sqe := h.GetSqe() + b := make([]byte, 20) + gouring.PrepRead(sqe, fd, &b[0], len(b), 0) + log.Println("Buffer: ", b) + + submitted, err := h.SubmitAndWait(1) + if err != nil { + log.Fatal(err) + } + println(submitted) // 1 + + var cqe *gouring.IoUringCqe + err = h.WaitCqe(&cqe) + if err != nil { + log.Fatal(err) + } // check also EINTR + + log.Println("CQE: ", cqe) + log.Println("Buffer: ", b) + log.Println("Buffer: ", string(b)) + + _ = cqe.UserData + _ = cqe.Res + _ = cqe.Flags +} diff --git a/examples/write.go b/examples/write.go new file mode 100644 index 0000000..c589127 --- /dev/null +++ b/examples/write.go @@ -0,0 +1,37 @@ +package main + +import ( + "github.com/ii64/gouring" + "golang.org/x/sys/unix" + "log" +) + +func main() { + + h, err := gouring.New(256, 0) + if err != nil { + log.Fatal(err) + } + defer h.Close() + + fd, err := unix.Open("/tmp/test", unix.O_RDWR, 0677) + + sqe := h.GetSqe() + b := []byte("hello from io_uring!\n") + gouring.PrepWrite(sqe, fd, &b[0], len(b), 0) + + submitted, err := h.SubmitAndWait(1) + if err != nil { /*...*/ + } + println(submitted) // 1 + + var cqe *gouring.IoUringCqe + err = h.WaitCqe(&cqe) + if err != nil { + log.Fatal(err) + } // check also EINTR + + _ = cqe.UserData + _ = cqe.Res + _ = cqe.Flags +} diff --git a/go.mod b/go.mod index 546250a..227886f 100644 --- a/go.mod +++ b/go.mod @@ -2,10 +2,13 @@ module github.com/ii64/gouring go 1.18 -require github.com/stretchr/testify v1.7.0 +require ( + github.com/davecgh/go-spew v1.1.0 + github.com/stretchr/testify v1.7.0 + golang.org/x/sys v0.0.0-20221010170243-090e33056c14 +) require ( - github.com/davecgh/go-spew v1.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) diff --git a/go.sum b/go.sum index acb88a4..9daff57 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc= +golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=