libgo: Update to Go 1.1.1.
[official-gcc.git] / libgo / go / runtime / debug / garbage_test.go
blob149bafc6f3c106e375d6af17121e31fc8ce97563
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
7 import (
8 "runtime"
9 "testing"
10 "time"
13 func TestReadGCStats(t *testing.T) {
14 defer SetGCPercent(SetGCPercent(-1))
16 var stats GCStats
17 var mstats runtime.MemStats
18 var min, max time.Duration
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()
26 // Assume these will return same data: no GC during ReadGCStats.
27 ReadGCStats(&stats)
28 runtime.ReadMemStats(&mstats)
30 if stats.NumGC != int64(mstats.NumGC) {
31 t.Errorf("stats.NumGC = %d, but mstats.NumGC = %d", stats.NumGC, mstats.NumGC)
33 if stats.PauseTotal != time.Duration(mstats.PauseTotalNs) {
34 t.Errorf("stats.PauseTotal = %d, but mstats.PauseTotalNs = %d", stats.PauseTotal, mstats.PauseTotalNs)
36 if stats.LastGC.UnixNano() != int64(mstats.LastGC) {
37 t.Errorf("stats.LastGC.UnixNano = %d, but mstats.LastGC = %d", stats.LastGC.UnixNano(), mstats.LastGC)
39 n := int(mstats.NumGC)
40 if n > len(mstats.PauseNs) {
41 n = len(mstats.PauseNs)
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])
52 if max < dt {
53 max = dt
55 if min > dt || i == 0 {
56 min = dt
58 off = (off + len(mstats.PauseNs) - 1) % len(mstats.PauseNs)
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)
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])
75 var big = make([]byte, 1<<20)
77 func TestFreeOSMemory(t *testing.T) {
78 var ms1, ms2 runtime.MemStats
80 if big == nil {
81 t.Skip("test is not reliable when run multiple times")
83 big = nil
84 runtime.GC()
85 runtime.ReadMemStats(&ms1)
86 FreeOSMemory()
87 runtime.ReadMemStats(&ms2)
88 if ms1.HeapReleased >= ms2.HeapReleased {
89 t.Errorf("released before=%d; released after=%d; did not go up", ms1.HeapReleased, ms2.HeapReleased)
93 func TestSetGCPercent(t *testing.T) {
94 // Test that the variable is being set and returned correctly.
95 // Assume the percentage itself is implemented fine during GC,
96 // which is harder to test.
97 old := SetGCPercent(123)
98 new := SetGCPercent(old)
99 if new != 123 {
100 t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new)