Skip to content

Commit

Permalink
perf: use block syscall enter for epoll_wait
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Sep 18, 2023
1 parent 5719b53 commit fb9c77b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
18 changes: 15 additions & 3 deletions poll_default_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,25 @@ func (p *defaultPoll) Wait() (err error) {
if n == p.size && p.size < 128*1024 {
p.Reset(p.size<<1, caps)
}
n, err = EpollWait(p.fd, p.events, msec)
// msec: 0(raw) => 1ms(sched,raw) => -1(block_syscall)
// poller's G will hold P at most 1ms
if msec > 0 {
n, err = EpollWaitRaw(p.fd, p.events, msec)

Check failure on line 103 in poll_default_linux.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.15, ARM64)

undefined: EpollWaitRaw

Check failure on line 103 in poll_default_linux.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.21, ARM64)

undefined: EpollWaitRaw
} else if msec == 0 {
n, err = EpollWaitRaw(p.fd, p.events, msec)

Check failure on line 105 in poll_default_linux.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.15, ARM64)

undefined: EpollWaitRaw

Check failure on line 105 in poll_default_linux.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.21, ARM64)

undefined: EpollWaitRaw
} else { // < 0
n, err = EpollWaitBlock(p.fd, p.events, msec)

Check failure on line 107 in poll_default_linux.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.15, ARM64)

undefined: EpollWaitBlock

Check failure on line 107 in poll_default_linux.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.21, ARM64)

undefined: EpollWaitBlock
}
if err != nil && err != syscall.EINTR {
return err
}
if n <= 0 {
msec = -1
runtime.Gosched()
if msec > 0 {
msec = -1 // no need to sched, because we will use block syscall
} else if msec == 0 {
msec = 1
runtime.Gosched()
}
continue
}
msec = 0
Expand Down
35 changes: 31 additions & 4 deletions sys_epoll_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ type epollevent struct {
data [8]byte // unaligned uintptr
}

//go:nosplit
//go:linkname entersyscallblock runtime.entersyscallblock
func entersyscallblock()

//go:nosplit
//go:nowritebarrierrec

Check failure on line 37 in sys_epoll_linux.go

View workflow job for this annotation

GitHub Actions / style-test

//go:nowritebarrierrec only allowed in runtime

Check failure on line 37 in sys_epoll_linux.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.15, X64)

//go:nowritebarrierrec only allowed in runtime

Check failure on line 37 in sys_epoll_linux.go

View workflow job for this annotation

GitHub Actions / compatibility-test (1.21, X64)

//go:nowritebarrierrec only allowed in runtime
//go:linkname exitsyscall runtime.exitsyscall
func exitsyscall()

// EpollCreate implements epoll_create1.
func EpollCreate(flag int) (fd int, err error) {
var r0 uintptr
Expand All @@ -52,11 +61,29 @@ func EpollCtl(epfd int, op int, fd int, event *epollevent) (err error) {
func EpollWait(epfd int, events []epollevent, msec int) (n int, err error) {
var r0 uintptr
var _p0 = unsafe.Pointer(&events[0])
if msec == 0 {
r0, _, err = syscall.RawSyscall6(syscall.SYS_EPOLL_WAIT, uintptr(epfd), uintptr(_p0), uintptr(len(events)), 0, 0, 0)
} else {
r0, _, err = syscall.Syscall6(syscall.SYS_EPOLL_WAIT, uintptr(epfd), uintptr(_p0), uintptr(len(events)), uintptr(msec), 0, 0)
r0, _, err = syscall.Syscall6(syscall.SYS_EPOLL_WAIT, uintptr(epfd), uintptr(_p0), uintptr(len(events)), uintptr(msec), 0, 0)
if err == syscall.Errno(0) {
err = nil
}
return int(r0), err
}

func EpollWaitRaw(epfd int, events []epollevent, msec int) (n int, err error) {
var r0 uintptr
var _p0 = unsafe.Pointer(&events[0])
r0, _, err = syscall.RawSyscall6(syscall.SYS_EPOLL_WAIT, uintptr(epfd), uintptr(_p0), uintptr(len(events)), 0, 0, 0)
if err == syscall.Errno(0) {
err = nil
}
return int(r0), err
}

func EpollWaitBlock(epfd int, events []epollevent, msec int) (n int, err error) {
var r0 uintptr
var _p0 = unsafe.Pointer(&events[0])
entersyscallblock()
r0, _, err = syscall.RawSyscall6(syscall.SYS_EPOLL_WAIT, uintptr(epfd), uintptr(_p0), uintptr(len(events)), uintptr(msec), 0, 0)
exitsyscall()
if err == syscall.Errno(0) {
err = nil
}
Expand Down

0 comments on commit fb9c77b

Please sign in to comment.