go: runtime: Simple HTTP server causes high thread count on macOS Monterey
What version of Go are you using (go version)?
$ go version go version go1.17.2 darwin/arm64
Does this issue reproduce with the latest release?
Cannot test 1.17.3 currently.
What operating system and processor architecture are you using (go env)?
Apple M1 Max MacBook Pro 16.
go env Output
$ go env GO111MODULE="" GOARCH="arm64" GOBIN="" GOCACHE="/Users/XXX/Library/Caches/go-build" GOENV="/Users/XXX/Library/Application Support/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="arm64" GOHOSTOS="darwin" GOINSECURE="" GOMODCACHE="/Users/XXX/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/XXX/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/opt/homebrew/Cellar/go/1.17.2/libexec" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/opt/homebrew/Cellar/go/1.17.2/libexec/pkg/tool/darwin_arm64" GOVCS="" GOVERSION="go1.17.2" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/dev/null" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/06/1d1vrn6541133ymsn2s2vwvw0000gn/T/go-build3566489552=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
- Spawn a simple http server
package main
import "net/http"
func main() {
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(200)
})
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
- Run some requests against it
wrk -c500 -d10 -t1 http://localhost:8080
What did you expect to see?
With the previous macOS versions (as with every other OS I’ve tested lately) the app uses a couple of threads (usually around 8-12 all the time).
What did you see instead?
Thread count goes very high and never drops:

About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 31 (13 by maintainers)
After the release 1.18 I did some more tests and found some interesting observations that I would like to share, maybe it helps.
The M1 Max (Pro) has 8 performance cores and 2 efficiency cores. When I limit the processor count using GOMAXPROCS, the behaviour changes completely.
If I set a value between 1-4 (GOMAXPROCS=1 … GOMAXPROCS=4), the problem disappears. The problem suddenly starts with GOMAXPROCS=5+. Also, with a value between 1-4 the performance is superb, as usual and expected. The numbers mean nothing, but the delta is interesting:
With 1-4 I get 175K req/s with wrk and the thread count stays under 10. With 5+ I get “only” 60-75K req/s and the thread count goes wild, 300+.
I am not too much into how Go’s scheduler works exactly but I do know that slow syscalls might cause other threads to spawn to take over. Now, maybe the efficiency cores are too slow to take over and Go thinks the syscall takes too long and therefore simply spawns another thread?
If this is true, even modern Alder Lake CPUs with Big.Little might have similar issues. Again, it’s a guess, but I have the feeling that it is related to this architecture design.
Let me know your thoughts.
@odeke-em @ianlancetaylor
To be clear, this is a well-known (to me at least 😃) issue with the Go scheduler. If there is lots to do, the Go scheduler does well keeping busy. If there are long periods of nothing to do, things go to sleep and it works well. However, if the application switches between active and idle very rapidly it behaves poorly due to the relatively high cost of going to sleep and waking back up. This is one of the cases that #54622 aims to improve.
The immediate cause appears to be as I theorized, that
wrkis not providing enough load to keep the server awake.I found the
wrksource a bit difficult to follow, but I believe it only sends per request at a time per thread.The general flow that I believe is happening:
wrkhas 4 worker threads (-t4). Go allows up to 8 worker threads (GOMAXPROCS=8on my machine). In practice only 4 will run at a time.wrksends a request.wrkreceives the response and sends another request.With 4 worker threads, I see ~40k wakeups. If I give
wrk8 worker threads, it should be able to keep Go busier, and indeed that drops the wakeups to ~9k. If I givewrk16 worker threads, it drops further to ~3k wakeups.I can also see in CPU profiles that time in the Go scheduler drops from 16% -> 5% -> 2%.
The final 3k number is still pretty high. I’m not sure why, it needs more investigation. It might just be that Go still ends up with no work due to OS scheduling effects sometimes not running enough
wrkthreads (we have more threads than CPUs now).I’m also not sure why tokio is doing better by default. It may be running fewer worker threads, or it may be spinning longer waiting for work before going to sleep.
We have been thinking for a long time about ways to eliminate GOMAXPROCS entirely and dynamically determine the amount of parallelism currently available in order to dynamically adjust. This problem sort of fits in that domain, but it is backwards: the machine has plenty of available parallelism to run one thread per request, but the request handler is so fast that it would be more efficient to just run one thread and queue the requests behind one another (this is what happens with GOMAXPROCS=1). However, the Go scheduler today is work conserving (#54622), meaning if there are idle CPUs and runnable work, we always use the idle CPUs. Determining in the scheduler that it is better to break this property because the cost of wake+sleep is higher than the latency of waiting to run sequential is difficult to determine a priori.
Can you collect a CPU profile of this server while it is under the load test? My initial guess would be that something about the load makes it such that runtime threads are constantly going to sleep and waking back up rather than just staying awake like they should under high load.
Just tested lately with latest 1.19 and up-to-date M1 Pro device, same result. The simple HTTP server listed above still generates hundreds of threads on macOS.
I’ve already mentioned that only a value of GOMAXPROCS=5+ causes issues. An M1 Pro/Max has 8 performance cores and 2 efficiency cores. A value of 4 works perfectly, so half of the performance cores. Not sure, maybe it’s linked.
I really believe that the efficiency cores are the problem, maybe they take so much longer compared to high performance cores for certain operations that Go thinks it has to spawn a new thread to keep up with the load. Could this be related?
@davecheney It’s a stock device used in our lab. Based on the feedback it really sounds like an M1 Max issue.
Interesting, thank you @chrisprobst! Let me kindly ask a colleague @kirbyquerby who has an M1 to try to reproduce it, and then I shall go purchase an M1 Pro or M1 Max computer tomorrow morning too and try to reproduce it then debug it.