github.com/FenixAra/go@v0.0.0-20170127160404-96ea0918e670/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 // Profile all outstanding allocations. 61 runtime.GC() 62 // compilebench parses the memory profile to extract memstats, 63 // which are only written in the legacy pprof format. 64 // See golang.org/issue/18641 and runtime/pprof/pprof.go:writeHeap. 65 const writeLegacyFormat = 1 66 if err := pprof.Lookup("heap").WriteTo(f, writeLegacyFormat); err != nil { 67 Fatalf("%v", err) 68 } 69 }) 70 } 71 if traceprofile != "" && traceHandler != nil { 72 traceHandler(traceprofile) 73 } 74 }