diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2016-10-17 16:54:25 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2016-10-17 16:54:25 +0000 |
commit | 8cce07d1ddccd9ffcdcaafaaa4cf13ffa95ec360 (patch) | |
tree | 99f8a824655109bd2cd36ab0a8502e2fd713101a /libgo/go | |
parent | 31a84d4a65c9e820fa6f928f8a031f6dfe213c85 (diff) | |
download | gcc-8cce07d1ddccd9ffcdcaafaaa4cf13ffa95ec360.zip gcc-8cce07d1ddccd9ffcdcaafaaa4cf13ffa95ec360.tar.gz gcc-8cce07d1ddccd9ffcdcaafaaa4cf13ffa95ec360.tar.bz2 |
runtime: copy rdebug code from Go 1.7 runtime
While we're at it, update the runtime/debug package, and start running
its testsuite by default. I'm not sure why runtime/debug was not
previously updated to 1.7. Doing that led me to fix some minor aspects
of runtime.Stack and the C function runtime/debug.readGCStats.
Reviewed-on: https://go-review.googlesource.com/31251
From-SVN: r241261
Diffstat (limited to 'libgo/go')
-rw-r--r-- | libgo/go/runtime/debug/garbage.go | 46 | ||||
-rw-r--r-- | libgo/go/runtime/debug/garbage_test.go | 13 | ||||
-rw-r--r-- | libgo/go/runtime/debug/stack_test.go | 10 | ||||
-rw-r--r-- | libgo/go/runtime/debug/stubs.go | 17 | ||||
-rw-r--r-- | libgo/go/runtime/mprof.go | 6 | ||||
-rw-r--r-- | libgo/go/runtime/rdebug.go | 27 | ||||
-rw-r--r-- | libgo/go/runtime/stubs.go | 16 | ||||
-rw-r--r-- | libgo/go/runtime/traceback_gccgo.go | 6 |
8 files changed, 111 insertions, 30 deletions
diff --git a/libgo/go/runtime/debug/garbage.go b/libgo/go/runtime/debug/garbage.go index 8d52837..81444971 100644 --- a/libgo/go/runtime/debug/garbage.go +++ b/libgo/go/runtime/debug/garbage.go @@ -16,17 +16,10 @@ type GCStats struct { NumGC int64 // number of garbage collections PauseTotal time.Duration // total pause for all collections Pause []time.Duration // pause history, most recent first + PauseEnd []time.Time // pause end times 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. @@ -38,25 +31,36 @@ func setMaxThreads(int) int 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. + // to the caller and the other will be used as transfer buffer + // for end times history and 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) + if cap(stats.Pause) < 2*maxPause+3 { + stats.Pause = make([]time.Duration, 2*maxPause+3) } - // 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 fills in the pause and end times histories (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] + n /= 2 // buffer holds pauses and end times stats.Pause = stats.Pause[:n] + if cap(stats.PauseEnd) < maxPause { + stats.PauseEnd = make([]time.Time, 0, maxPause) + } + stats.PauseEnd = stats.PauseEnd[:0] + for _, ns := range stats.Pause[n : n+n] { + stats.PauseEnd = append(stats.PauseEnd, time.Unix(0, int64(ns))) + } + if len(stats.PauseQuantiles) > 0 { if n == 0 { for i := range stats.PauseQuantiles { @@ -91,9 +95,9 @@ func (x byDuration) Less(i, j int) bool { return x[i] < x[j] } // at startup, or 100 if the variable is not set. // A negative percentage disables garbage collection. func SetGCPercent(percent int) int { - old := setGCPercent(percent) + old := setGCPercent(int32(percent)) runtime.GC() - return old + return int(old) } // FreeOSMemory forces a garbage collection followed by an @@ -145,7 +149,9 @@ func SetMaxThreads(threads int) int { // 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 +func SetPanicOnFault(enabled bool) bool { + return setPanicOnFault(enabled) +} // WriteHeapDump writes a description of the heap and the objects in // it to the given file descriptor. diff --git a/libgo/go/runtime/debug/garbage_test.go b/libgo/go/runtime/debug/garbage_test.go index 3d07cbb..6ec94aa 100644 --- a/libgo/go/runtime/debug/garbage_test.go +++ b/libgo/go/runtime/debug/garbage_test.go @@ -71,6 +71,19 @@ func TestReadGCStats(t *testing.T) { t.Errorf("stats.PauseQuantiles[%d]=%d > stats.PauseQuantiles[%d]=%d", i, q[i], i+1, q[i+1]) } } + + // compare memory stats with gc stats: + if len(stats.PauseEnd) != n { + t.Fatalf("len(stats.PauseEnd) = %d, want %d", len(stats.PauseEnd), n) + } + off := (int(mstats.NumGC) + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd) + for i := 0; i < n; i++ { + dt := stats.PauseEnd[i] + if dt.UnixNano() != int64(mstats.PauseEnd[off]) { + t.Errorf("stats.PauseEnd[%d] = %d, want %d", i, dt, mstats.PauseEnd[off]) + } + off = (off + len(mstats.PauseEnd) - 1) % len(mstats.PauseEnd) + } } var big = make([]byte, 1<<20) diff --git a/libgo/go/runtime/debug/stack_test.go b/libgo/go/runtime/debug/stack_test.go index 5f9f60c..67931d1 100644 --- a/libgo/go/runtime/debug/stack_test.go +++ b/libgo/go/runtime/debug/stack_test.go @@ -50,10 +50,12 @@ func TestStack(t *testing.T) { check(t, lines[n], line) n++ } - frame("stack_test.go", "\tmethod.N15_runtime_debug.T: return Stack()") - frame("stack_test.go", "\tmethod.N15_runtime_debug.T: return t.ptrmethod()") - frame("stack_test.go", "\tTestStack: b := T(0).method()") - frame("testing/testing.go", "") + n++ + frame("stack.go", "runtime_debug.Stack") + frame("stack_test.go", "ptrmethod") + frame("stack_test.go", "method") + frame("stack_test.go", "runtime_debug_test.TestStack") + frame("testing.go", "") } func check(t *testing.T, line, has string) { diff --git a/libgo/go/runtime/debug/stubs.go b/libgo/go/runtime/debug/stubs.go new file mode 100644 index 0000000..2cba136 --- /dev/null +++ b/libgo/go/runtime/debug/stubs.go @@ -0,0 +1,17 @@ +// 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 ( + "time" +) + +// Implemented in package runtime. +func readGCStats(*[]time.Duration) +func freeOSMemory() +func setMaxStack(int) int +func setGCPercent(int32) int32 +func setPanicOnFault(bool) bool +func setMaxThreads(int) int diff --git a/libgo/go/runtime/mprof.go b/libgo/go/runtime/mprof.go index 8d11031..a2701e3 100644 --- a/libgo/go/runtime/mprof.go +++ b/libgo/go/runtime/mprof.go @@ -623,7 +623,7 @@ func Stack(buf []byte, all bool) int { gp.m.traceback = 1 gp.writebuf = buf[0:0:len(buf)] goroutineheader(gp) - traceback() + traceback(1) if all { tracebackothers(gp) } @@ -653,7 +653,7 @@ func tracealloc(p unsafe.Pointer, size uintptr, typ *_type) { } if gp.m.curg == nil || gp == gp.m.curg { goroutineheader(gp) - traceback() + traceback(1) } else { goroutineheader(gp.m.curg) // FIXME: Can't do traceback of other g. @@ -669,7 +669,7 @@ func tracefree(p unsafe.Pointer, size uintptr) { gp.m.traceback = 2 print("tracefree(", p, ", ", hex(size), ")\n") goroutineheader(gp) - traceback() + traceback(1) print("\n") gp.m.traceback = 0 unlock(&tracelock) diff --git a/libgo/go/runtime/rdebug.go b/libgo/go/runtime/rdebug.go new file mode 100644 index 0000000..76535a9 --- /dev/null +++ b/libgo/go/runtime/rdebug.go @@ -0,0 +1,27 @@ +// 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 runtime + +import _ "unsafe" // for go:linkname + +// Define maxstacksize here for gccgo. For gc it is defined in +// stack.go, but gccgo doesn't use that file. Or, for that matter, +// maxstacksize. +var maxstacksize uintptr = 1 << 20 // enough until runtime.main sets it for real + +//go:linkname setMaxStack runtime_debug.setMaxStack +func setMaxStack(in int) (out int) { + out = int(maxstacksize) + maxstacksize = uintptr(in) + return out +} + +//go:linkname setPanicOnFault runtime_debug.setPanicOnFault +func setPanicOnFault(new bool) (old bool) { + _g_ := getg() + old = _g_.paniconfault + _g_.paniconfault = new + return old +} diff --git a/libgo/go/runtime/stubs.go b/libgo/go/runtime/stubs.go index 477c6be..3db8fea 100644 --- a/libgo/go/runtime/stubs.go +++ b/libgo/go/runtime/stubs.go @@ -444,3 +444,19 @@ func setprofilebucket(p unsafe.Pointer, b *bucket) // Currently in proc.c. func tracebackothers(*g) + +// Temporary for gccgo until we port mgc.go. +func setgcpercent(int32) int32 + +//go:linkname setGCPercent runtime_debug.setGCPercent +func setGCPercent(in int32) (out int32) { + return setgcpercent(in) +} + +// Temporary for gccgo until we port proc.go. +func setmaxthreads(int) int + +//go:linkname setMaxThreads runtime_debug.setMaxThreads +func setMaxThreads(in int) (out int) { + return setmaxthreads(in) +} diff --git a/libgo/go/runtime/traceback_gccgo.go b/libgo/go/runtime/traceback_gccgo.go index 4f3d7c0..b102826 100644 --- a/libgo/go/runtime/traceback_gccgo.go +++ b/libgo/go/runtime/traceback_gccgo.go @@ -67,9 +67,9 @@ func callers(skip int, locbuf []location) int { // traceback prints a traceback of the current goroutine. // This differs from the gc version, which is given pc, sp, lr and g and // can print a traceback of any goroutine. -func traceback() { +func traceback(skip int32) { var locbuf [100]location - c := c_callers(1, &locbuf[0], int32(len(locbuf)), false) + c := c_callers(skip+1, &locbuf[0], int32(len(locbuf)), false) printtrace(locbuf[:c], getg()) } @@ -77,7 +77,7 @@ func traceback() { func printtrace(locbuf []location, gp *g) { for i := range locbuf { if showframe(locbuf[i].function, gp) { - print(locbuf[i].function, "\n\t", locbuf[i].filename, ":", locbuf[i].lineno) + print(locbuf[i].function, "\n\t", locbuf[i].filename, ":", locbuf[i].lineno, "\n") } } } |