Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions manager/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ type containerInfo struct {
Spec info.ContainerSpec
}

// atomicTime is a lock-free wrapper for storing and retrieving time values.
// It stores time as Unix nanoseconds in an atomic.Int64, enabling concurrent
// reads and writes without mutex contention.
type atomicTime struct {
atomic.Int64
}

// Time returns the stored time value as a time.Time.
func (t *atomicTime) Time() time.Time {
return time.Unix(0, t.Load())
}

type containerData struct {
oomEvents uint64
handler container.ContainerHandler
Expand All @@ -77,8 +89,8 @@ type containerData struct {
housekeepingInterval time.Duration
maxHousekeepingInterval time.Duration
allowDynamicHousekeeping bool
infoLastUpdatedTime time.Time
statsLastUpdatedTime time.Time
infoLastUpdatedTime atomicTime // Unix nano
statsLastUpdatedTime atomicTime // Unix nano
lastErrorTime time.Time
// used to track time
clock clock.Clock
Expand Down Expand Up @@ -145,9 +157,7 @@ func (cd *containerData) allowErrorLogging() bool {
// periodic housekeeping to reset. This should be used sparingly, as calling OnDemandHousekeeping frequently
// can have serious performance costs.
func (cd *containerData) OnDemandHousekeeping(maxAge time.Duration) {
cd.lock.Lock()
timeSinceStatsLastUpdate := cd.clock.Since(cd.statsLastUpdatedTime)
cd.lock.Unlock()
timeSinceStatsLastUpdate := cd.clock.Since(cd.statsLastUpdatedTime.Time())
if timeSinceStatsLastUpdate > maxAge {
housekeepingFinishedChan := make(chan struct{})
cd.onDemandChan <- housekeepingFinishedChan
Expand All @@ -172,7 +182,7 @@ func (cd *containerData) notifyOnDemand() {

func (cd *containerData) GetInfo(shouldUpdateSubcontainers bool) (*containerInfo, error) {
// Get spec and subcontainers.
if cd.clock.Since(cd.infoLastUpdatedTime) > 5*time.Second || shouldUpdateSubcontainers {
if cd.clock.Since(cd.infoLastUpdatedTime.Time()) > 5*time.Second || shouldUpdateSubcontainers {
err := cd.updateSpec()
if err != nil {
return nil, err
Expand All @@ -183,7 +193,7 @@ func (cd *containerData) GetInfo(shouldUpdateSubcontainers bool) (*containerInfo
return nil, err
}
}
cd.infoLastUpdatedTime = cd.clock.Now()
cd.infoLastUpdatedTime.Store(cd.clock.Now().UnixNano())
}
cd.lock.Lock()
defer cd.lock.Unlock()
Expand Down Expand Up @@ -594,9 +604,7 @@ func (cd *containerData) housekeepingTick(timer <-chan time.Time, longHousekeepi
klog.V(3).Infof("[%s] Housekeeping took %s", cd.info.Name, duration)
}
cd.notifyOnDemand()
cd.lock.Lock()
defer cd.lock.Unlock()
cd.statsLastUpdatedTime = cd.clock.Now()
cd.statsLastUpdatedTime.Store(cd.clock.Now().UnixNano())
return true
}

Expand Down
Loading
Loading