Fix potential CPU leak

This commit is contained in:
Ingo Oppermann 2024-10-31 22:11:53 +01:00
parent dfbf55883d
commit a79004388f
No known key found for this signature in database
GPG Key ID: 2AB32426E9DD229E

View File

@ -148,28 +148,9 @@ 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,
@ -177,6 +158,16 @@ func (p *process) CPU() (*CPUInfo, error) {
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
}