go: runtime: epoll scalability problem with 192 core machine and 1k+ ready sockets
Split from https://github.com/golang/go/issues/31908#issuecomment-1887667141 and full write-up at https://jazco.dev/2024/01/10/golang-and-epoll/.
tl;dr is that a program on a 192 core machine with >2500 sockets and with >1k becoming ready at once results in huge costs in netpoll -> epoll_wait (~65% of total CPU).
Most interesting is that sharding these connections across 8 processes seems to solve the problem, implying some kind of super-linear scaling.
That the profile shows the time spent in epoll_wait itself, this may be a scalability problem in the kernel itself, but we may still be able to mitigate.
@ericvolp12, some questions if you don’t mind answering:
- Which version of Go are you using? And which kernel version?
- Do you happen to have a reproducer for this problem that you could share? (Sounds like no?)
- On a similar note, do you have a
perfprofile of this problem that shows where the time in the kernel is spent? - The 128 event buffer size is mentioned several times, but it is not obvious to me that increasing this size would actually solve the problem. Did you try increasing the size and see improved results?
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 31
- Comments: 53 (35 by maintainers)
Commits related to this issue
- separate lock from common grid to avoid epoll cotention epoll contention on TCP causes latency build-up when we have high volume ingress. This PR is an attempt to relieve this pressure. upstream iss... — committed to harshavardhana/minio by harshavardhana 2 years ago
- separate lock from common grid to avoid epoll contention epoll contention on TCP causes latency build-up when we have high volume ingress. This PR is an attempt to relieve this pressure. upstream is... — committed to harshavardhana/minio by harshavardhana 2 years ago
- separate lock from common grid to avoid epoll contention epoll contention on TCP causes latency build-up when we have high volume ingress. This PR is an attempt to relieve this pressure. upstream is... — committed to harshavardhana/minio by harshavardhana 2 years ago
- separate lock from common grid to avoid epoll contention epoll contention on TCP causes latency build-up when we have high volume ingress. This PR is an attempt to relieve this pressure. upstream is... — committed to harshavardhana/minio by harshavardhana 2 years ago
- separate lock from common grid to avoid epoll contention epoll contention on TCP causes latency build-up when we have high volume ingress. This PR is an attempt to relieve this pressure. upstream is... — committed to harshavardhana/minio by harshavardhana 2 years ago
- separate lock from common grid to avoid epoll contention (#20180) epoll contention on TCP causes latency build-up when we have high volume ingress. This PR is an attempt to relieve this pressure. ... — committed to minio/minio by harshavardhana 2 years ago
- runtime: only poll network from one P at a time in findRunnable For #65064 Change-Id: Ifecd7e332d2cf251750752743befeda4ed396f33 — committed to amwolff/go by ianlancetaylor 3 years ago
- runtime: only poll network from one P at a time in findRunnable For #65064 Change-Id: Ifecd7e332d2cf251750752743befeda4ed396f33 — committed to amwolff/go by ianlancetaylor 3 years ago
- runtime: only poll network from one P at a time in findRunnable For #65064 Change-Id: Ifecd7e332d2cf251750752743befeda4ed396f33 — committed to amwolff/go by ianlancetaylor 3 years ago
- add 03-largecores.diff, see https://github.com/golang/go/issues/65064 — committed to phuslu/go by phuslu 2 years ago
- separate lock from common grid to avoid epoll contention (#20180) epoll contention on TCP causes latency build-up when we have high volume ingress. This PR is an attempt to relieve this pressure. ... — committed to minio/minio by harshavardhana 2 years ago
- runtime: only poll network from one P at a time in findRunnable For #65064 Change-Id: Ifecd7e332d2cf251750752743befeda4ed396f33 — committed to amwolff/go by ianlancetaylor 3 years ago
- runtime: only poll network from one P at a time in findRunnable For #65064 Change-Id: Ifecd7e332d2cf251750752743befeda4ed396f33 — committed to amwolff/go by ianlancetaylor 3 years ago
- runtime: only poll network from one P at a time in findRunnable For #65064 Change-Id: Ifecd7e332d2cf251750752743befeda4ed396f33 Reviewed-on: https://go-review.googlesource.com/c/go/+/564197 LUCI-Try... — committed to golang/go by ianlancetaylor 3 years ago
- sweet/harnesses: update the version of Cockroachdb benchmarked This change updates the version of Cockroachdb benchmarked by sweet. When we submitted a change to how the network is polled in CL 56419... — committed to golang/benchmarks by cagedmantis 2 years ago
- runtime: only poll network from one P at a time in findRunnable This change reintroduces CL 564197. It was reverted due to a failing benchmark. That failure has been resolved. For #65064 Change-Id:... — committed to golang/go by cagedmantis 2 years ago
- runtime: only poll network from one P at a time in findRunnable This change reintroduces CL 564197. It was reverted due to a failing benchmark. That failure has been resolved. For #65064 Change-Id:... — committed to Sirherobrine23/go by cagedmantis 2 years ago
Good point! And to answer your question, yes, Go has been using the single (and global)
epoll/kqueue/pollinstance internally since the day Gonetpollwas introduced. I actually had this concern for a few years, but never got a chance to spot that kind of performance bottleneck emerge. What I had in mind is that we can make a transition from singleepollinstance to per-Pepollinstances, or just multiple globalepollinstances simply, which could also help.From where I stand, I reckon that refactoring the current
epollfrom a single instance to multiple instances would require much less work than introducingio_uring. What is more, given the current Go codebase,io_uringis better suited for file I/O than for network I/O. Oh boy, I can already imagine now how many obstacles we’ll have to go through beforeio_uringis implemented for network I/O eventually, and also transparently.To sum up, multiple
epollinstances should be able to gain sufficient credits for the performance boost of network I/O, and in consideration of the complexity from introducingio_uringfor network I/O, I think the former is more feasible at this stage.I think it would be great if Go runtime could maintain a separate epoll file descriptor (epfd) per each P. Then every P could register file descriptors in its own local epfd and call
epoll()on it when its local list of goroutines ready to run becomes empty and it needs to find runnable goroutine. This scheme has the following benefits:epoll()calls, since every P will poll its own epfd, thus removing bottlenecks related to access synchronization to shared epfd in kernel space.Such a scheme may result in imbalance of goroutines among P workers, if a single goroutine creates many network connections (e.g. server
acceptloop). Then the Go scheduler will migrate all the goroutines, which make IO on these connections, to the original P where the original goroutine created all these network connections. This can be solved by periodic even re-distribution of the registered network connections among P-local epfds. For example, if P cannot find ready to run goroutines in local queue and in local epfd, then it can steal a few network connections from the busiest P, to de-register them from that P’s epfd and then to register them in local epfd. The busiest P can be determined from some rough per-P CPU usage stats.is go using the same epoll instance accross all threads? that might be the underlying problem, most high-throughput applications (nginx, envoy, netty) create several instances (usually one per thread together with an event loop) and connections get distributed to all epoll instances some way or another.
If we want to go deep here, it might even be possible for the Go scheduler to become RX queue aware using sockopts like
SO_INCOMING_CPUorSO_INCOMING_NAPI_ID. I suspect we can do a lot better without bringing in that complexity, but it’s an interesting opportunity to consider.We will likely merge CL 564197 (or something similar) for Go 1.25.
I don’t expect that we will backport to 1.23 or 1.24 [1], as this is primarily a performance improvement.
[1] I know 1.24 isn’t released yet, but it is mere days away!
okay, I found a reliable though somewhat clunky reproducer and tested with EPYC 7443P and EPYC 9754 and go1.23.5 and go1.23.5+https://go.dev/cl/564197.
Reproducer: random-socket-reader
EPYC 9754 (128 cores, 256 threads)
go1.23.5
flamegraph image | profile (pprof.host | download)
go1.23.5+564197
flamegraph image | profile (pprof.host | download)
EPYC 7443P (24 cores, 48 threads)
go1.23.5
flamegraph image | profile (pprof.host | download)
go1.23.5+564197
flamegraph image | profile (pprof.host | download)
testing “process”
I haven’t done this in a while now, so if anyone wants to double-check if I compiled go1.23.5+564197 correctly, here’s a list of steps:
It seems like CL 564197 does help in some way, but I’m unsure whether the CPU time spent on locks in
findRunnableis now just a proxy for latency and that the program waits for more work. The CPU usage is ~30-40% less while running the updated program on EPYC 9754 and more time spent on the task indicates we don’t poll as much, so even though we’re slower, we have the capacity to take on more work (?).@prattmic @ianlancetaylor if it would be helpful, I’m happy to give you access to the test machines. Also let me know if these pprof profiles are enough or if perf profiles going down to the kernel calls are more helpful here.
Some thoughts from brainstorming for posterity:
My best theory at the moment (though I’d really like to see perf to confirm) is that ~90 threads are calling epoll_wait at once (probably at this non-blocking netpoll: https://cs.opensource.google/go/go/+/master:src/runtime/proc.go;l=3230;drc=dcbe77246922fe7ef41f07df228f47a37803f360). The kernel has a mutex around the entire copy-out portion of epoll_wait, so there is probably a lot of time waiting for the mutex. If that is the case, some form of rate-limiting on how many threads make the syscall at once may be effective. N.B. that this non-blocking netpoll is not load-bearing for correctness, so occasionally skipping it would be OK.
Thanks for the details! I’ll try to write a reproducer when I have some free time, not sure when I’ll get to it.
Indeed, you’d need to manually modify the runtime. Note that is possible to simply edit the runtime source in GOROOT and rebuild your program (no special steps required for the runtime, it is treated like any other package). But if you build in a Docker container it is probably a pain to edit the runtime source.
@prattmic Yes, of course. To explain the scale of the production workload a little better, a bit more context: one of services we run is a program that takes the inbound traffic, which is usually a large file(s), processes it and sends the processed data (expended by a constant factor) to many smaller servers we call storage nodes. In short: an upload to a machine like the one I took a profile on results in many (100+) connections and uploads to other machines in different locations. I suspect this characteristic is why the scalability problem shows up so well there.
Profiles in my previous comment are from a EPYC 9754 (128 cores, 256 vCPUs=GOMAXPROCS) machine, which we’re testing in one of our locations. We typically run a fleet of 7443Ps (24 cores) but would love to see Go scale to higher core count ones for various reasons.
Some data from the time of the experiment:
Network traffic:
Network packets:
QPS:
CPU:
RAM:
Profiles were taken somewhere around the peaks. Let me know if you would like to see anything additional.
For comparison, here’s a profile from a 7443P machine (without CL 564197): https://pprof.host/vc40/flamegraph
will this be solved with io_uring? epoll is obsolete with io_uring around.
Correct. I’ll ask today if I can share an example.
It seems to me that we can partially mitigate the immediate issue by just limiting the number of P’s that do a non-blocking
epollcall infindRunnable. We get no advantage from having multiple P’s callnetpoll(0)simultaneously. And if we prevent that from happening, then we seem likely to avoid the contention in the kernel. We’ll still have contention in userspace, but we have that anyhow, and that will continue until we are able to do a major overhaul of the scheduler for NUMA support.If anybody who can easily recreate the issue has time for experimentation, it might be interesting to see whether https://go.dev/cl/564197 makes any difference. Thanks.
Nevermind! I was reading your profile backwards. 😅
@aclements profile as requested. Taken with
go1.21.5on a 128 core machine:I don’t want to derail this issue, let me know if I should move this to a separate bug…
We are seeing a similar issue on a system with 128 cores, we’re only reading from 96 Unix Sockets, 1 per goroutine. Go was spending much time in
netpoll -> epoll_waitandperf topreported much of time in the kernel inosq_lock.I’m looking for the profiles from the Go App, in the mean time I can share that we reproduced this issue with a simple
socatinvocation:I wrote a workaround that does not invoke
netpollat all, instead it just makes raw syscalls and throughput improved by 5x (which is the next bottleneck in the App, also related to Mutex being slow). My microbenchmark with raw syscalls just reading from unix sockets performed >10x in terms of bandwidth with the 96 producers.Let me know if there’s anything I can do to help.
Will CL 564197 be merged anytime soon? Will this be backported to go1.23.x?
Thanks, this is very useful!
Here are two views from ~5ms of the traces.
go1.23.5:
go1.24rc2+564197
In the 1.23.5 trace, all the blank spaces are times when that P is in the scheduler instead of running a goroutine, presumably spending way way too much time in epoll. The trace is actually very interesting because you can see each P run a clump of short-running goroutines between each gap. I think that is the P running all of the things in its local run queue before it needs to go look for more work.
There are lots of goroutines that run for very short periods (1-5us). Those are probably contributing to the epoll probably by entering the scheduler very frequently.
The go1.24rc2+564197 trace looks much better. Densely packed and spending most time running goroutines, even though there are still very short running goroutines around.
From the go1.24rc2+564197 profiles above, I would say that 1.24 + CL 564197 completely solves the scheduling scalability issues for this application. Only 6% of time is in runtime.schedule, which is in range for a normal Go program.
There is definitely still a scalability problem in the GC around managing work buffers. I think that is most related to #21056, so I will post discussion of those symptoms there.
Thanks @amwolff, that’s great. The GOMAXPROCS=24 profile is a nice comparison as well.
Actually, most of this is the scheduler, not the GC. The GC is primary in
runtime.gcBgMarkWorker(see the GC Guide for tips on identifying the GC in profiles), which is only 6.6% of time (vs 4.7% in the GOMAXPROCS=24 profile, so still up a little).The time under
runtime.schedule(~32%) is the Go scheduler. You probably got tripped up byruntime.(*gcControllerState).findRunnableGCWorker, which is related to scheduling the GC workers. This poor scaling is tracked by #68399. Per https://github.com/golang/go/issues/68399#issuecomment-2264258044, we think we’ve actually fixed this primary problem with https://go.dev/cl/602477, which is in 1.24. So I recommend trying out 1.24rc2 + https://go.dev/cl/564197, or just patch https://go.dev/cl/602477 into 1.23.Other parts under
runtime.schedule, such asruntime.lockandruntime.resetSpinninglook like scaling issues as well. That said, even the GOMAXPROCS=24 profile is spending a lot of time in the scheduler, so I suspect that this application is entering the scheduler very often.If you are willing to share a short (~1-5s) execution trace from the application that could better show the scheduling behavior. You can email me privately if you don’t want to share publicly.
@bwerthmann Thanks for the detailed profile! This seems to confirm my suspicion in https://github.com/golang/go/issues/65064#issuecomment-1887902738.
The mutex being taken appears to be https://elixir.bootlin.com/linux/v5.10.209/source/fs/eventpoll.c#L696 [1]. This is held around a loop over the ready list (https://elixir.bootlin.com/linux/v5.10.209/source/fs/eventpoll.c#L1722), which double-checks that events are still ready (
ep_item_poll) and then copies them to the userspace output buffer (__put_user). This loop exits when it reaches the end of the ready list or when it has written the max events that fit in the userspace output buffer, whichever is sooner.With a very long ready list, we’re probably hitting the 128 event limit specified by netpoll. It’s possible shrinking this could actually help by making the critical section shorter, but probably not nearly as much as reducing concurrent calls to epoll_wait (either directly, or sharding across multiple epoll FDs).
As an aside, I also see a fair amount of contention on runtime locks in your profile (probably
sched.lock), so that bottleneck would likely come up next.[1]
ep_scan_ready_listwas removed completely in 2020 in Linux v5.11 in a fairly large epoll refactor: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a825a6a0e7eb55c83c06f3c74631c2eeeb7d27f, but the lock simply moved to the caller and seems to protect a similar critical section. https://github.com/golang/go/issues/65064#issuecomment-1887729630 noted Linux v5.15, so presumably it has similar issues.These kernel patches may be of interest:
locking/osq_lock: Fix false sharing of optimistic_spin_node in osq_lock, may not be accepted yet?https://lore.kernel.org/lkml/20230615120152.20836-1-guohui@uniontech.com/
We’re running on the
golang:1.21-bullseyedocker image base which is currently using:go version go1.21.6 linux/amd64, kernel version5.15.0-91-genericon UbuntuWe don’t have a reproducer for this problem right now unfortunately, but our suspicion is that it should be easy to replicate by serving or making hundreds of thousands of fast network requests in a go application using TCP.
We don’t have a
perfprofile unfortunately, most of our discovery was done viapprofprofiles from the running binary and testing different configurations (4, then 8 containers per host).We did not try increasing the buffer size, it wasn’t apparent there was a way to do that without running a custom build of Go and at the time running more than one container was a more accessible solution for us.
Thanks for looking into this, it was definitely a interesting thing to find in the wild!