Skip to content

Commit

Permalink
resources: make maxsamples configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed May 11, 2023
1 parent 64e81ec commit 9b68967
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion executor/resources/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (r *cgroupRecord) Start() {
if stat, err := r.monitor.proc.Stat(); err == nil {
r.startCPUStat = &stat.CPUTotal
}
s := NewSampler(2*time.Second, r.sample)
s := NewSampler(2*time.Second, 10, r.sample)
r.sampler = s.Record()
r.closeSampler = s.Close
}
Expand Down
6 changes: 4 additions & 2 deletions executor/resources/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type WithTimestamp interface {
type Sampler[T WithTimestamp] struct {
mu sync.RWMutex
minInterval time.Duration
maxSamples int
callback func(ts time.Time) (T, error)
doneOnce sync.Once
done chan struct{}
Expand Down Expand Up @@ -57,9 +58,10 @@ func (s *Sub[T]) Close(captureLast bool) ([]T, error) {
return out, nil
}

func NewSampler[T WithTimestamp](minInterval time.Duration, cb func(time.Time) (T, error)) *Sampler[T] {
func NewSampler[T WithTimestamp](minInterval time.Duration, maxSamples int, cb func(time.Time) (T, error)) *Sampler[T] {
s := &Sampler[T]{
minInterval: minInterval,
maxSamples: maxSamples,
callback: cb,
done: make(chan struct{}),
subs: make(map[*Sub[T]]struct{}),
Expand Down Expand Up @@ -115,7 +117,7 @@ func (s *Sampler[T]) run() {
ss.err = nil
}
dur := ss.last.Sub(ss.first)
if time.Duration(ss.interval)*10 <= dur {
if time.Duration(ss.interval)*time.Duration(s.maxSamples) <= dur {
ss.interval *= 2
}
}
Expand Down
2 changes: 1 addition & 1 deletion executor/resources/sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func NewSysSampler() (*Sampler[*types.SysSample], error) {
return nil, err
}

return NewSampler(2*time.Second, func(tm time.Time) (*types.SysSample, error) {
return NewSampler(2*time.Second, 20, func(tm time.Time) (*types.SysSample, error) {
return sampleSys(procfs, tm)
}), nil
}
Expand Down

0 comments on commit 9b68967

Please sign in to comment.