| | |
| | |
| | |
| |
|
| | package debug_test |
| |
|
| | import ( |
| | "internal/testenv" |
| | "os" |
| | "runtime" |
| | . "runtime/debug" |
| | "testing" |
| | "time" |
| | ) |
| |
|
| | func TestReadGCStats(t *testing.T) { |
| | defer SetGCPercent(SetGCPercent(-1)) |
| |
|
| | var stats GCStats |
| | var mstats runtime.MemStats |
| | var minimum, maximum time.Duration |
| |
|
| | |
| | |
| | stats.PauseQuantiles = make([]time.Duration, 10) |
| | ReadGCStats(&stats) |
| | runtime.GC() |
| |
|
| | |
| | ReadGCStats(&stats) |
| | runtime.ReadMemStats(&mstats) |
| |
|
| | if stats.NumGC != int64(mstats.NumGC) { |
| | t.Errorf("stats.NumGC = %d, but mstats.NumGC = %d", stats.NumGC, mstats.NumGC) |
| | } |
| | if stats.PauseTotal != time.Duration(mstats.PauseTotalNs) { |
| | t.Errorf("stats.PauseTotal = %d, but mstats.PauseTotalNs = %d", stats.PauseTotal, mstats.PauseTotalNs) |
| | } |
| | if stats.LastGC.UnixNano() != int64(mstats.LastGC) { |
| | t.Errorf("stats.LastGC.UnixNano = %d, but mstats.LastGC = %d", stats.LastGC.UnixNano(), mstats.LastGC) |
| | } |
| | n := int(mstats.NumGC) |
| | if n > len(mstats.PauseNs) { |
| | n = len(mstats.PauseNs) |
| | } |
| | if len(stats.Pause) != n { |
| | t.Errorf("len(stats.Pause) = %d, want %d", len(stats.Pause), n) |
| | } else { |
| | off := (int(mstats.NumGC) + len(mstats.PauseNs) - 1) % len(mstats.PauseNs) |
| | for i := 0; i < n; i++ { |
| | dt := stats.Pause[i] |
| | if dt != time.Duration(mstats.PauseNs[off]) { |
| | t.Errorf("stats.Pause[%d] = %d, want %d", i, dt, mstats.PauseNs[off]) |
| | } |
| | maximum = max(maximum, dt) |
| | if i == 0 { |
| | minimum = dt |
| | } else { |
| | minimum = min(minimum, dt) |
| | } |
| | off = (off + len(mstats.PauseNs) - 1) % len(mstats.PauseNs) |
| | } |
| | } |
| |
|
| | q := stats.PauseQuantiles |
| | nq := len(q) |
| | if q[0] != minimum || q[nq-1] != maximum { |
| | t.Errorf("stats.PauseQuantiles = [%d, ..., %d], want [%d, ..., %d]", q[0], q[nq-1], minimum, maximum) |
| | } |
| |
|
| | for i := 0; i < nq-1; i++ { |
| | if q[i] > q[i+1] { |
| | t.Errorf("stats.PauseQuantiles[%d]=%d > stats.PauseQuantiles[%d]=%d", i, q[i], i+1, q[i+1]) |
| | } |
| | } |
| |
|
| | |
| | if len(stats.PauseEnd) != n { |
| | t.Fatalf("len(stats.PauseEnd) = %d, want %d", len(stats.PauseEnd), n) |
| | } |
| | off := (int(mstats.NumGC) + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd) |
| | for i := 0; i < n; i++ { |
| | dt := stats.PauseEnd[i] |
| | if dt.UnixNano() != int64(mstats.PauseEnd[off]) { |
| | t.Errorf("stats.PauseEnd[%d] = %d, want %d", i, dt.UnixNano(), mstats.PauseEnd[off]) |
| | } |
| | off = (off + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd) |
| | } |
| | } |
| |
|
| | var big []byte |
| |
|
| | func TestFreeOSMemory(t *testing.T) { |
| | |
| | |
| | |
| |
|
| | const bigBytes = 32 << 20 |
| | big = make([]byte, bigBytes) |
| |
|
| | |
| | runtime.GC() |
| |
|
| | var before runtime.MemStats |
| | runtime.ReadMemStats(&before) |
| |
|
| | |
| | |
| | big = nil |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | FreeOSMemory() |
| |
|
| | var after runtime.MemStats |
| | runtime.ReadMemStats(&after) |
| |
|
| | |
| | |
| | |
| | if after.HeapReleased <= before.HeapReleased { |
| | t.Fatalf("no memory released: %d -> %d", before.HeapReleased, after.HeapReleased) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | slack := uint64(bigBytes / 2) |
| | pageSize := uint64(os.Getpagesize()) |
| | if pageSize > 8<<10 { |
| | slack += pageSize * 2 |
| | } |
| | if slack > bigBytes { |
| | |
| | return |
| | } |
| | if after.HeapReleased-before.HeapReleased < bigBytes-slack { |
| | t.Fatalf("less than %d released: %d -> %d", bigBytes-slack, before.HeapReleased, after.HeapReleased) |
| | } |
| | } |
| |
|
| | var ( |
| | setGCPercentBallast any |
| | setGCPercentSink any |
| | ) |
| |
|
| | func TestSetGCPercent(t *testing.T) { |
| | testenv.SkipFlaky(t, 20076) |
| |
|
| | |
| | old := SetGCPercent(123) |
| | new := SetGCPercent(old) |
| | if new != 123 { |
| | t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new) |
| | } |
| |
|
| | |
| | defer func() { |
| | SetGCPercent(old) |
| | setGCPercentBallast, setGCPercentSink = nil, nil |
| | }() |
| | SetGCPercent(100) |
| | runtime.GC() |
| | |
| | const baseline = 100 << 20 |
| | var ms runtime.MemStats |
| | runtime.ReadMemStats(&ms) |
| | setGCPercentBallast = make([]byte, baseline-ms.Alloc) |
| | runtime.GC() |
| | runtime.ReadMemStats(&ms) |
| | if abs64(baseline-int64(ms.Alloc)) > 10<<20 { |
| | t.Fatalf("failed to set up baseline live heap; got %d MB, want %d MB", ms.Alloc>>20, baseline>>20) |
| | } |
| | |
| | const thresh = 20 << 20 |
| | if want := int64(2 * baseline); abs64(want-int64(ms.NextGC)) > thresh { |
| | t.Errorf("NextGC = %d MB, want %d±%d MB", ms.NextGC>>20, want>>20, thresh>>20) |
| | } |
| | |
| | for i := 0; i < int(1.2*baseline); i += 1 << 10 { |
| | setGCPercentSink = make([]byte, 1<<10) |
| | } |
| | setGCPercentSink = nil |
| | |
| | SetGCPercent(50) |
| | runtime.ReadMemStats(&ms) |
| | if want := int64(1.5 * baseline); abs64(want-int64(ms.NextGC)) > thresh { |
| | t.Errorf("NextGC = %d MB, want %d±%d MB", ms.NextGC>>20, want>>20, thresh>>20) |
| | } |
| |
|
| | |
| | SetGCPercent(100) |
| | runtime.GC() |
| | |
| | setGCPercentSink = make([]byte, int(0.2*baseline)) |
| | |
| | runtime.ReadMemStats(&ms) |
| | ngc1 := ms.NumGC |
| | SetGCPercent(10) |
| | |
| | setGCPercentSink = make([]byte, 1<<20) |
| | runtime.ReadMemStats(&ms) |
| | ngc2 := ms.NumGC |
| | if ngc1 == ngc2 { |
| | t.Errorf("expected GC to run but it did not") |
| | } |
| | } |
| |
|
| | func abs64(a int64) int64 { |
| | if a < 0 { |
| | return -a |
| | } |
| | return a |
| | } |
| |
|
| | func TestSetMaxThreadsOvf(t *testing.T) { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | nt := SetMaxThreads(1 << (30 + ^uint(0)>>63)) |
| | SetMaxThreads(nt) |
| | } |
| |
|