github.com/aloncn/graphics-go@v0.0.1/src/runtime/debug/garbage_test.go (about) 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 5 package debug_test 6 7 import ( 8 "runtime" 9 . "runtime/debug" 10 "testing" 11 "time" 12 ) 13 14 func TestReadGCStats(t *testing.T) { 15 defer SetGCPercent(SetGCPercent(-1)) 16 17 var stats GCStats 18 var mstats runtime.MemStats 19 var min, max time.Duration 20 21 // First ReadGCStats will allocate, second should not, 22 // especially if we follow up with an explicit garbage collection. 23 stats.PauseQuantiles = make([]time.Duration, 10) 24 ReadGCStats(&stats) 25 runtime.GC() 26 27 // Assume these will return same data: no GC during ReadGCStats. 28 ReadGCStats(&stats) 29 runtime.ReadMemStats(&mstats) 30 31 if stats.NumGC != int64(mstats.NumGC) { 32 t.Errorf("stats.NumGC = %d, but mstats.NumGC = %d", stats.NumGC, mstats.NumGC) 33 } 34 if stats.PauseTotal != time.Duration(mstats.PauseTotalNs) { 35 t.Errorf("stats.PauseTotal = %d, but mstats.PauseTotalNs = %d", stats.PauseTotal, mstats.PauseTotalNs) 36 } 37 if stats.LastGC.UnixNano() != int64(mstats.LastGC) { 38 t.Errorf("stats.LastGC.UnixNano = %d, but mstats.LastGC = %d", stats.LastGC.UnixNano(), mstats.LastGC) 39 } 40 n := int(mstats.NumGC) 41 if n > len(mstats.PauseNs) { 42 n = len(mstats.PauseNs) 43 } 44 if len(stats.Pause) != n { 45 t.Errorf("len(stats.Pause) = %d, want %d", len(stats.Pause), n) 46 } else { 47 off := (int(mstats.NumGC) + len(mstats.PauseNs) - 1) % len(mstats.PauseNs) 48 for i := 0; i < n; i++ { 49 dt := stats.Pause[i] 50 if dt != time.Duration(mstats.PauseNs[off]) { 51 t.Errorf("stats.Pause[%d] = %d, want %d", i, dt, mstats.PauseNs[off]) 52 } 53 if max < dt { 54 max = dt 55 } 56 if min > dt || i == 0 { 57 min = dt 58 } 59 off = (off + len(mstats.PauseNs) - 1) % len(mstats.PauseNs) 60 } 61 } 62 63 q := stats.PauseQuantiles 64 nq := len(q) 65 if q[0] != min || q[nq-1] != max { 66 t.Errorf("stats.PauseQuantiles = [%d, ..., %d], want [%d, ..., %d]", q[0], q[nq-1], min, max) 67 } 68 69 for i := 0; i < nq-1; i++ { 70 if q[i] > q[i+1] { 71 t.Errorf("stats.PauseQuantiles[%d]=%d > stats.PauseQuantiles[%d]=%d", i, q[i], i+1, q[i+1]) 72 } 73 } 74 75 // compare memory stats with gc stats: 76 if len(stats.PauseEnd) != n { 77 t.Fatalf("len(stats.PauseEnd) = %d, want %d", len(stats.PauseEnd), n) 78 } 79 off := (int(mstats.NumGC) + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd) 80 for i := 0; i < n; i++ { 81 dt := stats.PauseEnd[i] 82 if dt.UnixNano() != int64(mstats.PauseEnd[off]) { 83 t.Errorf("stats.PauseEnd[%d] = %d, want %d", i, dt, mstats.PauseEnd[off]) 84 } 85 off = (off + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd) 86 } 87 } 88 89 var big = make([]byte, 1<<20) 90 91 func TestFreeOSMemory(t *testing.T) { 92 if runtime.GOARCH == "arm64" || runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "mips64" || runtime.GOARCH == "mips64le" || 93 runtime.GOOS == "nacl" { 94 t.Skip("issue 9993; scavenger temporarily disabled on systems with physical pages larger than logical pages") 95 } 96 var ms1, ms2 runtime.MemStats 97 98 if big == nil { 99 t.Skip("test is not reliable when run multiple times") 100 } 101 big = nil 102 runtime.GC() 103 runtime.ReadMemStats(&ms1) 104 FreeOSMemory() 105 runtime.ReadMemStats(&ms2) 106 if ms1.HeapReleased >= ms2.HeapReleased { 107 t.Errorf("released before=%d; released after=%d; did not go up", ms1.HeapReleased, ms2.HeapReleased) 108 } 109 } 110 111 func TestSetGCPercent(t *testing.T) { 112 // Test that the variable is being set and returned correctly. 113 // Assume the percentage itself is implemented fine during GC, 114 // which is harder to test. 115 old := SetGCPercent(123) 116 new := SetGCPercent(old) 117 if new != 123 { 118 t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new) 119 } 120 }