...
Source file src/pkg/cmd/compile/internal/gc/util.go
1
2
3
4
5 package gc
6
7 import (
8 "os"
9 "runtime"
10 "runtime/pprof"
11 )
12
13
14
15 func (n *Node) Line() string {
16 return linestr(n.Pos)
17 }
18
19 var atExitFuncs []func()
20
21 func atExit(f func()) {
22 atExitFuncs = append(atExitFuncs, f)
23 }
24
25 func Exit(code int) {
26 for i := len(atExitFuncs) - 1; i >= 0; i-- {
27 f := atExitFuncs[i]
28 atExitFuncs = atExitFuncs[:i]
29 f()
30 }
31 os.Exit(code)
32 }
33
34 var (
35 blockprofile string
36 cpuprofile string
37 memprofile string
38 memprofilerate int64
39 traceprofile string
40 traceHandler func(string)
41 mutexprofile string
42 )
43
44 func startProfile() {
45 if cpuprofile != "" {
46 f, err := os.Create(cpuprofile)
47 if err != nil {
48 Fatalf("%v", err)
49 }
50 if err := pprof.StartCPUProfile(f); err != nil {
51 Fatalf("%v", err)
52 }
53 atExit(pprof.StopCPUProfile)
54 }
55 if memprofile != "" {
56 if memprofilerate != 0 {
57 runtime.MemProfileRate = int(memprofilerate)
58 }
59 f, err := os.Create(memprofile)
60 if err != nil {
61 Fatalf("%v", err)
62 }
63 atExit(func() {
64
65 runtime.GC()
66
67
68
69 const writeLegacyFormat = 1
70 if err := pprof.Lookup("heap").WriteTo(f, writeLegacyFormat); err != nil {
71 Fatalf("%v", err)
72 }
73 })
74 } else {
75
76 runtime.MemProfileRate = 0
77 }
78 if blockprofile != "" {
79 f, err := os.Create(blockprofile)
80 if err != nil {
81 Fatalf("%v", err)
82 }
83 runtime.SetBlockProfileRate(1)
84 atExit(func() {
85 pprof.Lookup("block").WriteTo(f, 0)
86 f.Close()
87 })
88 }
89 if mutexprofile != "" {
90 f, err := os.Create(mutexprofile)
91 if err != nil {
92 Fatalf("%v", err)
93 }
94 startMutexProfiling()
95 atExit(func() {
96 pprof.Lookup("mutex").WriteTo(f, 0)
97 f.Close()
98 })
99 }
100 if traceprofile != "" && traceHandler != nil {
101 traceHandler(traceprofile)
102 }
103 }
104
View as plain text