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