diff --git a/cpu/cpu_unix.go b/cpu/cpu_unix.go index 4871fcd..fd488a2 100644 --- a/cpu/cpu_unix.go +++ b/cpu/cpu_unix.go @@ -1,13 +1,13 @@ -// +build linux,freebsd,darwin +// +build linux freebsd darwin package cpu -import ( - "encoding/json" - "strconv" - "strings" - "time" -) +import "time" + +func init() { + lastCPUTimes, _ = CPUTimes(false) + lastPerCPUTimes, _ = CPUTimes(true) +} func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) { getAllBusy := func(t CPUTimesStat) (float64, float64) { @@ -59,32 +59,3 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) { } return ret, nil } - -func (c CPUTimesStat) String() string { - v := []string{ - `"cpu":"` + c.CPU + `"`, - `"user":` + strconv.FormatFloat(c.User, 'f', 1, 64), - `"system":` + strconv.FormatFloat(c.System, 'f', 1, 64), - `"idle":` + strconv.FormatFloat(c.Idle, 'f', 1, 64), - `"nice":` + strconv.FormatFloat(c.Nice, 'f', 1, 64), - `"iowait":` + strconv.FormatFloat(c.Iowait, 'f', 1, 64), - `"irq":` + strconv.FormatFloat(c.Irq, 'f', 1, 64), - `"softirq":` + strconv.FormatFloat(c.Softirq, 'f', 1, 64), - `"steal":` + strconv.FormatFloat(c.Steal, 'f', 1, 64), - `"guest":` + strconv.FormatFloat(c.Guest, 'f', 1, 64), - `"guest_nice":` + strconv.FormatFloat(c.GuestNice, 'f', 1, 64), - `"stolen":` + strconv.FormatFloat(c.Stolen, 'f', 1, 64), - } - - return `{` + strings.Join(v, ",") + `}` -} - -func (c CPUInfoStat) String() string { - s, _ := json.Marshal(c) - return string(s) -} - -func init() { - lastCPUTimes, _ = CPUTimes(false) - lastPerCPUTimes, _ = CPUTimes(true) -} diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index 0980aa0..a19e4ce 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -98,6 +98,7 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) { if err != nil { p = 0 } + // but windows can only get one percent. ret = append(ret, float64(p)/100.0) } return ret, nil