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 {
|
2015-04-20 13:32:40 -07:00
|
|
|
return Ctxt.LineHist.LineString(int(n.Lineno))
|
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
|
|
|
|
|
)
|
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() {
|
2015-04-22 17:53:32 -07:00
|
|
|
runtime.GC() // profile all outstanding allocations
|
2015-02-23 10:22:26 -05:00
|
|
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
2015-08-30 23:10:03 +02:00
|
|
|
Fatalf("%v", err)
|
2015-02-23 10:22:26 -05:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|