diff --git a/measure-backend/measure-go/replay/cpu.go b/measure-backend/measure-go/replay/cpu.go index 9ae931f7d..c233ef140 100644 --- a/measure-backend/measure-go/replay/cpu.go +++ b/measure-backend/measure-go/replay/cpu.go @@ -23,8 +23,20 @@ func ComputeCPUUsage(events []event.EventField) (result []CPUUsage) { } func calculate(u *event.CPUUsage) (usage float64) { + // Sum the CPU times total := float64(u.UTime + u.STime + u.CUTime + u.CSTime) + + // Convert interval configuration from milliseconds to seconds elapsed := float64(u.IntervalConfig) / 1000 - return (total / float64(u.ClockSpeed)) / (elapsed * float64(u.NumCores)) * 100.0 + // Divide total CPU time by the clock speed to get time in seconds + usageTime := total / float64(u.ClockSpeed) + + // Divide by the elapsed time to get usage over the interval + usageOverInterval := usageTime / elapsed + + // Divide by the number of cores to get the average usage per core + averageUsage := (usageOverInterval / float64(u.NumCores)) * 100.0 + + return averageUsage }