-
Notifications
You must be signed in to change notification settings - Fork 3
/
profile.go
317 lines (261 loc) · 8.94 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package profile
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
"runtime/trace"
"strconv"
"time"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/qlik-oss/gopherciser/enummap"
"github.com/qlik-oss/gopherciser/helpers"
)
type (
ProfileType int
profile struct {
typ ProfileType
closeFuncs []func() error
}
)
const (
ProfileTypeUnknown ProfileType = iota // Default value of int
ProfileTypeCPU
ProfileTypeBlock
ProfileTypeGoRoutine
ProfileTypeTreadCreate
ProfileTypeHeap
ProfileTypeMutex
ProfileTypeTrace
ProfileTypeMem
)
var (
// ProfileTypeEnum enum of profiling options
ProfileTypeEnum = enummap.NewEnumMapOrPanic(map[string]int{
"cpu": int(ProfileTypeCPU),
"block": int(ProfileTypeBlock),
"goroutine": int(ProfileTypeGoRoutine),
"threadcreate": int(ProfileTypeTreadCreate),
"heap": int(ProfileTypeHeap),
"mutex": int(ProfileTypeMutex),
"trace": int(ProfileTypeTrace),
"mem": int(ProfileTypeMem),
})
profiler *profile
)
// Close profiler, registered close functions will be executed LIFO
func Close() error {
if profiler == nil {
return nil
}
var multiErr *multierror.Error
for i := len(profiler.closeFuncs) - 1; i >= 0; i-- {
if err := profiler.closeFuncs[i](); err != nil {
multiErr = multierror.Append(multiErr, err)
}
}
return errors.WithStack(helpers.FlattenMultiError(multiErr))
}
func validateProfiler() error {
if profiler != nil {
return errors.Errorf("Profiler already set to %s, only one profer can be used at a time",
ProfileTypeEnum.StringDefault(int(profiler.typ), "unknown"))
}
return nil
}
// StartCPUProfiler and save to file
func StartCPUProfiler(file string) error {
if err := validateProfiler(); err != nil {
return errors.Wrap(err, "failed to start cpu profiler")
}
cpuProfile, err := os.Create(file)
if err != nil {
return errors.Wrapf(err, "failed to create cpu profiling file<%s>", file)
}
profiler = &profile{typ: ProfileTypeCPU}
profiler.closeFuncs = append(profiler.closeFuncs, cpuProfile.Close)
if err := pprof.StartCPUProfile(cpuProfile); err != nil {
return errors.Wrap(err, "failed to start cpu profiler")
}
profiler.closeFuncs = append(profiler.closeFuncs, func() error { pprof.StopCPUProfile(); return nil })
return nil
}
// StartBlockProfiler and save to file, debug flag adds stacks to profile
func StartBlockProfiler(file string, debug int) error {
if err := validateProfiler(); err != nil {
return errors.Wrap(err, "failed to start block profiler")
}
blockProfile, err := os.Create(file)
if err != nil {
return errors.Wrap(err, "Failed to create block profiling file")
}
profiler = &profile{typ: ProfileTypeBlock}
runtime.SetBlockProfileRate(1)
profiler.closeFuncs = append(profiler.closeFuncs, blockProfile.Close)
profiler.closeFuncs = append(profiler.closeFuncs, func() error { return pprof.Lookup("block").WriteTo(blockProfile, debug) })
return nil
}
// StartGoroutineProfiler and save to a fileprefix_%i.out file each t
func StartGoroutineProfiler(fileprefix string, t time.Duration) error {
if err := validateProfiler(); err != nil {
return errors.Wrap(err, "failed to start goroutine profiler")
}
profiler = &profile{typ: ProfileTypeGoRoutine}
ticker := time.NewTicker(t)
i := 0
go func() {
for range ticker.C {
i++
func() {
goroutineProfile, err := os.Create(fmt.Sprintf("%s_%d.out", fileprefix, i))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", errors.Wrap(err, "Failed to create goroutine profiling file"))
}
defer func() {
if err := goroutineProfile.Close(); err != nil {
fmt.Fprintf(os.Stderr, "failed to close file<%s>\n", goroutineProfile.Name())
}
}()
defer func() {
if err := pprof.Lookup("goroutine").WriteTo(goroutineProfile, 0); err != nil {
fmt.Fprintf(os.Stderr, "error writing goroutine profile to %s: %v\n", goroutineProfile.Name(), err)
}
}()
}()
}
}()
profiler.closeFuncs = append(profiler.closeFuncs, func() error { ticker.Stop(); return nil })
return nil
}
// StartThreadCreateProfiler and save to file
func StartThreadCreateProfiler(file string) error {
if err := validateProfiler(); err != nil {
return errors.Wrap(err, "failed to start threadcreate profiler")
}
profiler = &profile{typ: ProfileTypeTreadCreate}
tcProfile, err := os.Create(file)
if err != nil {
return errors.Wrapf(err, "failed to create threadcreate profiling file<%s>", file)
}
profiler.closeFuncs = append(profiler.closeFuncs, tcProfile.Close)
profiler.closeFuncs = append(profiler.closeFuncs, func() error { return pprof.Lookup("threadcreate").WriteTo(tcProfile, 0) })
return nil
}
// StartHeapProfiler and save to file
func StartHeapProfiler(file string) error {
if err := validateProfiler(); err != nil {
return errors.Wrap(err, "failed to start heap profiler")
}
profiler = &profile{typ: ProfileTypeHeap}
heapProfile, err := os.Create(file)
if err != nil {
return errors.Wrapf(err, "failed to create heap profiling file<%s>", file)
}
profiler.closeFuncs = append(profiler.closeFuncs, heapProfile.Close)
profiler.closeFuncs = append(profiler.closeFuncs, func() error { return pprof.Lookup("heap").WriteTo(heapProfile, 0) })
return nil
}
// StartMutexProfiler and save to file, debug flag adds stacks to profile
func StartMutexProfiler(file string, debug int) error {
if err := validateProfiler(); err != nil {
return errors.Wrap(err, "failed to start mutex profiler")
}
profiler = &profile{typ: ProfileTypeMutex}
runtime.SetMutexProfileFraction(1)
mutexProfile, err := os.Create(file)
if err != nil {
return errors.Wrapf(err, "failed to create mutex profiling file<%s>", file)
}
profiler.closeFuncs = append(profiler.closeFuncs, mutexProfile.Close)
profiler.closeFuncs = append(profiler.closeFuncs, func() error { return pprof.Lookup("mutex").WriteTo(mutexProfile, debug) })
return nil
}
// StartTraceProfiler and save to file
func StartTraceProfiler(file string) error {
if err := validateProfiler(); err != nil {
return errors.Wrap(err, "failed to start trace profiler")
}
profiler = &profile{typ: ProfileTypeTrace}
traceFile, err := os.Create(file)
if err != nil {
return errors.Wrapf(err, "failed to create trace file<%s>", file)
}
profiler.closeFuncs = append(profiler.closeFuncs, traceFile.Close)
if err = trace.Start(traceFile); err != nil {
return errors.Wrap(err, "failed to start trace")
}
profiler.closeFuncs = append(profiler.closeFuncs, func() error { trace.Stop(); return nil })
return nil
}
// StartMemProfiler and save to file
func StartMemProfiler(file string) error {
if err := validateProfiler(); err != nil {
return errors.Wrap(err, "failed to start mem profiler")
}
profiler = &profile{typ: ProfileTypeTrace}
runtime.MemProfileRate = 1
memProfile, err := os.Create(file)
if err != nil {
return errors.Wrapf(err, "failed to create mem profiling file<%s>", file)
}
profiler.closeFuncs = append(profiler.closeFuncs, memProfile.Close)
profiler.closeFuncs = append(profiler.closeFuncs, func() error { return pprof.WriteHeapProfile(memProfile) })
return nil
}
// StartProfiler start profiler of type (typ) with default parameters
func StartProfiler(typ ProfileType) error {
switch typ {
case ProfileTypeBlock:
return errors.WithStack(StartBlockProfiler("block.out", 1))
case ProfileTypeCPU:
return errors.WithStack(StartCPUProfiler("cpu.out"))
case ProfileTypeGoRoutine:
return errors.WithStack(StartGoroutineProfiler("goroutine", time.Second))
case ProfileTypeHeap:
return errors.WithStack(StartHeapProfiler("heap.out"))
case ProfileTypeMem:
return errors.WithStack(StartMemProfiler("mem.out"))
case ProfileTypeMutex:
return errors.WithStack(StartMutexProfiler("mutex.out", 1))
case ProfileTypeTrace:
return errors.WithStack(StartTraceProfiler("trace.out"))
case ProfileTypeTreadCreate:
return errors.WithStack(StartThreadCreateProfiler("threadcreate.out"))
default:
return errors.Errorf("Unsupported profile type<%s>", ProfileTypeEnum.StringDefault(int(typ), strconv.Itoa(int(typ))))
}
}
// ResolveParameter of string or int to profiletype
func ResolveParameter(param string) (ProfileType, error) {
var i int
var err error
// Do we have an int?
if i, err = strconv.Atoi(param); err != nil {
// it's a string
i, err = ProfileTypeEnum.Int(param)
if err != nil {
return ProfileTypeUnknown, errors.Wrapf(err, "failed to parse <%s> to profile type", param)
}
}
// it's an int
// make sure it's a valid type
_, err = ProfileTypeEnum.String(i)
if err != nil {
return ProfileTypeUnknown, errors.Wrapf(err, "failed to parse <%s> to profile type", param)
}
return ProfileType(i), nil
}
// Help formats a string to be displayed as help
func Help() string {
buf := helpers.NewBuffer()
buf.WriteString("Set a profiler to be started. One of:\n")
ProfileTypeEnum.ForEachSorted(func(k int, v string) {
buf.WriteString("[")
buf.WriteString(strconv.Itoa(k))
buf.WriteString("]: ")
buf.WriteString(v)
buf.WriteString("\n")
})
return buf.String()
}