diff --git a/service/oomkiller/service.go b/service/oomkiller/service.go index 01e078b8..d2bef722 100644 --- a/service/oomkiller/service.go +++ b/service/oomkiller/service.go @@ -52,21 +52,6 @@ func NewService(ctx context.Context, logger log.ContextLogger, tag string, optio }, nil } -func (s *Service) createTimer() { - s.adaptiveTimer = newAdaptiveTimer(s.logger, s.network, s.timerConfig, s.writeOOMReport) -} - -func (s *Service) startTimer() { - s.createTimer() - s.adaptiveTimer.start() -} - -func (s *Service) stopTimer() { - if s.adaptiveTimer != nil { - s.adaptiveTimer.stop() - } -} - func (s *Service) writeOOMReport(memoryUsage uint64) { now := time.Now().Unix() lastReport := s.lastReportTime.Load() diff --git a/service/oomkiller/service_darwin.go b/service/oomkiller/service_darwin.go index 0efdb7ab..166ddc7d 100644 --- a/service/oomkiller/service_darwin.go +++ b/service/oomkiller/service_darwin.go @@ -54,7 +54,7 @@ func (s *Service) Start(stage adapter.StartStage) error { return nil } if s.timerConfig.policyMode == policyModeNetworkExtension { - s.createTimer() + s.adaptiveTimer = newAdaptiveTimer(s.logger, s.network, s.timerConfig, nil) globalAccess.Lock() isFirst := len(globalServices) == 0 globalServices = append(globalServices, s) @@ -67,12 +67,15 @@ func (s *Service) Start(stage adapter.StartStage) error { if !s.timerConfig.policyMode.hasTimerMode() { return E.New("memory pressure monitoring is not available on this platform without memory_limit") } - s.startTimer() + s.adaptiveTimer = newAdaptiveTimer(s.logger, s.network, s.timerConfig, s.writeOOMReport) + s.adaptiveTimer.start() return nil } func (s *Service) Close() error { - s.stopTimer() + if s.adaptiveTimer != nil { + s.adaptiveTimer.stop() + } if s.timerConfig.policyMode == policyModeNetworkExtension { globalAccess.Lock() for i, svc := range globalServices { diff --git a/service/oomkiller/service_stub.go b/service/oomkiller/service_stub.go index 5eaf8204..81ebbf03 100644 --- a/service/oomkiller/service_stub.go +++ b/service/oomkiller/service_stub.go @@ -14,11 +14,14 @@ func (s *Service) Start(stage adapter.StartStage) error { if !s.timerConfig.policyMode.hasTimerMode() { return E.New("memory pressure monitoring is not available on this platform without memory_limit") } - s.startTimer() + s.adaptiveTimer = newAdaptiveTimer(s.logger, s.network, s.timerConfig, s.writeOOMReport) + s.adaptiveTimer.start() return nil } func (s *Service) Close() error { - s.stopTimer() + if s.adaptiveTimer != nil { + s.adaptiveTimer.stop() + } return nil } diff --git a/service/oomkiller/timer.go b/service/oomkiller/timer.go index 6e9db4c1..f8ab1475 100644 --- a/service/oomkiller/timer.go +++ b/service/oomkiller/timer.go @@ -105,7 +105,6 @@ type adaptiveTimer struct { limitThresholds pressureThresholds access sync.Mutex - cleanupTriggered bool timer *time.Timer state pressureState currentInterval time.Duration @@ -162,12 +161,6 @@ func (t *adaptiveTimer) poll() { t.access.Unlock() return } - if t.timerConfig.policyMode == policyModeNetworkExtension { - if t.cleanupTriggered { - runtimeDebug.FreeOSMemory() - t.cleanupTriggered = true - } - } if t.pendingPressureBaseline { t.pressureBaseline = sample t.pressureBaselineTime = time.Now() @@ -190,8 +183,8 @@ func (t *adaptiveTimer) poll() { growth := sample.usage - t.pressureBaseline.usage ratePerSecond := float64(growth) / elapsed.Seconds() headroom := t.memoryLimit - sample.usage - timeToLimit := time.Duration(float64(headroom)/ratePerSecond) * time.Second - if timeToLimit < t.minInterval { + secondsUntilLimit := float64(headroom) / ratePerSecond + if secondsUntilLimit < t.minInterval.Seconds() { triggered = true rateTriggered = true t.state = pressureStateTriggered @@ -202,8 +195,9 @@ func (t *adaptiveTimer) poll() { if !triggered { return } - t.cleanupTriggered = false - t.onTriggered(sample.usage) + if t.onTriggered != nil { + t.onTriggered(sample.usage) + } if rateTriggered { if t.killerDisabled { t.logger.Warn("memory growth rate critical (report only), usage: ", byteformats.FormatMemoryBytes(sample.usage), t.logDetails(sample)) diff --git a/service/oomkiller/timer_darwin.go b/service/oomkiller/timer_darwin.go index 6c4f7efa..f73ab28f 100644 --- a/service/oomkiller/timer_darwin.go +++ b/service/oomkiller/timer_darwin.go @@ -2,7 +2,10 @@ package oomkiller +import runtimeDebug "runtime/debug" + func (t *adaptiveTimer) notifyPressure() { + runtimeDebug.FreeOSMemory() t.access.Lock() t.startLocked() t.forceMinInterval = true