Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
kargh committed Jan 19, 2023
1 parent 1b78743 commit 0e5c056
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ func (c TimesStat) Total() float64 {
}

func UsedTime(t1, t2 TimesStat) {
fmt.Println("t1:", t1)
fmt.Println("t2:", t2)

var usedTime TimesStat

usedTime.User = t2.User - t1.User
Expand All @@ -113,7 +110,27 @@ func UsedTime(t1, t2 TimesStat) {
usedTime.Guest = t2.Guest - t1.Guest
usedTime.GuestNice = t2.GuestNice - t1.GuestNice

tot = usedTime.Total()
if runtime.GOOS == "linux" {
tot -= usedTime.Guest // Linux 2.6.24+
tot -= usedTime.GuestNice // Linux 3.2.0+
}

busy := tot - usedTime.Idle - usedTime.Iowait

usedTime.User = math.Min(100, math.Max(0, userTime.User/tot*100))
usedTime.System = math.Min(100, math.Max(0, userTime.System/tot*100))
usedTime.Idle = math.Min(100, math.Max(0, userTime.Idle/tot*100))
usedTime.Nice = math.Min(100, math.Max(0, userTime.Nice/tot*100))
usedTime.Iowait = math.Min(100, math.Max(0, userTime.Iowait/tot*100))
usedTime.Irq = math.Min(100, math.Max(0, userTime.Irq/tot*100))
usedTime.Softirq = math.Min(100, math.Max(0, userTime.Softirq/tot*100))
usedTime.Steal = math.Min(100, math.Max(0, userTime.Steal/tot*100))
usedTime.Guest = math.Min(100, math.Max(0, userTime.Guest/tot*100))
usedTime.GuestNice = math.Min(100, math.Max(0, userTime.GuestNice/tot*100))

fmt.Println("usedTime:", usedTime)
fmt.Println("UsedPct:", math.Min(100, math.Max(0, busy/tot*100)))
}

func (c InfoStat) String() string {
Expand Down

0 comments on commit 0e5c056

Please sign in to comment.