diff options
Diffstat (limited to 'src/pkg/runtime/debug')
-rw-r--r-- | src/pkg/runtime/debug/debug.c | 9 | ||||
-rw-r--r-- | src/pkg/runtime/debug/garbage.go | 153 | ||||
-rw-r--r-- | src/pkg/runtime/debug/garbage_test.go | 102 | ||||
-rw-r--r-- | src/pkg/runtime/debug/heapdump_test.go | 33 | ||||
-rw-r--r-- | src/pkg/runtime/debug/stack.go | 98 | ||||
-rw-r--r-- | src/pkg/runtime/debug/stack_test.go | 62 |
6 files changed, 0 insertions, 457 deletions
diff --git a/src/pkg/runtime/debug/debug.c b/src/pkg/runtime/debug/debug.c deleted file mode 100644 index a7292c477..000000000 --- a/src/pkg/runtime/debug/debug.c +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Nothing to see here. -// This file exists so that the go command knows that parts of the -// package are implemented in C, so that it does not instruct the -// Go compiler to complain about extern declarations. -// The actual implementations are in package runtime. diff --git a/src/pkg/runtime/debug/garbage.go b/src/pkg/runtime/debug/garbage.go deleted file mode 100644 index edb364387..000000000 --- a/src/pkg/runtime/debug/garbage.go +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package debug - -import ( - "runtime" - "sort" - "time" -) - -// GCStats collect information about recent garbage collections. -type GCStats struct { - LastGC time.Time // time of last collection - NumGC int64 // number of garbage collections - PauseTotal time.Duration // total pause for all collections - Pause []time.Duration // pause history, most recent first - PauseQuantiles []time.Duration -} - -// Implemented in package runtime. -func readGCStats(*[]time.Duration) -func enableGC(bool) bool -func setGCPercent(int) int -func freeOSMemory() -func setMaxStack(int) int -func setMaxThreads(int) int - -// ReadGCStats reads statistics about garbage collection into stats. -// The number of entries in the pause history is system-dependent; -// stats.Pause slice will be reused if large enough, reallocated otherwise. -// ReadGCStats may use the full capacity of the stats.Pause slice. -// If stats.PauseQuantiles is non-empty, ReadGCStats fills it with quantiles -// summarizing the distribution of pause time. For example, if -// len(stats.PauseQuantiles) is 5, it will be filled with the minimum, -// 25%, 50%, 75%, and maximum pause times. -func ReadGCStats(stats *GCStats) { - // Create a buffer with space for at least two copies of the - // pause history tracked by the runtime. One will be returned - // to the caller and the other will be used as a temporary buffer - // for computing quantiles. - const maxPause = len(((*runtime.MemStats)(nil)).PauseNs) - if cap(stats.Pause) < 2*maxPause { - stats.Pause = make([]time.Duration, 2*maxPause) - } - - // readGCStats fills in the pause history (up to maxPause entries) - // and then three more: Unix ns time of last GC, number of GC, - // and total pause time in nanoseconds. Here we depend on the - // fact that time.Duration's native unit is nanoseconds, so the - // pauses and the total pause time do not need any conversion. - readGCStats(&stats.Pause) - n := len(stats.Pause) - 3 - stats.LastGC = time.Unix(0, int64(stats.Pause[n])) - stats.NumGC = int64(stats.Pause[n+1]) - stats.PauseTotal = stats.Pause[n+2] - stats.Pause = stats.Pause[:n] - - if len(stats.PauseQuantiles) > 0 { - if n == 0 { - for i := range stats.PauseQuantiles { - stats.PauseQuantiles[i] = 0 - } - } else { - // There's room for a second copy of the data in stats.Pause. - // See the allocation at the top of the function. - sorted := stats.Pause[n : n+n] - copy(sorted, stats.Pause) - sort.Sort(byDuration(sorted)) - nq := len(stats.PauseQuantiles) - 1 - for i := 0; i < nq; i++ { - stats.PauseQuantiles[i] = sorted[len(sorted)*i/nq] - } - stats.PauseQuantiles[nq] = sorted[len(sorted)-1] - } - } -} - -type byDuration []time.Duration - -func (x byDuration) Len() int { return len(x) } -func (x byDuration) Swap(i, j int) { x[i], x[j] = x[j], x[i] } -func (x byDuration) Less(i, j int) bool { return x[i] < x[j] } - -// SetGCPercent sets the garbage collection target percentage: -// a collection is triggered when the ratio of freshly allocated data -// to live data remaining after the previous collection reaches this percentage. -// SetGCPercent returns the previous setting. -// The initial setting is the value of the GOGC environment variable -// at startup, or 100 if the variable is not set. -// A negative percentage disables garbage collection. -func SetGCPercent(percent int) int { - old := setGCPercent(percent) - runtime.GC() - return old -} - -// FreeOSMemory forces a garbage collection followed by an -// attempt to return as much memory to the operating system -// as possible. (Even if this is not called, the runtime gradually -// returns memory to the operating system in a background task.) -func FreeOSMemory() { - freeOSMemory() -} - -// SetMaxStack sets the maximum amount of memory that -// can be used by a single goroutine stack. -// If any goroutine exceeds this limit while growing its stack, -// the program crashes. -// SetMaxStack returns the previous setting. -// The initial setting is 1 GB on 64-bit systems, 250 MB on 32-bit systems. -// -// SetMaxStack is useful mainly for limiting the damage done by -// goroutines that enter an infinite recursion. It only limits future -// stack growth. -func SetMaxStack(bytes int) int { - return setMaxStack(bytes) -} - -// SetMaxThreads sets the maximum number of operating system -// threads that the Go program can use. If it attempts to use more than -// this many, the program crashes. -// SetMaxThreads returns the previous setting. -// The initial setting is 10,000 threads. -// -// The limit controls the number of operating system threads, not the number -// of goroutines. A Go program creates a new thread only when a goroutine -// is ready to run but all the existing threads are blocked in system calls, cgo calls, -// or are locked to other goroutines due to use of runtime.LockOSThread. -// -// SetMaxThreads is useful mainly for limiting the damage done by -// programs that create an unbounded number of threads. The idea is -// to take down the program before it takes down the operating system. -func SetMaxThreads(threads int) int { - return setMaxThreads(threads) -} - -// SetPanicOnFault controls the runtime's behavior when a program faults -// at an unexpected (non-nil) address. Such faults are typically caused by -// bugs such as runtime memory corruption, so the default response is to crash -// the program. Programs working with memory-mapped files or unsafe -// manipulation of memory may cause faults at non-nil addresses in less -// dramatic situations; SetPanicOnFault allows such programs to request -// that the runtime trigger only a panic, not a crash. -// SetPanicOnFault applies only to the current goroutine. -// It returns the previous setting. -func SetPanicOnFault(enabled bool) bool - -// WriteHeapDump writes a description of the heap and the objects in -// it to the given file descriptor. -// The heap dump format is defined at http://golang.org/s/go13heapdump. -func WriteHeapDump(fd uintptr) diff --git a/src/pkg/runtime/debug/garbage_test.go b/src/pkg/runtime/debug/garbage_test.go deleted file mode 100644 index 149bafc6f..000000000 --- a/src/pkg/runtime/debug/garbage_test.go +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package debug - -import ( - "runtime" - "testing" - "time" -) - -func TestReadGCStats(t *testing.T) { - defer SetGCPercent(SetGCPercent(-1)) - - var stats GCStats - var mstats runtime.MemStats - var min, max time.Duration - - // First ReadGCStats will allocate, second should not, - // especially if we follow up with an explicit garbage collection. - stats.PauseQuantiles = make([]time.Duration, 10) - ReadGCStats(&stats) - runtime.GC() - - // Assume these will return same data: no GC during ReadGCStats. - 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]) - } - if max < dt { - max = dt - } - if min > dt || i == 0 { - min = dt - } - off = (off + len(mstats.PauseNs) - 1) % len(mstats.PauseNs) - } - } - - q := stats.PauseQuantiles - nq := len(q) - if q[0] != min || q[nq-1] != max { - t.Errorf("stats.PauseQuantiles = [%d, ..., %d], want [%d, ..., %d]", q[0], q[nq-1], min, max) - } - - 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]) - } - } -} - -var big = make([]byte, 1<<20) - -func TestFreeOSMemory(t *testing.T) { - var ms1, ms2 runtime.MemStats - - if big == nil { - t.Skip("test is not reliable when run multiple times") - } - big = nil - runtime.GC() - runtime.ReadMemStats(&ms1) - FreeOSMemory() - runtime.ReadMemStats(&ms2) - if ms1.HeapReleased >= ms2.HeapReleased { - t.Errorf("released before=%d; released after=%d; did not go up", ms1.HeapReleased, ms2.HeapReleased) - } -} - -func TestSetGCPercent(t *testing.T) { - // Test that the variable is being set and returned correctly. - // Assume the percentage itself is implemented fine during GC, - // which is harder to test. - old := SetGCPercent(123) - new := SetGCPercent(old) - if new != 123 { - t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new) - } -} diff --git a/src/pkg/runtime/debug/heapdump_test.go b/src/pkg/runtime/debug/heapdump_test.go deleted file mode 100644 index 920190115..000000000 --- a/src/pkg/runtime/debug/heapdump_test.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package debug - -import ( - "io/ioutil" - "os" - "runtime" - "testing" -) - -func TestWriteHeapDumpNonempty(t *testing.T) { - if runtime.GOOS == "nacl" { - t.Skip("WriteHeapDump is not available on NaCl.") - } - f, err := ioutil.TempFile("", "heapdumptest") - if err != nil { - t.Fatalf("TempFile failed: %v", err) - } - defer os.Remove(f.Name()) - defer f.Close() - WriteHeapDump(f.Fd()) - fi, err := f.Stat() - if err != nil { - t.Fatalf("Stat failed: %v", err) - } - const minSize = 1 - if size := fi.Size(); size < minSize { - t.Fatalf("Heap dump size %d bytes, expected at least %d bytes", size, minSize) - } -} diff --git a/src/pkg/runtime/debug/stack.go b/src/pkg/runtime/debug/stack.go deleted file mode 100644 index c29b0a226..000000000 --- a/src/pkg/runtime/debug/stack.go +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package debug contains facilities for programs to debug themselves while -// they are running. -package debug - -import ( - "bytes" - "fmt" - "io/ioutil" - "os" - "runtime" -) - -var ( - dunno = []byte("???") - centerDot = []byte("·") - dot = []byte(".") - slash = []byte("/") -) - -// PrintStack prints to standard error the stack trace returned by Stack. -func PrintStack() { - os.Stderr.Write(stack()) -} - -// Stack returns a formatted stack trace of the goroutine that calls it. -// For each routine, it includes the source line information and PC value, -// then attempts to discover, for Go functions, the calling function or -// method and the text of the line containing the invocation. -// -// This function is deprecated. Use package runtime's Stack instead. -func Stack() []byte { - return stack() -} - -// stack implements Stack, skipping 2 frames -func stack() []byte { - buf := new(bytes.Buffer) // the returned data - // As we loop, we open files and read them. These variables record the currently - // loaded file. - var lines [][]byte - var lastFile string - for i := 2; ; i++ { // Caller we care about is the user, 2 frames up - pc, file, line, ok := runtime.Caller(i) - if !ok { - break - } - // Print this much at least. If we can't find the source, it won't show. - fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc) - if file != lastFile { - data, err := ioutil.ReadFile(file) - if err != nil { - continue - } - lines = bytes.Split(data, []byte{'\n'}) - lastFile = file - } - line-- // in stack trace, lines are 1-indexed but our array is 0-indexed - fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line)) - } - return buf.Bytes() -} - -// source returns a space-trimmed slice of the n'th line. -func source(lines [][]byte, n int) []byte { - if n < 0 || n >= len(lines) { - return dunno - } - return bytes.Trim(lines[n], " \t") -} - -// function returns, if possible, the name of the function containing the PC. -func function(pc uintptr) []byte { - fn := runtime.FuncForPC(pc) - if fn == nil { - return dunno - } - name := []byte(fn.Name()) - // The name includes the path name to the package, which is unnecessary - // since the file name is already included. Plus, it has center dots. - // That is, we see - // runtime/debug.*T·ptrmethod - // and want - // *T.ptrmethod - // Since the package path might contains dots (e.g. code.google.com/...), - // we first remove the path prefix if there is one. - if lastslash := bytes.LastIndex(name, slash); lastslash >= 0 { - name = name[lastslash+1:] - } - if period := bytes.Index(name, dot); period >= 0 { - name = name[period+1:] - } - name = bytes.Replace(name, centerDot, dot, -1) - return name -} diff --git a/src/pkg/runtime/debug/stack_test.go b/src/pkg/runtime/debug/stack_test.go deleted file mode 100644 index bbd662618..000000000 --- a/src/pkg/runtime/debug/stack_test.go +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package debug - -import ( - "strings" - "testing" -) - -type T int - -func (t *T) ptrmethod() []byte { - return Stack() -} -func (t T) method() []byte { - return t.ptrmethod() -} - -/* - The traceback should look something like this, modulo line numbers and hex constants. - Don't worry much about the base levels, but check the ones in our own package. - - /Users/r/go/src/pkg/runtime/debug/stack_test.go:15 (0x13878) - (*T).ptrmethod: return Stack() - /Users/r/go/src/pkg/runtime/debug/stack_test.go:18 (0x138dd) - T.method: return t.ptrmethod() - /Users/r/go/src/pkg/runtime/debug/stack_test.go:23 (0x13920) - TestStack: b := T(0).method() - /Users/r/go/src/pkg/testing/testing.go:132 (0x14a7a) - tRunner: test.F(t) - /Users/r/go/src/pkg/runtime/proc.c:145 (0xc970) - ???: runtime·unlock(&runtime·sched); -*/ -func TestStack(t *testing.T) { - b := T(0).method() - lines := strings.Split(string(b), "\n") - if len(lines) < 6 { - t.Fatal("too few lines") - } - n := 0 - frame := func(line, code string) { - check(t, lines[n], line) - n++ - // The source might not be available while running the test. - if strings.HasPrefix(lines[n], "\t") { - check(t, lines[n], code) - n++ - } - } - frame("src/pkg/runtime/debug/stack_test.go", "\t(*T).ptrmethod: return Stack()") - frame("src/pkg/runtime/debug/stack_test.go", "\tT.method: return t.ptrmethod()") - frame("src/pkg/runtime/debug/stack_test.go", "\tTestStack: b := T(0).method()") - frame("src/pkg/testing/testing.go", "") -} - -func check(t *testing.T, line, has string) { - if strings.Index(line, has) < 0 { - t.Errorf("expected %q in %q", has, line) - } -} |