cpu: fix build option on unix.

This commit is contained in:
Shirou WAKAYAMA 2015-03-11 23:06:00 +09:00
parent ead8393137
commit 170390c0df
2 changed files with 8 additions and 36 deletions

View File

@ -1,13 +1,13 @@
// +build linux,freebsd,darwin // +build linux freebsd darwin
package cpu package cpu
import ( import "time"
"encoding/json"
"strconv" func init() {
"strings" lastCPUTimes, _ = CPUTimes(false)
"time" lastPerCPUTimes, _ = CPUTimes(true)
) }
func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) { func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
getAllBusy := func(t CPUTimesStat) (float64, float64) { getAllBusy := func(t CPUTimesStat) (float64, float64) {
@ -59,32 +59,3 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
} }
return ret, nil 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)
}

View File

@ -98,6 +98,7 @@ func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
if err != nil { if err != nil {
p = 0 p = 0
} }
// but windows can only get one percent.
ret = append(ret, float64(p)/100.0) ret = append(ret, float64(p)/100.0)
} }
return ret, nil return ret, nil