Skip to content

Commit 41be02e

Browse files
suhuaqinsuhuaqin
andauthored
feat: WithCPUSamplingTime (#155)
Co-authored-by: suhuaqin <[email protected]>
1 parent 3e971f6 commit 41be02e

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

holmes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ func (h *Holmes) cpuProfile(curCPUUsage int, c typeOption) bool {
611611
return false
612612
}
613613

614-
time.Sleep(defaultCPUSamplingTime)
614+
time.Sleep(h.opts.CPUSamplingTime)
615615
pprof.StopCPUProfile()
616616

617617
rptOpts, bfCpy := h.opts.GetReporterOpts(), []byte{}

options.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type options struct {
4848
// move may result of the system crash.
4949
CPUMaxPercent int
5050

51+
// cpu sampling time
52+
CPUSamplingTime time.Duration
53+
5154
// if write lock is held mean holmes's
5255
// configuration is being modified.
5356
L *sync.RWMutex
@@ -173,6 +176,7 @@ func newOptions() *options {
173176
threadOpts: newThreadOptions(),
174177
CollectInterval: defaultInterval,
175178
intervalResetting: make(chan struct{}, 1),
179+
CPUSamplingTime: defaultCPUSamplingTime,
176180
DumpOptions: &DumpOptions{
177181
DumpPath: defaultDumpPath,
178182
DumpProfileType: defaultDumpProfileType,
@@ -230,6 +234,26 @@ func WithCPUMax(max int) Option {
230234
})
231235
}
232236

237+
// WithCPUSamplingTime set cpu sampling time
238+
func WithCPUSamplingTime(duration string) Option {
239+
return optionFunc(func(opts *options) (err error) {
240+
// CPUSamplingTime wouldn't be zero value, because it
241+
// will be initialized as defaultInterval at newOptions()
242+
newDuration, err := time.ParseDuration(duration)
243+
if err != nil {
244+
return
245+
}
246+
247+
if newDuration <= 0 {
248+
newDuration = defaultInterval
249+
}
250+
251+
opts.CPUSamplingTime = newDuration
252+
253+
return
254+
})
255+
}
256+
233257
// WithBinaryDump set dump mode to binary.
234258
func WithBinaryDump() Option {
235259
return withDumpProfileType(binaryDump)
@@ -320,8 +344,8 @@ func (base *typeOption) Set(min, abs, diff int, coolDown time.Duration) {
320344

321345
// newMemOptions
322346
// enable the heap dumper, should dump if one of the following requirements is matched
323-
// 1. memory usage > TriggerMin && memory usage diff > TriggerDiff
324-
// 2. memory usage > TriggerAbs.
347+
// 1. memory usage > TriggerMin && memory usage diff > TriggerDiff
348+
// 2. memory usage > TriggerAbs.
325349
func newMemOptions() *typeOption {
326350
return newTypeOpts(
327351
defaultMemTriggerMin,
@@ -341,8 +365,9 @@ func WithMemDump(min int, diff int, abs int, coolDown time.Duration) Option {
341365

342366
// newGCHeapOptions
343367
// enable the heap dumper, should dump if one of the following requirements is matched
344-
// 1. GC heap usage > TriggerMin && GC heap usage diff > TriggerDiff
345-
// 2. GC heap usage > TriggerAbs
368+
// 1. GC heap usage > TriggerMin && GC heap usage diff > TriggerDiff
369+
// 2. GC heap usage > TriggerAbs
370+
//
346371
// in percent.
347372
func newGCHeapOptions() *typeOption {
348373
return newTypeOpts(
@@ -398,8 +423,9 @@ func WithThreadDump(min, diff, abs int, coolDown time.Duration) Option {
398423
// newCPUOptions
399424
// enable the cpu dumper, should dump if one of the following requirements is matched
400425
// in percent
401-
// 1. cpu usage > CPUTriggerMin && cpu usage diff > CPUTriggerDiff
402-
// 2. cpu usage > CPUTriggerAbs
426+
// 1. cpu usage > CPUTriggerMin && cpu usage diff > CPUTriggerDiff
427+
// 2. cpu usage > CPUTriggerAbs
428+
//
403429
// in percent.
404430
func newCPUOptions() *typeOption {
405431
return newTypeOpts(

0 commit comments

Comments
 (0)