github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/cmd/compile/internal/gc/util.go (about) 1 package gc 2 3 import ( 4 "os" 5 "runtime" 6 "runtime/pprof" 7 ) 8 9 func (n *Node) Line() string { 10 return Ctxt.LineHist.LineString(int(n.Lineno)) 11 } 12 13 var atExitFuncs []func() 14 15 func AtExit(f func()) { 16 atExitFuncs = append(atExitFuncs, f) 17 } 18 19 func Exit(code int) { 20 for i := len(atExitFuncs) - 1; i >= 0; i-- { 21 f := atExitFuncs[i] 22 atExitFuncs = atExitFuncs[:i] 23 f() 24 } 25 os.Exit(code) 26 } 27 28 var ( 29 cpuprofile string 30 memprofile string 31 memprofilerate int64 32 ) 33 34 func startProfile() { 35 if cpuprofile != "" { 36 f, err := os.Create(cpuprofile) 37 if err != nil { 38 Fatalf("%v", err) 39 } 40 if err := pprof.StartCPUProfile(f); err != nil { 41 Fatalf("%v", err) 42 } 43 AtExit(pprof.StopCPUProfile) 44 } 45 if memprofile != "" { 46 if memprofilerate != 0 { 47 runtime.MemProfileRate = int(memprofilerate) 48 } 49 f, err := os.Create(memprofile) 50 if err != nil { 51 Fatalf("%v", err) 52 } 53 AtExit(func() { 54 runtime.GC() // profile all outstanding allocations 55 if err := pprof.WriteHeapProfile(f); err != nil { 56 Fatalf("%v", err) 57 } 58 }) 59 } 60 }