1
2
3
4
5 package debug_test
6
7 import (
8 "internal/testenv"
9 "os"
10 "runtime"
11 . "runtime/debug"
12 "testing"
13 "time"
14 )
15
16 func 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
24
25 stats.PauseQuantiles = make([]time.Duration, 10)
26 ReadGCStats(&stats)
27 runtime.GC()
28
29
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
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
91 var big []byte
92
93 func TestFreeOSMemory(t *testing.T) {
94
95
96
97
98 const bigBytes = 32 << 20
99 big = make([]byte, bigBytes)
100
101
102 runtime.GC()
103
104 var before runtime.MemStats
105 runtime.ReadMemStats(&before)
106
107
108
109 big = nil
110
111
112
113
114
115
116
117
118
119 FreeOSMemory()
120
121 var after runtime.MemStats
122 runtime.ReadMemStats(&after)
123
124
125
126
127 if after.HeapReleased <= before.HeapReleased {
128 t.Fatalf("no memory released: %d -> %d", before.HeapReleased, after.HeapReleased)
129 }
130
131
132
133
134
135
136
137
138
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
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
153 var (
154 setGCPercentBallast any
155 setGCPercentSink any
156 )
157
158 func TestSetGCPercent(t *testing.T) {
159 testenv.SkipFlaky(t, 20076)
160
161
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
169 defer func() {
170 SetGCPercent(old)
171 setGCPercentBallast, setGCPercentSink = nil, nil
172 }()
173 SetGCPercent(100)
174 runtime.GC()
175
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
186 const thresh = 20 << 20
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
191 for i := 0; i < int(1.2*baseline); i += 1 << 10 {
192 setGCPercentSink = make([]byte, 1<<10)
193 }
194 setGCPercentSink = nil
195
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
203 SetGCPercent(100)
204 runtime.GC()
205
206 setGCPercentSink = make([]byte, int(0.2*baseline))
207
208 runtime.ReadMemStats(&ms)
209 ngc1 := ms.NumGC
210 SetGCPercent(10)
211
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
220 func abs64(a int64) int64 {
221 if a < 0 {
222 return -a
223 }
224 return a
225 }
226
227 func TestSetMaxThreadsOvf(t *testing.T) {
228
229
230
231
232
233
234
235
236 nt := SetMaxThreads(1 << (30 + ^uint(0)>>63))
237 SetMaxThreads(nt)
238 }
239
View as plain text