2016-02-23 02:19:25 -08:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
|
2015-02-13 14:40:36 -05:00
|
|
|
package gc
|
|
|
|
|
|
|
|
|
|
import (
|
2015-02-23 10:22:26 -05:00
|
|
|
"os"
|
2015-04-22 17:53:32 -07:00
|
|
|
"runtime"
|
2015-02-23 10:22:26 -05:00
|
|
|
"runtime/pprof"
|
2015-02-13 14:40:36 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (n *Node) Line() string {
|
2016-12-09 17:15:05 -08:00
|
|
|
return linestr(n.Pos)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2015-02-23 10:22:26 -05:00
|
|
|
var atExitFuncs []func()
|
|
|
|
|
|
2016-09-15 15:45:10 +10:00
|
|
|
func atExit(f func()) {
|
2015-02-23 10:22:26 -05:00
|
|
|
atExitFuncs = append(atExitFuncs, f)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Exit(code int) {
|
|
|
|
|
for i := len(atExitFuncs) - 1; i >= 0; i-- {
|
|
|
|
|
f := atExitFuncs[i]
|
|
|
|
|
atExitFuncs = atExitFuncs[:i]
|
|
|
|
|
f()
|
|
|
|
|
}
|
|
|
|
|
os.Exit(code)
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-22 17:53:32 -07:00
|
|
|
var (
|
|
|
|
|
cpuprofile string
|
|
|
|
|
memprofile string
|
|
|
|
|
memprofilerate int64
|
2016-08-01 20:34:12 +10:00
|
|
|
traceprofile string
|
|
|
|
|
traceHandler func(string)
|
2015-04-22 17:53:32 -07:00
|
|
|
)
|
2015-02-23 10:22:26 -05:00
|
|
|
|
|
|
|
|
func startProfile() {
|
|
|
|
|
if cpuprofile != "" {
|
|
|
|
|
f, err := os.Create(cpuprofile)
|
|
|
|
|
if err != nil {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("%v", err)
|
2015-02-23 10:22:26 -05:00
|
|
|
}
|
|
|
|
|
if err := pprof.StartCPUProfile(f); err != nil {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("%v", err)
|
2015-02-23 10:22:26 -05:00
|
|
|
}
|
2016-09-15 15:45:10 +10:00
|
|
|
atExit(pprof.StopCPUProfile)
|
2015-02-23 10:22:26 -05:00
|
|
|
}
|
|
|
|
|
if memprofile != "" {
|
2015-04-22 17:53:32 -07:00
|
|
|
if memprofilerate != 0 {
|
|
|
|
|
runtime.MemProfileRate = int(memprofilerate)
|
|
|
|
|
}
|
2015-02-23 10:22:26 -05:00
|
|
|
f, err := os.Create(memprofile)
|
|
|
|
|
if err != nil {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("%v", err)
|
2015-02-23 10:22:26 -05:00
|
|
|
}
|
2016-09-15 15:45:10 +10:00
|
|
|
atExit(func() {
|
2017-01-20 08:11:34 -08:00
|
|
|
// Profile all outstanding allocations.
|
|
|
|
|
runtime.GC()
|
|
|
|
|
// compilebench parses the memory profile to extract memstats,
|
|
|
|
|
// which are only written in the legacy pprof format.
|
|
|
|
|
// See golang.org/issue/18641 and runtime/pprof/pprof.go:writeHeap.
|
|
|
|
|
const writeLegacyFormat = 1
|
|
|
|
|
if err := pprof.Lookup("heap").WriteTo(f, writeLegacyFormat); err != nil {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("%v", err)
|
2015-02-23 10:22:26 -05:00
|
|
|
}
|
|
|
|
|
})
|
2017-01-27 14:04:23 -08:00
|
|
|
} else {
|
|
|
|
|
// Not doing memory profiling; disable it entirely.
|
|
|
|
|
runtime.MemProfileRate = 0
|
2015-02-23 10:22:26 -05:00
|
|
|
}
|
2016-08-01 20:34:12 +10:00
|
|
|
if traceprofile != "" && traceHandler != nil {
|
|
|
|
|
traceHandler(traceprofile)
|
|
|
|
|
}
|
2015-02-23 10:22:26 -05:00
|
|
|
}
|