runtime: use eventfd as the event wait/notify mechanism for epoll

Fixes #65443

Change-Id: I9ad4689b36e87ee930d35a38322a8797896483b4
Reviewed-on: https://go-review.googlesource.com/c/go/+/560615
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Andy Pan 2024-02-02 11:22:57 +08:00 committed by Gopher Robot
parent c4d55ab912
commit d068c2cb62
13 changed files with 58 additions and 24 deletions

View file

@ -0,0 +1,9 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package syscall
const (
EFD_CLOEXEC = 0x80000
)

View file

@ -10,6 +10,7 @@ const (
SYS_EPOLL_PWAIT = 319 SYS_EPOLL_PWAIT = 319
SYS_EPOLL_CREATE1 = 329 SYS_EPOLL_CREATE1 = 329
SYS_EPOLL_PWAIT2 = 441 SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 328
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -21,6 +22,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x800
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -10,6 +10,7 @@ const (
SYS_EPOLL_PWAIT = 281 SYS_EPOLL_PWAIT = 281
SYS_EPOLL_CREATE1 = 291 SYS_EPOLL_CREATE1 = 291
SYS_EPOLL_PWAIT2 = 441 SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 290
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -21,6 +22,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x800
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -10,6 +10,7 @@ const (
SYS_EPOLL_PWAIT = 346 SYS_EPOLL_PWAIT = 346
SYS_EPOLL_CREATE1 = 357 SYS_EPOLL_CREATE1 = 357
SYS_EPOLL_PWAIT2 = 441 SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 356
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -21,6 +22,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x800
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -10,6 +10,7 @@ const (
SYS_EPOLL_PWAIT = 22 SYS_EPOLL_PWAIT = 22
SYS_FCNTL = 25 SYS_FCNTL = 25
SYS_EPOLL_PWAIT2 = 441 SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 19
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -21,6 +22,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x800
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -10,6 +10,7 @@ const (
SYS_EPOLL_PWAIT = 22 SYS_EPOLL_PWAIT = 22
SYS_FCNTL = 25 SYS_FCNTL = 25
SYS_EPOLL_PWAIT2 = 441 SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 19
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -21,6 +22,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x800
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -12,6 +12,7 @@ const (
SYS_EPOLL_PWAIT = 5272 SYS_EPOLL_PWAIT = 5272
SYS_EPOLL_CREATE1 = 5285 SYS_EPOLL_CREATE1 = 5285
SYS_EPOLL_PWAIT2 = 5441 SYS_EPOLL_PWAIT2 = 5441
SYS_EVENTFD2 = 5284
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -23,6 +24,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x80
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -12,6 +12,7 @@ const (
SYS_EPOLL_PWAIT = 4313 SYS_EPOLL_PWAIT = 4313
SYS_EPOLL_CREATE1 = 4326 SYS_EPOLL_CREATE1 = 4326
SYS_EPOLL_PWAIT2 = 4441 SYS_EPOLL_PWAIT2 = 4441
SYS_EVENTFD2 = 4325
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -23,6 +24,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x80
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -12,6 +12,7 @@ const (
SYS_EPOLL_PWAIT = 303 SYS_EPOLL_PWAIT = 303
SYS_EPOLL_CREATE1 = 315 SYS_EPOLL_CREATE1 = 315
SYS_EPOLL_PWAIT2 = 441 SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 314
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -23,6 +24,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x800
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -10,6 +10,7 @@ const (
SYS_EPOLL_PWAIT = 22 SYS_EPOLL_PWAIT = 22
SYS_FCNTL = 25 SYS_FCNTL = 25
SYS_EPOLL_PWAIT2 = 441 SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 19
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -21,6 +22,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x800
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -10,6 +10,7 @@ const (
SYS_EPOLL_PWAIT = 312 SYS_EPOLL_PWAIT = 312
SYS_EPOLL_CREATE1 = 327 SYS_EPOLL_CREATE1 = 327
SYS_EPOLL_PWAIT2 = 441 SYS_EPOLL_PWAIT2 = 441
SYS_EVENTFD2 = 323
EPOLLIN = 0x1 EPOLLIN = 0x1
EPOLLOUT = 0x4 EPOLLOUT = 0x4
@ -21,6 +22,7 @@ const (
EPOLL_CTL_ADD = 0x1 EPOLL_CTL_ADD = 0x1
EPOLL_CTL_DEL = 0x2 EPOLL_CTL_DEL = 0x2
EPOLL_CTL_MOD = 0x3 EPOLL_CTL_MOD = 0x3
EFD_NONBLOCK = 0x800
) )
type EpollEvent struct { type EpollEvent struct {

View file

@ -60,3 +60,8 @@ func EpollCtl(epfd, op, fd int32, event *EpollEvent) (errno uintptr) {
_, _, e := Syscall6(SYS_EPOLL_CTL, uintptr(epfd), uintptr(op), uintptr(fd), uintptr(unsafe.Pointer(event)), 0, 0) _, _, e := Syscall6(SYS_EPOLL_CTL, uintptr(epfd), uintptr(op), uintptr(fd), uintptr(unsafe.Pointer(event)), 0, 0)
return e return e
} }
func Eventfd(initval, flags int32) (fd int32, errno uintptr) {
r1, _, e := Syscall6(SYS_EVENTFD2, uintptr(initval), uintptr(flags), 0, 0, 0, 0)
return int32(r1), e
}

View file

@ -13,11 +13,9 @@ import (
) )
var ( var (
epfd int32 = -1 // epoll descriptor epfd int32 = -1 // epoll descriptor
netpollEventFd uintptr // eventfd for netpollBreak
netpollBreakRd, netpollBreakWr uintptr // for netpollBreak netpollWakeSig atomic.Uint32 // used to avoid duplicate calls of netpollBreak
netpollWakeSig atomic.Uint32 // used to avoid duplicate calls of netpollBreak
) )
func netpollinit() { func netpollinit() {
@ -27,26 +25,25 @@ func netpollinit() {
println("runtime: epollcreate failed with", errno) println("runtime: epollcreate failed with", errno)
throw("runtime: netpollinit failed") throw("runtime: netpollinit failed")
} }
r, w, errpipe := nonblockingPipe() efd, errno := syscall.Eventfd(0, syscall.EFD_CLOEXEC|syscall.EFD_NONBLOCK)
if errpipe != 0 { if errno != 0 {
println("runtime: pipe failed with", -errpipe) println("runtime: eventfd failed with", -errno)
throw("runtime: pipe failed") throw("runtime: eventfd failed")
} }
ev := syscall.EpollEvent{ ev := syscall.EpollEvent{
Events: syscall.EPOLLIN, Events: syscall.EPOLLIN,
} }
*(**uintptr)(unsafe.Pointer(&ev.Data)) = &netpollBreakRd *(**uintptr)(unsafe.Pointer(&ev.Data)) = &netpollEventFd
errno = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, r, &ev) errno = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, efd, &ev)
if errno != 0 { if errno != 0 {
println("runtime: epollctl failed with", errno) println("runtime: epollctl failed with", errno)
throw("runtime: epollctl failed") throw("runtime: epollctl failed")
} }
netpollBreakRd = uintptr(r) netpollEventFd = uintptr(efd)
netpollBreakWr = uintptr(w)
} }
func netpollIsPollDescriptor(fd uintptr) bool { func netpollIsPollDescriptor(fd uintptr) bool {
return fd == uintptr(epfd) || fd == netpollBreakRd || fd == netpollBreakWr return fd == uintptr(epfd) || fd == netpollEventFd
} }
func netpollopen(fd uintptr, pd *pollDesc) uintptr { func netpollopen(fd uintptr, pd *pollDesc) uintptr {
@ -73,10 +70,11 @@ func netpollBreak() {
return return
} }
var one uint64 = 1
oneSize := int32(unsafe.Sizeof(one))
for { for {
var b byte n := write(netpollEventFd, noescape(unsafe.Pointer(&one)), oneSize)
n := write(netpollBreakWr, unsafe.Pointer(&b), 1) if n == oneSize {
if n == 1 {
break break
} }
if n == -_EINTR { if n == -_EINTR {
@ -136,17 +134,19 @@ retry:
continue continue
} }
if *(**uintptr)(unsafe.Pointer(&ev.Data)) == &netpollBreakRd { if *(**uintptr)(unsafe.Pointer(&ev.Data)) == &netpollEventFd {
if ev.Events != syscall.EPOLLIN { if ev.Events != syscall.EPOLLIN {
println("runtime: netpoll: break fd ready for", ev.Events) println("runtime: netpoll: eventfd ready for", ev.Events)
throw("runtime: netpoll: break fd ready for something unexpected") throw("runtime: netpoll: eventfd ready for something unexpected")
} }
if delay != 0 { if delay != 0 {
// netpollBreak could be picked up by a // netpollBreak could be picked up by a
// nonblocking poll. Only read the byte // nonblocking poll. Only read the 8-byte
// if blocking. // integer if blocking.
var tmp [16]byte // Since EFD_SEMAPHORE was not specified,
read(int32(netpollBreakRd), noescape(unsafe.Pointer(&tmp[0])), int32(len(tmp))) // the eventfd counter will be reset to 0.
var one uint64
read(int32(netpollEventFd), noescape(unsafe.Pointer(&one)), int32(unsafe.Sizeof(one)))
netpollWakeSig.Store(0) netpollWakeSig.Store(0)
} }
continue continue