Skip to content

Commit

Permalink
Fix potential CPU leak
Browse files Browse the repository at this point in the history
  • Loading branch information
ioppermann committed Oct 31, 2024
1 parent dfbf558 commit a790043
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions resources/psutil/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,35 +148,26 @@ func (p *process) Resume() error {
func (p *process) CPU() (*CPUInfo, error) {
var diff float64

for {
p.lock.RLock()
nTicks := p.nTicks
p.lock.RUnlock()

if nTicks < 2 {
time.Sleep(100 * time.Millisecond)
continue
}

break
}

p.lock.RLock()
defer p.lock.RUnlock()

if p.hasCgroup && p.cpuLimit > 0 {
diff = float64(p.cpuLimit) * (p.statCurrentTime.Sub(p.statPreviousTime)).Seconds() / 1e9
} else {
diff = p.statCurrentTime.Sub(p.statPreviousTime).Seconds() * p.ncpu
}

s := &CPUInfo{
System: 0,
User: 0,
Idle: 0,
Other: 0,
}

if p.nTicks < 2 {
return s, nil
}

if p.hasCgroup && p.cpuLimit > 0 {
diff = float64(p.cpuLimit) * (p.statCurrentTime.Sub(p.statPreviousTime)).Seconds() / 1e9
} else {
diff = p.statCurrentTime.Sub(p.statPreviousTime).Seconds() * p.ncpu
}

if diff <= 0 {
return s, nil
}
Expand Down

0 comments on commit a790043

Please sign in to comment.