1
0
mirror of https://github.com/golang/go synced 2024-11-23 15:00:03 -07:00

[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

@ -72,7 +72,7 @@ const (
func usage() {
fmt.Printf("usage: %cg [options] file.go...\n", Thearch.Thechar)
obj.Flagprint(1)
os.Exit(2)
Exit(2)
}
func fault(s int) {
@ -225,6 +225,8 @@ func Main() {
obj.Flagcount("largemodel", "generate code that assumes a large memory model", &flag_largemodel)
}
obj.Flagstr("cpuprofile", "file: write cpu profile to file", &cpuprofile)
obj.Flagstr("memprofile", "file: write memory profile to file", &memprofile)
obj.Flagparse(usage)
Ctxt.Debugasm = int32(Debug['S'])
Ctxt.Debugvlog = int32(Debug['v'])
@ -233,6 +235,8 @@ func Main() {
usage()
}
startProfile()
if flag_race != 0 {
racepkg = mkpkg(newstrlit("runtime/race"))
racepkg.Name = "race"

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)
}
})
}
}

View File

@ -81,4 +81,5 @@ func main() {
gc.Thearch.Regnames = regnames
gc.Main()
gc.Exit(0)
}

View File

@ -106,4 +106,5 @@ func main() {
gc.Thearch.Regnames = regnames
gc.Main()
gc.Exit(0)
}

View File

@ -81,4 +81,5 @@ func main() {
gc.Thearch.Regnames = regnames
gc.Main()
gc.Exit(0)
}

View File

@ -89,4 +89,5 @@ func main() {
gc.Thearch.Regnames = regnames
gc.Main()
gc.Exit(0)
}