-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
124 changed files
with
13,023 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# cat | ||
|
||
``` | ||
go build . | ||
./cat file1 file2 | ||
``` |
Oops, something went wrong.