1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-01 13:48:52 +08:00
refactor(process): Refactoring the algorithm for calculating CPU usage
```
This commit is contained in:
liang.che 2024-08-16 11:45:57 +08:00
parent 6dfedba254
commit 07303dfe82

View File

@ -321,11 +321,23 @@ func (p *Process) CreateTimeWithContext(ctx context.Context) (int64, error) {
return p.createTime, err
}
// https://github.com/giampaolo/psutil/blob/550c825c95b537e9c3fee8452e5e1cd4fe5c704a/psutil/__init__.py#L1664-L1677
func calculateBusyTime(t *cpu.TimesStat) float64 {
tot := t.Total()
if runtime.GOOS == "linux" {
tot -= t.Guest // Linux 2.6.24+
tot -= t.GuestNice // Linux 3.2.0+
}
tot -= t.Idle
tot -= t.Iowait
return tot
}
func calculatePercent(t1, t2 *cpu.TimesStat, delta float64, numcpu int) float64 {
if delta == 0 {
return 0
}
delta_proc := t2.Total() - t1.Total()
delta_proc := calculateBusyTime(t2) - calculateBusyTime(t1)
overall_percent := ((delta_proc / delta) * 100) * float64(numcpu)
return overall_percent
}