Skip to content

Commit

Permalink
uring experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Dec 31, 2023
1 parent e882051 commit da3e62c
Show file tree
Hide file tree
Showing 124 changed files with 13,023 additions and 7 deletions.
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down Expand Up @@ -46,11 +44,8 @@ github.com/prometheus/procfs v0.11.1/go.mod h1:eesXgaPo1q7lBpVMoMy0ZOFTth9hBn4W/
github.com/redis/rueidis v1.0.25 h1:ziEL0ZfJKw/cBTwswhMikG2djHiDBmOPyBb1eSA8RaM=
github.com/redis/rueidis v1.0.25/go.mod h1:NT7lPuiVYijdZVsV0V8i9ZUhqe1OMGaq+NiQigNuKlg=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg=
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
github.com/segmentio/encoding v0.3.6 h1:E6lVLyDPseWEulBmCmAKPanDd3jiyGDo5gMcugCRwZQ=
github.com/segmentio/encoding v0.3.6/go.mod h1:n0JeuIqEQrQoPDGsjo8UNd1iA0U8d8+oHAA4E3G3OxM=
github.com/segmentio/encoding v0.4.0 h1:MEBYvRqiUB2nfR2criEXWqwdY6HJOUrCn5hboVOVmy8=
github.com/segmentio/encoding v0.4.0/go.mod h1:/d03Cd8PoaDeceuhUUUQWjU0KhWjrmYrWPgtJHYZSnI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -63,7 +58,6 @@ golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
Expand Down
3 changes: 2 additions & 1 deletion handler_websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
"testing"
"time"

"github.com/centrifugal/centrifuge/internal/websocket"

"github.com/centrifugal/protocol"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/require"
)

Expand Down
46 changes: 46 additions & 0 deletions internal/iouring-go/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:prefix github.com/iceber/iouring-go
gazelle(name = "gazelle")

go_library(
name = "iouring-go",
srcs = [
"errors.go",
"eventfd.go",
"fixed_buffers.go",
"fixed_files.go",
"iouring.go",
"link_request.go",
"mmap.go",
"options.go",
"poller.go",
"prep_request.go",
"probe.go",
"request.go",
"timeout.go",
"types.go",
"user_data.go",
"utils.go",
],
importpath = "github.com/iceber/iouring-go",
visibility = ["//visibility:public"],
deps = select({
"@io_bazel_rules_go//go/platform:android": [
"//syscall",
"@org_golang_x_sys//unix:go_default_library",
],
"@io_bazel_rules_go//go/platform:linux": [
"//syscall",
"@org_golang_x_sys//unix:go_default_library",
],
"//conditions:default": [],
}),
)

go_test(
name = "iouring-go_test",
srcs = ["iouring_test.go"],
embed = [":iouring-go"],
)
21 changes: 21 additions & 0 deletions internal/iouring-go/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 IceberGu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
169 changes: 169 additions & 0 deletions internal/iouring-go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
# What is io_uring
[io_uring](http://kernel.dk/io_uring.pdf)

[io_uring-wahtsnew](https://kernel.dk/io_uring-whatsnew.pdf)

[LWN io_uring](https://lwn.net/Kernel/Index/#io_uring)

[Lord of the io_uring](https://unixism.net/loti/)

[【译】高性能异步 IO —— io_uring (Effecient IO with io_uring)](http://icebergu.com/archives/linux-iouring)

[Go 与异步 IO - io_uring 的思考](http://icebergu.com/archives/go-iouring)

# Features
- [x] register a file set for io_uring instance
- [x] support file IO
- [x] support socket IO
- [x] support IO timeout
- [x] link request
- [x] set timer
- [x] add request extra info, could get it from the result
- [ ] set logger
- [ ] register buffers and IO with buffers
- [ ] support SQPoll

# OS Requirements
* Linux Kernel >= 5.6


# Installation
```
go get github.com/iceber/iouring-go
```
[doc](https://pkg.go.dev/github.com/iceber/iouring-go)

# Quickstart
```golang
package main

import (
"fmt"
"os"

"github.com/iceber/iouring-go"
)

var str = "io with iouring"

func main() {
iour, err := iouring.New(1)
if err != nil {
panic(fmt.Sprintf("new IOURing error: %v", err))
}
defer iour.Close()

file, err := os.Create("./tmp.txt")
if err != nil {
panic(err)
}

ch := make(chan iouring.Result, 1)

prepRequest := iouring.Write(int(file.Fd()), []byte(str))
if _, err := iour.SubmitRequest(prepRequest, ch); err != nil {
panic(err)
}

result := <-ch
i, err := result.ReturnInt()
if err != nil {
fmt.Println("write error: ", err)
return
}

fmt.Printf("write byte: %d\n", i)
}
```

# Request With Extra Info
```golang
prepRequest := iouring.Write(int(file.Fd()), []byte(str)).WithInfo(file.Name())

request, err := iour.SubmitRequest(prepRequest, nil)
if err != nil {
panic(err)
}

<- request.Done()
info, ok := request.GetRequestInfo().(string)
```

# Cancel Request
```golang
prepR := iouring.Timeout(5 * time.Second)
request, err := iour.SubmitRequest(prepR, nil)
if err != nil {
panic(err)
}

if _, err := request.Cancel(); err != nil{
fmt.Printf("cancel request error: %v\n", err)
return
}

<- request.Done()
if err := request.Err(); err != nil{
if err == iouring.ErrRequestCanceled{
fmt.Println("request is canceled"0
return
}
fmt.Printf("request error: %v\n", err)
return
}
```


# Submit multitude request

```golang
var offset uint64
buf1 := make([]byte, 1024)
prep1:= iouring.Pread(fd, buf1, offset)

offset += 1024
buf2 := make([]byte, 1024)
prep2:= iouring.Pread(fd, buf1, offset)

requests, err := iour.SubmitRequests([]iouring.PrepRequest{prep1,prep2}, nil)
if err != nil{
panic(err)
}
<- requests.Done()
fmt.Println("requests are completed")
```
requests is concurrent execution

# Link request
```golang
var offset uint64
buf := make([]byte, 1024)
prep1 := iouring.Pread(fd, buf1, offset)
prep2 := iouring.Write(int(os.Stdout.Fd()), buf)

iour.SubmitLinkRequests([]iouring.PrepRequest{prep1, prep2}, nil)
```

# Examples
[cat](https://github.com/Iceber/iouring-go/tree/main/examples/cat)

[concurrent-cat](https://github.com/Iceber/iouring-go/tree/main/examples/concurrent-cat)

[cp](https://github.com/Iceber/iouring-go/tree/main/examples/cp)

[request-with-timeout](https://github.com/Iceber/iouring-go/tree/main/examples/timeout/request-with-timeout)

[link-request](https://github.com/Iceber/iouring-go/tree/main/examples/link)

[link-with-timeout](https://github.com/Iceber/iouring-go/tree/main/examples/timeout/link-with-timeout)

[timer](https://github.com/Iceber/iouring-go/tree/main/examples/timeout/timer)

[echo](https://github.com/Iceber/iouring-go/tree/main/examples/echo)

[echo-with-callback](https://github.com/Iceber/iouring-go/tree/main/examples/echo-with-callback)

# TODO
* add tests
* arguments type (eg. int and int32)
* set logger
28 changes: 28 additions & 0 deletions internal/iouring-go/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "io_bazel_rules_go",
sha256 = "8e968b5fcea1d2d64071872b12737bbb5514524ee5f0a4f54f5920266c261acb",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.28.0/rules_go-v0.28.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.28.0/rules_go-v0.28.0.zip",
],
)

http_archive(
name = "bazel_gazelle",
sha256 = "62ca106be173579c0a167deb23358fdfe71ffa1e4cfdddf5582af26520f1c66f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.23.0/bazel-gazelle-v0.23.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.23.0/bazel-gazelle-v0.23.0.tar.gz",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.17")

gazelle_dependencies()
17 changes: 17 additions & 0 deletions internal/iouring-go/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build linux

package iouring

import "errors"

var (
ErrIOURingClosed = errors.New("iouring closed")

ErrRequestCanceled = errors.New("request is canceled")
ErrRequestNotFound = errors.New("request is not found")
ErrRequestCompleted = errors.New("request has already been completed")
ErrRequestNotCompleted = errors.New("request is not completed")
ErrNoRequestCallback = errors.New("no request callback")

ErrUnregisteredFile = errors.New("file is unregistered")
)
27 changes: 27 additions & 0 deletions internal/iouring-go/eventfd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build linux
// +build linux

package iouring

import (
"os"
"unsafe"

"golang.org/x/sys/unix"

iouring_syscall "github.com/centrifugal/centrifuge/internal/iouring-go/syscall"
)

func (iour *IOURing) registerEventfd() error {
eventfd, err := unix.Eventfd(0, unix.EFD_NONBLOCK|unix.EFD_CLOEXEC)
if err != nil {
return os.NewSyscallError("eventfd", err)
}
iour.eventfd = eventfd

return iouring_syscall.IOURingRegister(
iour.fd,
iouring_syscall.IORING_REGISTER_EVENTFD,
unsafe.Pointer(&iour.eventfd), 1,
)
}
15 changes: 15 additions & 0 deletions internal/iouring-go/examples/cat/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "cat_lib",
srcs = ["main.go"],
importpath = "github.com/iceber/iouring-go/examples/cat",
visibility = ["//visibility:private"],
deps = ["//:iouring-go"],
)

go_binary(
name = "cat",
embed = [":cat_lib"],
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions internal/iouring-go/examples/cat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# cat

```
go build .
./cat file1 file2
```
Loading

0 comments on commit da3e62c

Please sign in to comment.