File size: 5,913 Bytes
de452ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !plan9 && !windows

package main

/*
#include <stdatomic.h>
#include <stddef.h>
#include <pthread.h>

extern void Ready();
extern void BlockForeverInGo();

static _Atomic int spinning;
static _Atomic int released;

static void* enterGoThenSpinTwice(void* arg __attribute__ ((unused))) {
	Ready();
	atomic_fetch_add(&spinning, 1);
	while(atomic_load(&released) == 0) {};

	Ready();
	atomic_fetch_add(&spinning, 1);
	while(1) {};
	return NULL;
}

static void SpinTwiceInNewCThread() {
	pthread_t tid;
	pthread_create(&tid, NULL, enterGoThenSpinTwice, NULL);
}

static int Spinning() {
	return atomic_load(&spinning);
}

static void Release() {
	atomic_store(&spinning, 0);
	atomic_store(&released, 1);
}

static void* enterGoThenWait(void* arg __attribute__ ((unused))) {
	BlockForeverInGo();
	return NULL;
}

static void WaitInGoInNewCThread() {
	pthread_t tid;
	pthread_create(&tid, NULL, enterGoThenWait, NULL);
}

static void SpinForever() {
	atomic_fetch_add(&spinning, 1);
	while(1) {};
}
*/
import "C"

import (
	"os"
	"runtime"
	"runtime/metrics"
	"sync/atomic"
)

func init() {
	register("NotInGoMetricCgoCall", NotInGoMetricCgoCall)
	register("NotInGoMetricCgoCallback", NotInGoMetricCgoCallback)
	register("NotInGoMetricCgoCallAndCallback", NotInGoMetricCgoCallAndCallback)
}

// NotInGoMetric just double-checks that N goroutines in cgo count as the metric reading N.
func NotInGoMetricCgoCall() {
	const N = 10

	// Spin up the same number of goroutines that will all wait in a cgo call.
	for range N {
		go func() {
			C.SpinForever()
		}()
	}

	// Make sure we're all blocked and spinning.
	for C.Spinning() < N {
	}

	// Read not-in-go before taking the Ps back.
	s := []metrics.Sample{{Name: "/sched/goroutines/not-in-go:goroutines"}}
	failed := false
	metrics.Read(s)
	if n := s[0].Value.Uint64(); n != N {
		println("pre-STW: expected", N, "not-in-go goroutines, found", n)
	}

	// Do something that stops the world to take all the Ps back.
	//
	// This will force a re-accounting of some of the goroutines and
	// re-checking not-in-go will help catch bugs.
	runtime.ReadMemStats(&m)

	// Read not-in-go.
	metrics.Read(s)
	if n := s[0].Value.Uint64(); n != N {
		println("post-STW: expected", N, "not-in-go goroutines, found", n)
	}

	// Fail if we get a bad reading.
	if failed {
		os.Exit(2)
	}
	println("OK")
}

// NotInGoMetricCgoCallback tests that threads that called into Go, then returned
// to C with *no* Go on the stack, are *not* counted as not-in-go in the
// runtime/metrics package.
func NotInGoMetricCgoCallback() {
	const N = 10

	// Create N new C threads that have called into Go at least once.
	for range N {
		C.SpinTwiceInNewCThread()
	}

	// Synchronize with spinning threads twice.
	//
	// This helps catch bad accounting by taking at least a couple other
	// codepaths which would cause the accounting to change.
	for i := range 2 {
		// Make sure they pass through Go.
		// N.B. Ready is called twice by the new threads.
		for j := range N {
			<-readyCh
			if j == 2 {
				// Try to trigger an update in the immediate STW handoff case.
				runtime.ReadMemStats(&m)
			}
		}

		// Make sure they're back in C.
		for C.Spinning() < N {
		}

		// Do something that stops the world to take all the Ps back.
		runtime.ReadMemStats(&m)

		if i == 0 {
			C.Release()
		}
	}

	// Read not-in-go.
	s := []metrics.Sample{{Name: "/sched/goroutines/not-in-go:goroutines"}}
	metrics.Read(s)
	if n := s[0].Value.Uint64(); n != 0 {
		println("expected 0 not-in-go goroutines, found", n)
		os.Exit(2)
	}
	println("OK")
}

var m runtime.MemStats
var readyCh = make(chan bool)

//export Ready
func Ready() {
	readyCh <- true
}

// NotInGoMetricCgoCallAndCallback tests that threads that called into Go are not
// keeping the count of not-in-go threads negative. Specifically, needm sets
// isExtraInC to false, breaking some of the invariants behind the not-in-go
// runtime/metrics metric, causing the underlying count to break if we don't
// account for this. In go.dev/cl/726964 this amounts to nGsyscallNoP being negative.
// Unfortunately the runtime/metrics package masks a negative nGsyscallNoP because
// it can transiently go negative due to a race. Therefore, this test checks
// the condition by making sure not-in-go is positive when we expect it to be.
// That is, threads in a cgo callback are *not* cancelling out threads in a
// regular cgo call.
func NotInGoMetricCgoCallAndCallback() {
	const N = 10

	// Spin up some threads that will do a cgo callback and just wait in Go.
	// These threads are the ones we're worried about having the incorrect
	// accounting that skews the count later.
	for range N {
		C.WaitInGoInNewCThread()
	}

	// Spin up the same number of goroutines that will all wait in a cgo call.
	for range N {
		go func() {
			C.SpinForever()
		}()
	}

	// Make sure we're all blocked and spinning.
	for C.Spinning() < N || blockedForever.Load() < N {
	}

	// Read not-in-go before taking the Ps back.
	s := []metrics.Sample{{Name: "/sched/goroutines/not-in-go:goroutines"}}
	failed := false
	metrics.Read(s)
	if n := s[0].Value.Uint64(); n != N {
		println("pre-STW: expected", N, "not-in-go goroutines, found", n)
	}

	// Do something that stops the world to take all the Ps back.
	//
	// This will force a re-accounting of some of the goroutines and
	// re-checking not-in-go will help catch bugs.
	runtime.ReadMemStats(&m)

	// Read not-in-go.
	metrics.Read(s)
	if n := s[0].Value.Uint64(); n != N {
		println("post-STW: expected", N, "not-in-go goroutines, found", n)
	}

	// Fail if we get a bad reading.
	if failed {
		os.Exit(2)
	}
	println("OK")
}

var blockedForever atomic.Uint32

//export BlockForeverInGo
func BlockForeverInGo() {
	blockedForever.Add(1)
	select {}
}