Skip to content

Commit

Permalink
Merge pull request #1325 from shirou/feature/fix_cpu_total
Browse files Browse the repository at this point in the history
fix(cpu): fix cpu total and busy calc
  • Loading branch information
shirou committed Jul 11, 2022
2 parents cb1120d + d1cc7d2 commit 46f7642
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions cpu/cpu.go
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"math"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down

0 comments on commit 46f7642

Please sign in to comment.