github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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 traceprofile string 37 traceHandler func(string) 38 ) 39 40 func startProfile() { 41 if cpuprofile != "" { 42 f, err := os.Create(cpuprofile) 43 if err != nil { 44 Fatalf("%v", err) 45 } 46 if err := pprof.StartCPUProfile(f); err != nil { 47 Fatalf("%v", err) 48 } 49 atExit(pprof.StopCPUProfile) 50 } 51 if memprofile != "" { 52 if memprofilerate != 0 { 53 runtime.MemProfileRate = int(memprofilerate) 54 } 55 f, err := os.Create(memprofile) 56 if err != nil { 57 Fatalf("%v", err) 58 } 59 atExit(func() { 60 runtime.GC() // profile all outstanding allocations 61 if err := pprof.WriteHeapProfile(f); err != nil { 62 Fatalf("%v", err) 63 } 64 }) 65 } 66 if traceprofile != "" && traceHandler != nil { 67 traceHandler(traceprofile) 68 } 69 }