Commit 88d59863f0 for golang

commit 88d59863f017d1de2a6692af08bc6b4316f7a345
Author: Damien Neil <dneil@google.com>
Date:   Tue Nov 24 11:24:45 2026 -0800

    runtime: deflake TestPingPongHog

    This test runs two pairs of goroutines, one (the hogs) using
    more CPU than the others (the lights). Each pair runs only a
    single goroutine at a time, handing off via a channel.

    The test verifies that the light goroutines switch directly
    between each other, rather than being interleaved with the hogs.

    Instead of running for a defined amount of time and measuring
    the ratio of how often hogs/lights run, run until the light
    goroutines have had several turns and examine the number of
    handoffs without a hog interrupting.

    Hopefully avoids flakiness when preemption fails to happen
    within a fixed time period.

    For #65293

    Change-Id: I3db815d3ce0267245c57824f70477a616a6a6964
    Reviewed-on: https://go-review.googlesource.com/c/go/+/868626
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: Keith Randall <khr@google.com>
    Reviewed-by: Keith Randall <khr@golang.org>

diff --git a/src/runtime/proc_test.go b/src/runtime/proc_test.go
index 3c56e4f697..1a17063695 100644
--- a/src/runtime/proc_test.go
+++ b/src/runtime/proc_test.go
@@ -456,52 +456,97 @@ func TestPingPongHog(t *testing.T) {
 	}

 	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
-	done := make(chan bool)
-	hogChan, lightChan := make(chan bool), make(chan bool)
-	hogCount, lightCount := 0, 0

-	run := func(limit int, counter *int, wake chan bool) {
+	// Run two pairs of ping-ponging goroutines on a single P: a "hog" pair
+	// that does heavy work per handoff, and a "light" pair that does light
+	// work per handoff.
+	//
+	// Because each pair hands off the P directly to its buddy
+	// (inheriting the remainder of its time slice),
+	// the light pair should execute many consecutive handoffs
+	// before sysmon preempts it back to the hog pair.
+	const (
+		wantLightRuns  = 3
+		minLightStreak = 20
+	)
+
+	type state struct {
+		id        int32
+		streak    int
+		maxStreak int
+		iters     int
+		count     int
+		wakec     chan struct{}
+	}
+	hog := &state{
+		id:    1,
+		iters: 1e6,
+		wakec: make(chan struct{}),
+	}
+	const lightID = 2
+	light := &state{
+		id:    lightID,
+		iters: 1e3,
+		wakec: make(chan struct{}),
+	}
+
+	var (
+		done      = make(chan struct{}) // closed by a hog
+		lightRuns int                   // only written by a hog
+		lastPair  atomic.Int32
+		wg        sync.WaitGroup
+	)
+
+	run := func(st *state) {
 		for {
 			select {
 			case <-done:
 				return
-
-			case <-wake:
-				for i := 0; i < limit; i++ {
-					*counter++
+			case <-st.wakec:
+				last := lastPair.Swap(st.id)
+				if last == st.id {
+					st.streak++
+					st.maxStreak = max(st.maxStreak, st.streak)
+				} else {
+					if last == lightID {
+						// Stop running after the light goroutines have had
+						// several chances at running.
+						lightRuns++
+						if lightRuns >= wantLightRuns {
+							close(done)
+							return
+						}
+					}
+					st.streak = 0
+				}
+				for range st.iters {
+					st.count++
+				}
+				select {
+				case st.wakec <- struct{}{}:
+				case <-done:
+					return
 				}
-				wake <- true
 			}
 		}
 	}

-	// Start two co-scheduled hog goroutines.
-	for i := 0; i < 2; i++ {
-		go run(1e6, &hogCount, hogChan)
+	// Start two co-scheduled hog goroutines and two co-scheduled light goroutines.
+	for range 2 {
+		wg.Go(func() { run(hog) })
+	}
+	for range 2 {
+		wg.Go(func() { run(light) })
 	}

-	// Start two co-scheduled light goroutines.
-	for i := 0; i < 2; i++ {
-		go run(1e3, &lightCount, lightChan)
-	}
-
-	// Start goroutine pairs and wait for a few preemption rounds.
-	hogChan <- true
-	lightChan <- true
-	time.Sleep(100 * time.Millisecond)
-	close(done)
-	<-hogChan
-	<-lightChan
-
-	// Check that hogCount and lightCount are within a factor of
-	// 20, which indicates that both pairs of goroutines handed off
-	// the P within a time-slice to their buddy. We can use a
-	// fairly large factor here to make this robust: if the
-	// scheduler isn't working right, the gap should be ~1000X
-	// (was 5, increased to 20, see issue 52207).
-	const factor = 20
-	if hogCount/factor > lightCount || lightCount/factor > hogCount {
-		t.Fatalf("want hogCount/lightCount in [%v, %v]; got %d/%d = %g", 1.0/factor, factor, hogCount, lightCount, float64(hogCount)/float64(lightCount))
+	// Start both goroutine pairs and wait for several preemption rounds.
+	hog.wakec <- struct{}{}
+	light.wakec <- struct{}{}
+	wg.Wait()
+
+	if light.maxStreak < minLightStreak {
+		t.Fatalf("max consecutive light handoffs = %d, want >= %d (lightruns=%d, hogCount=%d, lightCount=%d)",
+			light.maxStreak, minLightStreak, lightRuns, hog.count, light.count)
 	}
 }