Skip to content

Commit

Permalink
Refactoring the algorithm for calculating CPU usage (#1692)
Browse files Browse the repository at this point in the history
refactor(process): Refactoring the algorithm for calculating CPU usage
  • Loading branch information
TheBestLL authored Aug 24, 2024
1 parent 2a37a1d commit 829f2fa
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ func calculatePercent(t1, t2 *cpu.TimesStat, delta float64, numcpu int) float64
if delta == 0 {
return 0
}
delta_proc := t2.Total() - t1.Total()
// https://github.com/giampaolo/psutil/blob/c034e6692cf736b5e87d14418a8153bb03f6cf42/psutil/__init__.py#L1064
delta_proc := (t2.User - t1.User) + (t2.System - t1.System)
if delta_proc <= 0 {
return 0
}
overall_percent := ((delta_proc / delta) * 100) * float64(numcpu)
return overall_percent
}
Expand Down

0 comments on commit 829f2fa

Please sign in to comment.