1// Copyright 2013 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
5package debug_test
6
7import (
8	"internal/testenv"
9	"os"
10	"runtime"
11	. "runtime/debug"
12	"testing"
13	"time"
14)
15
16func TestReadGCStats(t *testing.T) {
17	defer SetGCPercent(SetGCPercent(-1))
18
19	var stats GCStats
20	var mstats runtime.MemStats
21	var min, max time.Duration
22
23	// First ReadGCStats will allocate, second should not,
24	// especially if we follow up with an explicit garbage collection.
25	stats.PauseQuantiles = make([]time.Duration, 10)
26	ReadGCStats(&stats)
27	runtime.GC()
28
29	// Assume these will return same data: no GC during ReadGCStats.
30	ReadGCStats(&stats)
31	runtime.ReadMemStats(&mstats)
32
33	if stats.NumGC != int64(mstats.NumGC) {
34		t.Errorf("stats.NumGC = %d, but mstats.NumGC = %d", stats.NumGC, mstats.NumGC)
35	}
36	if stats.PauseTotal != time.Duration(mstats.PauseTotalNs) {
37		t.Errorf("stats.PauseTotal = %d, but mstats.PauseTotalNs = %d", stats.PauseTotal, mstats.PauseTotalNs)
38	}
39	if stats.LastGC.UnixNano() != int64(mstats.LastGC) {
40		t.Errorf("stats.LastGC.UnixNano = %d, but mstats.LastGC = %d", stats.LastGC.UnixNano(), mstats.LastGC)
41	}
42	n := int(mstats.NumGC)
43	if n > len(mstats.PauseNs) {
44		n = len(mstats.PauseNs)
45	}
46	if len(stats.Pause) != n {
47		t.Errorf("len(stats.Pause) = %d, want %d", len(stats.Pause), n)
48	} else {
49		off := (int(mstats.NumGC) + len(mstats.PauseNs) - 1) % len(mstats.PauseNs)
50		for i := 0; i < n; i++ {
51			dt := stats.Pause[i]
52			if dt != time.Duration(mstats.PauseNs[off]) {
53				t.Errorf("stats.Pause[%d] = %d, want %d", i, dt, mstats.PauseNs[off])
54			}
55			if max < dt {
56				max = dt
57			}
58			if min > dt || i == 0 {
59				min = dt
60			}
61			off = (off + len(mstats.PauseNs) - 1) % len(mstats.PauseNs)
62		}
63	}
64
65	q := stats.PauseQuantiles
66	nq := len(q)
67	if q[0] != min || q[nq-1] != max {
68		t.Errorf("stats.PauseQuantiles = [%d, ..., %d], want [%d, ..., %d]", q[0], q[nq-1], min, max)
69	}
70
71	for i := 0; i < nq-1; i++ {
72		if q[i] > q[i+1] {
73			t.Errorf("stats.PauseQuantiles[%d]=%d > stats.PauseQuantiles[%d]=%d", i, q[i], i+1, q[i+1])
74		}
75	}
76
77	// compare memory stats with gc stats:
78	if len(stats.PauseEnd) != n {
79		t.Fatalf("len(stats.PauseEnd) = %d, want %d", len(stats.PauseEnd), n)
80	}
81	off := (int(mstats.NumGC) + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd)
82	for i := 0; i < n; i++ {
83		dt := stats.PauseEnd[i]
84		if dt.UnixNano() != int64(mstats.PauseEnd[off]) {
85			t.Errorf("stats.PauseEnd[%d] = %d, want %d", i, dt.UnixNano(), mstats.PauseEnd[off])
86		}
87		off = (off + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd)
88	}
89}
90
91var big []byte
92
93func TestFreeOSMemory(t *testing.T) {
94	// Tests FreeOSMemory by making big susceptible to collection
95	// and checking that at least that much memory is returned to
96	// the OS after.
97
98	const bigBytes = 32 << 20
99	big = make([]byte, bigBytes)
100
101	// Make sure any in-progress GCs are complete.
102	runtime.GC()
103
104	var before runtime.MemStats
105	runtime.ReadMemStats(&before)
106
107	// Clear the last reference to the big allocation, making it
108	// susceptible to collection.
109	big = nil
110
111	// FreeOSMemory runs a GC cycle before releasing memory,
112	// so it's fine to skip a GC here.
113	//
114	// It's possible the background scavenger runs concurrently
115	// with this function and does most of the work for it.
116	// If that happens, it's OK. What we want is a test that fails
117	// often if FreeOSMemory does not work correctly, and a test
118	// that passes every time if it does.
119	FreeOSMemory()
120
121	var after runtime.MemStats
122	runtime.ReadMemStats(&after)
123
124	// Check to make sure that the big allocation (now freed)
125	// had its memory shift into HeapReleased as a result of that
126	// FreeOSMemory.
127	if after.HeapReleased <= before.HeapReleased {
128		t.Fatalf("no memory released: %d -> %d", before.HeapReleased, after.HeapReleased)
129	}
130
131	// Check to make sure bigBytes was released, plus some slack. Pages may get
132	// allocated in between the two measurements above for a variety for reasons,
133	// most commonly for GC work bufs. Since this can get fairly high, depending
134	// on scheduling and what GOMAXPROCS is, give a lot of slack up-front.
135	//
136	// Add a little more slack too if the page size is bigger than the runtime page size.
137	// "big" could end up unaligned on its ends, forcing the scavenger to skip at worst
138	// 2x pages.
139	slack := uint64(bigBytes / 2)
140	pageSize := uint64(os.Getpagesize())
141	if pageSize > 8<<10 {
142		slack += pageSize * 2
143	}
144	if slack > bigBytes {
145		// We basically already checked this.
146		return
147	}
148	if after.HeapReleased-before.HeapReleased < bigBytes-slack {
149		t.Fatalf("less than %d released: %d -> %d", bigBytes-slack, before.HeapReleased, after.HeapReleased)
150	}
151}
152
153var (
154	setGCPercentBallast any
155	setGCPercentSink    any
156)
157
158func TestSetGCPercent(t *testing.T) {
159	testenv.SkipFlaky(t, 20076)
160
161	// Test that the variable is being set and returned correctly.
162	old := SetGCPercent(123)
163	new := SetGCPercent(old)
164	if new != 123 {
165		t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new)
166	}
167
168	// Test that the percentage is implemented correctly.
169	defer func() {
170		SetGCPercent(old)
171		setGCPercentBallast, setGCPercentSink = nil, nil
172	}()
173	SetGCPercent(100)
174	runtime.GC()
175	// Create 100 MB of live heap as a baseline.
176	const baseline = 100 << 20
177	var ms runtime.MemStats
178	runtime.ReadMemStats(&ms)
179	setGCPercentBallast = make([]byte, baseline-ms.Alloc)
180	runtime.GC()
181	runtime.ReadMemStats(&ms)
182	if abs64(baseline-int64(ms.Alloc)) > 10<<20 {
183		t.Fatalf("failed to set up baseline live heap; got %d MB, want %d MB", ms.Alloc>>20, baseline>>20)
184	}
185	// NextGC should be ~200 MB.
186	const thresh = 20 << 20 // TODO: Figure out why this is so noisy on some builders
187	if want := int64(2 * baseline); abs64(want-int64(ms.NextGC)) > thresh {
188		t.Errorf("NextGC = %d MB, want %d±%d MB", ms.NextGC>>20, want>>20, thresh>>20)
189	}
190	// Create some garbage, but not enough to trigger another GC.
191	for i := 0; i < int(1.2*baseline); i += 1 << 10 {
192		setGCPercentSink = make([]byte, 1<<10)
193	}
194	setGCPercentSink = nil
195	// Adjust GOGC to 50. NextGC should be ~150 MB.
196	SetGCPercent(50)
197	runtime.ReadMemStats(&ms)
198	if want := int64(1.5 * baseline); abs64(want-int64(ms.NextGC)) > thresh {
199		t.Errorf("NextGC = %d MB, want %d±%d MB", ms.NextGC>>20, want>>20, thresh>>20)
200	}
201
202	// Trigger a GC and get back to 100 MB live with GOGC=100.
203	SetGCPercent(100)
204	runtime.GC()
205	// Raise live to 120 MB.
206	setGCPercentSink = make([]byte, int(0.2*baseline))
207	// Lower GOGC to 10. This must force a GC.
208	runtime.ReadMemStats(&ms)
209	ngc1 := ms.NumGC
210	SetGCPercent(10)
211	// It may require an allocation to actually force the GC.
212	setGCPercentSink = make([]byte, 1<<20)
213	runtime.ReadMemStats(&ms)
214	ngc2 := ms.NumGC
215	if ngc1 == ngc2 {
216		t.Errorf("expected GC to run but it did not")
217	}
218}
219
220func abs64(a int64) int64 {
221	if a < 0 {
222		return -a
223	}
224	return a
225}
226
227func TestSetMaxThreadsOvf(t *testing.T) {
228	// Verify that a big threads count will not overflow the int32
229	// maxmcount variable, causing a panic (see Issue 16076).
230	//
231	// This can only happen when ints are 64 bits, since on platforms
232	// with 32 bit ints SetMaxThreads (which takes an int parameter)
233	// cannot be given anything that will overflow an int32.
234	//
235	// Call SetMaxThreads with 1<<31, but only on 64 bit systems.
236	nt := SetMaxThreads(1 << (30 + ^uint(0)>>63))
237	SetMaxThreads(nt) // restore previous value
238}
239