Commit 53d4123f authored by Russ Cox's avatar Russ Cox

[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/5574Reviewed-by: 's avatarRob Pike <r@golang.org>
parent de50bad1
...@@ -72,7 +72,7 @@ const ( ...@@ -72,7 +72,7 @@ const (
func usage() { func usage() {
fmt.Printf("usage: %cg [options] file.go...\n", Thearch.Thechar) fmt.Printf("usage: %cg [options] file.go...\n", Thearch.Thechar)
obj.Flagprint(1) obj.Flagprint(1)
os.Exit(2) Exit(2)
} }
func fault(s int) { func fault(s int) {
...@@ -225,6 +225,8 @@ func Main() { ...@@ -225,6 +225,8 @@ func Main() {
obj.Flagcount("largemodel", "generate code that assumes a large memory model", &flag_largemodel) 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) obj.Flagparse(usage)
Ctxt.Debugasm = int32(Debug['S']) Ctxt.Debugasm = int32(Debug['S'])
Ctxt.Debugvlog = int32(Debug['v']) Ctxt.Debugvlog = int32(Debug['v'])
...@@ -233,6 +235,8 @@ func Main() { ...@@ -233,6 +235,8 @@ func Main() {
usage() usage()
} }
startProfile()
if flag_race != 0 { if flag_race != 0 {
racepkg = mkpkg(newstrlit("runtime/race")) racepkg = mkpkg(newstrlit("runtime/race"))
racepkg.Name = "race" racepkg.Name = "race"
......
...@@ -2,6 +2,8 @@ package gc ...@@ -2,6 +2,8 @@ package gc
import ( import (
"cmd/internal/obj" "cmd/internal/obj"
"os"
"runtime/pprof"
"strconv" "strconv"
"strings" "strings"
) )
...@@ -68,3 +70,45 @@ func stringsCompare(a, b string) int { ...@@ -68,3 +70,45 @@ func stringsCompare(a, b string) int {
} }
return +1 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)
}
})
}
}
...@@ -81,4 +81,5 @@ func main() { ...@@ -81,4 +81,5 @@ func main() {
gc.Thearch.Regnames = regnames gc.Thearch.Regnames = regnames
gc.Main() gc.Main()
gc.Exit(0)
} }
...@@ -106,4 +106,5 @@ func main() { ...@@ -106,4 +106,5 @@ func main() {
gc.Thearch.Regnames = regnames gc.Thearch.Regnames = regnames
gc.Main() gc.Main()
gc.Exit(0)
} }
...@@ -81,4 +81,5 @@ func main() { ...@@ -81,4 +81,5 @@ func main() {
gc.Thearch.Regnames = regnames gc.Thearch.Regnames = regnames
gc.Main() gc.Main()
gc.Exit(0)
} }
...@@ -89,4 +89,5 @@ func main() { ...@@ -89,4 +89,5 @@ func main() {
gc.Thearch.Regnames = regnames gc.Thearch.Regnames = regnames
gc.Main() gc.Main()
gc.Exit(0)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment