Skip to content

Commit 6ac87aa

Browse files
rhyshodeke-em
authored andcommitted
runtime: measure speed of procyield and osyield
These are delay primitives for lock2. If a mutex isn't immediately available, we can use procyield to tell the processor to wait for a moment, or osyield to allow the OS to run a different process or thread if one is waiting. We expect a processor-level yield to be faster than an os-level yield, and for both of them to be fast relative to entering a full sleep (via futexsleep or semasleep). Each architecture has its own way of hinting to the processor that it's in a spin-wait loop, so procyield presents an architecture-independent interface for use in lock_futex.go and lock_sema.go. Measure the (single-threaded) speed of these to confirm. For #68578 Change-Id: I90cd46ea553f2990395aceb048206285558c877e Reviewed-on: https://go-review.googlesource.com/c/go/+/601396 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent bd85a3b commit 6ac87aa

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/runtime/export_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,9 @@ func PageCachePagesLeaked() (leaked uintptr) {
12231223
return
12241224
}
12251225

1226+
var ProcYield = procyield
1227+
var OSYield = osyield
1228+
12261229
type Mutex = mutex
12271230

12281231
var Lock = lock

src/runtime/runtime_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,25 @@ func TestTimediv(t *testing.T) {
539539
})
540540
}
541541
}
542+
543+
func BenchmarkProcYield(b *testing.B) {
544+
benchN := func(n uint32) func(*testing.B) {
545+
return func(b *testing.B) {
546+
for i := 0; i < b.N; i++ {
547+
ProcYield(n)
548+
}
549+
}
550+
}
551+
552+
b.Run("1", benchN(1))
553+
b.Run("10", benchN(10))
554+
b.Run("30", benchN(30)) // active_spin_cnt in lock_sema.go and lock_futex.go
555+
b.Run("100", benchN(100))
556+
b.Run("1000", benchN(1000))
557+
}
558+
559+
func BenchmarkOSYield(b *testing.B) {
560+
for i := 0; i < b.N; i++ {
561+
OSYield()
562+
}
563+
}

0 commit comments

Comments
 (0)