forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 2
/
profile.go
202 lines (173 loc) · 4.75 KB
/
profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//go:build linux || darwin
// +build linux darwin
package proc
import (
"fmt"
"os"
"os/signal"
"path"
"runtime"
"runtime/pprof"
"runtime/trace"
"sync/atomic"
"syscall"
"time"
"github.com/xiaoshouchen/go-zero/core/logx"
)
// DefaultMemProfileRate is the default memory profiling rate.
// See also http://golang.org/pkg/runtime/#pkg-variables
const DefaultMemProfileRate = 4096
// started is non zero if a profile is running.
var started uint32
// Profile represents an active profiling session.
type Profile struct {
// closers holds cleanup functions that run after each profile
closers []func()
// stopped records if a call to profile.Stop has been made
stopped uint32
}
func (p *Profile) close() {
for _, closer := range p.closers {
closer()
}
}
func (p *Profile) startBlockProfile() {
fn := createDumpFile("block")
f, err := os.Create(fn)
if err != nil {
logx.Errorf("profile: could not create block profile %q: %v", fn, err)
return
}
runtime.SetBlockProfileRate(1)
logx.Infof("profile: block profiling enabled, %s", fn)
p.closers = append(p.closers, func() {
pprof.Lookup("block").WriteTo(f, 0)
f.Close()
runtime.SetBlockProfileRate(0)
logx.Infof("profile: block profiling disabled, %s", fn)
})
}
func (p *Profile) startCpuProfile() {
fn := createDumpFile("cpu")
f, err := os.Create(fn)
if err != nil {
logx.Errorf("profile: could not create cpu profile %q: %v", fn, err)
return
}
logx.Infof("profile: cpu profiling enabled, %s", fn)
pprof.StartCPUProfile(f)
p.closers = append(p.closers, func() {
pprof.StopCPUProfile()
f.Close()
logx.Infof("profile: cpu profiling disabled, %s", fn)
})
}
func (p *Profile) startMemProfile() {
fn := createDumpFile("mem")
f, err := os.Create(fn)
if err != nil {
logx.Errorf("profile: could not create memory profile %q: %v", fn, err)
return
}
old := runtime.MemProfileRate
runtime.MemProfileRate = DefaultMemProfileRate
logx.Infof("profile: memory profiling enabled (rate %d), %s", runtime.MemProfileRate, fn)
p.closers = append(p.closers, func() {
pprof.Lookup("heap").WriteTo(f, 0)
f.Close()
runtime.MemProfileRate = old
logx.Infof("profile: memory profiling disabled, %s", fn)
})
}
func (p *Profile) startMutexProfile() {
fn := createDumpFile("mutex")
f, err := os.Create(fn)
if err != nil {
logx.Errorf("profile: could not create mutex profile %q: %v", fn, err)
return
}
runtime.SetMutexProfileFraction(1)
logx.Infof("profile: mutex profiling enabled, %s", fn)
p.closers = append(p.closers, func() {
if mp := pprof.Lookup("mutex"); mp != nil {
mp.WriteTo(f, 0)
}
f.Close()
runtime.SetMutexProfileFraction(0)
logx.Infof("profile: mutex profiling disabled, %s", fn)
})
}
func (p *Profile) startThreadCreateProfile() {
fn := createDumpFile("threadcreate")
f, err := os.Create(fn)
if err != nil {
logx.Errorf("profile: could not create threadcreate profile %q: %v", fn, err)
return
}
logx.Infof("profile: threadcreate profiling enabled, %s", fn)
p.closers = append(p.closers, func() {
if mp := pprof.Lookup("threadcreate"); mp != nil {
mp.WriteTo(f, 0)
}
f.Close()
logx.Infof("profile: threadcreate profiling disabled, %s", fn)
})
}
func (p *Profile) startTraceProfile() {
fn := createDumpFile("trace")
f, err := os.Create(fn)
if err != nil {
logx.Errorf("profile: could not create trace output file %q: %v", fn, err)
return
}
if err := trace.Start(f); err != nil {
logx.Errorf("profile: could not start trace: %v", err)
return
}
logx.Infof("profile: trace enabled, %s", fn)
p.closers = append(p.closers, func() {
trace.Stop()
logx.Infof("profile: trace disabled, %s", fn)
})
}
// Stop stops the profile and flushes any unwritten data.
func (p *Profile) Stop() {
if !atomic.CompareAndSwapUint32(&p.stopped, 0, 1) {
// someone has already called close
return
}
p.close()
atomic.StoreUint32(&started, 0)
}
// StartProfile starts a new profiling session.
// The caller should call the Stop method on the value returned
// to cleanly stop profiling.
func StartProfile() Stopper {
if !atomic.CompareAndSwapUint32(&started, 0, 1) {
logx.Error("profile: Start() already called")
return noopStopper
}
var prof Profile
prof.startCpuProfile()
prof.startMemProfile()
prof.startMutexProfile()
prof.startBlockProfile()
prof.startTraceProfile()
prof.startThreadCreateProfile()
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT)
<-c
logx.Info("profile: caught interrupt, stopping profiles")
prof.Stop()
signal.Reset()
syscall.Kill(os.Getpid(), syscall.SIGINT)
}()
return &prof
}
func createDumpFile(kind string) string {
command := path.Base(os.Args[0])
pid := syscall.Getpid()
return path.Join(os.TempDir(), fmt.Sprintf("%s-%d-%s-%s.pprof",
command, pid, kind, time.Now().Format(timeFormat)))
}