1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00

Merge pull request #1325 from shirou/feature/fix_cpu_total

fix(cpu): fix cpu total and busy calc
This commit is contained in:
shirou 2022-07-11 20:52:44 +09:00 committed by GitHub
commit 46f7642940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"math"
"runtime"
"strconv"
"strings"
"sync"
@ -86,10 +87,12 @@ func (c TimesStat) String() string {
return `{` + strings.Join(v, ",") + `}`
}
// Total returns the total number of seconds in a CPUTimesStat
// Deprecated: Total returns the total number of seconds in a CPUTimesStat
// Please do not use this internal function.
func (c TimesStat) Total() float64 {
total := c.User + c.System + c.Nice + c.Iowait + c.Irq + c.Softirq +
c.Steal + c.Idle
total := c.User + c.System + c.Idle + c.Nice + c.Iowait + c.Irq +
c.Softirq + c.Steal + c.Guest + c.GuestNice
return total
}
@ -99,9 +102,15 @@ func (c InfoStat) String() string {
}
func getAllBusy(t TimesStat) (float64, float64) {
busy := t.User + t.System + t.Nice + t.Iowait + t.Irq +
t.Softirq + t.Steal
return busy + t.Idle, busy
tot := t.Total()
if runtime.GOOS == "linux" {
tot -= t.Guest // Linux 2.6.24+
tot -= t.GuestNice // Linux 3.2.0+
}
busy := tot - t.Idle - t.Iowait
return tot, busy
}
func calculateBusy(t1, t2 TimesStat) float64 {