fix(aggregator): prevent CPU cores overflow when processes restart
All checks were successful
ci / build (push) Successful in 28s
All checks were successful
ci / build (push) Successful in 28s
Guard against unsigned integer underflow in cgroup CPU calculation. When processes exit and new ones start, totalTicks can be less than the previous value, causing the subtraction to wrap around to a huge positive number. Now checks totalTicks >= prev before calculating delta, treating process churn as 0 CPU usage for that sample. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
5b983692c8
commit
0af8c28bc2
1 changed files with 8 additions and 3 deletions
|
|
@ -348,9 +348,14 @@ func (a *Aggregator) calculateCgroupMetrics(
|
|||
// Calculate CPU cores used from delta
|
||||
usedCores := 0.0
|
||||
if prev, ok := a.prevCgroupCPU[containerName]; ok && elapsed > 0 {
|
||||
deltaTicks := totalTicks - prev
|
||||
// Convert ticks to cores: deltaTicks / (elapsed_seconds * CLK_TCK)
|
||||
usedCores = float64(deltaTicks) / (elapsed * float64(proc.DefaultClockTicks))
|
||||
// Guard against underflow: if processes exited and new ones started,
|
||||
// totalTicks could be less than prev. In that case, skip this sample.
|
||||
if totalTicks >= prev {
|
||||
deltaTicks := totalTicks - prev
|
||||
// Convert ticks to cores: deltaTicks / (elapsed_seconds * CLK_TCK)
|
||||
usedCores = float64(deltaTicks) / (elapsed * float64(proc.DefaultClockTicks))
|
||||
}
|
||||
// If totalTicks < prev, usedCores stays 0 for this sample
|
||||
}
|
||||
a.prevCgroupCPU[containerName] = totalTicks
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue