github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/cmd/compile/internal/gc/util.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gc 6 7 import ( 8 "os" 9 "runtime" 10 "runtime/pprof" 11 ) 12 13 func (n *Node) Line() string { 14 return Ctxt.LineHist.LineString(int(n.Lineno)) 15 } 16 17 var atExitFuncs []func() 18 19 func AtExit(f func()) { 20 atExitFuncs = append(atExitFuncs, f) 21 } 22 23 func Exit(code int) { 24 for i := len(atExitFuncs) - 1; i >= 0; i-- { 25 f := atExitFuncs[i] 26 atExitFuncs = atExitFuncs[:i] 27 f() 28 } 29 os.Exit(code) 30 } 31 32 var ( 33 cpuprofile string 34 memprofile string 35 memprofilerate int64 36 ) 37 38 func startProfile() { 39 if cpuprofile != "" { 40 f, err := os.Create(cpuprofile) 41 if err != nil { 42 Fatalf("%v", err) 43 } 44 if err := pprof.StartCPUProfile(f); err != nil { 45 Fatalf("%v", err) 46 } 47 AtExit(pprof.StopCPUProfile) 48 } 49 if memprofile != "" { 50 if memprofilerate != 0 { 51 runtime.MemProfileRate = int(memprofilerate) 52 } 53 f, err := os.Create(memprofile) 54 if err != nil { 55 Fatalf("%v", err) 56 } 57 AtExit(func() { 58 runtime.GC() // profile all outstanding allocations 59 if err := pprof.WriteHeapProfile(f); err != nil { 60 Fatalf("%v", err) 61 } 62 }) 63 } 64 }