Home | History | Annotate | Download | only in debug
      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.UnixNano(), 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 	var ms1, ms2 runtime.MemStats
     93 
     94 	if big == nil {
     95 		t.Skip("test is not reliable when run multiple times")
     96 	}
     97 	big = nil
     98 	runtime.GC()
     99 	runtime.ReadMemStats(&ms1)
    100 	FreeOSMemory()
    101 	runtime.ReadMemStats(&ms2)
    102 	if ms1.HeapReleased >= ms2.HeapReleased {
    103 		t.Errorf("released before=%d; released after=%d; did not go up", ms1.HeapReleased, ms2.HeapReleased)
    104 	}
    105 }
    106 
    107 func TestSetGCPercent(t *testing.T) {
    108 	// Test that the variable is being set and returned correctly.
    109 	// Assume the percentage itself is implemented fine during GC,
    110 	// which is harder to test.
    111 	old := SetGCPercent(123)
    112 	new := SetGCPercent(old)
    113 	if new != 123 {
    114 		t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new)
    115 	}
    116 }
    117 
    118 func TestSetMaxThreadsOvf(t *testing.T) {
    119 	// Verify that a big threads count will not overflow the int32
    120 	// maxmcount variable, causing a panic (see Issue 16076).
    121 	//
    122 	// This can only happen when ints are 64 bits, since on platforms
    123 	// with 32 bit ints SetMaxThreads (which takes an int parameter)
    124 	// cannot be given anything that will overflow an int32.
    125 	//
    126 	// Call SetMaxThreads with 1<<31, but only on 64 bit systems.
    127 	nt := SetMaxThreads(1 << (30 + ^uint(0)>>63))
    128 	SetMaxThreads(nt) // restore previous value
    129 }
    130