From 07303dfe82dd7616b2b7b8ba996964ea08125cfd Mon Sep 17 00:00:00 2001 From: "liang.che" Date: Fri, 16 Aug 2024 11:45:57 +0800 Subject: [PATCH 1/4] ```txt refactor(process): Refactoring the algorithm for calculating CPU usage ``` --- process/process.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/process/process.go b/process/process.go index ba2766252..bfb6c160e 100644 --- a/process/process.go +++ b/process/process.go @@ -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 } From caed610b41b3f8b8f6d35d7e6954ef889bec89b4 Mon Sep 17 00:00:00 2001 From: "liang.che" Date: Fri, 16 Aug 2024 17:11:45 +0800 Subject: [PATCH 2/4] =?UTF-8?q?fix(process):=20=E7=A1=AE=E4=BF=9DCPU?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=8E=87=E8=AE=A1=E7=AE=97=E4=B8=BA=E9=9D=9E?= =?UTF-8?q?=E8=B4=9F=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- process/process.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/process/process.go b/process/process.go index bfb6c160e..03c62cf81 100644 --- a/process/process.go +++ b/process/process.go @@ -338,6 +338,9 @@ func calculatePercent(t1, t2 *cpu.TimesStat, delta float64, numcpu int) float64 return 0 } delta_proc := calculateBusyTime(t2) - calculateBusyTime(t1) + if delta_proc <= 0 { + return 0 + } overall_percent := ((delta_proc / delta) * 100) * float64(numcpu) return overall_percent } From e05e22b6cbfa87f3ab22a6867cfef8e44cf33dfd Mon Sep 17 00:00:00 2001 From: "liang.che" Date: Mon, 19 Aug 2024 11:22:57 +0800 Subject: [PATCH 3/4] https://github1s.com/giampaolo/psutil/blob/c034e6692cf736b5e87d14418a8153bb03f6cf42/psutil/__init__.py#L1064 --- process/process.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/process/process.go b/process/process.go index 03c62cf81..3c9e3f454 100644 --- a/process/process.go +++ b/process/process.go @@ -321,23 +321,12 @@ 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 := calculateBusyTime(t2) - calculateBusyTime(t1) + //https://github1s.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 } From cc21c22245ec50c9e997248ae243a03547dffd92 Mon Sep 17 00:00:00 2001 From: Cnpt <44840184+TheBestLL@users.noreply.github.com> Date: Sat, 24 Aug 2024 20:22:20 +0800 Subject: [PATCH 4/4] Update process/process.go Co-authored-by: shirou --- process/process.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/process/process.go b/process/process.go index 38c25f870..d73f1f972 100644 --- a/process/process.go +++ b/process/process.go @@ -325,7 +325,7 @@ func calculatePercent(t1, t2 *cpu.TimesStat, delta float64, numcpu int) float64 if delta == 0 { return 0 } - //https://github1s.com/giampaolo/psutil/blob/c034e6692cf736b5e87d14418a8153bb03f6cf42/psutil/__init__.py#L1064 + // 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