]> git.feebdaed.xyz Git - 0xmirror/go.git/commit
runtime: don't count nGsyscallNoP for extra Ms in C
authorMichael Anthony Knyszek <mknyszek@google.com>
Thu, 4 Dec 2025 23:27:03 +0000 (23:27 +0000)
committerMichael Knyszek <mknyszek@google.com>
Fri, 5 Dec 2025 21:48:24 +0000 (13:48 -0800)
commit745349712e837ef77eb7b5a21c4d4e5c7ca0371a
treeeaa0623b145ef591251f6a174b2140ef5cfbe170
parentf3d572d96a25d1a0956ef828c0ff510ebf214d22
runtime: don't count nGsyscallNoP for extra Ms in C

In #76435, it turns out that the new metric
/sched/goroutines/not-in-go:goroutines counts C threads that have called
into Go before (on Linux) as not-in-go goroutines. The reason for this
is that the M is still attached to the C thread on Linux as an
optimization, so we don't go through all the trouble of detaching the M
and, of course, decrementing nGsyscallNoP.

There's an easy fix to this accounting issue. The flag on the M,
isExtraInC, says whether a thread with an extra M attached no longer has
any Go on its (logical) stack. When we take the P from an M in this
state, we simply just don't increment nGsyscallNoP. When it calls back
into Go, we similarly skip the decrement to nGsyscallNoP.

This is more efficient than alternatives, like always updating
nGsyscallNoP in cgocallbackg, since that would add a new
read-modify-write atomic onto that fast path. It does mean we count
threads in C with a P still attached as not-in-go, but this transient in
most real programs, assuming the thread indeed does not call back into
Go any time soon.

Fixes #76435.

Change-Id: Id05563bacbe35d3fae17d67fb5ed45fa43fa0548
Reviewed-on: https://go-review.googlesource.com/c/go/+/726964
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/runtime/metrics_test.go
src/runtime/proc.go
src/runtime/runtime2.go
src/runtime/testdata/testprogcgo/notingo.go [new file with mode: 0644]