forked from mosn/holmes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholmes.go
385 lines (319 loc) · 9.5 KB
/
holmes.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package holmes
import (
"bytes"
"fmt"
"os"
"runtime/pprof"
"sync/atomic"
"time"
)
// Holmes is a self-aware profile dumper.
type Holmes struct {
opts *options
// stats
collectCount int
threadTriggerCount int
cpuTriggerCount int
memTriggerCount int
grTriggerCount int
// cooldown
threadCoolDownTime time.Time
cpuCoolDownTime time.Time
memCoolDownTime time.Time
grCoolDownTime time.Time
// stats ring
memStats ring
cpuStats ring
grNumStats ring
threadStats ring
// switch
stopped int64
}
// New creates a holmes dumper.
func New(opts ...Option) (*Holmes, error) {
holmes := &Holmes{
opts: newOptions(),
}
for _, opt := range opts {
if err := opt.apply(holmes.opts); err != nil {
return nil, err
}
}
return holmes, nil
}
// EnableThreadDump enables the goroutine dump.
func (h *Holmes) EnableThreadDump() *Holmes {
h.opts.ThreadOpts.Enable = true
return h
}
// DisableThreadDump disables the goroutine dump.
func (h *Holmes) DisableThreadDump() *Holmes {
h.opts.ThreadOpts.Enable = false
return h
}
// EnableGoroutineDump enables the goroutine dump.
func (h *Holmes) EnableGoroutineDump() *Holmes {
h.opts.GrOpts.Enable = true
return h
}
// DisableGoroutineDump disables the goroutine dump.
func (h *Holmes) DisableGoroutineDump() *Holmes {
h.opts.GrOpts.Enable = false
return h
}
// EnableCPUDump enables the CPU dump.
func (h *Holmes) EnableCPUDump() *Holmes {
h.opts.CPUOpts.Enable = true
return h
}
// DisableCPUDump disables the CPU dump.
func (h *Holmes) DisableCPUDump() *Holmes {
h.opts.CPUOpts.Enable = false
return h
}
// EnableMemDump enables the mem dump.
func (h *Holmes) EnableMemDump() *Holmes {
h.opts.MemOpts.Enable = true
return h
}
// DisableMemDump disables the mem dump.
func (h *Holmes) DisableMemDump() *Holmes {
h.opts.MemOpts.Enable = false
return h
}
// Start starts the dump loop of holmes.
func (h *Holmes) Start() {
atomic.StoreInt64(&h.stopped, 0)
h.initEnvironment()
go h.startDumpLoop()
}
// Stop the dump loop.
func (h *Holmes) Stop() {
atomic.StoreInt64(&h.stopped, 1)
}
func (h *Holmes) startDumpLoop() {
// init previous cool down time
now := time.Now()
h.cpuCoolDownTime = now
h.memCoolDownTime = now
h.grCoolDownTime = now
// init stats ring
h.cpuStats = newRing(minCollectCyclesBeforeDumpStart)
h.memStats = newRing(minCollectCyclesBeforeDumpStart)
h.grNumStats = newRing(minCollectCyclesBeforeDumpStart)
h.threadStats = newRing(minCollectCyclesBeforeDumpStart)
// dump loop
ticker := time.NewTicker(h.opts.CollectInterval)
defer ticker.Stop()
for range ticker.C {
if atomic.LoadInt64(&h.stopped) == 1 {
fmt.Println("[Holmes] dump loop stopped")
return
}
cpu, mem, gNum, tNum, err := collect()
if err != nil {
h.logf(err.Error())
continue
}
h.cpuStats.push(cpu)
h.memStats.push(mem)
h.grNumStats.push(gNum)
h.threadStats.push(tNum)
h.collectCount++
if h.collectCount < minCollectCyclesBeforeDumpStart {
// at least collect some cycles
// before start to judge and dump
h.logf("[Holmes] warming up cycle : %d", h.collectCount)
continue
}
h.goroutineCheckAndDump(gNum)
h.memCheckAndDump(mem)
h.cpuCheckAndDump(cpu)
h.threadCheckAndDump(tNum)
}
}
// goroutine start.
func (h *Holmes) goroutineCheckAndDump(gNum int) {
if !h.opts.GrOpts.Enable {
return
}
if h.grCoolDownTime.After(time.Now()) {
h.logf("[Holmes] goroutine dump is in cooldown")
return
}
if triggered := h.goroutineProfile(gNum); triggered {
h.grCoolDownTime = time.Now().Add(h.opts.CoolDown)
h.grTriggerCount++
}
}
func (h *Holmes) goroutineProfile(gNum int) bool {
c := h.opts.GrOpts
if !matchRule(h.grNumStats, gNum, c.GoroutineTriggerNumMin, c.GoroutineTriggerNumAbs, c.GoroutineTriggerPercentDiff) {
h.debugUniform("NODUMP", type2name[goroutine],
c.GoroutineTriggerNumMin, c.GoroutineTriggerPercentDiff, c.GoroutineTriggerNumAbs,
h.grNumStats.data, gNum)
return false
}
var buf bytes.Buffer
_ = pprof.Lookup("goroutine").WriteTo(&buf, int(h.opts.DumpProfileType)) // nolint: errcheck
h.writeProfileDataToFile(buf, goroutine, gNum)
return true
}
// memory start.
func (h *Holmes) memCheckAndDump(mem int) {
if !h.opts.MemOpts.Enable {
return
}
if h.memCoolDownTime.After(time.Now()) {
h.logf("[Holmes] mem dump is in cooldown")
return
}
if triggered := h.memProfile(mem); triggered {
h.memCoolDownTime = time.Now().Add(h.opts.CoolDown)
h.memTriggerCount++
}
}
func (h *Holmes) memProfile(rss int) bool {
c := h.opts.MemOpts
if !matchRule(h.memStats, rss, c.MemTriggerPercentMin, c.MemTriggerPercentAbs, c.MemTriggerPercentDiff) {
// let user know why this should not dump
h.debugUniform("NODUMP", type2name[mem],
c.MemTriggerPercentMin, c.MemTriggerPercentDiff, c.MemTriggerPercentAbs,
h.memStats.data, rss)
return false
}
var buf bytes.Buffer
_ = pprof.Lookup("heap").WriteTo(&buf, int(h.opts.DumpProfileType)) // nolint: errcheck
h.writeProfileDataToFile(buf, mem, rss)
return true
}
// thread start.
func (h *Holmes) threadCheckAndDump(threadNum int) {
if !h.opts.ThreadOpts.Enable {
return
}
if h.threadCoolDownTime.After(time.Now()) {
h.logf("[Holmes] thread dump is in cooldown")
return
}
if triggered := h.threadProfile(threadNum); triggered {
h.threadCoolDownTime = time.Now().Add(h.opts.CoolDown)
h.threadTriggerCount++
}
}
func (h *Holmes) threadProfile(curThreadNum int) bool {
c := h.opts.ThreadOpts
if !matchRule(h.threadStats, curThreadNum, c.ThreadTriggerPercentMin, c.ThreadTriggerPercentAbs, c.ThreadTriggerPercentDiff) {
// let user know why this should not dump
h.debugUniform("NODUMP", type2name[thread],
c.ThreadTriggerPercentMin, c.ThreadTriggerPercentDiff, c.ThreadTriggerPercentAbs,
h.threadStats.data, curThreadNum)
return false
}
var buf bytes.Buffer
_ = pprof.Lookup("threadcreate").WriteTo(&buf, int(h.opts.DumpProfileType)) // nolint: errcheck
_ = pprof.Lookup("goroutine").WriteTo(&buf, int(h.opts.DumpProfileType)) // nolint: errcheck
h.writeProfileDataToFile(buf, thread, curThreadNum)
return true
}
// thread end.
// cpu start.
func (h *Holmes) cpuCheckAndDump(cpu int) {
if !h.opts.CPUOpts.Enable {
return
}
if h.cpuCoolDownTime.After(time.Now()) {
h.logf("[Holmes] cpu dump is in cooldown")
return
}
if triggered := h.cpuProfile(cpu); triggered {
h.cpuCoolDownTime = time.Now().Add(h.opts.CoolDown)
h.cpuTriggerCount++
}
}
func (h *Holmes) cpuProfile(curCPUUsage int) bool {
c := h.opts.CPUOpts
if !matchRule(h.cpuStats, curCPUUsage, c.CPUTriggerPercentMin, c.CPUTriggerPercentAbs, c.CPUTriggerPercentDiff) {
// let user know why this should not dump
h.debugUniform("NODUMP", type2name[cpu],
c.CPUTriggerPercentMin, c.CPUTriggerPercentDiff, c.CPUTriggerPercentAbs,
h.cpuStats.data, curCPUUsage)
return false
}
binFileName := getBinaryFileName(h.opts.DumpPath, cpu)
bf, err := os.OpenFile(binFileName, defaultLoggerFlags, defaultLoggerPerm)
if err != nil {
h.logf("[Holmes] failed to create cpu profile file: %v", err.Error())
return false
}
defer bf.Close()
err = pprof.StartCPUProfile(bf)
if err != nil {
h.logf("[Holmes] failed to profile cpu: %v", err.Error())
return false
}
time.Sleep(defaultCPUSamplingTime)
pprof.StopCPUProfile()
h.infoUniform("pprof dump to log dir", type2name[cpu],
c.CPUTriggerPercentMin, c.CPUTriggerPercentDiff, c.CPUTriggerPercentAbs,
h.cpuStats.data, curCPUUsage)
return true
}
func (h *Holmes) writeProfileDataToFile(data bytes.Buffer, dumpType configureType, currentStat int) {
binFileName := getBinaryFileName(h.opts.DumpPath, dumpType)
switch dumpType {
case mem:
opts := h.opts.MemOpts
h.infoUniform("pprof", type2name[dumpType],
opts.MemTriggerPercentMin, opts.MemTriggerPercentDiff, opts.MemTriggerPercentAbs,
h.memStats.data, currentStat)
case goroutine:
opts := h.opts.GrOpts
h.infoUniform("pprof", type2name[dumpType],
opts.GoroutineTriggerNumMin, opts.GoroutineTriggerPercentDiff, opts.GoroutineTriggerNumAbs,
h.grNumStats.data, currentStat)
case thread:
opts := h.opts.ThreadOpts
h.infoUniform("pprof", type2name[dumpType],
opts.ThreadTriggerPercentMin, opts.ThreadTriggerPercentDiff, opts.ThreadTriggerPercentAbs,
h.threadStats.data, currentStat)
}
if h.opts.DumpProfileType == textDump {
// write to log
var res = data.String()
if !h.opts.DumpFullStack {
res = trimResult(data)
}
h.logf(res)
} else {
bf, err := os.OpenFile(binFileName, defaultLoggerFlags, defaultLoggerPerm)
if err != nil {
h.logf("[Holmes] pprof %v write to file failed : %v", type2name[dumpType], err.Error())
return
}
defer bf.Close()
if _, err = bf.Write(data.Bytes()); err != nil {
h.logf("[Holmes] pprof %v write to file failed : %v", type2name[dumpType], err.Error())
}
}
}
func (h *Holmes) debugUniform(msg string, name string, min, diff, abs int, data []int, cur int) {
h.debugf("[Holmes] %v %v, config_min : %v, config_diff : %v, config_abs : %v, previous : %v, current: %v",
msg, name, min, diff, abs, data, cur)
}
func (h *Holmes) infoUniform(msg string, name string, min, diff, abs int, data []int, cur int) {
h.logf("[Holmes] %v %v, config_min : %v, config_diff : %v, config_abs : %v, previous : %v, current: %v",
msg, name, min, diff, abs, data, cur)
}
func (h *Holmes) initEnvironment() {
// choose whether the max memory is limited by cgroup
if h.opts.UseCGroup {
// use cgroup
getUsage = getUsageCGroup
h.logf("[Holmes] use cgroup to limit memory")
} else {
// not use cgroup
getUsage = getUsageNormal
h.logf("[Holmes] use the default memory percent calculated by gopsutil")
}
}