AVR: -mlra is not documeted in TEXI.
[official-gcc.git] / libgo / go / runtime / debug / garbage_test.go
blob89866063dc15197b4e11aa298d10d6fe8c69fc5a
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.
5 package debug_test
7 import (
8 "internal/testenv"
9 "os"
10 "runtime"
11 . "runtime/debug"
12 "testing"
13 "time"
16 func TestReadGCStats(t *testing.T) {
17 defer SetGCPercent(SetGCPercent(-1))
19 var stats GCStats
20 var mstats runtime.MemStats
21 var min, max time.Duration
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()
29 // Assume these will return same data: no GC during ReadGCStats.
30 ReadGCStats(&stats)
31 runtime.ReadMemStats(&mstats)
33 if stats.NumGC != int64(mstats.NumGC) {
34 t.Errorf("stats.NumGC = %d, but mstats.NumGC = %d", stats.NumGC, mstats.NumGC)
36 if stats.PauseTotal != time.Duration(mstats.PauseTotalNs) {
37 t.Errorf("stats.PauseTotal = %d, but mstats.PauseTotalNs = %d", stats.PauseTotal, mstats.PauseTotalNs)
39 if stats.LastGC.UnixNano() != int64(mstats.LastGC) {
40 t.Errorf("stats.LastGC.UnixNano = %d, but mstats.LastGC = %d", stats.LastGC.UnixNano(), mstats.LastGC)
42 n := int(mstats.NumGC)
43 if n > len(mstats.PauseNs) {
44 n = len(mstats.PauseNs)
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])
55 if max < dt {
56 max = dt
58 if min > dt || i == 0 {
59 min = dt
61 off = (off + len(mstats.PauseNs) - 1) % len(mstats.PauseNs)
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)
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])
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)
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])
87 off = (off + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd)
91 var big []byte
93 func TestFreeOSMemory(t *testing.T) {
94 if runtime.Compiler == "gccgo" {
95 t.Skip("conservative GC")
98 // Tests FreeOSMemory by making big susceptible to collection
99 // and checking that at least that much memory is returned to
100 // the OS after.
102 const bigBytes = 32 << 20
103 big = make([]byte, bigBytes)
105 // Make sure any in-progress GCs are complete.
106 runtime.GC()
108 var before runtime.MemStats
109 runtime.ReadMemStats(&before)
111 // Clear the last reference to the big allocation, making it
112 // susceptible to collection.
113 big = nil
115 // FreeOSMemory runs a GC cycle before releasing memory,
116 // so it's fine to skip a GC here.
118 // It's possible the background scavenger runs concurrently
119 // with this function and does most of the work for it.
120 // If that happens, it's OK. What we want is a test that fails
121 // often if FreeOSMemory does not work correctly, and a test
122 // that passes every time if it does.
123 FreeOSMemory()
125 var after runtime.MemStats
126 runtime.ReadMemStats(&after)
128 // Check to make sure that the big allocation (now freed)
129 // had its memory shift into HeapReleased as a result of that
130 // FreeOSMemory.
131 if after.HeapReleased <= before.HeapReleased {
132 t.Fatalf("no memory released: %d -> %d", before.HeapReleased, after.HeapReleased)
135 // Check to make sure bigBytes was released, plus some slack. Pages may get
136 // allocated in between the two measurements above for a variety for reasons,
137 // most commonly for GC work bufs. Since this can get fairly high, depending
138 // on scheduling and what GOMAXPROCS is, give a lot of slack up-front.
140 // Add a little more slack too if the page size is bigger than the runtime page size.
141 // "big" could end up unaligned on its ends, forcing the scavenger to skip at worst
142 // 2x pages.
143 slack := uint64(bigBytes / 2)
144 pageSize := uint64(os.Getpagesize())
145 if pageSize > 8<<10 {
146 slack += pageSize * 2
148 if slack > bigBytes {
149 // We basically already checked this.
150 return
152 if after.HeapReleased-before.HeapReleased < bigBytes-slack {
153 t.Fatalf("less than %d released: %d -> %d", bigBytes, before.HeapReleased, after.HeapReleased)
157 var (
158 setGCPercentBallast any
159 setGCPercentSink any
162 func TestSetGCPercent(t *testing.T) {
163 testenv.SkipFlaky(t, 20076)
165 // Test that the variable is being set and returned correctly.
166 old := SetGCPercent(123)
167 new := SetGCPercent(old)
168 if new != 123 {
169 t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new)
172 // Test that the percentage is implemented correctly.
173 defer func() {
174 SetGCPercent(old)
175 setGCPercentBallast, setGCPercentSink = nil, nil
177 SetGCPercent(100)
178 runtime.GC()
179 // Create 100 MB of live heap as a baseline.
180 const baseline = 100 << 20
181 var ms runtime.MemStats
182 runtime.ReadMemStats(&ms)
183 setGCPercentBallast = make([]byte, baseline-ms.Alloc)
184 runtime.GC()
185 runtime.ReadMemStats(&ms)
186 if abs64(baseline-int64(ms.Alloc)) > 10<<20 {
187 t.Fatalf("failed to set up baseline live heap; got %d MB, want %d MB", ms.Alloc>>20, baseline>>20)
189 // NextGC should be ~200 MB.
190 const thresh = 20 << 20 // TODO: Figure out why this is so noisy on some builders
191 if want := int64(2 * baseline); abs64(want-int64(ms.NextGC)) > thresh {
192 t.Errorf("NextGC = %d MB, want %d±%d MB", ms.NextGC>>20, want>>20, thresh>>20)
194 // Create some garbage, but not enough to trigger another GC.
195 for i := 0; float64(i) < 1.2*baseline; i += 1 << 10 {
196 setGCPercentSink = make([]byte, 1<<10)
198 setGCPercentSink = nil
199 // Adjust GOGC to 50. NextGC should be ~150 MB.
200 SetGCPercent(50)
201 runtime.ReadMemStats(&ms)
202 if want := int64(1.5 * baseline); abs64(want-int64(ms.NextGC)) > thresh {
203 t.Errorf("NextGC = %d MB, want %d±%d MB", ms.NextGC>>20, want>>20, thresh>>20)
206 // Trigger a GC and get back to 100 MB live with GOGC=100.
207 SetGCPercent(100)
208 runtime.GC()
209 // Raise live to 120 MB.
210 setGCPercentSink = make([]byte, int(0.2*baseline))
211 // Lower GOGC to 10. This must force a GC.
212 runtime.ReadMemStats(&ms)
213 ngc1 := ms.NumGC
214 SetGCPercent(10)
215 // It may require an allocation to actually force the GC.
216 setGCPercentSink = make([]byte, 1<<20)
217 runtime.ReadMemStats(&ms)
218 ngc2 := ms.NumGC
219 if ngc1 == ngc2 {
220 t.Errorf("expected GC to run but it did not")
224 func abs64(a int64) int64 {
225 if a < 0 {
226 return -a
228 return a
231 func TestSetMaxThreadsOvf(t *testing.T) {
232 // Verify that a big threads count will not overflow the int32
233 // maxmcount variable, causing a panic (see Issue 16076).
235 // This can only happen when ints are 64 bits, since on platforms
236 // with 32 bit ints SetMaxThreads (which takes an int parameter)
237 // cannot be given anything that will overflow an int32.
239 // Call SetMaxThreads with 1<<31, but only on 64 bit systems.
240 nt := SetMaxThreads(1 << (30 + ^uint(0)>>63))
241 SetMaxThreads(nt) // restore previous value