[dev.cc] cmd/new6g, etc: reconvert to add profiling

Converted from rsc.io/c2go rev a9bc7f2.
Adds profiling support.

Change-Id: Ie04f86b71e0713c7294416c77d349e0d93798403
Reviewed-on: https://go-review.googlesource.com/5574
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
Russ Cox 2015-02-23 10:22:26 -05:00
parent de50bad121
commit 53d4123fbc
6 changed files with 53 additions and 1 deletions

View file

@ -2,6 +2,8 @@ package gc
import (
"cmd/internal/obj"
"os"
"runtime/pprof"
"strconv"
"strings"
)
@ -68,3 +70,45 @@ func stringsCompare(a, b string) int {
}
return +1
}
var atExitFuncs []func()
func AtExit(f func()) {
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)
}
var cpuprofile string
var memprofile string
func startProfile() {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
Fatal("%v", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
Fatal("%v", err)
}
AtExit(pprof.StopCPUProfile)
}
if memprofile != "" {
f, err := os.Create(memprofile)
if err != nil {
Fatal("%v", err)
}
AtExit(func() {
if err := pprof.WriteHeapProfile(f); err != nil {
Fatal("%v", err)
}
})
}
}