Skip to content

Commit

Permalink
chore(backend): refactor cpu usage calculation for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
anupcowkur committed Jul 23, 2024
1 parent fd19da4 commit 1f9d217
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion measure-backend/measure-go/replay/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 1f9d217

Please sign in to comment.