]> Cypherpunks repositories - gostls13.git/commit
runtime: replace trace seqlock with write flag
authorMichael Anthony Knyszek <mknyszek@google.com>
Wed, 19 Nov 2025 03:17:54 +0000 (03:17 +0000)
committerMichael Knyszek <mknyszek@google.com>
Fri, 21 Nov 2025 22:04:31 +0000 (14:04 -0800)
commitd68aec8db1bc3c167d2f0e5fdee8c1346ee35418
tree3faf1812974764db8e7c1941f8651f9faddb1470
parent8d9906cd34a1052868c1c0273e6f2d22632e0e84
runtime: replace trace seqlock with write flag

The runtime tracer currently uses a per-M seqlock to indicate whether a
thread is writing to a local trace buffer. The seqlock is updated with
two atomic adds, read-modify-write operations. These are quite
expensive, even though they're completely uncontended.

We can make these operations slightly cheaper by using an atomic store.
The key insight here is that only one thread ever writes to the value at
a time, so only the "write" of the read-modify-write actually matters.
At that point, it doesn't really matter that we have a monotonically
increasing counter. This is made clearer by the fact that nothing other
than basic checks make sure the counter is monotonically increasing:
everything only depends on whether the counter is even or odd.

At that point, all we really need is a flag: an atomic.Bool, which we
can update with an atomic Store, a write-only instruction.

Change-Id: I0cfe39b34c7634554c34c53c0f0e196d125bbc4a
Reviewed-on: https://go-review.googlesource.com/c/go/+/721840
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
src/runtime/trace.go
src/runtime/tracecpu.go
src/runtime/traceruntime.go