epoll: why write 1 byte to the pipe in netpollBreak() and read up to 16 bytes in netpoll()?

244 views
Skip to first unread message

shaouai

unread,
Jul 10, 2023, 9:10:37 AM7/10/23
to golang-nuts
In the implementation of the Go netpoller, `netpollBreak()` attempts to write 1 byte to `netpollBreakWr`, whereas `netpoll()` reads up to 16 bytes from `netpollBreakRd`, why 16 bytes rather than 1 byte?


Ian Lance Taylor

unread,
Jul 10, 2023, 9:00:36 PM7/10/23
to shaouai, golang-nuts
A single byte will wake up a goroutine sleeping in netpoll, so there
is no reason to write more than one byte.

If several different goroutines decide to wake up the polling
goroutine before the polling goroutine wakes up, they will each write
a single byte, and they will all be satisfied by a single wakeup.
And, if we don't read all those bytes, there will still be bytes in
the pipe and we'll wake up the next time around the poll loop even if
we don't have to. So we try to read all of their wakeup bytes at
once. The number 16 is arbitrary, based on the assumption that it's
not all that likely that more than 16 goroutines will try to wake up
the poller simultaneously.

Ian

metronome

unread,
Aug 14, 2023, 2:27:47 PM8/14/23
to golang-nuts
>> If several different goroutines decide to wake up the polling
>> goroutine before the polling goroutine wakes up, they will each write
>> a single byte

Wondering, with the introduction of "netpollWakeSig", does it still hold true? Thanks.

Ian Lance Taylor

unread,
Aug 14, 2023, 3:21:57 PM8/14/23
to metronome, golang-nuts
On Mon, Aug 14, 2023 at 11:28 AM metronome <xiangd...@gmail.com> wrote:
>
> >> If several different goroutines decide to wake up the polling
> >> goroutine before the polling goroutine wakes up, they will each write
> >> a single byte
>
> Wondering, with the introduction of "netpollWakeSig", does it still hold true? Thanks.

Good point, I think you're right. With netpollWakeSig we shouldn't
expect to see more than a single byte written to the pipe.

Doesn't hurt to try to read more bytes, though.

Ian

> On Tuesday, July 11, 2023 at 9:00:36 AM UTC+8 Ian Lance Taylor wrote:
>>
>> On Mon, Jul 10, 2023 at 6:10 AM shaouai <aou...@gmail.com> wrote:
>> >
>> > In the implementation of the Go netpoller, `netpollBreak()` attempts to write 1 byte to `netpollBreakWr`, whereas `netpoll()` reads up to 16 bytes from `netpollBreakRd`, why 16 bytes rather than 1 byte?
>> >
>> > write up to 1 byte: https://cs.opensource.google/go/go/+/refs/tags/go1.20.5:src/runtime/netpoll_epoll.go;l=77;drc=c7cc2b94c63af610a29b1b48cfbfb87cb8abf05b
>> >
>> > read up to 16 bytes: https://cs.opensource.google/go/go/+/refs/tags/go1.20.5:src/runtime/netpoll_epoll.go;l=146;drc=c7cc2b94c63af610a29b1b48cfbfb87cb8abf05b
>>
>> A single byte will wake up a goroutine sleeping in netpoll, so there
>> is no reason to write more than one byte.
>>
>> If several different goroutines decide to wake up the polling
>> goroutine before the polling goroutine wakes up, they will each write
>> a single byte, and they will all be satisfied by a single wakeup.
>> And, if we don't read all those bytes, there will still be bytes in
>> the pipe and we'll wake up the next time around the poll loop even if
>> we don't have to. So we try to read all of their wakeup bytes at
>> once. The number 16 is arbitrary, based on the assumption that it's
>> not all that likely that more than 16 goroutines will try to wake up
>> the poller simultaneously.
>>
>> Ian
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/8a3ae80e-1e78-441d-8c9a-c99e94e3c2c9n%40googlegroups.com.

metronome

unread,
Aug 15, 2023, 1:21:25 AM8/15/23
to golang-nuts
Hi Ian,

Thanks for clarifying, yes it's no harm to leave the code untouched.

Reply all
Reply to author
Forward
0 new messages