Skip to content

Commit 7955ece

Browse files
committed
runtime: add a test for asynchronous safe points
This adds a test of preempting a loop containing no synchronous safe points for STW and stack scanning. We couldn't add this test earlier because it requires scheduler, STW, and stack scanning preemption to all be working. For #10958, #24543. Change-Id: I73292db78ca3d14aab11bdafd26d03986920ef0a Reviewed-on: https://go-review.googlesource.com/c/go/+/201777 Run-TryBot: Austin Clements <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent 177a36a commit 7955ece

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/runtime/export_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ var PhysHugePageSize = physHugePageSize
4242

4343
var NetpollGenericInit = netpollGenericInit
4444

45+
const PreemptMSupported = preemptMSupported
46+
4547
type LFNode struct {
4648
Next uint64
4749
Pushcnt uintptr

src/runtime/proc_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,17 @@ func TestPreemptionGC(t *testing.T) {
356356
atomic.StoreUint32(&stop, 1)
357357
}
358358

359+
func TestAsyncPreempt(t *testing.T) {
360+
if !runtime.PreemptMSupported {
361+
t.Skip("asynchronous preemption not supported on this platform")
362+
}
363+
output := runTestProg(t, "testprog", "AsyncPreempt")
364+
want := "OK\n"
365+
if output != want {
366+
t.Fatalf("want %s, got %s\n", want, output)
367+
}
368+
}
369+
359370
func TestGCFairness(t *testing.T) {
360371
output := runTestProg(t, "testprog", "GCFairness")
361372
want := "OK\n"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"runtime"
9+
"runtime/debug"
10+
"sync/atomic"
11+
)
12+
13+
func init() {
14+
register("AsyncPreempt", AsyncPreempt)
15+
}
16+
17+
func AsyncPreempt() {
18+
// Run with just 1 GOMAXPROCS so the runtime is required to
19+
// use scheduler preemption.
20+
runtime.GOMAXPROCS(1)
21+
// Disable GC so we have complete control of what we're testing.
22+
debug.SetGCPercent(-1)
23+
24+
// Start a goroutine with no sync safe-points.
25+
var ready uint32
26+
go func() {
27+
for {
28+
atomic.StoreUint32(&ready, 1)
29+
}
30+
}()
31+
32+
// Wait for the goroutine to stop passing through sync
33+
// safe-points.
34+
for atomic.LoadUint32(&ready) == 0 {
35+
runtime.Gosched()
36+
}
37+
38+
// Run a GC, which will have to stop the goroutine for STW and
39+
// for stack scanning. If this doesn't work, the test will
40+
// deadlock and timeout.
41+
runtime.GC()
42+
43+
println("OK")
44+
}

0 commit comments

Comments
 (0)