]> Cypherpunks repositories - gostls13.git/commit
runtime: prefer to restart Ps on the same M after STW
authorMichael Pratt <mpratt@google.com>
Fri, 24 Oct 2025 19:14:59 +0000 (15:14 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 13 Nov 2025 15:44:41 +0000 (07:44 -0800)
commit4ebf295b0b1740caac6302cc824ebd0f6175c1d5
treeccc6d602bcf37d7a40d144323ee340d14e830ebd
parent625d8e9b9cd7ede188a8856c5ac88791333baa63
runtime: prefer to restart Ps on the same M after STW

Today, Ps jump around arbitrarily across STW. Instead, try to keep the P
on the previous M it ran on. In the future, we'll likely want to try to
expand this beyond STW to create a more general affinity for specific
Ms.

For this to be useful, the Ps need to have runnable Gs. Today, STW
preemption goes through goschedImpl, which places the G on the global
run queue. If that was the only G then the P won't have runnable
goroutines anymore.

It makes more sense to keep the G with its P across STW anyway, so add a
special case to goschedImpl for that.

On my machine, this CL reduces the error rate in TestTraceSTW from 99.8%
to 1.9%.

As a nearly 2% error rate shows, there are still cases where this best
effort scheduling doesn't work. The most obvious is that while
procresize assigns Ps back to their original M, startTheWorldWithSema
calls wakep to start a spinning M. The spinning M may steal a goroutine
from another P if that P is too slow to start.

For #65694.

Change-Id: I6a6a636c0969c587d039b68bc68ea16c74ff1fc9
Reviewed-on: https://go-review.googlesource.com/c/go/+/714801
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
src/internal/trace/testtrace/helpers.go
src/runtime/proc.go
src/runtime/proc_test.go
src/runtime/runtime2.go
src/runtime/testdata/testprog/stw_mexit.go [new file with mode: 0644]
src/runtime/testdata/testprog/stw_trace.go [new file with mode: 0644]